From: Nobody on
I am implementing a callback function for multiple callbacks, but the
callback function does not have a parameter such as a handle or number to
tell which callback the call is for. I have no control over it, so I can't
add my own parameters, so I have to provide different entry points that
calls a centralized routine with a sequence number. So I have functions like
this:

int func0(int para1,int para2) { return func(0,para1,para2);};
int func1(int para1,int para2) { return func(1,para1,para2);};
int func2(int para1,int para2) { return func(2,para1,para2);};
int func3(int para1,int para2) { return func(3,para1,para2);};
....

I plan to use a macro like this to generate the above using the
Token-Pasting Operator (##):

#define paster( n ) int func##n(int para1,int para2) { return
func(##n,para1,para2);}

So, to generate the above, I would use:

paster(0);
paster(1);
paster(2);
paster(3);
....

Is there a way to repeatedly call the macro a fixed number of times?
Something like #while or #for loop? Currently, I am using an Excel sheet to
generate the code. Example:

= "paster(" & A1 & ");"

Cells A1 to Ann contain sequence numbers filled by Edit-->Fill-->Series, so
I only have to do one copy and paste.

I am using VC6 and 2003.

Thank you


From: Igor Tandetnik on
Nobody wrote:
> I am implementing a callback function for multiple callbacks, but the
> callback function does not have a parameter such as a handle or number to
> tell which callback the call is for. I have no control over it, so I can't
> add my own parameters, so I have to provide different entry points that
> calls a centralized routine with a sequence number. So I have functions like
> this:
>
> int func0(int para1,int para2) { return func(0,para1,para2);};
> int func1(int para1,int para2) { return func(1,para1,para2);};
> int func2(int para1,int para2) { return func(2,para1,para2);};
> int func3(int para1,int para2) { return func(3,para1,para2);};
>
> I plan to use a macro like this to generate the above using the
> Token-Pasting Operator (##):

Consider a template instead:

template <int seq>
int func(int para1, int para2) { return func(seq, para1, para2); }

func<2>(42, 84);

> #define paster( n ) int func##n(int para1,int para2) { return
> func(##n,para1,para2);}

You don't need second ##. Drop it.

> Is there a way to repeatedly call the macro a fixed number of times?

Use the template, and you wouldn't need to. If for some strange reason you insist on using macros, there might be something like this in Boost Preprocessor library:

http://www.boost.org/doc/libs/1_42_0/libs/preprocessor/doc/index.html

--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
From: Barry Schwarz on
On Sun, 28 Feb 2010 13:00:59 -0500, "Nobody" <nobody(a)nobody.com>
wrote:

>I am implementing a callback function for multiple callbacks, but the
>callback function does not have a parameter such as a handle or number to
>tell which callback the call is for. I have no control over it, so I can't
>add my own parameters, so I have to provide different entry points that
>calls a centralized routine with a sequence number. So I have functions like
>this:
>
>int func0(int para1,int para2) { return func(0,para1,para2);};
>int func1(int para1,int para2) { return func(1,para1,para2);};
>int func2(int para1,int para2) { return func(2,para1,para2);};
>int func3(int para1,int para2) { return func(3,para1,para2);};
>...
>
>I plan to use a macro like this to generate the above using the
>Token-Pasting Operator (##):
>
>#define paster( n ) int func##n(int para1,int para2) { return
>func(##n,para1,para2);}

The preprocessor works on tokens. The purpose of the first ##
operator is to concatenate the literal func with the parameter n,
resulting in a single token (e.g., func1). What is the purpose of the
second ## operator? What would happen if you deleted it?

>
>So, to generate the above, I would use:
>
>paster(0);
>paster(1);
>paster(2);
>paster(3);
>...
>
>Is there a way to repeatedly call the macro a fixed number of times?

Not in C.

>Something like #while or #for loop? Currently, I am using an Excel sheet to
>generate the code. Example:
>
>= "paster(" & A1 & ");"
>
>Cells A1 to Ann contain sequence numbers filled by Edit-->Fill-->Series, so
>I only have to do one copy and paste.
>
>I am using VC6 and 2003.
>
>Thank you
>

--
Remove del for email
From: Nobody on
"Barry Schwarz" <schwarzb(a)dqel.com> wrote in message
news:7chlo5hohjp5c56ij8h68tbvnl106nnsv0(a)4ax.com...
> On Sun, 28 Feb 2010 13:00:59 -0500, "Nobody" <nobody(a)nobody.com>
> wrote:
>
>>I am implementing a callback function for multiple callbacks, but the
>>callback function does not have a parameter such as a handle or number to
>>tell which callback the call is for. I have no control over it, so I can't
>>add my own parameters, so I have to provide different entry points that
>>calls a centralized routine with a sequence number. So I have functions
>>like
>>this:
>>
>>int func0(int para1,int para2) { return func(0,para1,para2);};
>>int func1(int para1,int para2) { return func(1,para1,para2);};
>>int func2(int para1,int para2) { return func(2,para1,para2);};
>>int func3(int para1,int para2) { return func(3,para1,para2);};
>>...
>>
>>I plan to use a macro like this to generate the above using the
>>Token-Pasting Operator (##):
>>
>>#define paster( n ) int func##n(int para1,int para2) { return
>>func(##n,para1,para2);}
>
> The preprocessor works on tokens. The purpose of the first ##
> operator is to concatenate the literal func with the parameter n,
> resulting in a single token (e.g., func1). What is the purpose of the
> second ## operator? What would happen if you deleted it?

I guess I should have used less confusing names. In the example, I have a
central function func() to be called by func0(), func1(), func2(), etc. This
central function func(), has an additional parameter "int seq", and these
"n" functions supply a sequential number to func(). This example maybe
illustrate it better:

// Central function: Has 3 parameters, the first is sequence number
int func(int seq, int para1,int para2)
{
// Some code
};

// "n" functions: Have 2 parameters

int func0(int para1,int para2)
{
return func(0,para1,para2);
};

int func1(int para1,int para2)
{
return func(1,para1,para2);
};



From: Nobody on
"Nobody" <nobody(a)nobody.com> wrote in message
news:uUJaDSNuKHA.5940(a)TK2MSFTNGP02.phx.gbl...
> // Central function: Has 3 parameters, the first is sequence number

In case I wasn't clear enough, the sequence parameter is used to lookup an
array of structures representing the corresponding callback.


 |  Next  |  Last
Pages: 1 2 3 4
Prev: C++ Redistributables
Next: Force loading of DLL