preprocessor statements

Started by
2 comments, last by Enigma 17 years, 10 months ago
Im currently implementing a profiler. Would you guys say it is harmfull doing something like this considering the ugliness of #ifdef: void somefunc() { #ifdef PROFILER_ACTIVE my profiler code here #endif ... }
Advertisement
What's ugly is putting ifdefs everywhere you have profiler code. It'll ugly your code, so you may want to move the ifdefs somewhere more central. I'm not sure how this is typically done these days, but this would be a great use case for AOP (aspect-oriented programming).
ick.
You could clean that up by doing the following

//in your profiler.h#ifdef USING_MY_PROFILER#define ProfileSample(f)     MyProfilerSample(f)#else#define ProfileSample(f)#endif//in your other code#include "profiler.h"void some_function (){   ProfileSameple(__FUNCTION__);   ...   ...}


that would keep #ifdef's from being everywhere
Quote:Original post by KulSeran
ick.
You could clean that up by doing the following

*** Source Snippet Removed ***

that would keep #ifdef's from being everywhere

ick.
A preprocessor definition that isn't ALL_CAPS_WITH_UNDERSCORES? Evil. Apart from that your solution is good though. I use PP_PROFILE as my #define symbol, so my code looks like:
void function_i_want_to_profile(){	PP_PROFILE;	// code}// profiling source code#define PP_PROFILE Profile local_profile_object(__FUNCTION__)

Σnigma

This topic is closed to new replies.

Advertisement