1

The code has a number of following sections:

int filter;
#ifdef INPUTFILTER_FOO
 LOG4CXX_DEBUG(log, "FOO filter used");
         filter = F_FOO;
#endif

They are used multiple times in the code (used to provide I/O, threading support etc for all testing configurations), Circa they are essential for debugging but make the code look harsh, want to replace them with macros, one for each category_type namespace.

So, want to expand the following:

MACROSTUFFBAZ(log2, stuff, "BAZ") <- the text part is unique for each class, so it needs to be included in macro too.

to:

#ifdef INPUTSTUFF_BAZ
  LOG4CXX_DEBUG(log2, "BAZ stuff used");
  stuff = S_BAZ;
#endif

To define macros, plan to use this:

debug.hpp:

   #ifdef INPUTSTUFF_BAZ
    #define MACROSTUFFBAZ ...
   #else
   #define MACROSTUFFBAZ
    .. no code!
   #endif
  #endif

(at least this will give a clear overview of the things currently undergoing probation, without seeing them around the code)

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
kagali-san
  • 2,964
  • 7
  • 48
  • 87
  • 1
    You're not clear about `filter = F_FOO` and `stuff = S_BAZ` : when is it `S_` ? When is it `F_` ? Does it depend on macro parameters ? – icecrime Jan 01 '11 at 23:40
  • @icecrime, they're just another macro-defined values (filename prefix, counters, delays etc). They do not depend on parameters, instead they're defined by a macro. – kagali-san Jan 01 '11 at 23:43

1 Answers1

3

Here's my try, although i'm not 100% sure if it works cause i'm unable to test it right now, and also different compilers handle preprocessor macros a little bit differently. But I think something like this works at least in visual studio.

Basically, you have to use a helper macro to convert the parameter into a string. And secondly, you can use ## to concatenate identifiers:

#define PRMTOSTR_HLPR(x) #x
#define PRMTOSTR(x) PRMTOSTR_HLPR(x)

#ifdef INPUTSTUFF_BAZ
  #define MACROSTUFFBAZ(A,B,C) \
  LOG4CXX_DEBUG(A, PRMTOSTR(C)" stuff used"); \
  B = S_##C;
#else
  #define MACROSTUFFBAZ(A,B,C)
#endif

//used like this:
MACROSTUFFBAZ(log2, stuff, BAZ)

edit: The helper macro is actually not needed here, so you can just put #C directly in the definition of MACROSTUFFBAZ.

Timo
  • 5,125
  • 3
  • 23
  • 29
  • `#x` is correct, but you shouldn't need a helper macro. You can simply put `#C` directly in the second macro. Spaces between the strings are also fine, they will be concatenated the same. – edA-qa mort-ora-y Jan 02 '11 at 17:12
  • You're right. I was remembering the double level of indirection was needed for something, but it was for quoting the values of other macros: http://stackoverflow.com/questions/216875/in-macros/217181#217181 – Timo Jan 02 '11 at 23:48