Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > preprocessor question

Reply
Thread Tools

preprocessor question

 
 
David Resnick
Guest
Posts: n/a
 
      12-01-2008
Given this code fragment:

#define IFV(func, number) \
incomingFunctVal<CVxicBridgedXferIncomingAction> val##number
( \
number, &CVxicBridgedXferIncomingAction::func); \
m_transFnMap.insert(fnMap::value_type(#func, val##number));

IFV( DoJoin, 1 );
IFV( DoJoinRestartReco, 2 );
IFV( DummyAction, 3 );
IFV( RestartReco, 4 );
....
IFV( SomeFunc, 100 );

I'm wondering if there is a way to have the numbers in the function
macro increment automatically?
The code in the macro isn't really subject to change (it is the way to
use a state machine
implementation that I can't modify). I'm just finding it to be a
maintenance pain when
I want to delete a function with a middle number. I've been swapping
the last
function into the place of the deleted one to avoid renumbering the
rest, which is
OK in that order doesn't matter, but is there a better way? The fact
that ## is
used to glue the number into an identifier makes me doubt that this
can be repaired,
but I'd be very happy to be proved wrong.

Thanks!

-David
 
Reply With Quote
 
 
 
 
AnonMail2005@gmail.com
Guest
Posts: n/a
 
      12-01-2008
On Dec 1, 10:16*am, David Resnick <lndresn...@gmail.com> wrote:
> Given this code fragment:
>
> * * * * #define IFV(func, number) \
> * * * * incomingFunctVal<CVxicBridgedXferIncomingAction> val##number
> ( \
> * * * * * * number, &CVxicBridgedXferIncomingAction::func); \
> * * * * m_transFnMap.insert(fnMap::value_type(#func, val##number));
>
> * * * * IFV( DoJoin, 1 );
> * * * * IFV( DoJoinRestartReco, 2 );
> * * * * IFV( DummyAction, 3 );
> * * * * IFV( RestartReco, 4 );
> * * * * ....
> * * * * IFV( SomeFunc, 100 );
>
> I'm wondering if there is a way to have the numbers in the function
> macro increment automatically?
> The code in the macro isn't really subject to change (it is the way to
> use a state machine
> implementation that I can't modify). *I'm just finding it to be a
> maintenance pain when
> I want to delete a function with a middle number. *I've been swapping
> the last
> function into the place of the deleted one to avoid renumbering the
> rest, which is
> OK in that order doesn't matter, but is there a better way? *The fact
> that ## is
> used to glue the number into an identifier makes me doubt that this
> can be repaired,
> but I'd be very happy to be proved wrong.
>
> Thanks!
>
> -David


First cut, create a function called IFV2 (for instance), with a static
counter variable that you increment. The call to IFV and the counter
would be within the function scope.

Just because someone creates crazy preprocessor programming doesn't
mean you can't isolate it and use real programming solution to
minimize it's impact.

HTH
 
Reply With Quote
 
 
 
 
Maxim Yegorushkin
Guest
Posts: n/a
 
      12-01-2008
On Dec 1, 3:16*pm, David Resnick <lndresn...@gmail.com> wrote:
> Given this code fragment:
>
> * * * * #define IFV(func, number) \
> * * * * incomingFunctVal<CVxicBridgedXferIncomingAction> val##number
> ( \
> * * * * * * number, &CVxicBridgedXferIncomingAction::func); \
> * * * * m_transFnMap.insert(fnMap::value_type(#func, val##number));
>
> * * * * IFV( DoJoin, 1 );
> * * * * IFV( DoJoinRestartReco, 2 );
> * * * * IFV( DummyAction, 3 );
> * * * * IFV( RestartReco, 4 );
> * * * * ....
> * * * * IFV( SomeFunc, 100 );
>
> I'm wondering if there is a way to have the numbers in the function
> macro increment automatically?


You could use __COUNTER__ macro. If it is supported by your compiler.

Otherwise, draw inspiration from boost preprocessor library:
http://www.boost.org/doc/libs/1_37_0...f/counter.html

> The code in the macro isn't really subject to change (it is the way to
> use a state machine
> implementation that I can't modify).


That's too bad, since such an interface is real pain.

I wonder why incomingFunctVal<> constructor needs that integer? If it
can be auto-generated (possibly in incomingFunctVal<> constructor) by
incrementing a global or class static variable, and if you don't
really need valN objects (their copies are stored in m_transFnMap
anyway) the macro could be simplified to:

typedef CVxicBridgedXferIncomingAction X; // for brevity
int x = 0; // the global counter

#define IFV(func) \
m_transFnMap.insert( \
fnMap::value_type( \
#func \
, incomingFunctVal<X>(++x, &X::func) \
) \
);

--
Max


 
Reply With Quote
 
David Resnick
Guest
Posts: n/a
 
      12-01-2008
On Dec 1, 11:34*am, Maxim Yegorushkin <maxim.yegorush...@gmail.com>
wrote:
> On Dec 1, 3:16*pm, David Resnick <lndresn...@gmail.com> wrote:
>
>
>
> > Given this code fragment:

>
> > * * * * #define IFV(func, number) \
> > * * * * incomingFunctVal<CVxicBridgedXferIncomingAction> val##number
> > ( \
> > * * * * * * number, &CVxicBridgedXferIncomingAction::func); \
> > * * * * m_transFnMap.insert(fnMap::value_type(#func, val##number));

>
> > * * * * IFV( DoJoin, 1 );
> > * * * * IFV( DoJoinRestartReco, 2 );
> > * * * * IFV( DummyAction, 3 );
> > * * * * IFV( RestartReco, 4 );
> > * * * * ....
> > * * * * IFV( SomeFunc, 100 );

>
> > I'm wondering if there is a way to have the numbers in the function
> > macro increment automatically?

>
> You could use __COUNTER__ macro. If it is supported by your compiler.
>
> Otherwise, draw inspiration from boost preprocessor library:http://www.boost.org/doc/libs/1_37_0...doc/ref/counte...
>
> > The code in the macro isn't really subject to change (it is the way to
> > use a state machine
> > implementation that I can't modify).

>
> That's too bad, since such an interface is real pain.
>
> I wonder why incomingFunctVal<> constructor needs that integer? If it
> can be auto-generated (possibly in incomingFunctVal<> constructor) by
> incrementing a global or class static variable, and if you don't
> really need valN objects (their copies are stored in m_transFnMap
> anyway) the macro could be simplified to:
>
> * * typedef CVxicBridgedXferIncomingAction X; // for brevity
> * * int x = 0; // the global counter
>
> * * #define IFV(func) \
> * * m_transFnMap.insert( \
> * * * * fnMap::value_type( \
> * * * * * * * #func \
> * * * * * * , incomingFunctVal<X>(++x, &X::func) \
> * * * * * * ) \
> * * * * );
>
> --
> Max


That is a very helpful suggestion, thanks. The use of the integer in
the original macro (at least the ## version) was apparently just to
avoid re-declaring identifiers in the same scope, which is fixed by
your macro.

-David
 
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
Compiler error occurred when try to use a flexible template expression in preprocessor definesCompiler error occurred when try to use a flexible template expression in preprocessor defines snnn C++ 6 03-14-2005 04:09 PM
preprocessor, token concatenation, no valid preprocessor token Cronus C++ 1 07-14-2004 11:10 PM
a question about preprocessor xuatla C++ 1 07-10-2004 11:03 AM
preprocessor implementation GURU question Dan W. C++ 9 12-04-2003 02:24 AM
Preprocessor's question ³á³á³á³á C Programming 1 09-12-2003 05:32 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57