apply parameters to another function - in a generic way

Discussion in 'C Programming' started by Josef Angermeier, Jul 18, 2005.

  1. Hello

    I need to write lots of wrapper functions, which look
    like that:

    wrapper_my_func(int a, int b)
    {
    my_func(a, b);

    <extra_code>
    }

    Now my problem to express this in a generic way, by using macro.

    #define DEF_WRAPPER(ret, func, params...) \
    \
    ret \
    wrapper ## func (params) \
    { \
    /* Here the problem, because */ \
    func(params); \
    \
    }

    The main problem is to strip off the parameter
    types of the arguments for the embedded function call, because

    DEF_WRAPPER(void, my_func, int a, int b);

    results in
    ....
    wrapper_my_func(...)
    {
    my_func(int a, in b); <---

    ....

    how to solve that ?


    thanks in advance
    josef
    Josef Angermeier, Jul 18, 2005
    #1
    1. Advertising

  2. Josef Angermeier

    Artie Gold Guest

    Josef Angermeier wrote:
    >
    > Hello
    >
    > I need to write lots of wrapper functions, which look
    > like that:
    >
    > wrapper_my_func(int a, int b)
    > {
    > my_func(a, b);
    >
    > <extra_code>
    > }
    >
    > Now my problem to express this in a generic way, by using macro.
    >
    > #define DEF_WRAPPER(ret, func, params...) \
    > \
    > ret \
    > wrapper ## func (params) \
    > { \
    > /* Here the problem, because */ \
    > func(params); \
    > \
    > }
    >
    > The main problem is to strip off the parameter
    > types of the arguments for the embedded function call, because
    >
    > DEF_WRAPPER(void, my_func, int a, int b);
    >
    > results in
    > ...
    > wrapper_my_func(...)
    > {
    > my_func(int a, in b); <---
    >
    > ...
    >


    You *could* do something like:

    #define DEF_WRAPPER(ret, func, params, args) \
    ret wrapper_ ## func params { \
    /* wrapper code */ \
    func args; \
    /* more wrapper code */ \
    }

    ....and use it like:

    DEF_WRAPPER(void, my_func, (int a, int b), (a, b));

    It's ugly in many ways...but sometimes ugly is the best solution. ;-) YMMV.

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas
    http://it-matters.blogspot.com (new post 12/5)
    http://www.cafepress.com/goldsays
    *If it belongs to `it' it's `its'.*
    Artie Gold, Jul 18, 2005
    #2
    1. Advertising

  3. Josef Angermeier

    Malcolm Guest

    "Josef Angermeier" <> wrote
    >
    > how to solve that ?
    >

    Basically you can't build an argument list at run-time using C, and you
    can't do much at compile time either. Functions must be called explicitly
    with the arguments listed in their prototypes.

    (There are minor workarounds, but this is essentially the situation).
    Malcolm, Jul 18, 2005
    #3
  4. "Malcolm" <> wrote in message
    news:dbgtes$i3c$-infra.bt.com...
    >
    > "Josef Angermeier" <> wrote
    > >
    > > how to solve that ?
    > >

    > Basically you can't build an argument list at run-time using C, and you
    > can't do much at compile time either. Functions must be called explicitly
    > with the arguments listed in their prototypes.
    >
    > (There are minor workarounds, but this is essentially the situation).


    Well, that (the dynamic building of the arg list at run time) can be done
    for the particular platform, of course, suffering from all the problems of
    this solution. But isn't this what is done in COM?

    The best workaround would probably to transfer the arguments to a function
    either as optional ones (using the ellipsis) with a way to distinguish them
    inside or just by reference and some internal parsing.

    Alex
    Alexei A. Frounze, Jul 18, 2005
    #4
  5. Josef Angermeier

    Guest

    A generic, but not compiler portable solution, is to use gcc's
    exetension "Constructing Function Calls".

    -Leonardo
    , Jul 18, 2005
    #5
  6. Josef Angermeier

    SM Ryan Guest

    "Josef Angermeier" <> wrote:

    # #define DEF_WRAPPER(ret, func, params...) \

    CPP has limitted functionality. You can do this with a more advance
    macro processor like M4 or text processor written in Perl, awk, Tcl,
    etc.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    OOOOOOOOOO! NAVY SEALS!
    SM Ryan, Jul 18, 2005
    #6

  7. > ...and use it like:
    >
    > DEF_WRAPPER(void, my_func, (int a, int b), (a, b));
    >
    > It's ugly in many ways...but sometimes ugly is the best solution. ;-)
    > YMMV.
    >


    Thanks alot! Sure it's kind of ugly, but i didn't think of that. Best
    wishes!

    josef
    Josef Angermeier, Jul 19, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Murat Tasan
    Replies:
    1
    Views:
    8,025
    Chaitanya
    Feb 3, 2009
  2. Stefan Siegl
    Replies:
    1
    Views:
    949
    Marrow
    Jul 18, 2003
  3. Replies:
    2
    Views:
    423
  4. Andrey Fedorov
    Replies:
    9
    Views:
    276
    Carl Banks
    Sep 10, 2009
  5. HopfZ

    Function.call and Function.apply

    HopfZ, Aug 25, 2006, in forum: Javascript
    Replies:
    1
    Views:
    87
    Lasse Reichstein Nielsen
    Aug 25, 2006
Loading...

Share This Page