MAIA bb96820c
Multiphysics at AIA
Loading...
Searching...
No Matches
MFactory< AbstractProduct, IdentifierType, ReturnType, ProductCreator, Args > Class Template Reference

Class implementing the object factory pattern. More...

#include <factory.h>

Inheritance diagram for MFactory< AbstractProduct, IdentifierType, ReturnType, ProductCreator, Args >:
[legend]
Collaboration diagram for MFactory< AbstractProduct, IdentifierType, ReturnType, ProductCreator, Args >:
[legend]

Public Member Functions

MBool add (const IdentifierType &id, ProductCreator creator)
 Function to add new types to the factory. More...
 
MBool remove (const IdentifierType &id)
 Function to remove existing types from the factory. More...
 
ReturnType create (const IdentifierType &id, Args... args) const
 Function to create a new instance of a type. More...
 

Private Types

typedef std::map< IdentifierType, ProductCreator > AssocMap
 

Private Attributes

AssocMap m_assoc
 

Detailed Description

template<class AbstractProduct, typename IdentifierType, typename ReturnType = std::unique_ptr<AbstractProduct>, class ProductCreator = std::function<ReturnType()>, class... Args>
class MFactory< AbstractProduct, IdentifierType, ReturnType, ProductCreator, Args >
Author
Michael Schlottke (mic) mic@a.nosp@m.ia.r.nosp@m.wth-a.nosp@m.ache.nosp@m.n.de
Date
2014-04-05
Template Parameters
AbstractProductThe (abstract) base class of the objects to be created.
IdentifierTypeThe type to be used to identify derived types.
ReturnTypeReturn type of the create() method.
ProductCreatorThe type of the creating function.
ArgsAdditional (optional) arguments that should be passed to the constructor of the created objects.

This follow the factory pattern as described in Alexandrescu (2001).

References: Andrei Alexandrescu (2001): Modern C++ Design.

Definition at line 36 of file factory.h.

Member Typedef Documentation

◆ AssocMap

template<class AbstractProduct , typename IdentifierType , typename ReturnType = std::unique_ptr<AbstractProduct>, class ProductCreator = std::function<ReturnType()>, class... Args>
typedef std::map<IdentifierType, ProductCreator> MFactory< AbstractProduct, IdentifierType, ReturnType, ProductCreator, Args >::AssocMap
private

Definition at line 71 of file factory.h.

Member Function Documentation

◆ add()

template<class AbstractProduct , typename IdentifierType , typename ReturnType = std::unique_ptr<AbstractProduct>, class ProductCreator = std::function<ReturnType()>, class... Args>
MBool MFactory< AbstractProduct, IdentifierType, ReturnType, ProductCreator, Args >::add ( const IdentifierType &  id,
ProductCreator  creator 
)
inline
Parameters
[in]idIdentifier for newly added type.
[in]creatorFunction returning a new instance of the new type.
Returns
True if the type was sucessfully added.

Definition at line 44 of file factory.h.

44 {
45 return m_assoc.insert(typename AssocMap::value_type(id, creator)).second;
46 }
AssocMap m_assoc
Definition: factory.h:72

◆ create()

template<class AbstractProduct , typename IdentifierType , typename ReturnType = std::unique_ptr<AbstractProduct>, class ProductCreator = std::function<ReturnType()>, class... Args>
ReturnType MFactory< AbstractProduct, IdentifierType, ReturnType, ProductCreator, Args >::create ( const IdentifierType &  id,
Args...  args 
) const
inline
Parameters
[in]idIdentifier of the type to be created.
[in]argsOptional arguments to be passed to the type's constructor.
Returns
A handle to the newly created object.

Definition at line 61 of file factory.h.

61 {
62 auto i = m_assoc.find(id);
63 if(i != m_assoc.end()) {
64 return (i->second)(args...);
65 } else {
66 TERMM(1, "Identifier not found.");
67 }
68 }

◆ remove()

template<class AbstractProduct , typename IdentifierType , typename ReturnType = std::unique_ptr<AbstractProduct>, class ProductCreator = std::function<ReturnType()>, class... Args>
MBool MFactory< AbstractProduct, IdentifierType, ReturnType, ProductCreator, Args >::remove ( const IdentifierType &  id)
inline
Parameters
[in]idIdentifier for the type to be removed.
Returns
True if the type was sucessfully removed.

Definition at line 53 of file factory.h.

53{ return m_assoc.erase(id) == true; }

Member Data Documentation

◆ m_assoc

template<class AbstractProduct , typename IdentifierType , typename ReturnType = std::unique_ptr<AbstractProduct>, class ProductCreator = std::function<ReturnType()>, class... Args>
AssocMap MFactory< AbstractProduct, IdentifierType, ReturnType, ProductCreator, Args >::m_assoc
private

Definition at line 72 of file factory.h.


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