Advertisement
Guest User

Untitled

a guest
May 26th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.01 KB | None | 0 0
  1. #include <boost/foreach.hpp>
  2. #include <memory>
  3. #include <vector>
  4.  
  5. struct GameInfoPtr {
  6.   bool operator==(const GameInfoPtr&) const { return false; }
  7. }; // параметры для алгоритма
  8.  
  9. struct ParamType1 {};
  10. struct ParamType2 {};
  11. struct ParamType3 {};
  12.  
  13. template <class ParamType_> class FunctionBase
  14. {
  15. public:
  16.   typedef std::tr1::shared_ptr<FunctionBase<ParamType_> > FunctionBasePtr;
  17.   typedef typename ParamType_ ParamType;
  18.   virtual void func(ParamType* params) = 0;
  19. };
  20.  
  21.  
  22. template <class ParamType_> class Func1 : public FunctionBase<ParamType_>
  23. {
  24.   GameInfoPtr m_info;
  25. public:
  26.  
  27.   Func1(GameInfoPtr info_) : m_info(info_) {}
  28.   virtual void func(ParamType* params_) { do_func(params_); }
  29.  
  30.   void do_func(ParamType1* params_) {}
  31.  
  32.   bool isEqual(GameInfoPtr game_info) const { return game_info == m_info;  }
  33.  
  34. };
  35.  
  36.  
  37. template <class ParamType_> class Func2 : public FunctionBase<ParamType_>
  38. {
  39. public:
  40.  
  41.   struct DoFunc1
  42.   {
  43.     template <class ThisType, class U> static bool func(ThisType,U*) { return false; }
  44.     template <class ThisType> static bool func(ThisType,ParamType1*) { return true;  }
  45.   };
  46.  
  47.   struct DoFunc2
  48.   {
  49.     template <class ThisType, class U> static bool func(ThisType,U*) { return false; }
  50.     template <class ThisType> static bool func(ThisType,ParamType2*) { return true;  }
  51.   };
  52.  
  53.   struct DoFunc3
  54.   {
  55.     template <class ThisType, class U> static bool func(ThisType,U*) { return false; }
  56.     template <class ThisType> static bool func(ThisType,ParamType3*) { return true;  }
  57.   };
  58.  
  59.   Func2(GameInfoPtr info_) : m_info(info_) {}
  60.   virtual void func(ParamType_* params_)
  61.   {
  62.     if (DoFunc2::func(this, params_))
  63.       return;
  64.     if (DoFunc3::func(this, params_))
  65.       return;
  66.     DoFunc1::func(this, params_);
  67.   }
  68.  
  69.   bool isEqual(GameInfoPtr game_info) const { return game_info == m_info;  }
  70.  
  71.   GameInfoPtr m_info;
  72. };
  73.  
  74.  
  75.  
  76. template <class ParamType> class IPrototypeFactory
  77. {
  78. public:
  79.   typedef typename FunctionBase<ParamType>::FunctionBasePtr FunctionBasePtr;
  80.   virtual FunctionBasePtr create(GameInfoPtr game_info) = 0;
  81. };
  82.  
  83.  
  84.  
  85. template <class FuncType> class PrototypeFactory : public IPrototypeFactory<typename FuncType::ParamType>
  86. {
  87. public:
  88.  
  89.   typedef typename FuncType::ParamType   ParamType;
  90.   typedef std::tr1::shared_ptr<FuncType> FuncTypePtr;
  91.  
  92.   virtual FunctionBasePtr create(GameInfoPtr game_info)
  93.   {
  94.     BOOST_FOREACH(FuncTypePtr& p, m_vPrototypes)
  95.       if (p->isEqual(game_info))
  96.         return p;
  97.     FuncTypePtr newFunc(new FuncType(game_info));
  98.     m_vPrototypes.push_back(newFunc);
  99.     return newFunc;
  100.   }
  101. private:
  102.  
  103.   std::vector<FuncTypePtr> m_vPrototypes;
  104.  
  105. };
  106.  
  107. struct MegaParamType1 : public ParamType1, public ParamType2, public ParamType3 {};
  108. struct MegaParamType2 : public ParamType1, public ParamType3 {};
  109.  
  110. int main()
  111. {
  112.   GameInfoPtr game_info1;
  113.   GameInfoPtr game_info2;
  114.   GameInfoPtr game_info3;
  115.  
  116.   PrototypeFactory<Func1<MegaParamType1> > factory1_1;
  117.   PrototypeFactory<Func2<MegaParamType1> > factory1_2;
  118.   std::vector<FunctionBase<MegaParamType1>::FunctionBasePtr> vSequence1;
  119.  
  120.   vSequence1.push_back(factory1_1.create(game_info1));
  121.   vSequence1.push_back(factory1_2.create(game_info2));
  122.   vSequence1.push_back(factory1_1.create(game_info3));
  123.   vSequence1.push_back(factory1_1.create(game_info1));
  124.  
  125.   MegaParamType1 params1;
  126.   BOOST_FOREACH(FunctionBase<MegaParamType1>::FunctionBasePtr& action, vSequence1)
  127.     action->func(&params1);
  128.  
  129.  
  130.  
  131.  
  132.   PrototypeFactory<Func1<MegaParamType2> > factory2_1;
  133.   PrototypeFactory<Func2<MegaParamType2> > factory2_2;
  134.   std::vector<FunctionBase<MegaParamType2>::FunctionBasePtr> vSequence2;
  135.  
  136.   vSequence2.push_back(factory2_1.create(game_info1));
  137.   vSequence2.push_back(factory2_2.create(game_info2));
  138.   vSequence2.push_back(factory2_1.create(game_info2));
  139.   vSequence2.push_back(factory2_2.create(game_info1));
  140.  
  141.   MegaParamType2 params2;
  142.   BOOST_FOREACH(FunctionBase<MegaParamType2>::FunctionBasePtr& action, vSequence2)
  143.     action->func(&params2);
  144.  
  145.   return  0;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement