структура из типов
От: Vinick Россия  
Дата: 27.09.06 15:22
Оценка:
Не знаю даже как объяснить по человечески чего я хочу...
Хочу сделать как нибудь так.
namespace draw
{
  template <typename T>
  struct draw_crt
  {
     void operator()(const T & t) const{/*...*/}
  };
  template <typename T>
  struct draw_lcd
  {
     void operator()(const T & t) const{/*...*/}
  };
  template <typename T>
  struct draw_on_printer
  {
    void operator()(const T & t) const{/*...*/}
  };
};

namespace rotate
{
   template <typename T>
   struct rotate_clockwise
   {
      void operator()(T & t) {/*...*/}
   };
   template <typename T>
   struct rotate_counterclockwise
   {
      void operator()(T & t) {/*...*/}
   };
};

template < typename FunctorMap, typename Object>
void draw_and_rotate(Object obj)
{
   typedef FunctorMap::rotator  rotator;
   typedef FunctorMap::drawer   drawer;
   rotator r;
   r(obj);
   drawer d;
   d(obj);
}

int main()
{
 ... 
 FunctorMap fm; // ????
 switch(atoi(argv[1]))
 {
   case 1:
      fm.add_drawer(draw_crt);
      break;
   case 2:
      fm.add_drawer(draw_lcd);
      break;
 };
 switch(atoi(argv[2]))
 {
   case 1:
       fm.add_rotator(rotate_clockwise) ;
       break;
   case 2:
       fm.add_rotator(rotate_counterclockwise) ;
       break;
  };
  Circle c;
  draw_and_rotate</*???*/>(c)
...
}


Получать и устанавливать тип от FunctionMap можно и по индексу....
Т.е нужен такой хитрый compile-runtime полиморфизм.

Можно конечно сделать функторы draw_* и rotate_* унаследованные от абстрактного класа и сделать виртуальный метод, а потом в main
создавать их через new. Но мне так не нравится.

Вопрос 1: Такое вобще реализуемо на C++ ?
Вопрос 2: Может ли тут помочь boost::mpl и как?
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.