The Gaudi Framework  v32r2 (46d42edc)
CounterHolder.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_COUNTERHOLDER_H
2 #define GAUDIKERNEL_COUNTERHOLDER_H
3 
4 #include <algorithm>
5 #include <functional>
6 #include <map>
7 #include <mutex>
8 #include <string>
9 #include <type_traits>
10 
11 #include "GaudiKernel/Counters.h"
12 
13 class INamedInterface;
14 
15 template <class BASE>
16 class GAUDI_API CounterHolder : public BASE {
17  static_assert( std::is_base_of_v<INamedInterface, BASE>,
18  "CounterHolder template argument must inherit from INamedInterface" );
19 
20 public:
21  using BASE::BASE;
22 
24  std::lock_guard lock{m_mutex};
25  m_counters.emplace( tag, r );
26  }
27 
29  std::lock_guard lock{m_mutex};
30  auto p = m_counters.find( tag );
31  return p != m_counters.end() ? &p->second.get() : nullptr;
32  }
33 
34  template <typename Callable>
35  void forEachCounter( Callable&& f ) const {
36  std::lock_guard lock{m_mutex};
37  std::for_each( m_counters.begin(), m_counters.end(),
38  [f = std::forward<Callable>( f )]( const auto& p ) { std::invoke( f, p.first, p.second.get() ); } );
39  }
40 
41  int nCounters() const {
42  std::lock_guard lock{m_mutex};
43  return m_counters.size();
44  }
45 
47  std::lock_guard lock{m_mutex};
48  return count_if( begin( m_counters ), end( m_counters ),
49  []( const auto& c ) { return c.second.get().toBePrinted(); } );
50  }
51 
52  void clearCounters() {
53  std::lock_guard lock{m_mutex};
54  m_counters.clear();
55  }
56 
57 private:
60 };
61 #endif
std::mutex m_mutex
Definition: CounterHolder.h:59
const Gaudi::Accumulators::PrintableCounter * findCounter(const std::string &tag) const
Definition: CounterHolder.h:28
An empty ancester of all counters that knows how to print themselves.
Definition: Counters.h:601
STL class.
STL class.
void declareCounter(const std::string &tag, Gaudi::Accumulators::PrintableCounter &r)
Definition: CounterHolder.h:23
std::size_t nOfCountersToBePrinted()
Definition: CounterHolder.h:46
void forEachCounter(Callable &&f) const
Definition: CounterHolder.h:35
def end
Definition: IOTest.py:113
void clearCounters()
Definition: CounterHolder.h:52
IInterface compliant class extending IInterface with the name() method.
AttribStringParser::Iterator begin(const AttribStringParser &parser)
T for_each(T... args)
#define GAUDI_API
Definition: Kernel.h:71
std::map< std::string, std::reference_wrapper< Gaudi::Accumulators::PrintableCounter > > m_counters
Definition: CounterHolder.h:58
int nCounters() const
Definition: CounterHolder.h:41