10#ifndef DAQ_CONFIG_MANAGER_HPP
11#define DAQ_CONFIG_MANAGER_HPP
17#include <unordered_map>
19#include <boost/algorithm/string/join.hpp>
20#include <config-ng/ciiConfigApi.hpp>
21#include <fmt/chrono.h>
22#include <fmt/format.h>
23#include <fmt/ostream.h>
24#include <fmt/ranges.h>
26#include <log4cplus/logger.h>
27#include <log4cplus/loggingmacros.h>
28#include <rad/cii/oldbTypes.hpp>
90 static rad::cii::OldbType
ToCii(T
const& t) {
97 static rad::cii::OldbType
ToCii(std::filesystem::path
const& p) {
104 static rad::cii::OldbType
ToCii(std::chrono::seconds
const& s) {
111 static rad::cii::OldbType
ToCii(std::chrono::hours
const& s) {
121 static rad::cii::OldbType
ToCii(std::vector<std::string>
const& s) {
122 return boost::algorithm::join(s,
", ");
168 Manager(log4cplus::Logger
const& logger) : m_logger(logger) {
178 template <
class AttrType>
181 template <
class AttrType,
class Converter>
193 template <
class AttrType,
class T>
199 template <
class AttrType>
220 template <
class AttrType>
229 template <
class Func>
231 for (
auto const& entry : m_update) {
233 CiiValue{entry.second.as_cii(), entry.second.
origin, entry.second.metadata};
242 std::function<rad::cii::OldbType()> as_cii;
245 log4cplus::Logger m_logger;
247 std::unordered_map<void*, State> m_update;
250template <
class AttrType>
255 if (
auto it = m_update.find(ptr); it != m_update.end()) {
256 throw std::invalid_argument(
"Attribute already registered");
259 std::make_pair(
static_cast<void*
>(ptr),
261 LOG4CPLUS_INFO(m_logger,
262 fmt::format(
"Registering configuration parameter <{}> with default value {}",
267template <
class AttrType,
class Converter>
272 if (
auto it = m_update.find(ptr); it != m_update.end()) {
273 throw std::invalid_argument(
"Attribute already registered");
276 std::make_pair(
static_cast<void*
>(ptr),
277 State{m, {}, [ptr, c = std::move(converter)]() {
return c(*ptr); }}));
280template <
class AttrType>
285 if (
auto it = m_update.find(ptr); it == m_update.end()) {
286 throw std::invalid_argument(
"Attribute not previously registered");
292template <
class AttrType,
class T>
294 if (
auto it = m_update.find(ptr); it == m_update.end()) {
295 throw std::invalid_argument(
"Attribute not previously registered");
298 auto const& old_value = *ptr;
299 auto const& new_value = value;
300 auto const& meta = it->second.metadata;
301 auto const& old_origin = it->second.origin;
303 if (new_origin.
origin <= it->second.origin.origin) {
305 LOG4CPLUS_INFO(m_logger,
306 fmt::format(
"Updating configuration value for <{}> from "
307 "old value {} ({}) -> new value {} ({})",
313 it->second.origin = new_origin;
318 LOG4CPLUS_DEBUG(m_logger,
319 fmt::format(
"Will _not_ update configuration value for <{}> from "
320 "old value {} (origin {}) -> new value {} (origin {}) as "
321 "current origin has higher priority",
338std::optional<std::reference_wrapper<elt::configng::CiiConfigInstanceNode const>>
339GetParam(std::string
const& selector, elt::configng::CiiConfigInstanceNode
const& node);
350GetParamAs(std::string
const& query, elt::configng::CiiConfigInstanceNode
const& node) {
351 if (
auto opt =
GetParam(query, node); opt.has_value()) {
352 return opt->get().As<T>();
360struct fmt::formatter<
daq::config::OriginInfo> : ostream_formatter {};
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::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::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::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(std::vector< std::string > const &s)
static rad::cii::OldbType ToCii(T const &t)