Simox  2.3.74.0
AbstractFactoryMethod< Base, constructorArg > Class Template Reference

Data Structures

struct  SubClassRegistry
 

Public Types

typedef std::shared_ptr< Base >(* initialisationFunction) (constructorArg)
 

Public Member Functions

void setDescription (const std::string &newDescription)
 
std::string getDescription () const
 

Static Public Member Functions

static std::shared_ptr< Base > fromName (const std::string &name, constructorArg params)
 
static std::shared_ptr< Base > first (constructorArg params)
 
static std::string getName ()
 
static std::shared_ptr< Base > createInstance (constructorArg)
 
static void registerClass (const std::string &name, initialisationFunction init)
 
static std::vector< std::string > getSubclassList ()
 

Detailed Description

template<typename Base, typename constructorArg>
class AbstractFactoryMethod< Base, constructorArg >

A template that can be used as a superclass of a class hierarchy that wants to provide a factory method which allows instantiation of objects based on a string identifier.

The first template argument is the base class of your class hierarchy. The second argument is the parameter type for the initialisation function each subclass has to provide. If you need multiple constructor arguments it is recommended to use a boost::tuple.

Member Typedef Documentation

◆ initialisationFunction

template<typename Base, typename constructorArg>
typedef std::shared_ptr<Base>(* AbstractFactoryMethod< Base, constructorArg >::initialisationFunction) (constructorArg)

The function pointer type of subclass initialisation functions. This matches the createInstance method.

Member Function Documentation

◆ createInstance()

template<typename Base, typename constructorArg>
static std::shared_ptr<Base> AbstractFactoryMethod< Base, constructorArg >::createInstance ( constructorArg  )
inlinestatic

Initialisation function which needs to be provided by every subclass. It calls the constructor and returns a shared_ptr to the resulting object.

◆ first()

template<typename Base, typename constructorArg>
static std::shared_ptr<Base> AbstractFactoryMethod< Base, constructorArg >::first ( constructorArg  params)
inlinestatic

Function which can be used to retrieve the first registered object.

◆ fromName()

template<typename Base, typename constructorArg>
static std::shared_ptr<Base> AbstractFactoryMethod< Base, constructorArg >::fromName ( const std::string &  name,
constructorArg  params 
)
inlinestatic

Function which can be used to retrieve an object specified by string name.

◆ getDescription()

template<typename Base, typename constructorArg>
std::string AbstractFactoryMethod< Base, constructorArg >::getDescription ( ) const
inline

Return the description of the current instance.

◆ getName()

template<typename Base, typename constructorArg>
static std::string AbstractFactoryMethod< Base, constructorArg >::getName ( )
inlinestatic

Returns the class's name. This is used to identify the class which the user requests an instance of.

◆ getSubclassList()

template<typename Base, typename constructorArg>
static std::vector<std::string> AbstractFactoryMethod< Base, constructorArg >::getSubclassList ( )
inlinestatic

Return a list of all registered subclasses.

◆ registerClass()

template<typename Base, typename constructorArg>
static void AbstractFactoryMethod< Base, constructorArg >::registerClass ( const std::string &  name,
initialisationFunction  init 
)
inlinestatic

Statically called by subclasses to register their name and initialisation function so they can be found by fromName.

◆ setDescription()

template<typename Base, typename constructorArg>
void AbstractFactoryMethod< Base, constructorArg >::setDescription ( const std::string &  newDescription)
inline

Set a description on the instance it is called on.