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
25
#include "
CepGen/Modules/NamedModule.h
"
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
44
namespace
cepgen
{
47
template
<
typename
T>
48
class
ModuleFactory
{
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
;
92
ParametersDescription
describeParameters
(
const
ParametersList
&)
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(); }
106
108
inline
const
std::unordered_map<int, std::string>&
indices
()
const
{
return
indices_
; }
109
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
NamedModule.h
cepgen::ModuleFactory
A generic factory to build modules.
Definition
ModuleFactory.h:48
cepgen::ModuleFactory::Builder
std::unique_ptr< T >(* Builder)(const ParametersList &)
Constructor type for a module.
Definition
ModuleFactory.h:87
cepgen::ModuleFactory::build
std::unique_ptr< T > build(const ParametersList &) const
Build one instance of a named module.
Definition
ModuleFactory.cpp:29
cepgen::ModuleFactory::ModuleFactory
ModuleFactory(const ModuleFactory &)=delete
Disabled copy constructor.
cepgen::ModuleFactory::size
size_t size() const
Number of modules registered in the database.
Definition
ModuleFactory.h:105
cepgen::ModuleFactory::describe
std::string describe(const std::string &name) const
Describe one named module Describe the parameters of one named module.
Definition
ModuleFactory.cpp:77
cepgen::ModuleFactory::~ModuleFactory
virtual ~ModuleFactory()=default
Default destructor.
cepgen::ModuleFactory::empty
bool empty() const
Is the database empty?
Definition
ModuleFactory.h:104
cepgen::ModuleFactory::modules
std::vector< std::string > modules() const
List of modules registred in the database.
Definition
ModuleFactory.cpp:123
cepgen::ModuleFactory::operator=
void operator=(const ModuleFactory &)=delete
Disabled assignment operator.
cepgen::ModuleFactory::describeParameters
ParametersDescription describeParameters(const ParametersList &) const
Definition
ModuleFactory.cpp:82
cepgen::ModuleFactory::has
bool has(const std::string &name) const
Check if a named module is registered.
Definition
ModuleFactory.h:109
cepgen::ModuleFactory::description
const std::string & description() const
Describe the modules factory.
Definition
ModuleFactory.h:54
cepgen::ModuleFactory::registerModule
void registerModule(const std::string &name, const ParametersList &def_params=ParametersList())
Register a named module in the database.
Definition
ModuleFactory.h:59
cepgen::ModuleFactory::indices_
std::unordered_map< int, std::string > indices_
Index-to-map association map.
Definition
ModuleFactory.h:124
cepgen::ModuleFactory::indices
const std::unordered_map< int, std::string > & indices() const
Definition
ModuleFactory.h:106
cepgen::ParametersDescription
A description object for parameters collection.
Definition
ParametersDescription.h:26
cepgen::ParametersList
Definition
ParametersList.h:52
cepgen
Common namespace for this Monte Carlo generator.
Definition
CommandLineHandler.cpp:36
cepgen::Element::U
@ U
CepGen
Modules
ModuleFactory.h
Generated on Mon Jul 29 2024 for CepGen by
1.9.7