ATLAS Offline Software
Loading...
Searching...
No Matches
MonitoredScalar.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
5#ifndef AthenaMonitoringKernel_MonitoredScalar_h
6#define AthenaMonitoringKernel_MonitoredScalar_h
7
8
9#include <vector>
10
12
13
14namespace Monitored {
15
34 template <class T> class Scalar : public IMonitoredVariable {
35 public:
36 static_assert(std::is_convertible<T, double>::value or std::is_constructible<std::string, T>::value, "Conversion of scalar template type to double or string is impossible");
37
40
50 Scalar(std::string name, const T& defaultValue = {}) :
52 m_value(defaultValue)
53 {}
54
65 Scalar(std::string name, const T& defaultValue, std::function<double(const T&)> valueTransform) :
67 m_value(defaultValue),
68 m_valueTransform(std::move(valueTransform))
69 {}
70
80 Scalar(std::string name, std::function<T()> generator) :
82 m_valueGenerator(std::move(generator))
83 {}
84
86
87 Scalar(Scalar&&) = default;
88 Scalar(Scalar const&) = delete;
89
90 Scalar& operator=(Scalar const&) = delete;
91 // cppcheck-suppress passedByValue
92 T operator=(T value)
93 {
94 m_value = value;
95 return value;
96 }
97
98 operator const T&() const { return m_value; }
99 operator T&() { return m_value; }
100
101 // string concatenation
102 friend std::string operator+(const Scalar& lhs, const std::string& rhs) {
103 return lhs.m_value + rhs;
104 }
105
106 friend std::string operator+(const std::string& lhs, const Scalar& rhs) {
107 return lhs + rhs.m_value;
108 }
109
110 virtual double get(size_t) const override {
111 if constexpr (std::is_convertible_v<double, T>) {
112 return (m_valueGenerator ? static_cast<double>(m_valueGenerator()) :
113 m_valueTransform ? m_valueTransform(m_value) : static_cast<double>(m_value));
114 }
115 else return 0;
116 }
117
118 virtual std::string getString(size_t) const override {
119 if constexpr (std::is_constructible_v<std::string, T>) {
121 }
122 else return {};
123 }
124
125 virtual bool hasStringRepresentation() const override {
126 return std::is_constructible<std::string, T>::value;
127 }
128
129 virtual size_t size() const override {
130 return 1;
131 }
132
133 private:
135 std::function<double(const T&)> m_valueTransform;
136 std::function<T()> m_valueGenerator;
137 };
138
139} // namespace Monitored
140
141#endif /* AthenaMonitoringKernel_MonitoredScalar_h */
const std::string & name() const
friend std::string operator+(const std::string &lhs, const Scalar &rhs)
Scalar(std::string name, std::function< T()> generator)
Scalar with generator function to retrieve the value.
virtual size_t size() const override
gives size of vector representation
Scalar(Scalar &&)=default
std::function< T()> m_valueGenerator
friend std::string operator+(const Scalar &lhs, const std::string &rhs)
std::function< double(const T &)> m_valueTransform
virtual std::string getString(size_t) const override
Scalar(std::string name, const T &defaultValue, std::function< double(const T &)> valueTransform)
Scalar with default value and optional transformation applied before filling.
virtual double get(size_t) const override
Scalar & operator=(Scalar const &)=delete
Scalar(std::string name, const T &defaultValue={})
Scalar with optional default value.
virtual bool hasStringRepresentation() const override
indcates that the stored content can be converted to strings
Scalar(Scalar const &)=delete
Generic monitoring tool for athena components.
STL namespace.