Boost logo

Boost Users :

Subject: Re: [Boost-users] [Preprocessor] Auitomatic switch generation for template class instanciation
From: Manfred Doudar (manfred.doudar_at_[hidden])
Date: 2008-11-23 11:35:14


Hello Oliver,

See inline below..

On Sun, 23 Nov 2008 15:31:12 +0100
"Olivier Tournaire" <olitour_at_[hidden]> wrote:

>
> switch(priorType)
> {
> case 0:
> switch(dataType)
> {
> case 0:
> {
> MyModel<MyPriorEnergy,MyDataEnergy> aModel;
> aModel.SetInput(...);
> aModel.Optimize(...);
> }
> break;
> case 1:
> {
> MyModel<MyPriorEnergy,AnotherDataEnergy> aModel;
> aModel.SetInput(...);
> aModel.Optimize(...);
> }
> break;
> default:
> break;
> }
> case 1:
> switch(dataType)
> {
> case 0:
> {
> MyModel<MyPriorEnergy,MyDataEnergy> aModel;
> aModel.SetInput(...);
> aModel.Optimize(...);
> }
> break;
> case 1:
> {
> MyModel<MyPriorEnergy,AnotherDataEnergy> aModel;
> aModel.SetInput(...);
> aModel.Optimize(...);
> }
> break;
> default:
> break;
> }
> default:
> break;
> }
>
> I would like to know if there is a way to automate the above switch
> generation. As I have 5 templates, and not only 2, this will really
> helps ... I tried using BOOST_PP_SEQ_FOR_EACH but did not came to a
> result. Then, there is a lot of duplication in the above code. Am I
> doing the right way ?
>

Looks like an awful lot of repetition, and would have to take a closer
look at your problem for advice on design. However, to get you going,
below is the Boost.PP work to do what you want - but think even that
could be done better..

#include <boost/preprocessor/repetition/repeat.hpp>
#include <boost/preprocessor/seq/for_each_i.hpp>

#define ECKY(r, data, i, elem) \
              case i: \
              { \
                  MyModel<MyPriorEnergy, elem> aModel; \
                  aModel.SetInput(...); \
                  aModel.Optimize(...); \
              } \
              break;
/**/

#define BAR(z, n, text) \
              case n: \
              swtich(dataType) \
              { \
                  BOOST_PP_SEQ_FOR_EACH_I(ECKY, n, text) \
                  default: \
                  break; \
              }
/**/

#define FOO(n, seq) \
             switch(priorType) \
             { \
                 BOOST_PP_REPEAT(n, BAR, seq) \
                 default: \
                 break; \
             }

FOO(2, (MyDataEnergy)(AnotherDataEnergy))

Cheers,

-- 
Manfred
24 November, 2008

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net