DD4hep - The AIDA detector description toolkit for high energy physics experiments
DD4hep  Rev:Unversioneddirectory
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
InstanceCount.h
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation for LCD
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_GEOMETRY_INSTANCECOUNT_H
14 #define DD4HEP_GEOMETRY_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,
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_GEOMETRY_INSTANCECOUNT_H */
Counter()=default
Default constructor.
void decrement()
Decrement counter.
Definition: InstanceCount.h:72
static void decrement(T *)
Decrement count according to type information.
long long int counter_t
Definition: InstanceCount.h:33
~Counter()=default
Destructor.
counter_t m_tot
Increment counter value.
Definition: InstanceCount.h:55
static void dump(int which=ALL)
Dump list of instance counters.
Helper class to count call stack depths of certain functions.
static int & counter()
static bool doTrace()
Check if tracing is enabled.
counter_t total() const
Access counter value.
Definition: InstanceCount.h:80
counter_t m_max
Maximum number of simultaneous instances.
Definition: InstanceCount.h:57
static void increment(T *)
Increment count according to type information.
Definition: InstanceCount.h:98
counter_t maximum() const
Access maximum counter value.
Definition: InstanceCount.h:84
void increment()
Increment counter.
Definition: InstanceCount.h:66
InstanceCount()
Standard Constructor - No need to call explicitly.
virtual ~InstanceCount()
Standard Destructor - No need to call explicitly.
static void clear(int which=ALL)
Clear list of instance counters.
Helper to support object counting when debugging memory leaks.
Definition: InstanceCount.h:31
Internal class to could object constructions and destructions.
Definition: InstanceCount.h:50
counter_t value() const
Access counter value.
Definition: InstanceCount.h:76
static Counter * getCounter(const std::type_info &typ)
Access counter object for local caching on optimizations.
counter_t m_count
Reference counter value.
Definition: InstanceCount.h:53
static void doTracing(bool value)
Enable/Disable tracing.