0

I have a macro:

#define CREATE_COMPONENT(_name, ...)     \
    typedef struct __VA_ARGS__ _name;    \
    const int COMPONENT_##_name##  = 0; 

Such that it can be invoked like:

CREATE_COMPONENT(Position, { float x, y }; )
CREATE_COMPONENT(Scale,    { float x, y }; )

Ideally this would create a struct, and an enum entry (I guess) among other things omitted, so I would like COMPONENT_##_enumName## to increment with each use. I'd end up with (in this case) two values:

COMPONENT_Position = 0;
COMPONENT_Scale    = 1;

Again, I sorta just want this to construct an enum, so if there's another way to do something similar without drastically changing the usage of the macro, I'm open to suggestions.

Ferrixx
  • 3
  • 1
  • 1
    X-Y problem for sure. What do you want to archive? What problem do you have? (not with macro but more generally) – 0___________ Aug 07 '23 at 13:54
  • There is the non-Standard (I guess) `__COUNTER__` macro that MSVC and some other compilers implement. That *may* be of some use but would likely create very non-portable code. – Adrian Mole Aug 07 '23 at 13:58
  • It's hard to tell exactly what the goal is, but you might want to learn about [X-macros](https://stackoverflow.com/questions/6635851/real-world-use-of-x-macros). – ad absurdum Aug 07 '23 at 16:54
  • @0___________ X-Y Problem more so because I just wanted to know if what I was trying to do was possible using macros... I was curious I guess. The actual intention is that I would like to construct some sort of API that one could use to easily declare (in this case) a struct definition and a corresponding enum entry, ideally at compile-time. I am aware of other ways I can do this, I would just like to know if this specific solution could be made possible. I guess a better question might be "How can I construct an enum through multiple calls of a macro?", or something like that idk,,, – Ferrixx Aug 08 '23 at 01:52
  • @Ferrixx avoid macros as a plaque – 0___________ Aug 08 '23 at 08:25

1 Answers1

0

I've figured a solution with ad absurdum's suggestion of using X-Macros. The user would instead declare the components like:

#define COMPONENT_LIST \
    CREATE_COMPONENT(Position, { float x , y ; } ) \
    CREATE_COMPONENT(Scal,     { float x , y ; } ) \

Which would populate the enum and create the structs with:

typedef enum 
{
    #define CREATE_COMPONENT(_name, ...) \
        COMPONENT_##_name,
        COMPONENT_LIST
    #undef CREATE_COMPONENT

} Component;

#define CREATE_COMPONENT(_name, ...) \
    typedef struct __VA_ARGS__ _name;
    COMPONENT_LIST
#undef CREATE_COMPONENTS

I would like to apologise that my question was maybe a little too particular, or X-Y... I just wanted a way to construct an enum through the calls to CREATE_COMPONENT, so maybe the question title "Counting Macro Invocations" might've been a little too specific ;p. In the future I will better differentiate between my attempted/suggested solution and the actual problem. Thank you for your suggestions though.

Ferrixx
  • 3
  • 1
  • 1
    You may consider using an X-macro in form `#define XMACRO(X) X(.. param1 ..) X(...params...) ...`. This style allows reusing macros bound to `X` or giving them more descriptive names. – tstanisl Aug 08 '23 at 08:27