Preprocessor magic to expand template instantiation (2)

Discussion in 'C++' started by mathieu, Aug 6, 2009.

  1. mathieu

    mathieu Guest

    Let's try again,

    Hi there,

    I am looking for a trick to avoid maintaining the 'Create' function
    as describe below. All it does is a simple template instantiation, are
    there any trick which would avoid having to maintain this 'Create' as
    the number of enum grows ?

    Thanks

    #include <string.h>

    typedef enum {
    TYPE1,
    TYPE2
    } TYPES;

    class type1 {};
    class type2 {};

    template <int T> struct Factory;
    template <> struct Factory<TYPE1> { typedef type1 Type; };
    template <> struct Factory<TYPE2> { typedef type2 Type; };

    template <int N>
    typename Factory<N>::Type* Create()
    {
    return new typename Factory<N>::Type;
    }

    int main()
    {
    const char *file[] = {
    "TYPE2",
    "TYPE1",
    };
    const unsigned int n = sizeof(file) / sizeof(*file);
    for(unsigned int i = 0; i < n; ++i)
    {
    if( strcmp(file, "TYPE1" ) == 0 )
    {
    type1 *t = Create<TYPE1>();
    }
    else if( strcmp(file, "TYPE2" ) == 0 )
    {
    type2 *t = Create<TYPE2>();
    }
    }
    return 0;
    }
     
    mathieu, Aug 6, 2009
    #1
    1. Advertising

  2. mathieu

    mathieu Guest

    On Aug 6, 11:55 am, mathieu <> wrote:
    > Let's try again,
    >
    > Hi there,
    >
    >   I am looking for a trick to avoid maintaining the 'Create' function
    > as describe below. All it does is a simple template instantiation, are
    > there any trick which would avoid having to maintain this 'Create' as
    > the number of enum grows ?
    >
    > Thanks
    >
    > #include <string.h>
    >
    > typedef enum {
    >   TYPE1,
    >   TYPE2
    >
    > } TYPES;
    >
    > class type1 {};
    > class type2 {};
    >
    > template <int T> struct Factory;
    > template <> struct Factory<TYPE1> { typedef type1 Type; };
    > template <> struct Factory<TYPE2> { typedef type2 Type; };
    >
    > template <int N>
    > typename Factory<N>::Type* Create()
    > {
    >    return new typename Factory<N>::Type;
    >
    > }
    >
    > int main()
    > {
    >   const char *file[] = {
    >     "TYPE2",
    >     "TYPE1",
    >   };
    >   const unsigned int n = sizeof(file) / sizeof(*file);
    >   for(unsigned int i = 0; i < n; ++i)
    >     {
    >     if( strcmp(file, "TYPE1" ) == 0 )
    >       {
    >       type1 *t = Create<TYPE1>();
    >       }
    >     else if( strcmp(file, "TYPE2" ) == 0 )
    >       {
    >       type2 *t = Create<TYPE2>();
    >       }
    >     }
    >   return 0;
    >
    > }
    >
    >


    I just found out: BOOST_PP_LIST_FOR_EACH which looks pretty cool.

    http://www.boost.org/doc/libs/1_39_0/libs/preprocessor/doc/examples/catch_builtin.cpp

    Thx
     
    mathieu, Aug 6, 2009
    #2
    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. Preprocessor magic needed

    , Mar 17, 2006, in forum: C Programming
    Replies:
    8
    Views:
    348
    CBFalconer
    Mar 20, 2006
  2. Replies:
    1
    Views:
    587
    Salt_Peter
    Dec 25, 2006
  3. mathieu
    Replies:
    8
    Views:
    466
    Alf P. Steinbach
    Aug 6, 2009
  4. Artūras Šlajus

    Magic method instantiation

    Artūras Šlajus, Nov 5, 2006, in forum: Ruby
    Replies:
    1
    Views:
    102
    Daniel Sheppard
    Nov 5, 2006
  5. Giles Bowkett
    Replies:
    9
    Views:
    417
    Giles Bowkett
    Dec 17, 2007
Loading...

Share This Page