how to make a macro stringify and paste a -D defined arg

Discussion in 'C++' started by gry, Apr 14, 2012.

  1. gry

    gry Guest

    [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).
    gry, Apr 14, 2012
    #1
    1. Advertising

  2. gry <> 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
    Kalle Olavi Niemitalo, Apr 14, 2012
    #2
    1. Advertising

  3. gry

    gry Guest

    On Apr 14, 2:25 am, Kalle Olavi Niemitalo <> wrote:
    > gry <> 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!
    gry, Apr 14, 2012
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris

    Macro to stringify an enum

    Chris, Jun 2, 2004, in forum: C Programming
    Replies:
    3
    Views:
    987
    Chris
    Jun 7, 2004
  2. n00m
    Replies:
    5
    Views:
    388
  3. Edward Rutherford

    Macro to stringify an enum

    Edward Rutherford, Jun 5, 2011, in forum: C Programming
    Replies:
    4
    Views:
    1,413
  4. Replies:
    7
    Views:
    164
  5. Replies:
    21
    Views:
    276
    Barry Schwarz
    Mar 5, 2014
Loading...

Share This Page