ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
Utils::Factory< T > Class Template Reference

Factory template. More...

#include <Factory.hpp>

Public Types

using pointer_type = std::unique_ptr< T >
 The returned pointer type.
 
using builder_type = pointer_type(*)()
 Type of the constructor functions.
 

Public Member Functions

pointer_type make (const std::string &name) const
 Construct an instance by name.
 
bool has_builder (const std::string &name) const
 Check if the factory knows how to make name.
 
template<typename Derived >
void register_new (const std::string &name)
 Register a new type with the default construction function.
 
const std::string & type_name (T const &o) const
 Look up name for type.
 

Detailed Description

template<class T>
class Utils::Factory< T >

Factory template.

Can be used to construct registered instances of classes derived from the base type (T) by name. One registry per base type (T). To get a new one, use new type ( struct NewT : public T {}; ). To add a new type it has to be given a name an a function of type Factory<T>::builder_type to create an instance has to be provided. The class contains a default implementation for the creation function (Factory<T>::builder<Derived>) which just calls new to create an instance. A user provided function could be used to use a non-default constructor, or to allocate memory for the instance in a specific way, e.g. by placing all new instances in a vector.

Example usage:

struct A {};
struct B : public A {};
struct C : public A {
C(int c) : m_c(c) {}
int m_c;
};
// Register B as 'b' with default builder:
Factory<A>::register_new<B>("b");
// Register C as 'c' with user_defined builder:
return new C(5); });
// Create a B
auto b = Factory<A>::make("b");
assert(dynamic_cast<B *>(b.get()));
// Create a C
auto c = Factory<A>::make("c");
assert(dynamic_cast<C *>(c.get())->m_c == 5);
void register_new(const std::string &name)
Register a new type with the default construction function.
Definition Factory.hpp:113
pointer_type make(const std::string &name) const
Construct an instance by name.
Definition Factory.hpp:89
std::unique_ptr< T > pointer_type
The returned pointer type.
Definition Factory.hpp:81

Definition at line 78 of file Factory.hpp.

Member Typedef Documentation

◆ builder_type

template<class T >
using Utils::Factory< T >::builder_type = pointer_type (*)()

Type of the constructor functions.

Definition at line 83 of file Factory.hpp.

◆ pointer_type

template<class T >
using Utils::Factory< T >::pointer_type = std::unique_ptr<T>

The returned pointer type.

Definition at line 81 of file Factory.hpp.

Member Function Documentation

◆ has_builder()

template<class T >
bool Utils::Factory< T >::has_builder ( const std::string &  name) const
inline

Check if the factory knows how to make name.

Parameters
nameGiven name to check.
Returns
Whether we know how to make a name.

Definition at line 104 of file Factory.hpp.

◆ make()

template<class T >
pointer_type Utils::Factory< T >::make ( const std::string &  name) const
inline

Construct an instance by name.

Definition at line 89 of file Factory.hpp.

Referenced by ScriptInterface::LocalContext::make_shared().

◆ register_new()

◆ type_name()

template<class T >
const std::string & Utils::Factory< T >::type_name ( T const &  o) const
inline

Look up name for type.

For an object whose type can be created by the factory this returns the name under which it is registered. This will consider the dynamic type of polymorphic objects, e.g. it will return the name of the most derived type.

Parameters
oObject whose type is to be considered.
Exceptions
std::out_of_rangeIf the type is not registered.
Returns
Name by which T can be made.

Definition at line 132 of file Factory.hpp.


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