9#include <unordered_map>
39template <
typename AbstractProduct,
typename Identifier,
40 typename Builder = std::function<std::unique_ptr<AbstractProduct>()> >
72 template <
typename... Args>
74 Args &&...args)
const;
126template <
typename Identifier,
typename FunType>
131template <
typename AbstractProduct,
typename Identifier,
typename Builder>
140template <
typename AbstractProduct,
typename Identifier,
typename Builder>
144 auto f = _storage.find(name);
145 if(f == _storage.end())
147 std::stringstream idAsString;
151 "Identifier " + idAsString.str() +
" is not stored in the factory";
160template <
typename AbstractProduct,
typename Identifier,
typename Builder>
161template <
typename... Args>
162std::unique_ptr<AbstractProduct>
164 Args &&...args)
const
166 static_assert(!std::is_same_v<AbstractProduct, void>,
"You should use get() not create() on FunctionFactories");
168 return this->get(name)(std::forward<Args>(args)...);
171template <
typename AbstractProduct,
typename Identifier,
typename Builder>
176 auto f = _storage.insert(std::make_pair(name, func));
177 if(f.second ==
false)
179 std::stringstream idAsString;
181 std::string message =
182 std::string(
"Double registration in Factory of id: ") +
183 idAsString.str() + std::string(
" is not allowed");
188template <
typename AbstractProduct,
typename Identifier,
typename Builder>
189std::vector<Identifier>
192 std::vector<Identifier> tmp;
193 tmp.reserve(_storage.size());
194 for(
auto const &i : _storage)
195 tmp.emplace_back(i.first);
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