/********************************************************************
file name : Factory.h
author : Clark/陈泽丹
created : 2012-5-8
JAVA的反射机制已经在此被实现了 。
虽然目前代码还较凌乱,达不到“神一般的操作”。
但麻雀虽小,五脏俱全,“双杀”到“暴走”还是有机会的。
实现反射还是有必要的,因为有时基指针的信息并不足以让你的程序做出优化算法,得转成子类指针,
你才有足够的信息去做出准确高效的优化算法。
例如图形的检测碰撞。 不同的图形的碰撞检测有不同的高效算法, 如果全部都抽象到像素级的检测,则效率太低了。
而此时,你只有基类指针, 你并不清楚这基类到底是从什么子类来的。
所以需要反射!
*********************************************************************/
#pragma
#include <map>
using namespace std;
template
<
typename IdType,
typename CallbackType
>
class Factory
{
public:
bool Register(const IdType& id, CallbackType creator)
{
return associations.insert(IdToProductMap::value_type(id, creator)).second;
}
bool Unregister(const IdType& id)
{
return associations.erase(id) == 1;
}
protected:
typedef map<IdType, CallbackType> IdToProductMap;
IdToProductMap associations;
};
template<typename IdType, typename CallbackType>
class FactoryError
{
protected:
CallbackType OnUnknownType(const IdType& id)
{
cout<<"Unknow object type pass to factory!"<<endl;
return 0;
}
};
//建造者工厂-----------------------------------------------------------
template
<
class ResultType,
typename IdType,
typename CallbackType = ResultType* (*)(),
template<typename, class> class FactoryErrorPolicy = FactoryError
>
class CreaterFactory: public Factory<IdType, CallbackType>, public FactoryErrorPolicy<IdType, ResultType*>
{
public:
virtual ResultType* CreateObject(const IdType& id)
{
typename IdToProductMap::const_iterator i = associations.find(id);
if (i != associations.end())
{
return (i->second)();
}
return OnUnknownType(id);
}
};
//未完全封装的反射工厂-----------------------------------------------------------
template
<
class IdType,
class BaseLhs,
class BaseRhs = BaseLhs,
typename ResultType = int,
typename CallbackType = ResultType (*)(BaseLhs*, BaseRhs*),
template<typename, class> class FactoryErrorPolicy = FactoryError
>
class DispathFactory: protected Factory< std::pair< IdType, IdType >, CallbackType>, public FactoryErrorPolicy<std::pair< IdType, IdType >, ResultType>
{
public:
typedef std::pair<IdType,IdType> KeyType;
bool Add(const IdType& lhsID, const IdType& rhsID, CallbackType fun)
{
return Register(KeyType(lhsID, rhsID), fun);
}
bool Remove(const IdType& lhsID, const IdType& rhsID)
{
return Unregister(KeyType(lhsID, rhsID));
}
virtual ResultType Run(const IdType& lhsID, BaseLhs* lhs, const IdType& rhsID, BaseRhs* rhs)
{
typename IdToProductMap::const_iterator i = associations.find(KeyType(lhsID, rhsID));
if (i != associations.end())
{
return (i->second)(lhs, rhs);
}
return OnUnknownType(KeyType(lhsID, rhsID));
}
};
//封装反射类其工厂-----------------------------------------------------------
class Reflex
{
private:
enum {REAL_CLASS_ID = 0};
public:
virtual int GetRealClassID() = 0;
};
template<int v>
class ReflexEx:public Reflex
{
protected:
ReflexEx(){}
virtual ~ReflexEx(){}
public:
enum {REAL_CLASS_EX_ID = v};
virtual int GetRealClassID(){ return REAL_CLASS_EX_ID; }
};
//0不能用,因已做为基类ID了
template<>
class ReflexEx<0>{};
//反射工厂
template
<
typename ResultType = int,
typename CallbackType = ResultType (*)(Reflex*, Reflex*)
>
class DispathFactoryEx: public DispathFactory< int, Reflex, Reflex, ResultType, CallbackType, FactoryError >
{
public:
virtual ResultType Run(Reflex* lhs, Reflex* rhs)
{
typename IdToProductMap::const_iterator i = associations.find(KeyType(lhs->GetRealClassID(), rhs->GetRealClassID()));
if (i != associations.end())
{
return (i->second)(lhs, rhs);
}
return OnUnknownType(KeyType(lhs->GetRealClassID(), rhs->GetRealClassID()));
}
};
//封装注册功能
template<class L, class R, class FunPolicy>
class RegisterDispathFactory: public FunPolicy
{
public:
static void Init(DispathFactoryEx<>& _DispathFactory)
{
_DispathFactory.Add(L::REAL_CLASS_EX_ID, R::REAL_CLASS_EX_ID, RegisterDispathFactory<L, R, FunPolicy>::Run);
}
static int Run(Reflex* _lEx, Reflex* _rEx)
{
return FunPolicy::Run(_lEx, _rEx);
}
};
#include <vld.h>
#include <iostream>
#include "Factory.h"
using namespace std;
class LEx1: public ReflexEx<1>
{
public:
virtual void Show(){ cout<<"LEx1"<<endl; }
};
class REx1: public ReflexEx<2>
{
public:
virtual void Show(){ cout<<"REx1"<<endl; }
};
class LEx2: public ReflexEx<3>
{
public:
virtual void Show(){ cout<<"LEx2"<<endl; }
};
class REx2: public ReflexEx<4>
{
public:
virtual void Show(){ cout<<"REx2"<<endl; }
};
class Test
{
public:
static int Run(Reflex* _lEx, Reflex* _rEx)
{
cout<<"("<<_lEx->GetRealClassID()<<" , "<<_rEx->GetRealClassID()<<") 注册了功能!"<<endl;
return 0;
}
};
void main()
{
DispathFactoryEx<> G_DispathFactory;
RegisterDispathFactory<LEx1, REx1, Test>::Init(G_DispathFactory);
Reflex* pL1 = new LEx1();
Reflex* pR1 = new REx1();
Reflex* pL2 = new LEx2();
Reflex* pR2 = new REx2();
G_DispathFactory.Run(pL1, pR1);
G_DispathFactory.Run(pL2, pR2);
delete pL1;
delete pR1;
delete pL2;
delete pR2;
system("pause");
}