Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Etnu

Anyway to have a macro generate comments?

This topic is 5222 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is more of a formatting thing than anything else. I have several debugging options that run in my program while in debug that I don''t want in release. Currently, I''m just filtering stuff out blocks by putting
#ifdef _DEBUG
//Do stuff

#endif 
This is very, very ugly. What I want to do is something like the following:
#ifdef _DEBUG
#define BeginDebugBlock();
#define EndDebugBlock();
#else
#define BeginDebugBlock(); /*
#define EndDebugBlock(); */
#endif
which would let me change this:
#ifdef _DEBUG
DoFirstThing();
DoSecondThing();
DoSomethingElse();
#endif
to this:
BeginDebugBlock();
DoFirstThing();
DoSecondThing();
DoSomethingElse();
EndDebugBlock();
I suppose I could do something like:
#ifdef _DEBUG
#define BeginDebugBlock(); 
#define EndDebugBlock();
#else
#define BeginDebugBlock(); if(0){
#define EndDebugBlock(); }
But that would actually generate extra code, which I don''t want. Suggestions?

Share this post


Link to post
Share on other sites
Advertisement
I guess I would do it like this:


#ifdef _DEBUG
#define BeginDebugBlock(); if (1) {
#define EndDebugBlock(); }
#else
#define BeginDebugBlock(); if (0) {
#define EndDebugBlock(); }
#endif

However I think first I might try a different way of doing things, wrapping code in blocks like that doesn't sit well with me.

And, the "extra" code will be optimized out in a Release build by any compiler that isn't braindead.



[edited by - desert fox on June 5, 2004 1:04:56 AM]

Share this post


Link to post
Share on other sites
Yes, it''s ugly, but for selective compilation, I don''t see how you have much of a choice other than #ifdef _DEBUG.

I''m trying to minimize the amount of ugliness here to some degree.

Share this post


Link to post
Share on other sites
You could always do:
#ifdef _DEBUG
#define DebugBlock(x) {x}
#else
#define DebugBlock(x)
#endif

DebugBlock(
DebugOnlyFunc();
OtherDebugOnlyFunc();
AnotherDebugOnlyFunc();
OneMoreDebugOnlyFunc();
NextDebugOnlyFunc();
)

Share this post


Link to post
Share on other sites
As ugly as #ifdef _DEBUG is, it's one of the more common ways of doing this. (#ifndef NDEBUG may or may not be better to match the behavior of assert(), or #ifdef DEBUG_ since I'm paranoid about stepping on the implementation's namespace) This means that someone else reading your code will have an easier time understanding what's happening than if you created those macros. (Think of things like #define BEGIN {, #define END }, etc. )

As for the macros, I keep trying to think of the "perfect" way to do them. The closest I can think of is

#ifdef DEBUG_
#define beginDebug
#else
#define beginDebug while (0)
#endif /* #ifdef DEBUG_ */

so you can write code like

beginDebug {
debugThingOne();
debugThingTwo();
}

or

beginDebug
debugThing();

and not have to watch your else's like you would with an if() solution. This also doesn't require an endDebug, just a } like everything else. As mentioned before, removing something like while(0) is one of the most braindead optimizations that any compiler worth its salt will perform.

Really, I can't see any problems with this macro (other than not following the common idiom), but it's late and I'm probably missing something simple. Well, having a beginDebug alone will work in debug mode but probably fail quite horribly in release, but that doesn't seem too likely to come up. This could be fixed if an endDebug macro were required, but I find that ugly and if I weren't caring about ugly I'd just use #ifdef's

EDIT: I just realized that the if() solution would likely have no real world issues so I change my preference to

#ifdef DEBUG_
#define beginDebug if(1)
#else
#define beginDebug if(0)
#endif

since this preserves the same behavior in both release and debug.

[edited by - Way Walker on June 5, 2004 3:40:41 AM]

Share this post


Link to post
Share on other sites
That works, but it results in my code not really resembling "normal" code.

I suppose something like...


#ifdef _DEBUG
#define DebugBlock
#else
#define DebugBlock if(0)
#endif


DebugBlock
{
// Do stuff

}



Which is slightly prettier.


[edited by - Etnu on June 5, 2004 3:01:02 AM]

Share this post


Link to post
Share on other sites
Using while is better because ''if'' allows an ''else''. If you''re going to use if, make do ''if(1);else'' or ''if(0);else''

Share this post


Link to post
Share on other sites
you want to have functions which don''t get compiled in when not in debug.

so write a function to do the debug stuff and a version which does nothing. choose to have the debug .cpp included for debug builds and the do nothing version for release builds.

Share this post


Link to post
Share on other sites
Yes, I know I could do that (and I already do in many cases), but I''m not going to write (and maintain) 2 seperate functions when all I need is for 5 or 10 lines to be removed during compilation. I''m quite familiar with how debugging works - I just wanted something a little less ugly.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!