#include <OBTPrototypeFactory.h>
Public Member Functions | |
virtual | ~PrototypeFactory () |
destructor | |
const TCreator & | create (const TIdentifier &classId) const |
template<class TDerivedCreator> | |
bool | registerCreator (const TIdentifier &classId) |
register a creator to classId | |
Private Member Functions | |
PrototypeFactory () | |
constructor | |
PrototypeFactory (const PrototypeFactory &) | |
copy constructor | |
PrototypeFactory & | operator= (const PrototypeFactory &) |
assignment operator | |
Private Attributes | |
std::map < TIdentifier, TCreator * > | _creators |
map of the creators | |
Friends | |
class | Singleton< PrototypeFactory< TIdentifier, TCreator > > |
Helper class to provide you access to a template factory implementation. TIdentifier : type of the identifier used to register and retrieve a Creator. TCreator : creation functor ancestor, defines the ancestor class of the family of classes to be created by the factory and its constructor signature.
// ancestor class creator class ACreator { public: // the operator() argument must match A constructor arguments virtual A* operator() ( std::string name, unsigned int identifier ) const { return NULL ; } } ; // descendants will parametrize this class to register to the prototype factory template <class T> class ACreatorT : public ACreator { public: // this typedef is mandatory typedef T object_to_create ; // creates and returns a new instance of type T A* operator() ( std::string name, unsigned int identifier ) const { return new T( name, identifier ) ; } } ;
typedef OBT::Singleton< OBT::PrototypeFactory< unsigned int, ACreator > > AFactory ; *
AFactory::getInstance().registerCreator< ACreatorT< B > >( B::s_id ) ; AFactory::getInstance().registerCreator< ACreatorT< C > >( C::s_id ) ; *
A* myB = AFactory::getInstance().create( B::s_id )( aName, anIdentifier ) ;
Definition at line 69 of file OBTPrototypeFactory.h.
OBT::PrototypeFactory< TIdentifier, TCreator >::~PrototypeFactory | ( | ) | [inline, virtual] |
OBT::PrototypeFactory< TIdentifier, TCreator >::PrototypeFactory | ( | ) | [inline, private] |
OBT::PrototypeFactory< TIdentifier, TCreator >::PrototypeFactory | ( | const PrototypeFactory< TIdentifier, TCreator > & | ) | [private] |
copy constructor
const TCreator & OBT::PrototypeFactory< TIdentifier, TCreator >::create | ( | const TIdentifier & | classId | ) | const [inline] |
returns the creator registered to classId if no creator has been registered TCreator is returned
Definition at line 123 of file OBTPrototypeFactory.h.
References OBT::PrototypeFactory< TIdentifier, TCreator >::_creators.
00124 { 00125 typename std::map< TIdentifier, TCreator* >::const_iterator ite( _creators.find( classId ) ) ; 00126 if( ite != _creators.end() ) 00127 { 00128 return *ite->second ; 00129 } 00130 00131 static TCreator creator ; 00132 return creator ; 00133 }
bool OBT::PrototypeFactory< TIdentifier, TCreator >::registerCreator | ( | const TIdentifier & | classId | ) | [inline] |
register a creator to classId
Definition at line 141 of file OBTPrototypeFactory.h.
References OBT::PrototypeFactory< TIdentifier, TCreator >::_creators.
00142 { 00143 static TDerivedCreator creator ; 00144 _creators[classId] = &creator ; 00145 return true ; 00146 }
PrototypeFactory& OBT::PrototypeFactory< TIdentifier, TCreator >::operator= | ( | const PrototypeFactory< TIdentifier, TCreator > & | ) | [private] |
assignment operator
friend class Singleton< PrototypeFactory< TIdentifier, TCreator > > [friend] |
Definition at line 73 of file OBTPrototypeFactory.h.
std::map< TIdentifier, TCreator* > OBT::PrototypeFactory< TIdentifier, TCreator >::_creators [private] |
map of the creators
Definition at line 99 of file OBTPrototypeFactory.h.
Referenced by OBT::PrototypeFactory< TIdentifier, TCreator >::create(), and OBT::PrototypeFactory< TIdentifier, TCreator >::registerCreator().