[gameprogrammer] Re: Any C++ macro champions out there?

  • From: Stéphane Marchesin <stephane.marchesin@xxxxxxxxx>
  • To: gameprogrammer@xxxxxxxxxxxxx
  • Date: Thu, 3 Jan 2013 17:26:49 -0800

On Wed, Jan 2, 2013 at 10:29 PM, Alan Wolfe <alan.wolfe@xxxxxxxxx> wrote:
> Any C++ macro champions out there? I'm Trying to figure out how to do
> something and hoping i don't have to fall back on code generation or manual
> maintenance :P
>
> I have a macro list defined like this:
>
> #define TRIGGEREVENTLIST \
> \
> TRIGGEREVENT_BEGIN(PokeEvent, Base) \
> TRIGGEREVENT_ITEM(int, PokeCount, true, 0) \
> TRIGGEREVENT_ITEM(bool, PokeHard, false, false) \
> TRIGGEREVENT_END() \
> \
> TRIGGEREVENT_BEGIN(AnotherEvent, Base) \
> TRIGGEREVENT_ITEM(float, Something, true, 0.5f) \
> TRIGGEREVENT_END() \
> \
> TRIGGEREVENT_BEGIN(UberPokeEvent, Base) \
> TRIGGEREVENT_ITEM(float, UberPokeMultiplier, true, 2.0f) \
> TRIGGEREVENT_END()
>
> And I want to create a class per trigger event that contains each item as a
> member and also a const & getter and setter for each item within it. I have
> that part working, but I'm stuck on the next part...
>
> What i need to do next is make a constructor or some kind of construction
> mechanism which contains all the items as parameters to the function, and
> then sets the members to the passed in values.
>
> The problem I'm hitting is that to do that, I essentially need to loop
> through an events item list twice (once for params, once for setting the
> members to the param values) before moving onto the next event, but not sure
> how to do that (or even if there is a way).

You put all your things in one header, let's call that header listofthings.h:

DO_SOMETHING(0, "A")
DO_SOMETHING(2, "B")
...

then you can include that header multiple times, redefining
DO_SOMETHING differently every time:

#define DO_SOMETHING(A,B) printf("%d %s\n", A, B);
#include "listofthings.h"
#undef DO_SOMETHING

#define DO_SOMETHING(A,B) table[A] = B;
#include "listofthings.h"
#undef DO_SOMETHING

Stéphane

>
> Unfortunately, a potential added nail to the coffin is that the constructor
> (or construction mechanism) also needs to include as parameters all of the
> inherited items.
>
> I really want compile time assurance that when these are constructed, that
> they have all the necessary data supplied to them (so when you add or remove
> items, you get compile time errors about the places in the code you need to
> update).
>
> Thanks for any insight anyone can provide!
>
> PS if anyone is interested, I can share the code i have right now to
> generate classes with private members and getters and setters, as well as a
> COMPILE TIME calculation of the size of the largest class (so you could use
> it as a parameter to an object pool allocator or something).

---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html


Other related posts: