The Gaudi Framework  v33r1 (b1225454)
CounterHolder.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifndef GAUDIKERNEL_COUNTERHOLDER_H
12 #define GAUDIKERNEL_COUNTERHOLDER_H
13 
14 #include <Gaudi/Accumulators.h>
15 #include <algorithm>
16 #include <functional>
17 #include <map>
18 #include <mutex>
19 #include <string>
20 #include <type_traits>
21 
22 class INamedInterface;
23 
24 template <class BASE>
25 class GAUDI_API CounterHolder : public BASE {
26  static_assert( std::is_base_of_v<INamedInterface, BASE>,
27  "CounterHolder template argument must inherit from INamedInterface" );
28 
29 public:
30  using BASE::BASE;
31 
32  void declareCounter( std::string tag, Gaudi::Accumulators::PrintableCounter& r ) {
33  auto lock = std::scoped_lock{m_mutex};
34  m_counters.emplace( std::move( tag ), r );
35  }
36 
37  const Gaudi::Accumulators::PrintableCounter* findCounter( std::string_view tag ) const {
38  auto lock = std::scoped_lock{m_mutex};
39  auto p = m_counters.find( tag );
40  return p != m_counters.end() ? &p->second.get() : nullptr;
41  }
42 
43  template <typename Callable>
44  void forEachCounter( Callable&& f ) const {
45  auto lock = std::scoped_lock{m_mutex};
46  std::for_each( m_counters.begin(), m_counters.end(),
47  [f = std::forward<Callable>( f )]( const auto& p ) { std::invoke( f, p.first, p.second.get() ); } );
48  }
49 
50  int nCounters() const {
51  auto lock = std::scoped_lock{m_mutex};
52  return m_counters.size();
53  }
54 
56  auto lock = std::scoped_lock{m_mutex};
57  return count_if( begin( m_counters ), end( m_counters ),
58  []( const auto& c ) { return c.second.get().toBePrinted(); } );
59  }
60 
61  void clearCounters() {
62  auto lock = std::scoped_lock{m_mutex};
63  m_counters.clear();
64  }
65 
66 private:
69 };
70 #endif
std::mutex m_mutex
Definition: CounterHolder.h:68
STL class.
STL class.
std::size_t nOfCountersToBePrinted()
Definition: CounterHolder.h:55
void forEachCounter(Callable &&f) const
Definition: CounterHolder.h:44
def end
Definition: IOTest.py:123
T move(T... args)
void clearCounters()
Definition: CounterHolder.h:61
IInterface compliant class extending IInterface with the name() method.
std::map< std::string, std::reference_wrapper< Gaudi::Accumulators::PrintableCounter >, std::less<> > m_counters
Definition: CounterHolder.h:67
void declareCounter(std::string tag, Gaudi::Accumulators::PrintableCounter &r)
Definition: CounterHolder.h:32
const Gaudi::Accumulators::PrintableCounter * findCounter(std::string_view tag) const
Definition: CounterHolder.h:37
AttribStringParser::Iterator begin(const AttribStringParser &parser)
T for_each(T... args)
#define GAUDI_API
Definition: Kernel.h:81
int nCounters() const
Definition: CounterHolder.h:50