Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ conditional macro evaluation

I have a symbol defined globally that needs to be conditionally undefined for a given subset of my source files. All of the files that require special treatment are already wrapped in pre- and post-inclusions:

pre.h:

#undefine mysymbol // [1]

post.h:

#define mysymbol MY_SYMBOL_DEFINITION // [2]

My problem is that the pre.h and post.h can be included multiple times for a given source file due to various inclusion chaining. As such, I need 1 to happen the first time pre.h is included and I need 2 to happen the last time that post.h is included. Conceptually:

pre         // undefine
   pre      // no-op
      pre   // no-op
      post  // no-op
   post     // no-op
post        // redefine

Since I am using GCC 3.4.6, I do not have access to the push and pop macro pragmas that might otherwise solve this issue for me.

How can I emulate that behavior with the remaining preprocessor functionality?

I was attempting to do something like increment/decrement a value with the preprocessor, but I'm not sure that's possible.

"What am I really trying to do?"

We have macros to replace new with new(__FILE__, __LINE__) -- see my other question on this topic -- and we need to undefine those macros in the set of source files wrapped by the pre- and post-includes described above because we were unable to create a macro that's compatible with the placement new syntax used therein.

like image 595
David Citron Avatar asked Oct 14 '22 16:10

David Citron


1 Answers

You can add somethig like this to your pre.h file:

... 

#ifdef COUNT
#if COUNT == 2
#undef COUNT
#define COUNT 3
#endif

#if COUNT == 1
#undef COUNT
#define COUNT 2
#endif

#else
#define COUNT 1

... here put your pre.h code

#endif

And in post.h:

#ifdef COUNT
#if COUNT == 1
#undef COUNT
#endif

#if COUNT == 2
#undef COUNT
#define COUNT 1
#endif

#if COUNT == 3
#undef COUNT
#define COUNT 2
#endif

...    

#end

#ifndef COUNT

... here put your pre.h code

#endif

But you need to know how deep can you go.

like image 188
klew Avatar answered Nov 10 '22 18:11

klew