$include_dir="/home/hyper-archives/boost-users/include"; include("$include_dir/msg-header.inc") ?>
From: John Christopher (jcxxr_at_[hidden])
Date: 2006-03-19 13:18:31
"Paul Mensonides" wrote:
> Yeah, that won't work because the preprocessor cannot evaluate
> 'boost::mpl::size<TTypes>::value'.  If you're going to use the 
> preprocessor for
> this, you need to store the types in a preprocessor data structure, not an
> mpl::vector.  (Note, BTW, than an MPL vector can be trivial created from a
> preprocessor data structure.)
Thanks for the idea; the program compiles and works fine; it is easy to 
maintain since I have only the preprocessor data structure to keep updated 
when I add T classes. I am getting some type checking for instance if I add 
to the TYPES preprocessor data structure a type that does not exist. MPL and 
preprocesor library are great tools!
JCR
#define MY_TYPES (T0) (T1) (T2) (T3) (T4)
typedef boost::mpl::vector<BOOST_PP_SEQ_ENUM(MY_TYPES)> TTypes;
struct Functor
{
  void operator()(const int& i)
  {
    switch(i)
    {
      #define CASES(z, n, text) 
\
        case n: 
\
          vec.push_back(new boost::mpl::at<TTypes, boost::mpl::int_<n> 
 >::type);  \
          break;
      BOOST_PP_REPEAT(BOOST_PP_SEQ_SIZE(MY_TYPES), CASES, ~);
    }
  }
};
"Paul Mensonides" <pmenso57_at_[hidden]> wrote in message 
news:20060319010923.420C61055A1_at_wowbagger.osl.iu.edu...
>
>
>> -----Original Message-----
>> From: boost-users-bounces_at_[hidden]
>> [mailto:boost-users-bounces_at_[hidden]] On Behalf Of
>> John Christopher
>> Sent: Saturday, March 18, 2006 8:26 AM
>> To: boost-users_at_[hidden]
>> Subject: Re: [Boost-users] MPL to get rid of cumbersome switch
>>
>> > Given its prepetitive nature you could use boost
>> preprocessor library
>> > to generate this switch...
>>
>> That's a good idea.
>> I could define
>> #define NUMBER_CASES 4
>> and the switch would become something like that:
>>     switch(i)
>>     {
>>       #define CASES(z, n, text)
>> \
>>         case n:
>> \
>>           vec.push_back(new boost::mpl::at<TTypes,
>> boost::mpl::int_<n>  >::type);  \
>>           break;
>>       BOOST_PP_REPEAT(NUMBER_CASES, CASES, ~);
>>     }
>> this compiles and works fine, but, since the number of cases
>> in the switch must equal the size of the vector of type, I'd
>> rather write something like:
>> #define NUMBER_CASES boost::mpl::size<TTypes>::value
>>
>> but this does not compile and I am getting the error
>
> Yeah, that won't work because the preprocessor cannot evaluate
> 'boost::mpl::size<TTypes>::value'.  If you're going to use the 
> preprocessor for
> this, you need to store the types in a preprocessor data structure, not an
> mpl::vector.  (Note, BTW, than an MPL vector can be trivial created from a
> preprocessor data structure.)
>
> Regards,
> Paul Mensonides