public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: dick.streefland@altium.nl (Dick Streefland)
To: linux-kernel@vger.kernel.org
Subject: Re: question for C preprocessor wizards
Date: Thu, 23 Jul 2009 15:08:04 -0000	[thread overview]
Message-ID: <4c5d.4a687cd4.a6013@altium.nl> (raw)
In-Reply-To: 4A675F89.50506@nortel.com

"Chris Friesen" <cfriesen@nortel.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@altium.nl           (@ @)          http://www.altium.com
--------------------------------oOO--(_)--OOo---------------------------


      parent reply	other threads:[~2009-07-23 15:08 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-22 18:50 question for C preprocessor wizards Chris Friesen
2009-07-23  1:07 ` Amerigo Wang
2009-07-23  8:14   ` Bernd Petrovitsch
2009-07-23 15:08 ` Dick Streefland [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4c5d.4a687cd4.a6013@altium.nl \
    --to=dick.streefland@altium.nl \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox