DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
InstanceCount.h
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : M.Frank
11 //
12 //==========================================================================
13 #ifndef DD4HEP_INSTANCECOUNT_H
14 #define DD4HEP_INSTANCECOUNT_H
15 
16 // Framework include files
17 #include <typeinfo>
18 #include <string>
19 
21 namespace dd4hep {
22 
24 
31  struct InstanceCount {
32  public:
33  typedef long long int counter_t;
35  enum {
36  NONE = 1 << 0,
37  STRING = 1 << 1,
38  TYPEINFO = 1 << 2,
39  ALL = STRING | TYPEINFO
40  };
41 
43 
50  class Counter {
51  private:
58  public:
60  Counter() = default;
62  Counter(const Counter& c) = default;
64  ~Counter() = default;
66  void increment() {
67  ++m_count;
68  ++m_tot;
69  m_max = std::max(m_max,m_count);
70  }
72  void decrement() {
73  --m_count;
74  }
76  counter_t value() const {
77  return m_count;
78  }
80  counter_t total() const {
81  return m_tot;
82  }
84  counter_t maximum() const {
85  return m_max;
86  }
87  };
88  public:
90  InstanceCount();
92  virtual ~InstanceCount();
94  static Counter* getCounter(const std::type_info& typ);
96  static Counter* getCounter(const std::string& typ);
98  template <class T> static void increment(T*) {
99  increment(typeid(T));
100  }
102  template <class T> static void decrement(T*) {
103  decrement(typeid(T));
104  }
106  template <class T> static counter_t get(T*) {
107  return getCounter(typeid(T))->value();
108  }
110  static void increment(const std::type_info& typ);
112  static void decrement(const std::type_info& typ);
114  static counter_t get(const std::type_info& typ) {
115  return getCounter(typ)->value();
116  }
118  static void increment(const std::string& typ);
120  static void decrement(const std::string& typ);
122  static counter_t get(const std::string& typ) {
123  return getCounter(typ)->value();
124  }
126  static void dump(int which = ALL);
128  static void clear(int which = ALL);
130  static bool doTrace();
132  static void doTracing(bool value);
133  };
134 
136 
143  template <typename T> struct Increment {
144  static int& counter() { static int cnt=0; return cnt; }
145  Increment() { ++counter(); }
146  ~Increment() { --counter(); }
147  };
148 
149 } /* End namespace dd4hep */
150 #endif // DD4HEP_INSTANCECOUNT_H
dd4hep::InstanceCount::Counter::m_max
counter_t m_max
Maximum number of simultaneous instances.
Definition: InstanceCount.h:57
dd4hep::InstanceCount
Helper to support object counting when debugging memory leaks.
Definition: InstanceCount.h:31
dd4hep::InstanceCount::NONE
@ NONE
Definition: InstanceCount.h:36
dd4hep::InstanceCount::Counter::m_tot
counter_t m_tot
Increment counter value.
Definition: InstanceCount.h:55
dd4hep::InstanceCount::Counter::m_count
counter_t m_count
Reference counter value.
Definition: InstanceCount.h:53
dd4hep::InstanceCount::Counter
Internal class to could object constructions and destructions.
Definition: InstanceCount.h:50
dd4hep::InstanceCount::dump
static void dump(int which=ALL)
Dump list of instance counters.
Definition: InstanceCount.cpp:149
dd4hep::InstanceCount::Counter::increment
void increment()
Increment counter.
Definition: InstanceCount.h:66
dd4hep::InstanceCount::InstanceCount
InstanceCount()
Standard Constructor - No need to call explicitly.
Definition: InstanceCount.cpp:64
dd4hep::InstanceCount::Counter::Counter
Counter()=default
Default constructor.
dd4hep::InstanceCount::Counter::maximum
counter_t maximum() const
Access maximum counter value.
Definition: InstanceCount.h:84
dd4hep::InstanceCount::increment
static void increment(T *)
Increment count according to type information.
Definition: InstanceCount.h:98
dd4hep::InstanceCount::Counter::Counter
Counter(const Counter &c)=default
Copy constructor.
dd4hep::InstanceCount::Counter::~Counter
~Counter()=default
Destructor.
dd4hep::Increment::counter
static int & counter()
Definition: InstanceCount.h:144
dd4hep::InstanceCount::doTracing
static void doTracing(bool value)
Enable/Disable tracing.
Definition: InstanceCount.cpp:90
dd4hep::Increment::Increment
Increment()
Definition: InstanceCount.h:145
dd4hep::InstanceCount::counter_t
long long int counter_t
Definition: InstanceCount.h:33
dd4hep::InstanceCount::get
static counter_t get(T *)
Access current counter.
Definition: InstanceCount.h:106
dd4hep::InstanceCount::STRING
@ STRING
Definition: InstanceCount.h:37
dd4hep::InstanceCount::decrement
static void decrement(T *)
Decrement count according to type information.
Definition: InstanceCount.h:102
dd4hep::InstanceCount::get
static counter_t get(const std::string &typ)
Access current counter.
Definition: InstanceCount.h:122
dd4hep::InstanceCount::doTrace
static bool doTrace()
Check if tracing is enabled.
Definition: InstanceCount.cpp:85
dd4hep::Increment::~Increment
~Increment()
Definition: InstanceCount.h:146
dd4hep::InstanceCount::clear
static void clear(int which=ALL)
Clear list of instance counters.
dd4hep::InstanceCount::TYPEINFO
@ TYPEINFO
Definition: InstanceCount.h:38
dd4hep::InstanceCount::get
static counter_t get(const std::type_info &typ)
Access current counter.
Definition: InstanceCount.h:114
dd4hep::Increment
Helper class to count call stack depths of certain functions.
Definition: InstanceCount.h:143
dd4hep::InstanceCount::Counter::value
counter_t value() const
Access counter value.
Definition: InstanceCount.h:76
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::InstanceCount::Counter::decrement
void decrement()
Decrement counter.
Definition: InstanceCount.h:72
dd4hep::InstanceCount::Counter::total
counter_t total() const
Access counter value.
Definition: InstanceCount.h:80
dd4hep::InstanceCount::getCounter
static Counter * getCounter(const std::type_info &typ)
Access counter object for local caching on optimizations.
Definition: InstanceCount.cpp:95
dd4hep::InstanceCount::~InstanceCount
virtual ~InstanceCount()
Standard Destructor - No need to call explicitly.
Definition: InstanceCount.cpp:69
dd4hep::InstanceCount::ALL
@ ALL
Definition: InstanceCount.h:39