Go Back   Rhinocerus > Newsgroup > Newsgroup comp.language.c++

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 04-14-2012, 02:41 AM
gry
Guest
 
Posts: n/a
Default how to make a macro stringify and paste a -D defined arg

[g++ 4.4.0, linux, large SW project so I can't change compilers or the
api I'm talking to]
I have a macro:
#define RegisterFactory(clazz) \
clazz clazz##_instance; \
extern "C" Foo::HHFactory *get##clazz() { return
&clazz##_instance; } \
Foo::HHRegistrar clazz##_registrar(#clazz)
which works fine for literal arguments. But when I give it an
argument that was defined on the compile line:

g++ -DFACTORYNAME=MyFactory

it fails. I understand why... the arg is used in paste and stringify
contexts -- but is there some reasonable way to fix this?
I want
RegisterFactory(FACTORYNAME) to expand to:
MyFactory MyFactory_instance;
extern "C" Foo::HHFactory *getMyFactory() { return
&MyFactory_instance; } \
Foo::HHRegistrar MyFactory_registrar("MyFactory")

The original macro is used in many places so I need something that
cleanly replaces it. I tried #define FACTORYNAME MyFactory, but no
difference(not that I really expected one).
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 04-14-2012, 06:25 AM
Kalle Olavi Niemitalo
Guest
 
Posts: n/a
Default Re: how to make a macro stringify and paste a -D defined arg

gry <georgeryoung@gmail.com> writes:

> #define RegisterFactory(clazz) \
> clazz clazz##_instance; \
> extern "C" Foo::HHFactory *get##clazz() { return &clazz##_instance; } \
> Foo::HHRegistrar clazz##_registrar(#clazz)
> which works fine for literal arguments. But when I give it an
> argument that was defined on the compile line:
>
> g++ -DFACTORYNAME=MyFactory
>
> it fails.


Define a new macro that takes FACTORYNAME as an argument and
forwards it to your original macro (renamed below):

#define RegisterFactoryNoExpand(clazz) \
clazz clazz##_instance; \
extern "C" Foo::HHFactory *get##clazz() { return &clazz##_instance; } \
Foo::HHRegistrar clazz##_registrar(#clazz)
#define RegisterFactory(clazz) RegisterFactoryNoExpand(clazz)

#define FACTORYNAME MyFactory
RegisterFactory(FACTORYNAME);

Then, the preprocessor will expand FACTORYNAME.
http://c-faq.com/ansi/stringize.html
Reply With Quote
  #3 (permalink)  
Old 04-14-2012, 12:36 PM
gry
Guest
 
Posts: n/a
Default Re: how to make a macro stringify and paste a -D defined arg

On Apr 14, 2:25*am, Kalle Olavi Niemitalo <k...@iki.fi> wrote:
> gry <georgeryo...@gmail.com> writes:
> > #define RegisterFactory(clazz) \
> > * * clazz clazz##_instance; \
> > * * extern "C" Foo::HHFactory *get##clazz() { return &clazz##_instance; } \
> > * * Foo::HHRegistrar clazz##_registrar(#clazz)
> > which works fine for literal arguments. *But when I give it an
> > argument that was defined on the compile line:

>
> > g++ -DFACTORYNAME=MyFactory

>
> > it fails.

>
> Define a new macro that takes FACTORYNAME as an argument and
> forwards it to your original macro (renamed below):
>
> #define RegisterFactoryNoExpand(clazz) \
> * * clazz clazz##_instance; \
> * * extern "C" Foo::HHFactory *get##clazz() { return &clazz##_instance; } \
> * * Foo::HHRegistrar clazz##_registrar(#clazz)
> #define RegisterFactory(clazz) RegisterFactoryNoExpand(clazz)
>
> #define FACTORYNAME MyFactory
> RegisterFactory(FACTORYNAME);
>
> Then, the preprocessor will expand FACTORYNAME.http://c-faq.com/ansi/stringize.html


That works beautifully. Thanks!
Reply With Quote
 
Reply

Thread Tools
Display Modes

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 Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 01:51 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.