apply parameters to another function - in a generic way

J

Josef Angermeier

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
 
A

Artie Gold

Josef said:
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
 
M

Malcolm

Josef Angermeier said:
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).
 
A

Alexei A. Frounze

Malcolm said:
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
 
L

lpalozzi

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

-Leonardo
 
S

SM Ryan

# #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.
 
J

Josef Angermeier

...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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top