Vita
Classes | Public Member Functions | List of all members
vita::symbol_factory Class Reference

An abstract factory for symbols. More...

#include <factory.h>

Public Member Functions

std::size_t args (const std::string &) const
 
std::unique_ptr< symbolmake (const std::string &, cvect={0})
 Creates a specific instance of a symbol. More...
 
std::unique_ptr< symbolmake (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...
 

Detailed Description

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.

Definition at line 38 of file factory.h.

Constructor & Destructor Documentation

◆ symbol_factory()

vita::symbol_factory::symbol_factory ( )

The factory is preloaded with a number of common symbols.

Definition at line 42 of file factory.cc.

Member Function Documentation

◆ args()

std::size_t vita::symbol_factory::args ( const std::string &  name) const
Parameters
[in]namename of the symbol (case sensitive)
Returns
number of distinct categories needed to build the symbol

Definition at line 174 of file factory.cc.

◆ make() [1/2]

std::unique_ptr< symbol > vita::symbol_factory::make ( const std::string &  name,
cvect  c = {0} 
)

Creates a specific instance of a symbol.

Parameters
[in]namename of the symbol to be created (case sensitive)
[in]ca list of categories used by the the symbol constructor
Returns
an abstract pointer to the created symbol

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.

Attention
If name is not recognized as a preregistered symbol, it's registered on the fly as a constant.
Note
  • The client code has no knowledge whatsoever of the concrete type, not needing to include any header files or class declarations relating to the concrete type. The client code deals only with the abstract type. Objects of a concrete type are indeed created by the factory, but the client code accesses such objects only through their abstract interface.
  • Adding new concrete types is done by modifying the client code to use a different factory, a modification which is typically one line in one file (the different factory then creates objects of a different concrete type, but still returns a pointer of the same abstract type as before - thus insulating the client code from change). This is significantly easier than modifying the client code to instantiate a new type, which would require changing every location in the code where a new object is created (as well as making sure that all such code locations also have knowledge of the new concrete type, by including for instance a concrete class header file).

Definition at line 113 of file factory.cc.

◆ make() [2/2]

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.

Parameters
[in]ddomain of the symbol
[in]minlower bound for the number value
[in]maxupper bound for the number value
[in]ca category used by the symbol constructor
Returns
an abstract pointer to the created symbol

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.

◆ register_symbol()

template<class T >
bool vita::symbol_factory::register_symbol ( const std::string &  name,
std::size_t  n 
)

Registers a new symbol inside the factory.

Parameters
[in]namename of the symbol to be registered (case sensitive)
[in]nnumber of arguments for the constructor of the symbol
Returns
true if the symbol T has been added to the factory
Warning
name is a key: the function doesn't register different symbols with the same name.

Definition at line 79 of file factory.h.

◆ unregister_symbol()

bool vita::symbol_factory::unregister_symbol ( const std::string &  name)

Unregister the symbol from the factory.

Parameters
[in]namename of the symbol (case sensitive)
Returns
true if the symbol has been unregistered
Note
Constants and variable aren't registered in the factory, so they cannot be unregistered.

Definition at line 191 of file factory.cc.


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