|
svMultiPhysics
|
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. | |
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.
|
inlinestatic |
Instantiate a derived classs by name.
|
inlinestaticprotected |
Access the singleton instance.
|
inlinestatic |
Register a derived class in the factory.
| [in] | name | Label for the class to be registered. An instance of that class can then be created by passing this same label to the create method. |
|
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.
|
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.