N
Noob
Hello,
I have a long list of (40-50) initialization functions which I must call
at the start of an application.
As of now, the code looks like this:
unsigned long bitmap = 0;
if (FOO_init() != 0) bitmap |= (1 << 0);
if (BAR_init() != 0) bitmap |= (1 << 1);
if (BAZ_init() != 0) bitmap |= (1 << 2);
....
if (BOB_init() != 0) bitmap |= (1 << 30);
/* 1 << 31 is not used, out of superstition I imagine */
unsigned long bitmap2 = 0;
if (GOO_init() != 0) bitmap2 |= (1 << 0);
etc
I am trying to write a macro that would take a function name and spit
the corresponding invocation and potential bit-setting.
WUNDER_MACRO(FOO_init);
WUNDER_MACRO(BAR_init);
would expand to
if (FOO_init() != 0) bitmap |= (1 << 0);
if (BAR_init() != 0) bitmap |= (1 << 1);
I have two problems.
1) How do I write a macro that counts how many times it has been invoked?
So that the i-th invocation can produce bitmap |= (1 << i)
2) I'm not sure how to handle the fact that I have more than 32
functions to call.
Portability is not very important here, so I could maybe use an unsigned
long long bitmap (64 bits on my platform)... Bleh...
Or is there no easy way to "clean up" this code, and I should leave it
as it is?
Any and all suggestion welcome.
Regards.
I have a long list of (40-50) initialization functions which I must call
at the start of an application.
As of now, the code looks like this:
unsigned long bitmap = 0;
if (FOO_init() != 0) bitmap |= (1 << 0);
if (BAR_init() != 0) bitmap |= (1 << 1);
if (BAZ_init() != 0) bitmap |= (1 << 2);
....
if (BOB_init() != 0) bitmap |= (1 << 30);
/* 1 << 31 is not used, out of superstition I imagine */
unsigned long bitmap2 = 0;
if (GOO_init() != 0) bitmap2 |= (1 << 0);
etc
I am trying to write a macro that would take a function name and spit
the corresponding invocation and potential bit-setting.
WUNDER_MACRO(FOO_init);
WUNDER_MACRO(BAR_init);
would expand to
if (FOO_init() != 0) bitmap |= (1 << 0);
if (BAR_init() != 0) bitmap |= (1 << 1);
I have two problems.
1) How do I write a macro that counts how many times it has been invoked?
So that the i-th invocation can produce bitmap |= (1 << i)
2) I'm not sure how to handle the fact that I have more than 32
functions to call.
Portability is not very important here, so I could maybe use an unsigned
long long bitmap (64 bits on my platform)... Bleh...
Or is there no easy way to "clean up" this code, and I should leave it
as it is?
Any and all suggestion welcome.
Regards.