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.
My Goals:
What I have:
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<Base>::register_class<MyDerived>();
What I tried (or would like to but don't know how to properly):
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 Derived.hpp
and set it with the static registration method in Derived.cpp
-> again, never gets called.abstract_factory
a true singleton instead of having everything static?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;
...
};
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 ) );
This setup has a number of advantages:
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.