[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <4c5d.4a687cd4.a6013@altium.nl>
Date: Thu, 23 Jul 2009 15:08:04 -0000
From: dick.streefland@...ium.nl (Dick Streefland)
To: linux-kernel@...r.kernel.org
Subject: Re: question for C preprocessor wizards
"Chris Friesen" <cfriesen@...tel.com> 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@...ium.nl (@ @) 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@...r.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Powered by blists - more mailing lists