好几天没写博客了
接着以前写的Loki系列来
首先Loki Factory设计的目的就是让对象功能按名生成!
该文主要涉及Loki/Factory.h文件
首先上工厂异常类
如下:
/**
* \defgroup FactoryErrorPoliciesGroup Factory Error Policies
* \ingroup FactoryGroup
* \brief Manages the "Unknown Type" error in an object factory
*
* \class DefaultFactoryError
* \ingroup FactoryErrorPoliciesGroup
* \brief Default policy that throws an exception
*
*/
template <typename IdentifierType, class AbstractProduct>
struct DefaultFactoryError
{
struct Exception : public std::exception
{
const char* what() const throw() { return "Unknown Type"; }
};
static AbstractProduct* OnUnknownType(IdentifierType)
{
throw Exception();
}
};
这里使用了一个模板来处理当出现不可定名的对象标示时的处理方法
其中Identifier是对象的标示(比如id,字符串等等)
AbstrctProduct就是工厂产品的基类
不过感觉如果出现非存在的对象标示时应该返回空的对象指针为不是出现异常
所以我加入了下面模板
////////////////////////////////////////////////////////
/// 定义盖莫引擎工厂产品制作出错处理(出错则返回NULL!)
////////////////////////////////////////////////////////////
template <typename IdentifierType, class AbstractProduct>
struct EngineFactoryError
{
struct Exception : public std::exception
{
const char* what() const throw()
{
return "Not Found";
}
};
static AbstractProduct* OnUnknownType(IdentifierType)
{
return NULL;
}
};
当然这里没有谁对谁错的问题
而
struct FactoryImplBase
{
typedef EmptyType Parm1;
typedef EmptyType Parm2;
typedef EmptyType Parm3;
typedef EmptyType Parm4;
typedef EmptyType Parm5;
typedef EmptyType Parm6;
typedef EmptyType Parm7;
typedef EmptyType Parm8;
typedef EmptyType Parm9;
typedef EmptyType Parm10;
typedef EmptyType Parm11;
typedef EmptyType Parm12;
typedef EmptyType Parm13;
typedef EmptyType Parm14;
typedef EmptyType Parm15;
};
是一个作为工厂基类的数据结构
最大可以持有15个模板参数(太多了吧)
template <typename AP, typename Id, typename TList >
struct FactoryImpl;
这个前向声明表明了
FactoryImpl具有3个模板参数 一个工厂产品指针 一个对象标识 一个对象所在链表
而
template<typename AP, typename Id>
struct FactoryImpl<AP, Id, NullType>
: public FactoryImplBase
{
virtual ~FactoryImpl() {}
virtual AP* CreateObject(const Id & id ) = 0;
};
则是对象链表为空的特化形式
通过调用CreateObject来获取对象指针
接下来的
template <typename AP, typename Id, typename P1 >
struct FactoryImpl<AP,Id, Seq<P1> >
: public FactoryImplBase
{
typedef typename TypeTraits<P1>::ParameterType Parm1;
virtual ~FactoryImpl() {}
virtual AP* CreateObject(const Id& id,Parm1 ) = 0;
};
一直到 P15大致都是一样的
简单的说Pi说明了名字链表的上限
接下来就是Loki的Factory
////////////////////////////////////////////////////////////////////////////////
/// \class Factory
///
/// \ingroup FactoryGroup
/// Implements a generic object factory.
///
/// Create functions can have up to 15 parameters.
///
/// \par Singleton lifetime when used with Loki::SingletonHolder
/// Because Factory uses internally Functors which inherits from
/// SmallObject you must use the singleton lifetime
/// \code Loki::LongevityLifetime::DieAsSmallObjectChild \endcode
/// Alternatively you could suppress for Functor the inheritance
/// from SmallObject by defining the macro:
/// \code LOKI_FUNCTOR_IS_NOT_A_SMALLOBJECT \endcode
////////////////////////////////////////////////////////////////////////////////
从注释可以看出 她最多支持15个对象链表
再是模板头
template
<
class AbstractProduct,
typename IdentifierType,
typename CreatorParmTList = NullType,
template<typename, class> class FactoryErrorPolicy = EngineFactoryError//DefaultFactoryError
>
在这里我做了修改并没有使用其默认的异常处理
而是采用当出现非法标识时返回空的对象指针的策略
class Factory : public FactoryErrorPolicy<IdentifierType, AbstractProduct>
{
typedef FactoryImpl< AbstractProduct, IdentifierType, CreatorParmTList > Impl;
typedef typename Impl::Parm1 Parm1;
typedef typename Impl::Parm2 Parm2;
typedef typename Impl::Parm3 Parm3;
typedef typename Impl::Parm4 Parm4;
typedef typename Impl::Parm5 Parm5;
typedef typename Impl::Parm6 Parm6;
typedef typename Impl::Parm7 Parm7;
typedef typename Impl::Parm8 Parm8;
typedef typename Impl::Parm9 Parm9;
typedef typename Impl::Parm10 Parm10;
typedef typename Impl::Parm11 Parm11;
typedef typename Impl::Parm12 Parm12;
typedef typename Impl::Parm13 Parm13;
typedef typename Impl::Parm14 Parm14;
typedef typename Impl::Parm15 Parm15;
typedef Functor<AbstractProduct*, CreatorParmTList> ProductCreator;
typedef AssocVector<IdentifierType, ProductCreator> IdToProductMap;
IdToProductMap associations_;
在这里库作者使用了AssocVector容器 具体代码在对等头文件里面
构造和析构没什么说的了
Factory()
: associations_()
{
}
~Factory()
{
associations_.erase(associations_.begin(), associations_.end());
}
析构无非是对对象的清空动作
下面是对象的注册和反注册动作
bool Register(const IdentifierType& id, ProductCreator creator)
{
return associations_.insert(
typename IdToProductMap::value_type(id, creator)).second != 0;
}
template <class PtrObj, typename CreaFn>
bool Register(const IdentifierType& id, const PtrObj& p, CreaFn fn)
{
ProductCreator creator( p, fn );
return associations_.insert(
typename IdToProductMap::value_type(id, creator)).second != 0;
}
bool Unregister(const IdentifierType& id)
{
return associations_.erase(id) != 0;
}
可以看出 如果需要注册一个新的对象标识
只需要把对象标示和其对于的Creator加入链表即可
下面的这个函数就是获取对象标识链表
std::vector<IdentifierType> RegisteredIds()
{
std::vector<IdentifierType> ids;
for(typename IdToProductMap::iterator it = associations_.begin();
it != associations_.end(); ++it)
{
ids.push_back(it->first);
}
return ids;
}
之后的16个函数
AbstractProduct* CreateObject(const IdentifierType& id)
{
typename IdToProductMap::iterator i = associations_.find(id);
if (i != associations_.end())
return (i->second)( );
return this->OnUnknownType(id);
}
...
是对象的生成动作
可以看出
如果在链表中没有发现给定标识则模板异常策略发生作用
代码最后是复制工厂
/**
* \defgroup CloneFactoryGroup Clone Factory
* \ingroup FactoriesGroup
* \brief Creates a copy from a polymorphic object.
*
* \class CloneFactory
* \ingroup CloneFactoryGroup
* \brief Creates a copy from a polymorphic object.
*/
template
<
class AbstractProduct,
class ProductCreator =
AbstractProduct* (*)(const AbstractProduct*),
template<typename, class>
class FactoryErrorPolicy = DefaultFactoryError
>
class CloneFactory
: public FactoryErrorPolicy<TypeInfo, AbstractProduct>
{
public:
bool Register(const TypeInfo& ti, ProductCreator creator)
{
return associations_.insert(
typename IdToProductMap::value_type(ti, creator)).second != 0;
}
bool Unregister(const TypeInfo& id)
{
return associations_.erase(id) != 0;
}
AbstractProduct* CreateObject(const AbstractProduct* model)
{
if (model == NULL)
{
return NULL;
}
typename IdToProductMap::iterator i =
associations_.find(typeid(*model));
if (i != associations_.end())
{
return (i->second)(model);
}
return this->OnUnknownType(typeid(*model));
}
private:
typedef AssocVector<TypeInfo, ProductCreator> IdToProductMap;
IdToProductMap associations_;
};
其复制过程为
首先检测复制源是否为空为空则返回空指针
然后根据对象名字获取她在标识-产品链表中的迭代器位置
如果存在则返回复制
否则根据处置的异常处理策略处理它!
Loki Factory看完了 不过感觉有点华而不实
本来盖莫游戏引擎期望采用这个现在看来需要另外写了
下面是一大堆乱代码O(∩_∩)O~
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/////////////////////////////////////////////////////////////
/// 重定义引擎对象链表数据类型
////////////////////////////////////////////////////////////
typedef Loki::Seq
<
//! 在这里添加新的类型
#include <GEngine/Template/RegisterObject.inl>
>::Type ObjectList;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
namespace Loki
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/////////////////////////////////////////////////////////
/// 定义盖莫引擎工厂产品制作出错处理(出错则返回NULL!)
////////////////////////////////////////////////////////////
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*template <typename IdentifierType, class AbstractProduct>
struct EngineFactoryError
{
struct Exception : public std::exception
{
const char* what() const throw()
{
return "Not Found";
}
};
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
static AbstractProduct* OnUnknownType(IdentifierType)
{
return NULL;
}
};*/
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
namespace core
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/////////////////////////////////////////////////////////////
/// 重定义盖莫游戏引擎对象工厂类型
////////////////////////////////////////////////////////////
typedef Loki::SingletonHolder
<
Loki::Factory<Object,engine_string>
> ObjectFactory;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
static inline bool RegisterObject(engine_string key,RefPtr<Object>(*Creator)())
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
return ObjectFactory::Instance().Register(key,Creator);
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/////////////////////////////////////////////////////////////
/// 定义盖莫游戏引擎工厂基类(Interface必须是RefCount的子类)
////////////////////////////////////////////////////////////
template<typename Interface>
class Factory
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
virtual RefPtr<Interface> Create() = 0;
};
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/////////////////////////////////////////////////////////////
/// 获取一个新的工厂产品
////////////////////////////////////////////////////////////
template<class Product,class AbstractProduct>
RefPtr<AbstractProduct> CreateNewObject()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
return new Product;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/////////////////////////////////////////////////////////////
/// 获取一个新的工厂产品(模板特化)
////////////////////////////////////////////////////////////
template<class Product>
RefPtr<Product> CreateNewObject()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
return new Product();
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/////////////////////////////////////////////////////////////
/// 获取一个新的工厂产品
////////////////////////////////////////////////////////////
template<class Product,class AbstractProduct,class P1>
RefPtr<AbstractProduct> CreateNewObject(P1 p1)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
return new Product(p1);
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/////////////////////////////////////////////////////////////
/// 获取一个新的工厂产品(模板特化)
////////////////////////////////////////////////////////////
template<class Product,class P1>
RefPtr<Product> CreateNewObject(P1 p1)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
return new Product(p1);
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/////////////////////////////////////////////////////////////
/// 获取一个新的工厂产品
////////////////////////////////////////////////////////////
template<class Product,class AbstractProduct,class P1,class P2>
RefPtr<AbstractProduct> CreateNewObject(P1 p1,P2 p2)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
return new Product(p1,p2);
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/////////////////////////////////////////////////////////////
/// 获取一个新的工厂产品(模板特化)
////////////////////////////////////////////////////////////
template<class Product,class P1,class P2>
RefPtr<Product> CreateNewObject(P1 p1,P2 p2)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
return new Product(p1,p2);
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/////////////////////////////////////////////////////////////
/// 获取一个新的工厂产品
////////////////////////////////////////////////////////////
template<class Product,class AbstractProduct,class P1,class P2,class P3>
RefPtr<AbstractProduct> CreateNewObject(P1 p1,P2 p2,P3 p3)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
return new Product(p1,p2,p3);
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/////////////////////////////////////////////////////////////
/// 获取一个新的工厂产品(模板特化)
////////////////////////////////////////////////////////////
template<class Product,class P1,class P2,class P3>
RefPtr<Product> CreateNewObject(P1 p1,P2 p2,P3 p3)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
return new Product(p1,p2,p3);
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#define REGISTER_FUNCTION(Object)\
namespace Loki\
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{\
template<> \
bool RegisterFunction<Object>()\
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{\
return RegisterObject(#Object,&core::CreateNewObject<Object>);\
}\
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//*#define REGISTER_FUNCTION1(Object)\
template<> \
bool RegisterFunction<Object>()\
{\
return RegisterObject(#Object,&core::CreateNewObject<Object>);\
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
#define REGISTER_FUNCTION2(Object)\
template<> \
bool RegisterFunction<Object>()\
{\
return RegisterObject(#Object,&core::CreateNewObject<Object>);\
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
#define REGISTER_FUNCTION3(Object)\
template<> \
bool RegisterFunction<Object>()\
{\
return RegisterObject(#Object,&core::CreateNewObject<Object>);\
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
#define REGISTER_FUNCTION4(Object)\
template<> \
bool RegisterFunction<Object>()\
{\
return RegisterObject(#Object,&core::CreateNewObject<Object>);\
}*/
#define UNREGISTER_FUNCTION(Object)\
namespace Loki\
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{\
template<> \
bool UnRegisterFunction<Object>()\
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{\
return true;\
}\
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#define GlobalGetNewObjectByName(name)\
core::ObjectFactory::Instance().CreateObject(name);
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)