AstraLog-HPC
Loading...
Searching...
No Matches
GenericFactory::Factory< AbstractProduct, Identifier, Builder > Class Template Reference

A generic object factory. More...

#include <GenericFactory.hpp>

Collaboration diagram for GenericFactory::Factory< AbstractProduct, Identifier, Builder >:
Collaboration graph

Public Types

using AbstractProduct_type = AbstractProduct
 The container for the rules.
 
using Identifier_type = Identifier
 The identifier.
 
using Builder_type = Builder
 The builder type.
 
using Container_type = std::unordered_map< Identifier, Builder_type >
 Method to access the only instance of the factory.
 

Public Member Functions

template<typename... Args>
std::unique_ptr< AbstractProduct > create (Identifier const &name, Args &&...args) const
 Get the rule with given name .
 
Builder get (Identifier const &name) const
 
void add (Identifier const &, Builder_type const &)
 Register the given rule.
 
std::vector< Identifier > registered () const
 Returns a list of registered rules.
 
void unregister (Identifier const &name)
 Unregister a rule.
 
void clear ()
 clear the factory
 
bool empty () const
 Test if factory has some items registered.
 
Container_type const & getStorage () const
 
 Factory (Factory const &)=delete
 Deleted since it is a Singleton.
 
Factory & operator= (Factory const &)=delete
 Deleted since it is a Singleton.
 

Static Public Member Functions

static Factory & Instance ()
 

Private Member Functions

 Factory ()=default
 Made private since it is a Singleton.
 

Private Attributes

Container_type _storage
 It contains the actual object factory.
 

Detailed Description

template<typename AbstractProduct, typename Identifier, typename Builder = std::function<std::unique_ptr<AbstractProduct>()>>
class GenericFactory::Factory< AbstractProduct, Identifier, Builder >

A generic object factory.

Freely inspired from the Alexandrescu Book.

It is implemented as a Singleton. The compulsory way to access a method is Factory::Instance().method(). Typically to access the factory one does

myFactory.add(...)
static Factory & Instance()
Definition GenericFactory.hpp:133
Builder get(Identifier const &name) const
Definition GenericFactory.hpp:142
Template Parameters
AbstractProductThe type of the base abstract class whose object are produced by the factory
IdentifierThe identifier used to select the concrete object produced by the factory. It must have an ordering relation since they will be stored as key in a std::map
BuilderThe type of the callable object that produces the the concrete object derived from AbstractProduct It may have arguments but its return type must be std::unique_ptr<AbstractProduct>.
Exceptions
Anastralog::RuleError if something goes wrong when querying the factory (unknown or doubly-registered identifier).
Note
The Identifier should have a output stream operator for the construction of the exception.

Member Typedef Documentation

◆ AbstractProduct_type

template<typename AbstractProduct , typename Identifier , typename Builder = std::function<std::unique_ptr<AbstractProduct>()>>
using GenericFactory::Factory< AbstractProduct, Identifier, Builder >::AbstractProduct_type = AbstractProduct

The container for the rules.

◆ Builder_type

template<typename AbstractProduct , typename Identifier , typename Builder = std::function<std::unique_ptr<AbstractProduct>()>>
using GenericFactory::Factory< AbstractProduct, Identifier, Builder >::Builder_type = Builder

The builder type.

The default is a function wrapper that returns a unique_ptr<AbstractProduct> and with no arguments.

◆ Container_type

template<typename AbstractProduct , typename Identifier , typename Builder = std::function<std::unique_ptr<AbstractProduct>()>>
using GenericFactory::Factory< AbstractProduct, Identifier, Builder >::Container_type = std::unordered_map<Identifier, Builder_type>

Method to access the only instance of the factory.

◆ Identifier_type

template<typename AbstractProduct , typename Identifier , typename Builder = std::function<std::unique_ptr<AbstractProduct>()>>
using GenericFactory::Factory< AbstractProduct, Identifier, Builder >::Identifier_type = Identifier

The identifier.

It is used to identify the rules.

Constructor & Destructor Documentation

◆ Factory() [1/2]

template<typename AbstractProduct , typename Identifier , typename Builder = std::function<std::unique_ptr<AbstractProduct>()>>
GenericFactory::Factory< AbstractProduct, Identifier, Builder >::Factory ( Factory< AbstractProduct, Identifier, Builder > const &  )
delete

Deleted since it is a Singleton.

◆ Factory() [2/2]

template<typename AbstractProduct , typename Identifier , typename Builder = std::function<std::unique_ptr<AbstractProduct>()>>
GenericFactory::Factory< AbstractProduct, Identifier, Builder >::Factory ( )
privatedefault

Made private since it is a Singleton.

Member Function Documentation

◆ add()

◆ clear()

template<typename AbstractProduct , typename Identifier , typename Builder = std::function<std::unique_ptr<AbstractProduct>()>>
void GenericFactory::Factory< AbstractProduct, Identifier, Builder >::clear ( )
inline

clear the factory

◆ create()

template<typename... Args>
std::unique_ptr< AbstractProduct > GenericFactory::Factory< AbstractProduct, Identifier, Builder >::create ( Identifier const &  name,
Args &&...  args 
) const

Get the rule with given name .

The pointer is null if no rule is present.

Template Parameters
ArgsAutomatically deduced parameter pack, it is the list of types of the builder arguments. It may be empty.
Parameters
nameThe identifier
argsThe arguments pack to be passed to the builder. It may be empty
Note
The default builder does not take any argument. This version is provided in case the template is specialized with a builder that takes arguments. The method is disabled if AbstractProduct is void

◆ empty()

template<typename AbstractProduct , typename Identifier , typename Builder = std::function<std::unique_ptr<AbstractProduct>()>>
bool GenericFactory::Factory< AbstractProduct, Identifier, Builder >::empty ( ) const
inline

Test if factory has some items registered.

◆ get()

template<typename AbstractProduct , typename Identifier , typename Builder >
Builder GenericFactory::Factory< AbstractProduct, Identifier, Builder >::get ( Identifier const &  name) const

Returns the builder

◆ getStorage()

template<typename AbstractProduct , typename Identifier , typename Builder = std::function<std::unique_ptr<AbstractProduct>()>>
Container_type const & GenericFactory::Factory< AbstractProduct, Identifier, Builder >::getStorage ( ) const
inline

◆ Instance()

Here is the caller graph for this function:

◆ operator=()

Deleted since it is a Singleton.

◆ registered()

Returns a list of registered rules.

◆ unregister()

template<typename AbstractProduct , typename Identifier , typename Builder = std::function<std::unique_ptr<AbstractProduct>()>>
void GenericFactory::Factory< AbstractProduct, Identifier, Builder >::unregister ( Identifier const &  name)
inline

Unregister a rule.

Member Data Documentation

◆ _storage

template<typename AbstractProduct , typename Identifier , typename Builder = std::function<std::unique_ptr<AbstractProduct>()>>
Container_type GenericFactory::Factory< AbstractProduct, Identifier, Builder >::_storage
private

It contains the actual object factory.


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