Vita
factory.h
Go to the documentation of this file.
1
13#if !defined(VITA_PRIMITIVE_FACTORY_H)
14#define VITA_PRIMITIVE_FACTORY_H
15
16#include <map>
17
18#include "kernel/gp/symbol.h"
19
20namespace vita
21{
39{
40public:
42
43 std::unique_ptr<symbol> make(const std::string &, cvect = {0});
44 std::unique_ptr<symbol> make(domain_t, int, int, category_t = 0);
45
46 template<class> bool register_symbol(const std::string &, std::size_t);
47 bool unregister_symbol(const std::string &);
48
49 std::size_t args(const std::string &) const;
50
51private:
52 using build_func = std::unique_ptr<symbol> (*)(const cvect &);
53
54 template<class T> static std::unique_ptr<symbol> build(const cvect &c)
55 { return std::make_unique<T>(c); }
56
57 // Private data members.
58 struct build_info
59 {
60 build_func make;
61 std::size_t args;
62 };
63
64 std::map<std::string, build_info> factory_;
65};
66
78template<class T>
79bool symbol_factory::register_symbol(const std::string &name, std::size_t n)
80{
81 return factory_.insert({name, build_info{build<T>, n}}).second;
82}
83
84} // namespace vita
85
86#endif // include guard
An abstract factory for symbols.
Definition: factory.h:39
symbol_factory()
The factory is preloaded with a number of common symbols.
Definition: factory.cc:42
std::size_t args(const std::string &) const
Definition: factory.cc:174
bool unregister_symbol(const std::string &)
Unregister the symbol from the factory.
Definition: factory.cc:191
std::unique_ptr< symbol > make(const std::string &, cvect={0})
Creates a specific instance of a symbol.
Definition: factory.cc:113
bool register_symbol(const std::string &, std::size_t)
Registers a new symbol inside the factory.
Definition: factory.h:79
The main namespace for the project.
domain_t
In an environment where a symbol such as '+' may have many different meanings, it's useful to specify...
Definition: value.h:34
std::size_t category_t
A category provide operations which supplement or supersede those of the domain but which are restric...
Definition: common.h:44