AstraLog-HPC
Loading...
Searching...
No Matches
GenericFactory.hpp
Go to the documentation of this file.
1#ifndef H_FACTORY_HPP_
2#define H_FACTORY_HPP_
3#include <common/errors.hpp>
4#include <functional>
5#include <map>
6#include <memory>
7#include <sstream>
8#include <type_traits>
9#include <unordered_map>
10#include <vector>
11
12// Template for a generic factory which we used to create our RulesFactory
14{
39template <typename AbstractProduct, typename Identifier,
40 typename Builder = std::function<std::unique_ptr<AbstractProduct>()> >
42{
43public:
45 using AbstractProduct_type = AbstractProduct;
47
52
58 using Container_type = std::unordered_map<Identifier, Builder_type>;
59 static Factory &Instance();
61
72 template <typename... Args>
73 std::unique_ptr<AbstractProduct> create(Identifier const &name,
74 Args &&...args) const;
78 Builder get(Identifier const &name) const;
79 //
81 void add(Identifier const &, Builder_type const &);
83 std::vector<Identifier> registered() const;
85 void
87 {
88 _storage.erase(name);
89 }
91 void
93 {
94 _storage.clear();
95 }
97 [[nodiscard]] bool
98 empty() const
99 {
100 return _storage.empty();
101 }
103 {
104 return _storage;
105 }
106
108 Factory(Factory const &) = delete;
110 Factory &operator=(Factory const &) = delete;
111
112private:
114 Factory() = default;
117};
118
121
126template <typename Identifier, typename FunType>
128
129// **** IMPLEMENTATIONS ****
130
131template <typename AbstractProduct, typename Identifier, typename Builder>
134{
135 // We use the Meyer's trick to istantiate the factory as Singleton
136 static Factory theFactory;
137 return theFactory;
138}
139
140template <typename AbstractProduct, typename Identifier, typename Builder>
143{
144 auto f = _storage.find(name);
145 if(f == _storage.end())
146 {
147 std::stringstream idAsString;
148 // I am assuming that identifier has a output streaming operator
149 idAsString << name;
150 std::string out =
151 "Identifier " + idAsString.str() + " is not stored in the factory";
152 throw astralog::RuleError(out);
153 }
154 else
155 {
156 return f->second;
157 }
158}
159
160template <typename AbstractProduct, typename Identifier, typename Builder>
161template <typename... Args>
162std::unique_ptr<AbstractProduct>
164 Args &&...args) const
165{
166 static_assert(!std::is_same_v<AbstractProduct, void>, "You should use get() not create() on FunctionFactories");
167 // Use of std::forward to forward arguments to the constructor
168 return this->get(name)(std::forward<Args>(args)...);
169}
170
171template <typename AbstractProduct, typename Identifier, typename Builder>
172void
174 Builder_type const &func)
175{
176 auto f = _storage.insert(std::make_pair(name, func));
177 if(f.second == false)
178 {
179 std::stringstream idAsString;
180 idAsString << name;
181 std::string message =
182 std::string("Double registration in Factory of id: ") +
183 idAsString.str() + std::string(" is not allowed");
184 throw astralog::RuleError(message);
185 }
186}
187
188template <typename AbstractProduct, typename Identifier, typename Builder>
189std::vector<Identifier>
191{
192 std::vector<Identifier> tmp;
193 tmp.reserve(_storage.size());
194 for(auto const &i : _storage)
195 tmp.emplace_back(i.first);
196 return tmp;
197}
198
199}
200#endif /* H_FACTORY_HPP_ */
201
202
203
std::string Identifier
Definition RulesFactory.hpp:13
std::function< std::unique_ptr< RuleStrategy >(const RuleConfig &)> Builder
Definition RulesFactory.hpp:14
A generic object factory.
Definition GenericFactory.hpp:42
AbstractProduct AbstractProduct_type
The container for the rules.
Definition GenericFactory.hpp:45
Identifier Identifier_type
The identifier.
Definition GenericFactory.hpp:50
static Factory & Instance()
Definition GenericFactory.hpp:133
void add(Identifier const &, Builder_type const &)
Register the given rule.
Definition GenericFactory.hpp:173
void clear()
clear the factory
Definition GenericFactory.hpp:92
Builder get(Identifier const &name) const
Definition GenericFactory.hpp:142
void unregister(Identifier const &name)
Unregister a rule.
Definition GenericFactory.hpp:86
Container_type const & getStorage() const
Definition GenericFactory.hpp:102
std::unique_ptr< AbstractProduct > create(Identifier const &name, Args &&...args) const
Get the rule with given name .
Definition GenericFactory.hpp:163
std::unordered_map< Identifier, Builder_type > Container_type
Method to access the only instance of the factory.
Definition GenericFactory.hpp:58
Factory(Factory const &)=delete
Deleted since it is a Singleton.
Factory()=default
Made private since it is a Singleton.
Builder Builder_type
The builder type.
Definition GenericFactory.hpp:56
bool empty() const
Test if factory has some items registered.
Definition GenericFactory.hpp:98
Factory & operator=(Factory const &)=delete
Deleted since it is a Singleton.
Container_type _storage
It contains the actual object factory.
Definition GenericFactory.hpp:116
std::vector< Identifier > registered() const
Returns a list of registered rules.
Definition GenericFactory.hpp:190
A rule definition references an unknown type or is otherwise malformed.
Definition errors.hpp:67
Unified exception hierarchy for AstraLog-HPC.
Definition GenericFactory.hpp:14