ATLAS Offline Software
Loading...
Searching...
No Matches
MonitoredCollection.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_MonitoredCollection_h
6#define AthenaMonitoringKernel_MonitoredCollection_h
7
8#include <functional>
9#include <type_traits>
10#include <vector>
11
13
14namespace Monitored {
15
16 // Forward declares
17 template <class T> class ValuesCollection;
18 template <class T> class ValuesRefCollection;
19 template <class T, class R> class ObjectsCollection;
20 template <class T, class R> class ObjectsRefCollection;
21
38 template <class T> ValuesCollection<T> Collection(std::string name, const T& collection) {
39 return ValuesCollection<T>(std::move(name), collection);
40 }
41
42 // Disallow temporaries
43 template <class T> ValuesCollection<T> Collection(std::string name, T const&& collection) = delete;
44
61 template <class T> ValuesRefCollection<T> Collection(std::string name, const std::reference_wrapper<T>& collection) {
62 return ValuesRefCollection<T>(std::move(name), collection);
63 }
64
65 // Disallow temporaries
66 template <class T> ValuesRefCollection<T> Collection(std::string name, const std::reference_wrapper<T>&& collection) = delete;
67
68
89 template <class T>
91 Collection(std::string name, const T& collection,
92 std::function<double(const typename ObjectsCollection<T, double>::const_value_type&)> accessor) {
93 return ObjectsCollection<T, double>(std::move(name), collection, std::move(accessor));
94 }
95
96 template <class T>
97 ObjectsCollection<T, double>
98 Collection(std::string name, const T&& collection,
99 std::function<double(const typename ObjectsCollection<T, double>::const_value_type&)> accessor) = delete;
100
101
102 template <class T>
104 Collection(std::string name, const T& collection,
105 std::function<std::string(const typename ObjectsCollection<T, std::string>::const_value_type&)> accessor) {
106 return ObjectsCollection<T, std::string>(std::move(name), collection, std::move(accessor));
107 }
108
109 template <class T>
110 ObjectsCollection<T, std::string>
111 Collection(std::string name, const T&& collection,
112 std::function<std::string(const typename ObjectsCollection<T, std::string>::const_value_type&)> accessor) = delete;
113
114
135 template <class T>
137 Collection(std::string name, const std::reference_wrapper<T>& collection,
138 std::function<double(const typename ObjectsRefCollection<T, double>::const_value_type&)> accessor) {
139 return ObjectsRefCollection<T, double>(std::move(name), collection, std::move(accessor));
140 }
141
142 template <class T>
143 ObjectsRefCollection<T, double>
144 Collection(std::string name, const std::reference_wrapper<T>&& collection,
145 std::function<double(const typename ObjectsRefCollection<T, double>::const_value_type&)> accessor) = delete;
146
147 template <class T>
149 Collection(std::string name, const std::reference_wrapper<T>& collection,
150 std::function<std::string(const typename ObjectsRefCollection<T, std::string>::const_value_type&)> accessor) {
151 return ObjectsRefCollection<T, std::string>(std::move(name), collection, std::move(accessor));
152 }
153
154 template <class T>
155 ObjectsRefCollection<T, std::string>
156 Collection(std::string name, const std::reference_wrapper<T>&& collection,
157 std::function<std::string(const typename ObjectsRefCollection<T, std::string>::const_value_type&)> accessor) = delete;
158
159
160 namespace detail {
162 template <typename T> struct get_value_type { typedef typename T::value_type value_type; };
164 template <typename T, size_t N> struct get_value_type<T[N]> { typedef T value_type; };
165
167 template <typename T> struct get_value_type<std::reference_wrapper<T>> { typedef typename T::value_type value_type; };
168 template <typename T, size_t N> struct get_value_type<std::reference_wrapper<T[N]>> { typedef T value_type; };
169
170 template <typename T> struct make_pointer_const { typedef T type; };
171 template <typename T> struct make_pointer_const<T*> { typedef const T* type; };
172 } // namespace detail
173
174
178 template <class T> class ValuesCollection : public IMonitoredVariable {
179 public:
182
183 static_assert(std::is_convertible<value_type, double>::value or std::is_constructible<std::string, value_type>::value, "Conversion of collection values to double or string is impossible");
184
185 // @brief . \if empty doc string required due to https://github.com/doxygen/doxygen/issues/6251 \endif
186 friend ValuesCollection<T> Collection<T>(std::string name, const T& collection);
187 friend ValuesCollection<T> Collection<T>(std::string name, const T&& collection);
188
190
191 virtual double get([[maybe_unused]] size_t i) const override {
192 if constexpr (std::is_convertible_v<value_type, double>)
193 return m_collection[i];
194 else
195 return -1;
196 }
197
198 virtual std::string getString([[maybe_unused]] size_t i) const override {
199 if constexpr (std::is_constructible_v<std::string, value_type>)
200 return m_collection[i];
201 else
202 return {};
203 }
204
205 virtual bool hasStringRepresentation() const override {
206 return std::is_constructible<std::string, value_type>::value;
207 }
208
209 virtual size_t size() const override {
210 return std::size(m_collection);
211 }
212
213 private:
214 const T& m_collection;
215
216
217 ValuesCollection(std::string vname, const T& collection)
218 : IMonitoredVariable(std::move(vname)), m_collection{collection} {
219 }
220
223 };
224
225
229 template <class T> class ValuesRefCollection : public IMonitoredVariable {
230 public:
233
234 static_assert(std::is_convertible<value_type, double>::value or std::is_constructible<std::string, value_type>::value, "Conversion of collection values to double or string is impossible");
235
236 // @brief . \if empty doc string required due to https://github.com/doxygen/doxygen/issues/6251 \endif
237 friend ValuesRefCollection<T> Collection<T>(std::string name, const std::reference_wrapper<T>& collection);
238 friend ValuesRefCollection<T> Collection<T>(std::string name, const std::reference_wrapper<T>&& collection);
239
241
242 virtual double get([[maybe_unused]] size_t i) const override {
243 if constexpr (std::is_convertible_v<value_type, double>)
244 return m_collection.get()[i];
245 else
246 return -1;
247 }
248
249 virtual std::string getString([[maybe_unused]] size_t i) const override {
250 if constexpr (std::is_constructible_v<std::string, value_type>)
251 return m_collection.get()[i];
252 else
253 return {};
254 }
255
256 virtual bool hasStringRepresentation() const override {
257 return std::is_constructible<std::string, value_type>::value;
258 }
259
260 virtual size_t size() const override {
261 return std::size(m_collection.get());
262 }
263
264 private:
265 const std::reference_wrapper<T>& m_collection;
266
267
268 ValuesRefCollection(std::string vname, const std::reference_wrapper<T>& collection)
269 : IMonitoredVariable(std::move(vname)), m_collection{collection} {
270 }
271
274 };
275
276
283 template <class T, class R> class ObjectsCollection : public IMonitoredVariable {
284 public:
288
289 static_assert(std::is_convertible<R, double>::value or std::is_constructible<std::string, R>::value, "Conversion from type returned by the converter/accessor to double or string is impossible");
290
291 // @brief . \if empty doc string required due to https://github.com/doxygen/doxygen/issues/6251 \endif
292 template <class U> friend ObjectsCollection<U, double>
293 Collection(std::string name, const U& collection,
294 std::function<double(const typename ObjectsCollection<U, double>::const_value_type&)> accessor);
295
296 template <class U> friend ObjectsCollection<U, std::string>
297 Collection(std::string name, const U& collection,
298 std::function<std::string(const typename ObjectsCollection<U, std::string>::const_value_type&)> converterToString);
299
301
302 virtual double get([[maybe_unused]] size_t i) const override {
303 if constexpr (std::is_convertible_v<R, double>)
304 return m_converterToR(m_collection[i]);
305 else
306 return -1;
307 }
308
309 virtual std::string getString([[maybe_unused]] size_t i) const override {
310 if constexpr (std::is_constructible_v<std::string, R>)
311 return m_converterToR(m_collection[i]);
312 else
313 return {};
314 }
315
316 virtual bool hasStringRepresentation() const override {
317 return std::is_constructible<std::string, R>::value;
318 };
319 virtual size_t size() const override {
320 return std::size(m_collection);
321 }
322
323 private:
324 const T& m_collection;
325 std::function<R(const const_value_type&)> m_converterToR;
326
327 ObjectsCollection(std::string name, const T& collection,
328 std::function<R(const const_value_type&)> converterToR)
329 : IMonitoredVariable(std::move(name)),
330 m_collection(collection),
331 m_converterToR(std::move(converterToR)) {}
332
334
336
337 };
338
339
346 template <class T, class R> class ObjectsRefCollection : public IMonitoredVariable {
347 public:
351
352 static_assert(std::is_convertible<R, double>::value or std::is_constructible<std::string, R>::value, "Conversion from type returned by the converter/accessor to double or string is impossible");
353
354 // @brief . \if empty doc string required due to https://github.com/doxygen/doxygen/issues/6251 \endif
355 template <class U> friend ObjectsRefCollection<U, double>
356 Collection(std::string name, const std::reference_wrapper<U>& collection,
357 std::function<double(const typename ObjectsRefCollection<U, double>::const_value_type&)> accessor);
358
359 template <class U> friend ObjectsRefCollection<U, std::string>
360 Collection(std::string name, const std::reference_wrapper<U>& collection,
361 std::function<std::string(const typename ObjectsRefCollection<U, std::string>::const_value_type&)> converterToString);
362
364
365 virtual double get([[maybe_unused]] size_t i) const override {
366 if constexpr (std::is_convertible_v<R, double>)
367 return m_converterToR(m_collection.get()[i]);
368 else
369 return -1;
370 }
371
372 virtual std::string getString([[maybe_unused]] size_t i) const override {
373 if constexpr (std::is_constructible_v<std::string, R>)
374 return m_converterToR(m_collection.get()[i]);
375 else
376 return {};
377 }
378
379 virtual bool hasStringRepresentation() const override {
380 return std::is_constructible<std::string, R>::value;
381 };
382 virtual size_t size() const override {
383 return std::size(m_collection.get());
384 }
385
386 private:
387 const std::reference_wrapper<T>& m_collection;
388 std::function<R(const const_value_type&)> m_converterToR;
389
390 ObjectsRefCollection(std::string name, const std::reference_wrapper<T>& collection,
391 std::function<R(const const_value_type&)> converterToR)
392 : IMonitoredVariable(std::move(name)),
393 m_collection(collection),
394 m_converterToR(std::move(converterToR)) {}
395
397
399
400 };
401
402} // namespace Monitored
403
404#endif /* AthenaMonitoringKernel_MonitoredCollection_h */
const std::string & name() const
Monitoring of object collections (internal)
typename detail::make_pointer_const< value_type >::type const_value_type
virtual bool hasStringRepresentation() const override
indcates that the stored content can be converted to strings
ObjectsCollection(std::string name, const T &collection, std::function< R(const const_value_type &)> converterToR)
virtual double get(size_t i) const override
friend ObjectsCollection< U, double > Collection(std::string name, const U &collection, std::function< double(const typename ObjectsCollection< U, double >::const_value_type &)> accessor)
friend ObjectsCollection< U, std::string > Collection(std::string name, const U &collection, std::function< std::string(const typename ObjectsCollection< U, std::string >::const_value_type &)> converterToString)
virtual std::string getString(size_t i) const override
ObjectsCollection(ObjectsCollection &&)=default
ObjectsCollection & operator=(ObjectsCollection const &)=delete
ObjectsCollection(ObjectsCollection const &)=delete
virtual size_t size() const override
gives size of vector representation
typename detail::get_value_type< T >::value_type value_type
Type of the collection elements.
std::function< R(const const_value_type &)> m_converterToR
Monitoring of object collection references (internal)
ObjectsRefCollection & operator=(ObjectsRefCollection const &)=delete
virtual std::string getString(size_t i) const override
std::function< R(const const_value_type &)> m_converterToR
virtual bool hasStringRepresentation() const override
indcates that the stored content can be converted to strings
const std::reference_wrapper< T > & m_collection
virtual size_t size() const override
gives size of vector representation
ObjectsRefCollection(ObjectsRefCollection &&)=default
ObjectsRefCollection(ObjectsRefCollection const &)=delete
friend ObjectsRefCollection< U, std::string > Collection(std::string name, const std::reference_wrapper< U > &collection, std::function< std::string(const typename ObjectsRefCollection< U, std::string >::const_value_type &)> converterToString)
friend ObjectsRefCollection< U, double > Collection(std::string name, const std::reference_wrapper< U > &collection, std::function< double(const typename ObjectsRefCollection< U, double >::const_value_type &)> accessor)
typename detail::get_value_type< T >::value_type value_type
Type of the collection elements.
typename detail::make_pointer_const< value_type >::type const_value_type
virtual double get(size_t i) const override
ObjectsRefCollection(std::string name, const std::reference_wrapper< T > &collection, std::function< R(const const_value_type &)> converterToR)
Internal class not to be used by end user.
virtual double get(size_t i) const override
ValuesCollection & operator=(ValuesCollection const &)=delete
virtual std::string getString(size_t i) const override
typename detail::get_value_type< T >::value_type value_type
Type of the collection elements.
ValuesCollection(std::string vname, const T &collection)
virtual bool hasStringRepresentation() const override
indcates that the stored content can be converted to strings
ValuesCollection(ValuesCollection const &)=delete
virtual size_t size() const override
gives size of vector representation
friend ValuesCollection< T > Collection(std::string name, const T &collection)
Declare a monitored (double-convertible) collection.
ValuesCollection(ValuesCollection &&)=default
Internal class not to be used by end user.
ValuesRefCollection(ValuesRefCollection const &)=delete
ValuesRefCollection(ValuesRefCollection &&)=default
friend ValuesRefCollection< T > Collection(std::string name, const std::reference_wrapper< T > &collection)
Declare a monitored (double-convertible) collection reference.
const std::reference_wrapper< T > & m_collection
virtual std::string getString(size_t i) const override
virtual bool hasStringRepresentation() const override
indcates that the stored content can be converted to strings
typename detail::get_value_type< T >::value_type value_type
Type of the collection elements.
ValuesRefCollection & operator=(ValuesRefCollection const &)=delete
virtual double get(size_t i) const override
virtual size_t size() const override
gives size of vector representation
ValuesRefCollection(std::string vname, const std::reference_wrapper< T > &collection)
Generic monitoring tool for athena components.
ValuesCollection< T > Collection(std::string name, const T &collection)
Declare a monitored (double-convertible) collection.
STL namespace.
Get element type for containers.