0

I have a list of enums:

typedef enum {
     ENUM1,
     ENUM2,
     #if FLAG
     ENUM3,
     #endif
} enum_var_t;

And a corresponding list of strings to align:

typedef struct { char[50] name; int val; } name_val_map_t
name_val_map_t name_val_map_table[] = {
    {.name="string1", .val=ENUM1},
    {.name="string2", .val=ENUM2},
    #if FLAG
    {.name="string3", .val=ENUM3},
    #endif
};

FLAG is a build flag, and is either 0, or 1. I am trying to use X-Macros to align these according to an answer here:

#define IF(cond, foo) IF_IMPL(cond, foo)
#define IF_IMPL(cond, foo) IF_ ## cond (foo)
#define IF_0(foo)
#define IF_1(foo) foo

#define var_list \
X(ENUM1, "string1"), \
X(ENUM2, "string2"), \
IF(FLAG, X(ENUM3, "string3")), \

#define X(ENUMVAL, ...) ENUMVAL
typedef enum {
    var_list
}
#undef X
#define X(ENUMVAL, NAME) {.name = NAME, .val = ENUMVAL}
name_val_map_t name_val_map_table = {
var_list
}

This leads to an error which says I'm passing more arguments to the IF macro than declared. I presume it is treating the comma inside the X(ENUM3, "string3") as an argument separator for IF. I tried encapsulating the X() call with braces and removing the braces from IF_IMPL, but that didn't work either. If I try expand the argument list in IF() using ..., and VA_ARGS, I get expected expression errors. I'm trying to avoid using a def file as this makes my file unreadable. Some solution like I was trying would be perfect to avoid code replication, and for readability. Any pointers would be welcome, thanks!

  • What is `X` ? What is `FLAG` ? Please include a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) that showcases the issue in your question. – Sander De Dycker Mar 02 '20 at 07:56
  • `X` is a really bad name for anything. When someone else looks at it, they're not going to have any idea what `X` means or what it does without digging up its actual implementation. And that "someone else" includes the future you. – Andrew Henle Mar 02 '20 at 11:09

2 Answers2

1

Using variadic macros.

#define IF(cond, foo) IF_IMPL(cond, foo)
#define IF_IMPL(cond, ...) IF_ ## cond(__VA_ARGS__)
#define IF_0(foo, ...)
#define IF_1(foo, ...) foo, __VA_ARGS__

Test:

//usr/local/bin/tcc -run "$0"; exit $?
#include <stdio.h>

#define FLAG3 1
#define FLAG4 0
#define FLAG5 1

typedef struct { char *name; int val; } name_val_map_t;

#define IF(cond, foo) IF_IMPL(cond, foo)
#define IF_IMPL(cond, ...) IF_ ## cond(__VA_ARGS__)
#define IF_0(foo, ...)
#define IF_1(foo, ...) foo, __VA_ARGS__

#define var_list               \
X(ENUM1, "string1")            \
X(ENUM2, "string2")            \
IF(FLAG3, X(ENUM3, "string3")) \
IF(FLAG4, X(ENUM4, "string4")) \
IF(FLAG5, X(ENUM5, "string5")) \

typedef enum {
#define X(ENUMVAL, str) ENUMVAL,
    var_list
#undef X
} enum_var_t;

name_val_map_t name_val_map_table[] = {
#define X(ENUMVAL, NAME) { NAME, ENUMVAL },
    var_list
#undef X
    { "sentinel value", 99 }
};

int main(void){
    int x =0;
    while(name_val_map_table[x].val != 99){
        printf("%i, %s\n", name_val_map_table[x].val, name_val_map_table[x].name);
    x++;}
    return 0;
}

/* output:
    0, string1
    1, string2
    2, string3
    3, string5
*/

Another option is to manually create IF_FLAGx( X(bla, bla) ) macros for every case...

See also: macro specialization based on argument in case of MSVC bug.

hellork
  • 194
  • 7
0

This seems needlessly complicated. I would simply do this instead:

#if FLAG 
  #define var_list       \
  X(ENUM1, "string1")    \
  X(ENUM2, "string2")    \
  X(ENUM3, "string3")    
#else
  #define var_list       \
  X(ENUM1, "string1")    \
  X(ENUM2, "string2")    
#endif

Full example:

#include <stdio.h>

#define FLAG 1

#if FLAG 
  #define var_list       \
  X(ENUM1, "string1")    \
  X(ENUM2, "string2")    \
  X(ENUM3, "string3")    
#else
  #define var_list       \
  X(ENUM1, "string1")    \
  X(ENUM2, "string2")    
#endif


typedef enum
{
  #define X(enum_var, str) enum_var,
    var_list
  #undef X

  ENUM_N
} enum_var_t;

typedef struct
{
  char name[50];
  int  val;
} name_val_map_t;

const name_val_map_t name_val_map_table[] = 
{
  #define X(enum_var, str) { .name = str, .val = enum_var },
    var_list
  #undef X
};

int main (void)
{
  for(size_t i=0; i<ENUM_N; i++)
  {
    printf("%d %s\n", name_val_map_table[i].val, name_val_map_table[i].name);
  }
}
Lundin
  • 155,020
  • 33
  • 213
  • 341