cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
ModuleFactory.h
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2018-2024 Laurent Forthomme
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifndef CepGen_Modules_ModuleFactory_h
20#define CepGen_Modules_ModuleFactory_h
21
22#include <memory>
23#include <sstream>
24
26
28#define BUILDERNM(obj) obj##Builder
30#define DEFINE_FACTORY(name, obj_type, descr) \
31 struct name : public ModuleFactory<obj_type> { \
32 explicit name() : ModuleFactory(descr) {} \
33 inline static name& get() { \
34 static name instance; \
35 return instance; \
36 } \
37 inline name& addIndex(int index, const std::string& mod_name) { \
38 indices_[index] = mod_name; \
39 return *this; \
40 } \
41 }; \
42 static_assert(true, "")
43
44namespace cepgen {
47 template <typename T>
49 public:
50 ModuleFactory(const ModuleFactory&) = delete;
51 virtual ~ModuleFactory() = default;
52 void operator=(const ModuleFactory&) = delete;
53
54 inline const std::string& description() const { return description_; }
55
58 template <typename U>
59 inline void registerModule(const std::string& name, const ParametersList& def_params = ParametersList()) {
60 static_assert(std::is_base_of<T, U>::value,
61 "\n\n *** Failed to register an object with improper inheritance into the factory. ***\n");
62 if (has(name)) {
63 std::ostringstream oss;
64 oss << "\n\n *** " << description_ << " detected a duplicate module registration for index/name \"" << name
65 << "\"! ***\n";
66 throw std::invalid_argument(oss.str());
67 }
68 map_.insert(std::make_pair(name, &buildModule<U>));
69 auto desc = U::description();
70 if (!def_params.empty())
71 desc.parameters() += def_params;
72 desc.parameters().setName(name);
73 params_map_[name] = desc;
74 }
77 std::unique_ptr<T> build(const ParametersList&) const;
81 std::unique_ptr<T> build(const std::string& name, const ParametersList& params = ParametersList()) const;
85 std::unique_ptr<T> build(int index, const ParametersList& params = ParametersList()) const;
86
87 typedef std::unique_ptr<T> (*Builder)(const ParametersList&);
88
89 std::string describe(const std::string& name) const;
96 ParametersDescription describeParameters(const std::string& name,
97 const ParametersList& params = ParametersList()) const;
101 ParametersDescription describeParameters(int index, const ParametersList& params = ParametersList()) const;
102
103 std::vector<std::string> modules() const;
104 inline bool empty() const { return map_.empty(); }
105 inline size_t size() const { return map_.size(); }
108 inline const std::unordered_map<int, std::string>& indices() const { return indices_; }
111 inline bool has(const std::string& name) const { return map_.count(name) > 0; }
112
113 private:
115 template <typename U>
116 inline static std::unique_ptr<T> buildModule(const ParametersList& params) {
117 return std::unique_ptr<T>(new U(params));
118 }
119 const std::string description_;
120 std::unordered_map<std::string, Builder> map_;
121 std::unordered_map<std::string, ParametersDescription> params_map_;
122 const ParametersDescription empty_params_desc_;
123
124 protected:
125 explicit ModuleFactory(const std::string&);
126 std::unordered_map<int, std::string> indices_;
127 };
128} // namespace cepgen
129
130#endif
A generic factory to build modules.
std::unique_ptr< T >(* Builder)(const ParametersList &)
Constructor type for a module.
std::unique_ptr< T > build(const ParametersList &) const
Build one instance of a named module.
ModuleFactory(const ModuleFactory &)=delete
Disabled copy constructor.
size_t size() const
Number of modules registered in the database.
std::string describe(const std::string &name) const
Describe one named module Describe the parameters of one named module.
virtual ~ModuleFactory()=default
Default destructor.
bool empty() const
Is the database empty?
std::vector< std::string > modules() const
List of modules registred in the database.
void operator=(const ModuleFactory &)=delete
Disabled assignment operator.
ParametersDescription describeParameters(const ParametersList &) const
bool has(const std::string &name) const
Check if a named module is registered.
const std::string & description() const
Describe the modules factory.
void registerModule(const std::string &name, const ParametersList &def_params=ParametersList())
Register a named module in the database.
std::unordered_map< int, std::string > indices_
Index-to-map association map.
const std::unordered_map< int, std::string > & indices() const
A description object for parameters collection.
Common namespace for this Monte Carlo generator.