question for C preprocessor wizards
I'm hoping someone can help me out.
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
Where I could then do
WRAPPER(func1)
WRAPPER(func2)
...
WRAPPER(func100)
However, the preprocessor complains about having that "#" in the macro body where it isn't used for stringification.
Anyone have any ideas how to accomplish this? I had considered writing an app to programmatically generate an include file as a precursor to actually compiling the real app, but I was hoping there was a more elegant solution.
Thanks,
Chris
Quoted 2 lines · Chris Friesen
I'm hoping someone can help me out.
This is _not_ a question about kernel. :)
Quoted 42 lines
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
#definefunc_wrapper(args...)func(args)
#endifHowever, 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
Where I could then do
WRAPPER(func1)
WRAPPER(func2)
...
WRAPPER(func100)
Quoted 8 lines
However, the preprocessor complains about having that "#" in the macro body where it isn't used for stringification.
Anyone have any ideas how to accomplish this? I had considered writing an app to programmatically generate an include file as a precursor to actually compiling the real app, but I was hoping there was a more elegant solution.
Well, you can define func() as a real function instead of a macro, i.e:
#define WRAPPER(func) \
void func ## _wrapper(args, ...) \
{ \
dostuff(); \
func(args); \
do_more_stuff(); \}
Quoted 51 lines · Amerigo Wang
Quoted 2 lines · Chris Friesen
I'm hoping someone can help me out.
This is _not_ a question about kernel. :)
Quoted 44 lines
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
#definefunc_wrapper(args...)func(args)
#endifHowever, 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
Where I could then do
WRAPPER(func1)
WRAPPER(func2)
...
WRAPPER(func100)However, the preprocessor complains about having that "#" in the macro body where it isn't used for stringification.
One can't generate preprocessor directive via #define.
Quoted 4 lines
Quoted 4 lines
Anyone have any ideas how to accomplish this? I had considered writing an app to programmatically generate an include file as a precursor to actually compiling the real app, but I was hoping there was a more elegant solution.
Personally, I don't find it in elegant to generate simple/trivial .c or .h files with some small-ish perl/shell-script via sane Makefile rules. Perhaps unless the contents is (almost) never going to change (and even then there might be good reasons).
Within the kernel, it's probably better to avoid perl-scripts for easier acceptance.
Quoted 10 lines
Well, you can define
func()as a real function instead of a macro, i.e:#define WRAPPER(func) \
void func ##_wrapper(args, ...)\{ \ dostuff(); \ func(args); \ do_more_stuff(); \}
ACK.
Or the other way around:
Have dostuff() at the begin and do_more_stuff() at the end of the real function in the source and #if-them-away per
---- snip ----
#if FLAG
void dostuff();
void do_more_stuff();
#else
#define dostuff() ((void)0)
#define do_more_stuff() ((void)0)
#endif
---- snip ----
Bernd
Signature · 3 lines
Firmix Software GmbH http://www.firmix.at/
mobil: +43 664 4416156 fax: +43 1 7890849-55
Embedded Linux Development and Services"Chris Friesen" <[email protected]> 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)
| #endifYou 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)
...
Signature · 3 lines
Dick Streefland //// Altium BV [email protected] (@ @) http://www.altium.com --------------------------------oOO--(_)--OOo---------------------------