22#ifndef UTILS_FACTORY_HPP
23#define UTILS_FACTORY_HPP
31#include <unordered_map>
91 auto builder = m_map.at(name);
92 return assert(builder), builder();
93 }
catch (std::out_of_range
const &) {
94 throw std::domain_error(
"Class '" + name +
"' not found.");
105 return not(m_map.find(name) == m_map.end());
113 template <
typename Derived>
void register_new(
const std::string &name) {
114 m_map[name] = []() {
return pointer_type(
new Derived()); };
115 m_type_map[
typeid(Derived)] = name;
133 return m_type_map.at(
typeid(o));
138 std::unordered_map<std::string, builder_type> m_map;
140 std::unordered_map<std::type_index, std::string> m_type_map;
void register_new(const std::string &name)
Register a new type with the default construction function.
pointer_type make(const std::string &name) const
Construct an instance by name.
const std::string & type_name(T const &o) const
Look up name for type.
std::unique_ptr< T > pointer_type
The returned pointer type.
pointer_type(*)() builder_type
Type of the constructor functions.
bool has_builder(const std::string &name) const
Check if the factory knows how to make name.