Boost logo

Boost Users :

Subject: [Boost-users] [Preprocessor] Auitomatic switch generation for template class instanciation
From: Olivier Tournaire (olitour_at_[hidden])
Date: 2008-11-23 09:31:12


Hi all,

I am currently completly lost with boost::preprocessor. I am trying to
automatically generate a switch ... case structure with this lib and I
cannot figure how I can do what I want.
Here is the problem. I have a class templated over 2 parameters (in reality,
more ...). At run time, I need to choose a specific instanciation for a user
given values. An example :

class MyDataEnergy
{
   // ...
};

class AnotherDataEnergy
{
   // ...
};

class MyPriorEnergy
{
   // ...
};

class AnotherPriorEnergy
{
   // ...
};

template <typename DataEnergyType , typename PriorEnergyType> class MyModel
: public DataEnergyType , public PriorEnergyType
{
   // ...
};

Now, somewhere in the code, a user specifies which energies to use, and I
have to instanciate MyModel with those energies:

// ...
int priorType = ValueComingFromSomewhere();
int dataType = AnotherValueComingFromSomewhere();

    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 ?

Hope you could help,

Best regards,

Olivier

-- 
Dr. Olivier Tournaire
Chargé de recherches
MATIS - Institut Géographique National
73, Ave de Paris
94165 St Mandé cedex, France
tel: (+33) 1 43 98 80 00 - 71 25
fax: (+33) 1 43 98 85 81


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