Flow123d  JS_before_hm-1804-gf2ad740aa
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
Input::Factory< Type, Arguments > Class Template Reference

This class implements more general factory mechanism to construct classes. More...

#include <factory.hh>

Collaboration diagram for Input::Factory< Type, Arguments >:
Collaboration graph
[legend]

Public Member Functions

const shared_ptr< Type > create (string name, Arguments... arguments) const
 create an instance of a registered class More...
 

Static Public Member Functions

static Factoryinstance ()
 Get the single instance of the factory. More...
 
template<class Child >
static int register_class (string class_name)
 Register lambda function that calls default constructor of Type. More...
 

Private Member Functions

 Factory ()
 Forbid default constructor. More...
 

Private Attributes

map< string, boost::any > factory_registry_
 the registry of factory functions More...
 

Detailed Description

template<class Type, class... Arguments>
class Input::Factory< Type, Arguments >

This class implements more general factory mechanism to construct classes.

One factory allows constructing derived classes of one base class. This class is determined by template parameter Type and all descendants must implement constructor with same parameters (given by template parameter Arguments). This constructor is called by factory.

All descendants must contain:

  1. constructor with parameters given by Arguments
  2. declaration of parent class as typedef with name FactoryBaseType
  3. private static integer variable what is only for registration class to factory, this variable only allow to register class to factory and its implementation must call: (a) Factory::register_class that adds constructor of class to factory (b) generating function of Input::Type::Record (usually get_input_type() method)

Simple example of usage:

class SomeDescendant : public SomeBase
{
public:
/// typedef of parent class
typedef SomeBase FactoryBaseType;
/// Return initialization Record
static const Input::Type::Record & get_input_type();
/// constructor
SomeDescendant() {}
private:
/// registers class to factory
static const int reg;
}
/// implementation of registration variable
const int SomeDescendant::reg =
Input::register_class< SomeDescendant >("SomeDescendant") +
SomeDescendant::get_input_type().size();

Factory allow to accept constructor with one or more parameters. In this case Factory is also templated by these parameters. For example Factory< SomeBase, int, double > accepts constructors with two parameters (int, double).

If registered class is templated the following design have to be used:

/// Example of class templated by integer parameter
template <int dimension>
class SomeDescendant : public SomeBase<dimension>
{
public:
/// constructor
SomeDescendant(double time) {}
...
}
/// implementation of registration variable
const int SomeDescendant::reg =
Input::register_class< SomeDescendant<dimension>, double >("SomeDescendant") +
SomeDescendant<dimension>::get_input_type().size();

Factory can be used in two ways:

For correct functionality must be used two macros defined in global_defs.h:

Definition at line 132 of file factory.hh.

Constructor & Destructor Documentation

◆ Factory()

template<class Type , class... Arguments>
Input::Factory< Type, Arguments >::Factory ( )
inlineprivate

Forbid default constructor.

Definition at line 150 of file factory.hh.

Member Function Documentation

◆ create()

template<class Type , class... Arguments>
const shared_ptr< Type > Input::Factory< Type, Arguments >::create ( string  name,
Arguments...  arguments 
) const

create an instance of a registered class

Definition at line 49 of file factory_impl.hh.

Here is the caller graph for this function:

◆ instance()

template<class Type , class... Arguments>
Factory< Type, Arguments... > * Input::Factory< Type, Arguments >::instance
static

Get the single instance of the factory.

Definition at line 27 of file factory_impl.hh.

Here is the caller graph for this function:

◆ register_class()

template<class Type , class... Arguments>
template<class Child >
int Input::Factory< Type, Arguments >::register_class ( string  class_name)
static

Register lambda function that calls default constructor of Type.

Definition at line 36 of file factory_impl.hh.

Member Data Documentation

◆ factory_registry_

template<class Type , class... Arguments>
map<string, boost::any> Input::Factory< Type, Arguments >::factory_registry_
private

the registry of factory functions

Definition at line 153 of file factory.hh.


The documentation for this class was generated from the following files:
Input::Type::Record
Record type proxy class.
Definition: type_record.hh:182
Input::Factory::instance
static Factory * instance()
Get the single instance of the factory.
Definition: factory_impl.hh:27
Input::Factory::create
const shared_ptr< Type > create(string name, Arguments... arguments) const
create an instance of a registered class
Definition: factory_impl.hh:49