I got this macro that posts events to some event-queue.
POST_(myTarget, event)
The event may either be posted directly to a local queue or may be serialized and posted to the event-queue of a I2C service which sends the event to another microcontroller. Whether the receiving service is local or remote is defined like:
#define myTarget_REMOTE
#define anotherTarget_LOCAL
What I want to do is something like this (which is certainly not allowed):
#define POST(target, e) \
#ifdef target##_REMOTE \
/* create a i2c request-event with serialized(e)
as parameter and post to I2c-Manager */
#else \
/* post directly */
POST_(target, event) \
#endif
So, all the information is there at compile time, but I don’t know how to tell the preprocessor what to do.
- I could create two macros for each target depending on its local/remote define, but this would be messy.
- I could do the test at runtime, but this would be a sad story also.
EDIT:
An example how the program will look like clearer:
#define target1_LOCAL
#define target2_REMOTE
POST(target1, e) ==preprocessor==> POST_(target1, e)
POST(target2, e) ==preprocessor==>
do { \
req = createI2cRequest(serialize(e)); \
POST_(I2cManager, req); \
}while(0)
So, in the program I just use POST(target, event) and the location of the target is completely transparent.
So i finally figured it out.
The trick is to use the preprocessor to give the compiler all needed information and rely on the compiler to optimize the if/else away.
Output: