Sign in to follow this  
Jiia

Incrementing Preprocessor Defines

Recommended Posts

Jiia    592
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 this post


Link to post
Share on other sites
Kibble    504
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 this post


Link to post
Share on other sites
Jiia    592
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 this post


Link to post
Share on other sites
Dean Harding    546
What about something like this:


#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 this post


Link to post
Share on other sites
Jiia    592
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 this post


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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this