Re: question for C preprocessor wizards

From: Dick Streefland
Date: Thu Jul 23 2009 - 11:08:18 EST


"Chris Friesen" <cfriesen@xxxxxxxxxx> wrote:
| I've got a bunch of code that call a bunch of different wrapper
| routines, with varying numbers of arguments. Depending on whether a
| compile flag is set, I want to do some stuff before and after calling
| the "real" routine. I can do this easily enough with a macro.
|
| #if FLAG
| #define func_wrapper(args...) \
| do { \
| dostuff(); \
| func(args); \
| do_more_stuff(); \
| } while (0)
| #else
| #define func_wrapper(args...) func(args)
| #endif
|
|
| However, given that there are hundreds of functions, I'd like to
| generate these macros with another macro, sort of like:
|
| #if FLAG
| #define WRAPPER(func) \
| #define func # _wrapper(args...) \
| do { \
| dostuff(); \
| func(args); \
| do_more_stuff(); \
| } while (0)
| #else
| #define WRAPPER(func) \
| #define func ## _wrapper(args...) func(args)
| #endif

You cannot generate preprocessing directives with macro expansion.
An alternative is to use a generic wrapper macro, and use that to
define wrappers for the functions. Something like:

#if FLAG
#define generic_wrapper(func, args...) \
do { \
dostuff(); \
func(args); \
do_more_stuff(); \
} while (0)
#else
#define generic_wrapper(func, args...) func(args)
#endif

#define func1_wrapper(args...) generic_wrapper(func1, args)
#define func2_wrapper(args...) generic_wrapper(func2, args)
#define func3_wrapper(args...) generic_wrapper(func3, args)
...

--
Dick Streefland //// Altium BV
dick.streefland@xxxxxxxxx (@ @) http://www.altium.com
--------------------------------oOO--(_)--OOo---------------------------

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/