Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <boost/foreach.hpp>
- #include <memory>
- #include <vector>
- struct GameInfoPtr {
- bool operator==(const GameInfoPtr&) const { return false; }
- }; // параметры для алгоритма
- struct ParamType1 {};
- struct ParamType2 {};
- struct ParamType3 {};
- template <class ParamType_> class FunctionBase
- {
- public:
- typedef std::tr1::shared_ptr<FunctionBase<ParamType_> > FunctionBasePtr;
- typedef typename ParamType_ ParamType;
- virtual void func(ParamType* params) = 0;
- };
- template <class ParamType_> class Func1 : public FunctionBase<ParamType_>
- {
- GameInfoPtr m_info;
- public:
- Func1(GameInfoPtr info_) : m_info(info_) {}
- virtual void func(ParamType* params_) { do_func(params_); }
- template <class U> static bool do_func(U*) { ; }
- void do_func(ParamType1* params_) {;;}
- bool isEqual(GameInfoPtr game_info) const { return game_info == m_info; }
- };
- template <class ParamType_> class Func2 : public FunctionBase<ParamType_>
- {
- public:
- struct DoFunc1
- {
- template <class ThisType, class U> static bool func(ThisType,U*) { return false; }
- template <class ThisType> static bool func(ThisType,ParamType1*) { return true; }
- };
- struct DoFunc2
- {
- template <class ThisType, class U> static bool func(ThisType,U*) { return false; }
- template <class ThisType> static bool func(ThisType,ParamType2*) { return true; }
- };
- struct DoFunc3
- {
- template <class ThisType, class U> static bool func(ThisType,U*) { return false; }
- template <class ThisType> static bool func(ThisType,ParamType3*) { return true; }
- };
- Func2(GameInfoPtr info_) : m_info(info_) {}
- virtual void func(ParamType_* params_)
- {
- if (DoFunc2::func(this, params_))
- return;
- if (DoFunc3::func(this, params_))
- return;
- DoFunc1::func(this, params_);
- }
- bool isEqual(GameInfoPtr game_info) const { return game_info == m_info; }
- GameInfoPtr m_info;
- };
- template <class ParamType> class IPrototypeFactory
- {
- public:
- typedef typename FunctionBase<ParamType>::FunctionBasePtr FunctionBasePtr;
- virtual FunctionBasePtr create(GameInfoPtr game_info) = 0;
- };
- template <class FuncType> class PrototypeFactory : public IPrototypeFactory<typename FuncType::ParamType>
- {
- public:
- typedef typename FuncType::ParamType ParamType;
- typedef std::tr1::shared_ptr<FuncType> FuncTypePtr;
- virtual FunctionBasePtr create(GameInfoPtr game_info)
- {
- BOOST_FOREACH(FuncTypePtr& p, m_vPrototypes)
- if (p->isEqual(game_info))
- return p;
- FuncTypePtr newFunc(new FuncType(game_info));
- m_vPrototypes.push_back(newFunc);
- return newFunc;
- }
- private:
- std::vector<FuncTypePtr> m_vPrototypes;
- };
- struct MegaParamType1 : public ParamType1, public ParamType2, public ParamType3 {};
- struct MegaParamType2 : public ParamType1, public ParamType3 {};
- int main()
- {
- GameInfoPtr game_info1;
- GameInfoPtr game_info2;
- GameInfoPtr game_info3;
- PrototypeFactory<Func1<MegaParamType1> > factory1_1;
- PrototypeFactory<Func2<MegaParamType1> > factory1_2;
- std::vector<FunctionBase<MegaParamType1>::FunctionBasePtr> vSequence1;
- vSequence1.push_back(factory1_1.create(game_info1));
- vSequence1.push_back(factory1_2.create(game_info2));
- vSequence1.push_back(factory1_1.create(game_info3));
- vSequence1.push_back(factory1_1.create(game_info1));
- MegaParamType1 params1;
- BOOST_FOREACH(FunctionBase<MegaParamType1>::FunctionBasePtr& action, vSequence1)
- action->func(¶ms1);
- PrototypeFactory<Func1<MegaParamType2> > factory2_1;
- PrototypeFactory<Func2<MegaParamType2> > factory2_2;
- std::vector<FunctionBase<MegaParamType2>::FunctionBasePtr> vSequence2;
- vSequence2.push_back(factory2_1.create(game_info1));
- vSequence2.push_back(factory2_2.create(game_info2));
- vSequence2.push_back(factory2_1.create(game_info2));
- vSequence2.push_back(factory2_2.create(game_info1));
- MegaParamType2 params2;
- BOOST_FOREACH(FunctionBase<MegaParamType2>::FunctionBasePtr& action, vSequence2)
- action->func(¶ms2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement