10 #ifndef DAQ_CONFIG_MANAGER_HPP
11 #define DAQ_CONFIG_MANAGER_HPP
17 #include <unordered_map>
19 #include <config-ng/ciiConfigApi.hpp>
20 #include <fmt/chrono.h>
21 #include <fmt/format.h>
22 #include <fmt/ostream.h>
23 #include <log4cplus/logger.h>
24 #include <log4cplus/loggingmacros.h>
25 #include <rad/cii/oldbTypes.hpp>
87 static rad::cii::OldbType
ToCii(T
const& t) {
94 static rad::cii::OldbType
ToCii(std::filesystem::path
const& p) {
101 static rad::cii::OldbType
ToCii(std::chrono::seconds
const& s) {
108 static rad::cii::OldbType
ToCii(std::chrono::hours
const& s) {
155 Manager(log4cplus::Logger
const& logger) : m_logger(logger) {
165 template <
class AttrType>
168 template <
class AttrType,
class Converter>
180 template <
class AttrType,
class T>
186 template <
class AttrType>
207 template <
class AttrType>
216 template <
class Func>
218 for (
auto const& entry : m_update) {
220 CiiValue{entry.second.as_cii(), entry.second.
origin, entry.second.metadata};
229 std::function<rad::cii::OldbType()> as_cii;
232 log4cplus::Logger m_logger;
234 std::unordered_map<void*, State> m_update;
237 template <
class AttrType>
242 if (
auto it = m_update.find(ptr); it != m_update.end()) {
243 throw std::invalid_argument(
"Attribute already registered");
246 std::make_pair(
static_cast<void*
>(ptr),
248 LOG4CPLUS_INFO(m_logger,
249 fmt::format(
"Registering configuration parameter <{}> with default value {}",
254 template <
class AttrType,
class Converter>
259 if (
auto it = m_update.find(ptr); it != m_update.end()) {
260 throw std::invalid_argument(
"Attribute already registered");
263 std::make_pair(
static_cast<void*
>(ptr),
264 State{m, {}, [ptr, c = std::move(converter)]() {
return c(*ptr); }}));
267 template <
class AttrType>
272 if (
auto it = m_update.find(ptr); it == m_update.end()) {
273 throw std::invalid_argument(
"Attribute not previously registered");
279 template <
class AttrType,
class T>
281 if (
auto it = m_update.find(ptr); it == m_update.end()) {
282 throw std::invalid_argument(
"Attribute not previously registered");
285 auto const& old_value = *ptr;
286 auto const& new_value = value;
287 auto const& meta = it->second.metadata;
288 auto const& old_origin = it->second.origin;
290 if (new_origin.
origin <= it->second.origin.origin) {
292 LOG4CPLUS_INFO(m_logger,
293 fmt::format(
"Updating configuration value for <{}> from "
294 "old value {} ({}) -> new value {} ({})",
300 it->second.origin = new_origin;
305 LOG4CPLUS_DEBUG(m_logger,
306 fmt::format(
"Will _not_ update configuration value for <{}> from "
307 "old value {} (origin {}) -> new value {} (origin {}) as "
308 "current origin has higher priority",
325 std::optional<std::reference_wrapper<elt::configng::CiiConfigInstanceNode const>>
326 GetParam(std::string
const& selector, elt::configng::CiiConfigInstanceNode
const& node);
337 GetParamAs(std::string
const& query, elt::configng::CiiConfigInstanceNode
const& node) {
338 if (
auto opt =
GetParam(query, node); opt.has_value()) {
339 return opt->get().As<T>();
Maintains the associativity of configuration attributes with metadata and value origin/priority.
Metadata const & metadata
OriginInfo const & origin
void Visit(Func &&func)
Visit each registered parameter.
bool Update(AttrType *ptr, T const &value, OriginInfo const &origin)
Update configuration value taking into account the origin of the change.
OriginInfo const & origin
Manager(log4cplus::Logger const &logger)
Constructor.
void Register(AttrType *ptr, Metadata const &meta)
Registers a configuration parameter/attribute.
CurrentValue< AttrType > Get(AttrType *ptr) const
Get current configuration value and associated metadata and origin.
Metadata const & metadata
CII representation of real value.
std::optional< std::reference_wrapper< elt::configng::CiiConfigInstanceNode const > > GetParam(std::string const &query, elt::configng::CiiConfigInstanceNode const &node)
Performs lookup of parameters in the form root/node/leaf relative to the provided node.
std::string canonical_name
std::string description
May include additional information like which configuration file was used.
Origin
Configuration origins in descending priority.
@ Configuration
Configuration file.
@ Default
Built-in default value.
@ Runtime
Runtime change via e.g.
@ CommandLine
Command line argument.
@ EnvironmentVariable
Environment variable.
std::ostream & operator<<(std::ostream &os, Origin origin)
Format Origin.
std::optional< T > GetParamAs(std::string const &query, elt::configng::CiiConfigInstanceNode const &node)
Performs lookup of parameters in the form root/node/leaf relative to the provided node.
Mutable metadata about a configuration attribute that describes where a value comes from.
State
Observable states of the data acquisition process.
static rad::cii::OldbType ToCii(std::chrono::hours const &s)
static rad::cii::OldbType ToCii(std::chrono::seconds const &s)
static rad::cii::OldbType ToCii(std::filesystem::path const &p)
static rad::cii::OldbType ToCii(T const &t)