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:
- automatically register any derived class of my base class Base
- only classes I mark as registrable
- not only direct sub-classes of Base
- ex: Base -> Device -> Camera -> Webcam
- this would make using the CRTP like described in this question dificult
- minimal changes to the classes I want registered - dummies proof
- would prefer using a registrator class than macros
- like in this question, but I'm not sure if this is dependent on CRTP
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;
};
- templated abstract factory, with std::string as key type
- abstract factory has all members and methods static
- class name is recovered automatically with typeid (no need for text name when registering)
- registration by calling:
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 inDerived.cpp
and should callabstract_factory::register_class<Derived>()
in it's constructor- never gets called or instantiated
- if I make an instance of
Derived
inmain()
this works -> kinda defeats the purpose though
- declare a simple static variable in each
Derived.hpp
and set it with the static registration method inDerived.cpp
-> again, never gets called. - make
abstract_factory
a true singleton instead of having everything static?
Could use any advice, large or small, thanx.
Derived classes
are there. Do you know how to manage to export these symbols anyway? – Bisect