问题描述
我有一个方便的对象工厂模板,它可以通过类型 ID 名称创建对象.实现非常明显:ObjectFactory
包含从 std::string
到对象创建者函数的映射.那么所有要创建的对象都要在这个工厂注册.
I have the convenient object factory template that creates objects by their type id names. The implementation is pretty obvious: ObjectFactory
contains the map from std::string
to object creator function. Then all objects to be created shall be registered in this factory.
我使用以下宏来做到这一点:
I use the following macro to do that:
#define REGISTER_CLASS(className, interfaceName)
class className;
static RegisterClass<className, interfaceName> regInFactory##className;
class className : public interfaceName
RegisterClass
在哪里
template<class T, class I>
struct RegisterClass
{
RegisterClass()
{
ObjectFactory<I>::GetInstance().Register<T>();
}
};
用法
class IFoo
{
public:
virtual Do() = 0;
virtual ~IFoo() {}
}
REGISTER_CLASS(Foo, IFoo)
{
virtual Do() { /* do something */ }
}
REGISTER_CLASS(Bar, IFoo)
{
virtual Do() { /* do something else */ }
}
在工厂中同时定义和注册类.
Classes are defined and registered in factory simultaneously.
问题在于 regInFactory...
静态对象是在 .h 文件中定义的,因此它们将被添加到每个翻译单元中.同一个对象创建者会被多次注册,更重要的是会有很多静态存储时长的冗余对象.
The problem is that regInFactory...
static objects are defined in .h files, so they will be added to every translation unit. The same object creator will be registered several times, and, more important, there will be a lot of redundant objects with static storage duration.
有什么办法可以做到如此优雅的注册(不是复制/粘贴类和接口名称),但又不会在全球范围内传播冗余的静态对象?
Is there any way to do such elegant registration (not to copy/paste class and interface names), but do not spread redundant static objects around the globe?
如果一个好的解决方案需要一些 VC++ 特定的扩展(不符合 C++ 标准),我会接受.
If a good solution needs some VC++ specific extensions (not conforming to C++ standard), I will be OK with that.
推荐答案
所以你想把变量定义放在头文件中?有一种可移植的方式:模板类的静态变量.所以我们得到:
So you want to put variables definitions in header file? There is a portable way: static variables of template classes. So we get:
template <typename T, typename I>
struct AutoRegister: public I
{
// a constructor which use ourRegisterer so that it is instantiated;
// problem catched by bocco
AutoRegister() { &ourRegisterer; }
private:
static RegisterClass<T, I> ourRegisterer;
};
template <typename T, typename I>
RegisterClass<T, I> AutoRegister<T, I>::ourRegisterer;
class Foo: AutoRegister<Foo, IFoo>
{
public:
virtual void Do();
};
请注意,我保留了 IFoo 非虚拟的继承.如果存在多次从该类继承的风险,则它应该是虚拟的.
Note that I've kept the inheritance of IFoo non virtual. If there is any risk of inheriting from that class multiple times, it should be virtual.
这篇关于在对象工厂中注册对象创建者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!