Caffa
1.1.0
C++ Application Framework for Embedded Systems with introspection
|
#include <cafFactory.h>
Public Types | |
typedef std::map< KeyType, ObjectCreatorBase * >::iterator | iterator_type |
Public Member Functions | |
template<typename TypeToCreate > | |
bool | registerCreator (const KeyType &key) |
BaseType * | create (const KeyType &key) |
std::vector< KeyType > | allKeys () |
Static Public Member Functions | |
static Factory< BaseType, KeyType > * | instance () |
A generic Factory class template Usage: Simply add the classes that is supposed to be created by the factory by doing the folowing:
caffa::Factory<BaseType, KeyType>::instance()->registerCreator<TypeToCreate>(key);
This must only be done once for each TypeToCreate. It will assert if you try to do it several times. This method returns a bool to make it possible to make this initialization as a static variable initialization. That is useful if you do not want a centralized registering (but rather making each class register itself):
static bool uniqueVarname = caffa::Factory<BaseType, KeyType>::instance()->registerCreator<TypeToCreate>(key);
You can also use the macro CAFFA_FACTORY_REGISTER(BaseType, TypeToCreate, KeyType, key)
See also cafUiFieldEditorHandle.h for an advanced example.
When you need an object:
BaseType* newObject = caffa::Factory<BaseType, KeyType>::instance()->create(key);