File indexing completed on 2025-02-21 09:58:02
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef DD4HEP_INSTANCECOUNT_H
0014 #define DD4HEP_INSTANCECOUNT_H
0015
0016
0017 #include <typeinfo>
0018 #include <string>
0019
0020
0021 namespace dd4hep {
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031 struct InstanceCount {
0032 public:
0033 typedef long long int counter_t;
0034
0035 enum {
0036 NONE = 1 << 0,
0037 STRING = 1 << 1,
0038 TYPEINFO = 1 << 2,
0039 ALL = STRING | TYPEINFO
0040 };
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050 class Counter {
0051 private:
0052
0053 counter_t m_count = 0;
0054
0055 counter_t m_tot = 0;
0056
0057 counter_t m_max = 0;
0058 public:
0059
0060 Counter() = default;
0061
0062 Counter(const Counter& c) = default;
0063
0064 ~Counter() = default;
0065
0066 void increment() {
0067 ++m_count;
0068 ++m_tot;
0069 m_max = std::max(m_max,m_count);
0070 }
0071
0072 void decrement() {
0073 --m_count;
0074 }
0075
0076 counter_t value() const {
0077 return m_count;
0078 }
0079
0080 counter_t total() const {
0081 return m_tot;
0082 }
0083
0084 counter_t maximum() const {
0085 return m_max;
0086 }
0087 };
0088 public:
0089
0090 InstanceCount();
0091
0092 virtual ~InstanceCount();
0093
0094 static Counter* getCounter(const std::type_info& typ);
0095
0096 static Counter* getCounter(const std::string& typ);
0097
0098 template <class T> static void increment(T*) {
0099 increment(typeid(T));
0100 }
0101
0102 template <class T> static void decrement(T*) {
0103 decrement(typeid(T));
0104 }
0105
0106 template <class T> static counter_t get(T*) {
0107 return getCounter(typeid(T))->value();
0108 }
0109
0110 static void increment(const std::type_info& typ);
0111
0112 static void decrement(const std::type_info& typ);
0113
0114 static counter_t get(const std::type_info& typ) {
0115 return getCounter(typ)->value();
0116 }
0117
0118 static void increment(const std::string& typ);
0119
0120 static void decrement(const std::string& typ);
0121
0122 static counter_t get(const std::string& typ) {
0123 return getCounter(typ)->value();
0124 }
0125
0126 static void dump(int which = ALL);
0127
0128 static void clear(int which = ALL);
0129
0130 static bool doTrace();
0131
0132 static void doTracing(bool value);
0133 };
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143 template <typename T> struct Increment {
0144 static int& counter() { static int cnt=0; return cnt; }
0145 Increment() { ++counter(); }
0146 ~Increment() { --counter(); }
0147 };
0148
0149 }
0150 #endif