468,478 Members | 2,021 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,478 developers. It's quick & easy.

Optimizing away a class as best as possible?

Hi All,

I've got a small logging class that I would like to have a way to
optimize away as much as possible with the minimum of macro trickery.
I've got working code, but not having done C++ much for a few years, I
have the feeling that this code might not be the best. I'd like to be
able to have different source files compile in different states so that
the high performance cores can optionally be built with logging
dissapearing as much as possible while retaining logging in other parts
of the system. This works on 4.0.1, but that doesn't mean that what I
am doing is a good idea :)

Here is my first try at a header for a small class that can pick up the
location of a log call via a macro, which should be a pretty simple
example of what I am trying to do. I am bothered by having different
prototypes for each mode, but looking at the assembler code shows that
when NLOG is defined, the most of the code for the Where class
evaporates. Anyone want to shoot this code to pieces? :) Should I be
returning references to const static values?

The end goal is to write some think this and then have it optionally
get yanked out of the code with NLOG... I have the logger working
nicely, now I am going for the optimization potential.

logger << WHERE << " some state " << i << " " << j << endl;

Thanks!
-kurt
#define WHERE Where(__FILE__,__LINE__,__FUNCTION__)
#include <string>
#include <iostream>

#if !defined(NLOG)
class Where {
public:
Where(const std::string &_file, const int _lineno, const
std::string &_function);
inline std::string const &getFile() const {return file;};
inline int const &getLineno() const {return lineno;};
inline std::string const &getFunction() const {return function;};
private:
std::string file;
int lineno;
std::string function;
};

Where::Where(const std::string &_file, const int _lineno, const
std::string &_function)
: file(_file), lineno(_lineno), function(_function) {} // Nothin

#else
//////////////////////////////////////////////////////////////////////

// if gcc
#define UNUSED __attribute((__unused__))
class Where {
public:
Where(UNUSED const std::string &_file, UNUSED const int &_lineno,
UNUSED const std::string &_function) {};
inline std::string getFile() const {return std::string("unknown
file");};
inline int getLineno() const {return -1;};
inline std::string getFunction() const {return std::string("unknown
function");};
};
#endif // NLOG for where

Jul 23 '06 #1
0 875

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by A Future Computer Scientist | last post: by
4 posts views Thread by J. Campbell | last post: by
8 posts views Thread by Hagen | last post: by
5 posts views Thread by Daishi Harada | last post: by
5 posts views Thread by Tony | last post: by
24 posts views Thread by Richard G. Riley | last post: by
26 posts views Thread by jaysome | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.