Advertisement
Guest User

Untitled

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