Optimizing away a class as best as possible?

S

schwehr

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top