Vita
|
An abstract factory for symbols. More...
#include <factory.h>
Public Member Functions | |
std::size_t | args (const std::string &) const |
std::unique_ptr< symbol > | make (const std::string &, cvect={0}) |
Creates a specific instance of a symbol. More... | |
std::unique_ptr< symbol > | make (domain_t, int, int, category_t=0) |
Creates an instance of a number. More... | |
template<class > | |
bool | register_symbol (const std::string &, std::size_t) |
Registers a new symbol inside the factory. More... | |
symbol_factory () | |
The factory is preloaded with a number of common symbols. More... | |
bool | unregister_symbol (const std::string &) |
Unregister the symbol from the factory. More... | |
An abstract factory for symbols.
The essence of the pattern is to provide an interface for creating families of related or dependent objects, i.e. symbols, without specifying their concrete classes (e.g. numbers, functions...).
The factory determines the actual concrete type of the symbol to be created and it's here that the object is actually created. However, the factory only returns an abstract pointer to the created concrete object.
This insulates client code from object creation by having clients ask a factory object to create an object of the desired abstract type and to return an abstract pointer to the object.
vita::symbol_factory::symbol_factory | ( | ) |
The factory is preloaded with a number of common symbols.
Definition at line 42 of file factory.cc.
std::size_t vita::symbol_factory::args | ( | const std::string & | name | ) | const |
[in] | name | name of the symbol (case sensitive) |
Definition at line 174 of file factory.cc.
std::unique_ptr< symbol > vita::symbol_factory::make | ( | const std::string & | name, |
cvect | c = {0} |
||
) |
Creates a specific instance of a symbol.
[in] | name | name of the symbol to be created (case sensitive) |
[in] | c | a list of categories used by the the symbol constructor |
As the factory only returns an abstract pointer, the client code (which requests the object from the factory) does not know - and is not burdened by - the actual concrete type of the object which was just created. However, the type of the concrete object is known by the abstract factory via the name
and c
arguments.
name
is not recognized as a preregistered symbol, it's registered on the fly as a constant.Definition at line 113 of file factory.cc.
std::unique_ptr< symbol > vita::symbol_factory::make | ( | domain_t | d, |
int | min, | ||
int | max, | ||
category_t | c = 0 |
||
) |
Creates an instance of a number.
[in] | d | domain of the symbol |
[in] | min | lower bound for the number value |
[in] | max | upper bound for the number value |
[in] | c | a category used by the symbol constructor |
This is an alternative way to build a number. The other make
method finds the domain of the number checking the input string's format.
Here there're explicit parameters for domain / category and we can also specify a range ([min; max]
).
Definition at line 154 of file factory.cc.
bool vita::symbol_factory::register_symbol | ( | const std::string & | name, |
std::size_t | n | ||
) |
Registers a new symbol inside the factory.
[in] | name | name of the symbol to be registered (case sensitive) |
[in] | n | number of arguments for the constructor of the symbol |
true
if the symbol T
has been added to the factoryname
is a key: the function doesn't register different symbols with the same name. bool vita::symbol_factory::unregister_symbol | ( | const std::string & | name | ) |
Unregister the symbol from the factory.
[in] | name | name of the symbol (case sensitive) |
true
if the symbol has been unregisteredDefinition at line 191 of file factory.cc.