Public Member Functions | Static Public Member Functions | Private Attributes | List of all members
corbo::Factory< Base > Class Template Reference

Generic factory object. More...

#include <factory.h>

Public Member Functions

template<typename Derived = Base>
std::shared_ptr< Derived > create (const std::string &name, bool print_error=true) const
 Create a shared instance of the desired object. More...
 
const std::unordered_map< std::string, std::shared_ptr< Base > > & getObjectMap () const
 
bool hasObject (const std::string &name)
 Check if a specified object (name) has been registered. More...
 
void printObjects () const
 Print registered object names to console. More...
 
bool registerObject (const std::string &name, std::shared_ptr< Base > object_type)
 Register a new object. More...
 

Static Public Member Functions

static Factoryinstance ()
 < Retrieve static instance of the factory More...
 

Private Attributes

std::unordered_map< std::string, std::shared_ptr< Base > > _object_map
 map of identifiers and their corresponding objects (default-constructed) More...
 

Detailed Description

template<typename Base>
class corbo::Factory< Base >

Generic factory object.

A factory is used to create objects based on a known identifier (which is a string in this case). This facilitates the dynamic creation of specific interface implementations without including their actual declaration. Registration of objects is deferred from the library to the linker stage of the executable. This factory is implemented using the Singleton design pattern in order to facilitate the dynamic creation of objects wherever required without the need of sharing the factory among multiple classes and modules.

A macro for a convenient object registration is provided which can be invoked in the header file of the particular interface implementation. Hence, the source of the execuable only needs to include the particular header (containing the macro).

Note, interfaces must define a method of prototype:

virtual std::shared_ptr<Base> getInstance() = 0;

This method must be implemented in all subclasses in order to return a valid instance of the related subclass. Furthermore, the default constructure most not be deleted.

Remarks
It is recommended to keep the class' default constructors as simple and efficient as possible since a default-constructed instance for each registered object is kept in the factory.
Author
Christoph Rösmann (chris.nosp@m.toph.nosp@m..roes.nosp@m.mann.nosp@m.@tu-d.nosp@m.ortm.nosp@m.und.d.nosp@m.e)

Definition at line 68 of file factory.h.

Member Function Documentation

◆ create()

template<typename Base >
template<typename Derived = Base>
std::shared_ptr<Derived> corbo::Factory< Base >::create ( const std::string &  name,
bool  print_error = true 
) const
inline

Create a shared instance of the desired object.

Parameters
[in]nameUnique object identifier
[in]print_errorIf true, an error is printed if the object is unknown
Returns
shared pointer to the derived class if name is known, an empty shared_ptr otherwise
Template Parameters
DerivedSpecify proper subclass

Definition at line 93 of file factory.h.

◆ getObjectMap()

template<typename Base >
const std::unordered_map<std::string, std::shared_ptr<Base> >& corbo::Factory< Base >::getObjectMap ( ) const
inline

Definition at line 128 of file factory.h.

◆ hasObject()

template<typename Base >
bool corbo::Factory< Base >::hasObject ( const std::string &  name)
inline

Check if a specified object (name) has been registered.

Parameters
[in]nameUnique object identifier
Returns
true if object is registered, false otherwise

Definition at line 83 of file factory.h.

◆ instance()

template<typename Base >
static Factory& corbo::Factory< Base >::instance ( )
inlinestatic

< Retrieve static instance of the factory

Definition at line 72 of file factory.h.

◆ printObjects()

template<typename Base >
void corbo::Factory< Base >::printObjects ( ) const
inline

Print registered object names to console.

Definition at line 123 of file factory.h.

◆ registerObject()

template<typename Base >
bool corbo::Factory< Base >::registerObject ( const std::string &  name,
std::shared_ptr< Base >  object_type 
)
inline

Register a new object.

Parameters
[in]nameUnique object identifier
[in]object_typeDefault constructed object of the given type
Returns
true if successful

Definition at line 116 of file factory.h.

Member Data Documentation

◆ _object_map

template<typename Base >
std::unordered_map<std::string, std::shared_ptr<Base> > corbo::Factory< Base >::_object_map
private

map of identifiers and their corresponding objects (default-constructed)

Definition at line 132 of file factory.h.


The documentation for this class was generated from the following file:


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Mon Feb 28 2022 22:08:02