svMultiPhysics
Loading...
Searching...
No Matches
Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
Factory< BaseType > Class Template Reference

Abstract self-registering factory. More...

#include <factory.h>

Static Public Member Functions

template<class DerivedType >
static bool register_child (const std::string &name)
 Register a derived class in the factory.
 
static std::unique_ptr< BaseType > create (const std::string &name)
 Instantiate a derived classs by name.
 
static void visit (const std::function< void(const std::string &, const BaseType &)> &f)
 Iterate through the registered classes.
 

Protected Member Functions

 Factory ()=default
 Default constructor.
 

Static Protected Member Functions

static Factory & get_instance ()
 Access the singleton instance.
 

Protected Attributes

std::map< std::string, std::function< std::unique_ptr< BaseType >()> > children
 Registered derived classes.
 

Detailed Description

template<class BaseType>
class Factory< BaseType >

Abstract self-registering factory.

This class gives a way to create a dynamic register of classes derived from a certain base class, and then instantiate them by name. To be compatible with this factory, the derived classes must be default constructibleß.

It combines the factory and singleton patterns. There should always exist only one instance of this class, which cannot be accessed directly but only manipulated through the static methods of this class.

To register a class into the factory, you can call the register_child static method, passing a class derived from BaseType as template argument and a label for it as argument. A shortcut for this is to use the macro REGISTER_IN_FACTORY.

Constructor & Destructor Documentation

◆ Factory()

template<class BaseType >
Factory< BaseType >::Factory ( )
protecteddefault

Default constructor.

Member Function Documentation

◆ create()

template<class BaseType >
static std::unique_ptr< BaseType > Factory< BaseType >::create ( const std::string &  name)
inlinestatic

Instantiate a derived classs by name.

◆ get_instance()

template<class BaseType >
static Factory & Factory< BaseType >::get_instance ( )
inlinestaticprotected

Access the singleton instance.

◆ register_child()

template<class BaseType >
template<class DerivedType >
static bool Factory< BaseType >::register_child ( const std::string &  name)
inlinestatic

Register a derived class in the factory.

Parameters
[in]nameLabel for the class to be registered. An instance of that class can then be created by passing this same label to the create method.

◆ visit()

template<class BaseType >
static void Factory< BaseType >::visit ( const std::function< void(const std::string &, const BaseType &)> &  f)
inlinestatic

Iterate through the registered classes.

For every registered class derived from BaseType, creates a dummy instance of it, and then calls the provided function on that instance. All the dummy instances are destroyed after the function call.

Member Data Documentation

◆ children

template<class BaseType >
std::map<std::string, std::function<std::unique_ptr<BaseType>()> > Factory< BaseType >::children
protected

Registered derived classes.

Each derived class is represented by a function that takes no argument and returns a unique_ptr<BaseType> constructing an instance of that class. This requires classes derived from BaseType to be default constructible.


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