remage
Simulation framework for HPGe-based experiments
 
Loading...
Searching...
No Matches
RMGUserInit.hh
1// Copyright (C) 2024 Manuel Huber <https://orcid.org/0009-0000-5212-2999>
2//
3// This program is free software: you can redistribute it and/or modify it under
4// the terms of the GNU Lesser General Public License as published by the Free
5// Software Foundation, either version 3 of the License, or (at your option) any
6// later version.
7//
8// This program is distributed in the hope that it will be useful, but WITHOUT
9// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11// details.
12//
13// You should have received a copy of the GNU Lesser General Public License
14// along with this program. If not, see <https://www.gnu.org/licenses/>.
15
16#ifndef _RMG_USER_INIT_HH_
17#define _RMG_USER_INIT_HH_
18
19#include <functional>
20#include <map>
21#include <memory>
22#include <type_traits>
23#include <utility>
24#include <vector>
25
26#include "G4UserSteppingAction.hh"
27#include "G4UserTrackingAction.hh"
28
29#include "RMGLog.hh"
30#include "RMGVGenerator.hh"
31#include "RMGVOutputScheme.hh"
32
44
45 public:
46
50 RMGUserInit() = default;
54 ~RMGUserInit() = default;
55
56 RMGUserInit(RMGUserInit const&) = delete;
57 RMGUserInit& operator=(RMGUserInit const&) = delete;
58 RMGUserInit(RMGUserInit&&) = delete;
59 RMGUserInit& operator=(RMGUserInit&&) = delete;
60
61 // getters
66 [[nodiscard]] auto GetSteppingActions() const { return fSteppingActions; }
71 [[nodiscard]] auto GetTrackingActions() const { return fTrackingActions; }
76 [[nodiscard]] auto GetOutputSchemes() const { return fOutputSchemes; }
81 [[nodiscard]] auto GetOptionalOutputSchemes() const { return fOptionalOutputSchemes; }
86 [[nodiscard]] auto GetUserGenerator() const { return fUserGenerator; }
87
88 // setters
95 template<typename T, typename... Args> void AddSteppingAction(Args&&... args) {
96 Add<T>(&fSteppingActions, std::forward<Args>(args)...);
97 }
98
105 template<typename T, typename... Args> void AddTrackingAction(Args&&... args) {
106 Add<T>(&fTrackingActions, std::forward<Args>(args)...);
107 }
108
115 template<typename T, typename... Args> void AddOutputScheme(Args&&... args) {
116 Add<T>(&fOutputSchemes, std::forward<Args>(args)...);
117 }
118
128 template<typename T, typename... Args>
129 void AddOptionalOutputScheme(std::string name, Args&&... args) {
130 Add<T>(&fOptionalOutputSchemes, name, std::forward<Args>(args)...);
131 }
132
139 template<typename T, typename... Args> void SetUserGenerator(Args&&... args) {
140 Set<T>(fUserGenerator, std::forward<Args>(args)...);
141 }
142
143 // default output schemes
150
158 void ActivateOptionalOutputScheme(std::string name);
159
164 bool IsOptionalOutputSchemeActivated(std::string name);
165
166 private:
167
168 template<typename T> using late_init_fn = std::function<std::unique_ptr<T>()>;
169 template<typename T> using late_init_vec = std::vector<late_init_fn<T>>;
170 template<typename K, typename T> using late_init_map = std::map<K, late_init_fn<T>>;
171
172 template<typename B, typename T, typename... Args> late_init_fn<B> CreateInit(Args&&... args) {
173 return std::bind(&std::make_unique<T, Args&...>, std::forward<Args>(args)...);
174 }
175 template<typename B, typename T> late_init_fn<B> CreateInit() {
176 return [] { return std::make_unique<T>(); };
177 }
178
179 template<typename T, typename B, typename... Args>
180 void Add(late_init_vec<B>* vec, Args&&... args) {
181 static_assert(std::is_base_of<B, T>::value);
182
183 // capture the passed arguments for the constructor to be called later.
184 auto create = CreateInit<B, T>(std::forward<Args>(args)...);
185 vec->emplace_back(std::move(create));
186 }
187 template<typename T, typename B, typename K, typename... Args>
188 void Add(late_init_map<K, B>* map, K k, Args&&... args) {
189 static_assert(std::is_base_of<B, T>::value);
190
191 // capture the passed arguments for the constructor to be called later.
192 auto create = CreateInit<B, T>(std::forward<Args>(args)...);
193 map->emplace(k, std::move(create));
194 }
195 template<typename T, typename B, typename... Args>
196 void Set(late_init_fn<B>& fn, Args&&... args) {
197 static_assert(std::is_base_of<B, T>::value);
198
199 // capture the passed arguments for the constructor to be called later.
200 auto create = CreateInit<B, T>(std::forward<Args>(args)...);
201 fn.swap(create);
202 }
203
204 // store partial custom actions to be used later in RMGUserAction
205 late_init_vec<G4UserSteppingAction> fSteppingActions;
206 late_init_vec<G4UserTrackingAction> fTrackingActions;
207 late_init_vec<RMGVOutputScheme> fOutputSchemes;
208 late_init_map<std::string, RMGVOutputScheme> fOptionalOutputSchemes;
209 late_init_fn<RMGVGenerator> fUserGenerator;
210
211 std::vector<std::string> fActivatedOutputScheme;
212};
213
214#endif
215
216// vim: tabstop=2 shiftwidth=2 expandtab
auto GetTrackingActions() const
Retrieves the collection of tracking actions.
Definition RMGUserInit.hh:71
void SetUserGenerator(Args &&... args)
Sets the user generator to an instance of type T.
Definition RMGUserInit.hh:139
void AddOptionalOutputScheme(std::string name, Args &&... args)
Adds an optional output scheme of type T with a given name.
Definition RMGUserInit.hh:129
void AddSteppingAction(Args &&... args)
Adds a stepping action of type T.
Definition RMGUserInit.hh:95
void AddTrackingAction(Args &&... args)
Adds a tracking action of type T.
Definition RMGUserInit.hh:105
auto GetOptionalOutputSchemes() const
Retrieves the optional output schemes.
Definition RMGUserInit.hh:81
auto GetUserGenerator() const
Retrieves the user generator.
Definition RMGUserInit.hh:86
~RMGUserInit()=default
Default destructor.
auto GetOutputSchemes() const
Retrieves the registered output schemes.
Definition RMGUserInit.hh:76
bool IsOptionalOutputSchemeActivated(std::string name)
Check whether the optional output scheme is activated.
Definition RMGUserInit.cc:51
void RegisterDefaultOptionalOutputSchemes()
Registers the default optional output schemes.
Definition RMGUserInit.cc:26
void ActivateOptionalOutputScheme(std::string name)
Activates an optional output scheme by its name.
Definition RMGUserInit.cc:36
auto GetSteppingActions() const
Retrieves the collection of stepping actions.
Definition RMGUserInit.hh:66
void AddOutputScheme(Args &&... args)
Adds an output scheme of type T.
Definition RMGUserInit.hh:115
RMGUserInit()=default
Default constructor.