3

Our system has a large number of enums denoting stuff such as events, errors etc.

I'm trying to build infrastructure that would allow us to log each received event or error message as a string (instead of a plain integer), without having to build and maintain two lists for each enum.

I found the X Macro technique quite suitable for my needs. Thus, instead of doing this:

typedef enum
{
   valA,
   valB,
   valC
} some_enum;

const char* some_enum_strings[] =
{                                                                                       
    "valA",
    "valB",
    "valC"
};

I'm doing this:

#define DEF_STRINGIFY(_x_) #_x_,
#define DEF_ENUM(_x_) _x_,

#define ENUM_NAME(_x_) \
    _x_(valA) \
    _x_(valB) \
    _x_(valC) 

typedef enum
{
    SOME_ENUM(DEF_ENUM)
} some_enum;

const char* some_enum_strings[] =
{                                                                                       
    SOME_ENUM(DEF_STRINGIFY)
};

That's all good and well, but the thing is some of our enums are quite big, comprising hundreds of values and filled with conditional compilation, and look a bit more like this

typedef enum
{
   valA,
#if defined (SYS_A)
   valB,
#endif
   valC,
   valD,
#if defined (SYS_B) || defined (SYS_D)
   valE,
#else
   valF,
#endif
   valG
/* etc. */
} some_enum;

As is turns out, C wouldn't allow conditional compilation within a macro. Eclipse claims "Invalid use of macro pasting in macro: SOME_ENUM, and when I try to build the code it fails due to "error: '#' is not followed by a macro parameter" (With the line number matching the first #if statement within the macro).

So now I'm quite stuck, and would appreciate any advice.

Is there a solution to my conundrum?

When suggesting a solution, please note that:

  • I can't have more than one list per enum
  • This solution needs to be applied to a large number of enums (i.e., I can't really put each enum in its own .h file)

Thanks.

4

1 回答 1

1

P99P99_DECLARE_ENUM和朋友可以让你enum很好的处理s。您获得返回带有名称等字符串的函数。这对于条件编译应该是健壮的。唯一的限制是您可以在一种类型中拥有的值的总量enum,但可能会有扩展它的方法。

于 2013-09-13T16:47:50.343 回答