Boost logo

Boost Users :

From: John Christopher (jcxxr_at_[hidden])
Date: 2006-03-17 17:06:27


Hello,
Still reading... Chapter 11 is still a few pages away...
Any way, I rewrote the program and I have something much simpler.
The vector of types has not changed:
    typedef boost::mpl::vector<T0, T1, T2, T3> TTypes;
but the Functor has a new switch statement that is much easier to maintain.
The nnumber of cases in the switch must be the number of elements in the
vector of types... Adding new cases is no big deal anyway.
switch(i)
{
case 0:
    vec.push_back(new boost::mpl::at<TTypes, boost::mpl::int_<0> >::type);
    break;
case 1:
    vec.push_back(new boost::mpl::at<TTypes, boost::mpl::int_<1> >::type);
    break;
....

JCR

#include <iostream>
#include <vector>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/at.hpp>

struct Tbase
{
  virtual void display() = 0;
  virtual ~Tbase() = 0;
};
inline Tbase::~Tbase() { }
struct T0 : public Tbase
{
  virtual void display() { std::cout << "display T0" << std::endl; }
};
struct T1 : public Tbase
{
  virtual void display() { std::cout << "display T1" << std::endl; }
};
struct T2 : public Tbase
{
  virtual void display() { std::cout << "display T2" << std::endl; }
};
struct T3 : public Tbase
{
  virtual void display() { std::cout << "display T3" << std::endl; }
};
typedef boost::mpl::vector<T0, T1, T2, T3> TTypes;
std::vector<Tbase*> vec;
struct Functor
{
  void operator()(const int& i)
  {
    switch(i)
    {
      case 0:
        vec.push_back(new boost::mpl::at<TTypes, boost::mpl::int_<0>
>::type);
        break;
      case 1:
        vec.push_back(new boost::mpl::at<TTypes, boost::mpl::int_<1>
>::type);
        break;
      case 2:
        vec.push_back(new boost::mpl::at<TTypes, boost::mpl::int_<2>
>::type);
        break;
      case 3:
        vec.push_back(new boost::mpl::at<TTypes, boost::mpl::int_<3>
>::type);
        break;
    }
  }
};
struct Display
{
  void operator() (Tbase* p)
  {
    p->display();
  }
};
struct Clean
{
  void operator() (Tbase* p)
  {
    delete p;
  }
};
int main (int argc, char ** argv)
{
  std::vector<int> runtimeVec;
  runtimeVec.push_back(3);
  runtimeVec.push_back(0);
  runtimeVec.push_back(0);
  std::for_each(runtimeVec.begin(), runtimeVec.end(), Functor());
  std::cout << vec.size() << std::endl;
  std::for_each(vec.begin(), vec.end(), Display());
  std::for_each(vec.begin(), vec.end(), Clean());
 return 0;
}

"François Duranleau" <duranlef_at_[hidden]> wrote in message
news:Pine.LNX.4.63.0603171314001.25367_at_brocoli.iro.umontreal.ca...
On Thu, 16 Mar 2006, David Abrahams wrote:

> Chapter 11 of "C++ Template Metaprogramming" shows how to generate the
> equivalent of a switch statement using the MPL.

I will need to save up some money to get that book.

-- 
François Duranleau
LIGUM, Université de Montréal
"Conflict may erupt at any time and any place. War is the destiny of which
  humanity can never wash its hands."
                          - Emperor Dornkirk, in _The Vision of Escaflowne_
--------------------------------------------------------------------------------
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users 

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