像我之前的许多人一样,我正在尝试让我的派生类型自动注册到我的工厂.我通读了许多问题,并试图专注于我在那里没有找到的内容.
Like many before me, I'm trying so get my derived types to automatically register with my factory. I read through many question and tried to focus on what I didn't find there.
除了自动注册之外,其他一切都运行良好.
I've got everything running nicely except the automatic registration.
我的目标:
我有什么:
template <class T>
class abstract_factory
{
public:
template < typename Tsub > static void register_class();
static T* create( const std::string& name );
private:
// allocator<T> is a helper class to create a pointer of correct type
static std::map<std::string, boost::shared_ptr<allocator<T> > > s_map;
};
abstract_factory ::register_class();
abstract_factory<Base>::register_class<MyDerived>();我尝试过的(或想尝试但不知道如何正确操作):
registratorclass :在 Derived.cpp 中静态实例化的模板化类,应在其构造函数中调用 abstract_factory::register_class() main() 中创建一个 Derived 的实例,这有效 -> 虽然有点违背了目的 registrator<Derived> class: templateded class that is instantiated statically in Derived.cpp and should call abstract_factory::register_class<Derived>() in it's constructor
Derived in main() this works -> kinda defeats the purpose though 可以使用任何建议,无论大小,thanx.
Could use any advice, large or small, thanx.
我用单例带成员注册,基本上:
I use a singleton with a member for registration, basically:
template< typename KeyType, typename ProductCreatorType >
class Factory
{
typedef boost::unordered_map< KeyType, ProductCreatorType > CreatorMap;
...
};
使用 Loki 之后,我得到了一些类似的东西:
Using Loki I then have something along these lines:
typedef Loki::SingletonHolder< Factory< StringHash, boost::function< boost::shared_ptr< SomeBase >( const SomeSource& ) > >, Loki::CreateStatic > SomeFactory;
注册通常使用宏完成,例如:
Registration is usually done using a macro such as:
#define REGISTER_SOME_FACTORY( type ) static bool BOOST_PP_CAT( type, __regged ) = SomeFactory::Instance().RegisterCreator( BOOST_PP_STRINGIZE( type ), boost::bind( &boost::make_shared< type >, _1 ) );
这种设置有很多优点:
调用 .cpp 文件中的宏就足以在静态初始化期间在启动时注册类型.当类型注册是静态库的一部分时,这很有效,在这种情况下,它不会包含在您的二进制文件中.将注册编译为我见过的库的一部分的唯一解决方案是拥有一个巨大的文件,该文件将注册明确作为某种初始化例程的一部分.相反,我现在所做的是在我的库中创建一个客户端文件夹,用户将其作为二进制构建的一部分包含在内.
Invoking the macro in the .cpp file is then enough to get the type registered at start up during static initialization. This works dandy save for when the type registration is a part of a static library, in which case it won't be included in your binary. The only solutions which compiles the registration as a part of the library which I've seen work is to have one huge file that does the registration explicitly as a part of some sort of initialization routine. Instead what I do nowadays is to have a client folder with my lib which the user includes as a part of the binary build.
从您的要求列表中,我相信除了使用注册器类之外,这可以满足所有要求.
From your list of requirements I believe this satisfies everything save for using a registrator class.
这篇关于c ++派生类型的自动工厂注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
std::reference_wrapper 和简单指针的区别?Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和简单指针的区别?)
常量之间的区别.指针和引用?Difference between const. pointer and reference?(常量之间的区别.指针和引用?)
c++ - 如何从指向向量的指针访问向量的内容?How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何从指向向量的指针访问向量的内容?)
*& 的含义和**&在 C++ 中Meaning of *amp; and **amp; in C++(*amp; 的含义和**amp;在 C++ 中)
为什么我不能对普通变量进行多态?Why can#39;t I do polymorphism with normal variables?(为什么我不能对普通变量进行多态?)
取消引用已删除的指针总是会导致访问冲突?Dereferencing deleted pointers always result in an Access Violation?(取消引用已删除的指针总是会导致访问冲突?)