ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
Factory.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2026 The ESPResSo project
3 * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010
4 * Max-Planck-Institute for Polymer Research, Theory Group
5 *
6 * This file is part of ESPResSo.
7 *
8 * ESPResSo is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * ESPResSo is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
24#include <cassert>
25#include <memory>
26#include <stdexcept>
27#include <string>
28#include <typeindex>
29#include <unordered_map>
30
31namespace Utils {
32
33/**
34 * @brief Factory template.
35 *
36 * Can be used to construct registered instances of classes derived
37 * from the base type (`Base`) by name.
38 * Only one registry per base type (`Base`). To get a new one,
39 * use a new type ( `struct Derived : public Base {};` ).
40 * To add a new type it has to be given a name an a function of type
41 * `%Factory<Base>::%builder_type` to create an instance has to be provided.
42 * The class contains a default implementation for the creation
43 * function (`%Factory<Base>::%builder<Derived>`) which just calls
44 * new to create an instance. A user provided function could
45 * be used to use a non-default constructor, or to allocate memory
46 * for the instance in a specific way, e.g. by placing all new instances
47 * in a vector.
48 *
49 * Example usage:
50 * @code{.cpp}
51 * struct A {};
52 * struct B : public A {};
53 * struct C : public A {
54 * C(int c) : m_c(c) {}
55 * int m_c;
56 * };
57 *
58 * // Register B as 'b' with default builder:
59 * Factory<A>::register_new<B>("b");
60 * // Register C as 'c' with user_defined builder:
61 * Factory<A>::register_new("c", []() -> typename Factory<A>::pointer_type {
62 * return new C(5); });
63 *
64 * // Create a B
65 * auto b = Factory<A>::make("b");
66 * assert(dynamic_cast<B *>(b.get()));
67 *
68 * // Create a C
69 * auto c = Factory<A>::make("c");
70 * assert(dynamic_cast<C *>(c.get())->m_c == 5);
71 * @endcode
72 */
73template <class Base> class Factory {
74public:
75 /** The returned pointer type */
76 using pointer_type = std::unique_ptr<Base>;
77 /** Type of the constructor functions */
79
80public:
81 /**
82 * @brief Construct an instance by name.
83 */
84 pointer_type make(std::string const &name) const {
85 try {
86 auto builder = m_map.at(name);
87 return assert(builder), builder();
88 } catch (std::out_of_range const &) {
89 throw std::domain_error("Class '" + name + "' not found.");
90 }
91 }
92
93 /**
94 * @brief Check if the factory knows how to make `name`.
95 *
96 * @param name Given name to check.
97 * @return Whether we know how to make a `name`.
98 */
99 bool has_builder(std::string const &name) const {
100 return m_map.contains(name);
101 }
102
103 /**
104 * @brief Register a new type with the default construction function.
105 *
106 * @param name Given name for the type, must be unique.
107 */
108 template <typename Derived> void register_new(std::string const &name) {
109 if (m_map.contains(name)) {
110 throw std::runtime_error("Name '" + name + "' was already registered.");
111 }
112 m_map[name] = []() { return pointer_type(new Derived()); };
113 m_type_map[typeid(Derived)] = name;
114 }
115
116 /**
117 * @brief Look up name for type.
118 *
119 * For an object whose type can be created by
120 * the factory this returns the name under which
121 * it is registered. This will consider the
122 * dynamic type of polymorphic objects, e.g. it
123 * will return the name of the most derived type.
124 *
125 * @param o Object whose type is to be considered.
126 * @throw std::out_of_range If the type is not registered.
127 *
128 * @return Name by which `Base` can be made.
129 */
130 auto const &type_name(Base const &o) const {
131 return m_type_map.at(typeid(o));
132 }
133
134private:
135 /** Maps names to construction functions. */
136 std::unordered_map<std::string, builder_type> m_map;
137 /** Maps types to names */
138 std::unordered_map<std::type_index, std::string> m_type_map;
139};
140} /* namespace Utils */
Factory template.
Definition Factory.hpp:73
pointer_type(*)() builder_type
Type of the constructor functions.
Definition Factory.hpp:78
void register_new(std::string const &name)
Register a new type with the default construction function.
Definition Factory.hpp:108
bool has_builder(std::string const &name) const
Check if the factory knows how to make name.
Definition Factory.hpp:99
pointer_type make(std::string const &name) const
Construct an instance by name.
Definition Factory.hpp:84
std::unique_ptr< Base > pointer_type
The returned pointer type.
Definition Factory.hpp:76
auto const & type_name(Base const &o) const
Look up name for type.
Definition Factory.hpp:130
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.