Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Preprocessor magic to expand template instantiation (2)

Reply
Thread Tools

Preprocessor magic to expand template instantiation (2)

 
 
mathieu
Guest
Posts: n/a
 
      08-06-2009
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[i], "TYPE1" ) == 0 )
{
type1 *t = Create<TYPE1>();
}
else if( strcmp(file[i], "TYPE2" ) == 0 )
{
type2 *t = Create<TYPE2>();
}
}
return 0;
}
 
Reply With Quote
 
 
 
 
mathieu
Guest
Posts: n/a
 
      08-06-2009
On Aug 6, 11:55*am, mathieu <mathieu.malate...@gmail.com> 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[i], "TYPE1" ) == 0 )
> * * * {
> * * * type1 *t = Create<TYPE1>();
> * * * }
> * * else if( strcmp(file[i], "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...ch_builtin.cpp

Thx
 
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
Preprocessor magic to expand template instantiation ? mathieu C++ 8 08-06-2009 10:13 PM
Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. krunalbauskar@gmail.com C++ 1 12-25-2006 03:51 PM
Magic method instantiation Artūras Šlajus Ruby 1 11-05-2006 11:37 PM
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
Cinema.Craft.Encoder.SP.v2.70.01.05, Magic.Bullet.Editor.v.1.01.for.Avid.Xpress.Pro, Magic.Bullet.Editor.v.1.01.for.Premiere.Pro, Magic.Bullet.Editor.v.1.01.for.Sony.Vegas, Avid.Xpress.Pro.HD.v5.0 1CD, Sony.Vegas.v5.0d, ola DVD Video 0 01-14-2005 10:53 AM



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