VIPRA Documentation
Loading...
Searching...
No Matches
module.hpp
1#pragma once
2
3#include <tuple>
4#include <type_traits>
5
6#include "vipra/logging/logging.hpp"
7
8#include "vipra/concepts/has_parameters.hpp"
9#include "vipra/random/random.hpp"
10#include "vipra/special_modules/parameters.hpp"
11#include "vipra/util/crtp.hpp"
12
13namespace VIPRA::Modules {
14
20template <typename class_t>
21class BaseModule : public Util::CRTP<BaseModule<class_t>> {
23
24 public:
25 void register_base_params(Parameters& paramIn);
26
27 void config_base(Parameters& paramIn, VIPRA::Random::Engine& engine);
28};
29
35template <typename class_t>
36class Module : public Util::CRTP<Module<class_t>> {
37 using Util::CRTP<Module<class_t>>::self;
38
39 public:
40 void register_params(Parameters& paramIn);
41 void config(Parameters& paramIn, VIPRA::Random::Engine& engine);
42};
43
44// ----------------------------------------------------------------------------------------------------------------
45// --------------------------------------- IMPLEMENTATIONS --------------------------------------------------------
46// ----------------------------------------------------------------------------------------------------------------
47
54template <typename class_t>
55void Module<class_t>::register_params(Parameters& paramIn)
56{
58 "Module is missing VIPRA_REGISTER_PARAMS");
59
60 VIPRA::Log::debug("Registering Params For: {}", self().module_name());
61
62 // get module parameters customization point
63 auto params = self().module_params();
64
65 if constexpr ( ! std::is_same_v<std::tuple<>, std::remove_cvref_t<decltype(params)>> ) {
66 // register all of the parameters with the parameter loader
67 std::apply(
68 [&](auto const& first, auto const&... restArgs) {
69 auto regis = [&](auto const& param) {
70 paramIn.register_param(self().module_type(), self().module_name(),
71 param.first);
72 };
73
74 // recursively register each parameter
75 regis(first);
76 (regis(restArgs), ...);
77 },
78 params);
79 }
80
81 VIPRA::Log::debug("Done Registering Params For: {}", self().module_name());
82}
83
91template <typename class_t>
92void Module<class_t>::config(Parameters& paramIn, VIPRA::Random::Engine& engine)
93{
95 "Module is missing VIPRA_REGISTER_PARAMS");
96
97 VIPRA::Log::debug("Configuring Module: {}", self().module_name());
98
99 // get module parameters customization point
100 auto const& params = self().module_params();
101
102 if constexpr ( ! std::is_same_v<std::tuple<>, std::remove_cvref_t<decltype(params)>> ) {
103 // load each parameter and apply it to the coresponding variable
104 std::apply(
105 [&](auto const& first, auto const&... restArgs) {
106 auto configure = [&](auto const& param) {
107 using param_t = std::remove_cvref_t<decltype(param.second)>;
108 VIPRA::Log::debug("Loading Param: {}, {}", param.first,
109 static_cast<void*>(&param.second));
110
111 // set the parameter variable as loaded from input
112 param.second = paramIn.get_param<param_t>(
113 self().module_type(), self().module_name(), param.first, engine);
114 };
115
116 // recursively load the parameters
117 configure(first);
118 (configure(restArgs), ...);
119 },
120 params);
121 }
122
123 VIPRA::Log::debug("Done Configuring Module: {}", self().module_name());
124}
125
132template <typename class_t>
134{
135 // get module parameters customization point
136 auto params = self().base_module_params();
137
138 if constexpr ( ! std::is_same_v<std::tuple<>, std::remove_cvref_t<decltype(params)>> ) {
139 // register all of the parameters with the parameter loader
140 std::apply(
141 [&](auto const& first, auto const&... restArgs) {
142 auto regis = [&](auto const& param) {
143 paramIn.register_param(self().module_type(), "Base", param.first);
144 };
145
146 // recursively register each parameter
147 regis(first);
148 (regis(restArgs), ...);
149 },
150 params);
151 }
152}
153
161template <typename class_t>
163{
164 // get module parameters customization point
165 auto const& params = self().base_module_params();
166
167 if constexpr ( ! std::is_same_v<std::tuple<>, std::remove_cvref_t<decltype(params)>> ) {
168 // load each parameter and apply it to the coresponding variable
169 std::apply(
170 [&](auto const& first, auto const&... restArgs) {
171 auto configure = [&](auto const& param) {
172 using param_t = std::remove_cvref_t<decltype(param.second)>;
173 VIPRA::Log::debug("Loading Param: {}, {}", param.first,
174 static_cast<void*>(&param.second));
175
176 // set the parameter variable as loaded from input
177 param.second = paramIn.get_param<param_t>(self().module_type(), "Base",
178 param.first, engine);
179 };
180
181 // recursively load the parameters
182 configure(first);
183 (configure(restArgs), ...);
184 },
185 params);
186 }
187}
188} // namespace VIPRA::Modules
static VIPRA_INLINE void debug(fmt::format_string< param_ts... > message, param_ts &&... params)
Calls the provided Logger with Level DEBUG.
Definition logging.hpp:85
VIPRA Module Base CRTP Class.
Definition module.hpp:21
void config_base(Parameters &paramIn, VIPRA::Random::Engine &engine)
Loads in all parameters for the module.
Definition module.hpp:162
void register_base_params(Parameters &paramIn)
Registers the modules parameters with the parameter reader.
Definition module.hpp:133
VIPRA Module Base CRTP Class.
Definition module.hpp:36
void config(Parameters &paramIn, VIPRA::Random::Engine &engine)
Loads in all parameters for the module.
Definition module.hpp:92
void register_params(Parameters &paramIn)
Registers the modules parameters with the parameter reader.
Definition module.hpp:55
auto get_param(Modules::Type module, std::string const &moduleName, std::string const &paramName, Random::Engine &engine) const -> std::remove_cvref_t< data_t >
Returns the value of the parameter if it exists, otherwise throws an error.
Definition parameters.hpp:87
void register_param(Modules::Type module, std::string const &moduleName, std::string const &paramName)
Registers a parameter for a module.
Definition parameters.cpp:28
Psuedo Random number engine.
Definition random.hpp:22
Definition crtp.hpp:6
Definition has_parameters.hpp:7