Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > apply parameters to another function - in a generic way

Reply
Thread Tools

apply parameters to another function - in a generic way

 
 
Josef Angermeier
Guest
Posts: n/a
 
      07-18-2005

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

 
Reply With Quote
 
 
 
 
Artie Gold
Guest
Posts: n/a
 
      07-18-2005
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'.*
 
Reply With Quote
 
 
 
 
Malcolm
Guest
Posts: n/a
 
      07-18-2005

"Josef Angermeier" <(E-Mail Removed)> 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).


 
Reply With Quote
 
Alexei A. Frounze
Guest
Posts: n/a
 
      07-18-2005
"Malcolm" <(E-Mail Removed)> wrote in message
news:dbgtes$i3c$(E-Mail Removed)-infra.bt.com...
>
> "Josef Angermeier" <(E-Mail Removed)> 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


 
Reply With Quote
 
lpalozzi@gmail.com
Guest
Posts: n/a
 
      07-18-2005
A generic, but not compiler portable solution, is to use gcc's
exetension "Constructing Function Calls".

-Leonardo

 
Reply With Quote
 
SM Ryan
Guest
Posts: n/a
 
      07-18-2005
"Josef Angermeier" <(E-Mail Removed)> 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!
 
Reply With Quote
 
Josef Angermeier
Guest
Posts: n/a
 
      07-19-2005

> ...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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function to apply superset of arguments to a function Andrey Fedorov Python 9 09-10-2009 03:36 AM
generic interfaces with generic methods Murat Tasan Java 1 02-03-2009 12:17 PM
Function.call and Function.apply HopfZ Javascript 1 08-25-2006 06:26 PM
Class Member Data and Member Function Parameters - Should Parameters Be Data Members? Jason C++ 2 05-13-2006 07:11 AM
[XSLT] could not apply "apply-templates" Stefan Siegl XML 1 07-18-2003 09:43 AM



Advertisments