# Incrementing Preprocessor Defines

## Recommended Posts

Aye [grin] I'm looking for a way to convert this:
// I want to change this..

#define CHANIM_CHANGEMAIN	0x00000001
#define CHANIM_CHANGESUB	0x00000002
#define CHANIM_MAINENDED	0x00000004
#define CHANIM_SUBENDED	0x00000008
#define CHANIM_SUBBYMAIN	0x00000010
#define CHANIM_NOCOMMAND	0x00000020

// .. into something like this..

DEFINE_FLAGS_START
DEFINE_FLAG( CHANIM_CHANGEMAIN )
DEFINE_FLAG( CHANIM_CHANGESUB )
DEFINE_FLAG( CHANIM_MAINENDED )
DEFINE_FLAG( CHANIM_SUBENDED )
DEFINE_FLAG( CHANIM_SUBBYMAIN )
DEFINE_FLAG( CHANIM_NOCOMMAND )
DEFINE_FLAGS_END


The __COUNTER__ define in VS.net looks promising, but I can't seem to find any manner in which to start it at a certain value (ie, 1). Is this possible? Or is there another way I can hack this? My purpose is to remove the need to update the hex digits as I remove and add values in the middle of the flag defines. It's not important, but would be very helpful. Thanks for any suggestions [smile]

##### Share on other sites
You can make it slightly easier by doing this:
enum Whatever{   W_foo = 1 << 0,   W_foo1 = 1 << 1,   W_foo2 = 1 << 2,   W_foo3 = 1 << 3,   W_foo4 = 1 << 4,   W_bar = 1 << 5,   ...};

I find it a little bit easier that way.

edit: durrrr quote != code

##### Share on other sites
Hehe, yeah. That's how I planned to use the __COUNTER__ define, if it would work like I want it to. If I could reset it [smile]

##### Share on other sites

#define DEFINE_FLAGS_START(prefix)   static const int prefix##_FIRST = __COUNTER__ + 1#define DEFINE_FLAG(prefix, name)   static const int prefix ## _ ## name = 1 << (__COUNTER__ - prefix##_FIRST)DEFINE_FLAGS_START(CHANIM);DEFINE_FLAG(CHANIM, CHANGEMAIN);DEFINE_FLAG(CHANIM, CHANGESUB);DEFINE_FLAG(CHANIM, MAINENDED);DEFINE_FLAG(CHANIM, SUBENDED);DEFINE_FLAG(CHANIM, SUBBYMAIN);DEFINE_FLAG(CHANIM, NOCOMMAND);

Of course, it's ugly and relies on the fact that you have them grouped together, but it should work.

I would recommend you just write the numbers out yourself though.

##### Share on other sites
Actually, I had not thought of simply keeping track of where it starts and subtracting that amount. That's a great idea.

Like I said, it's not a big deal. I was simply using normal enums as flag indices and had a BITLFAG(_d_) (1<<_d_) type define to convert. Pretty sure it would all be done at pre-processing time. But using BITFLAG everywhere is more trouble than using hex digits [smile]

Oh well, let me know if anyone thinks of an elegant way to take the work out of this [wink]

##### Share on other sites
Edit: nevermind, I just suggested exactly what you're already doing, without paying attention. I suggest you stick with it :)

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628298
• Total Posts
2981890

• 9
• 9
• 11
• 10
• 10