Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-21 09:58:02

0001 //==========================================================================
0002 //  AIDA Detector description implementation 
0003 //--------------------------------------------------------------------------
0004 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
0005 // All rights reserved.
0006 //
0007 // For the licensing terms see $DD4hepINSTALL/LICENSE.
0008 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
0009 //
0010 // Author     : M.Frank
0011 //
0012 //==========================================================================
0013 #ifndef DD4HEP_INSTANCECOUNT_H
0014 #define DD4HEP_INSTANCECOUNT_H
0015 
0016 // Framework include files
0017 #include <typeinfo>
0018 #include <string>
0019 
0020 /// Namespace for the AIDA detector description toolkit
0021 namespace dd4hep {
0022 
0023   /// Helper to support object counting when debugging memory leaks
0024   /**
0025    * Small class to enable object construction/destruction tracing
0026    *
0027    *  \author  M.Frank
0028    *  \version 1.0
0029    *  \ingroup DD4HEP
0030    */
0031   struct InstanceCount {
0032   public:
0033     typedef long long int counter_t;
0034     /// Enumeration to steer the output
0035     enum {
0036       NONE     = 1 << 0,
0037       STRING   = 1 << 1,
0038       TYPEINFO = 1 << 2,
0039       ALL      = STRING | TYPEINFO
0040     };
0041 
0042     /// Internal class to could object constructions and destructions
0043     /**
0044      * Small class to enable object construction/destruction tracing.
0045      *
0046      *  \author  M.Frank
0047      *  \version 1.0
0048      *  \ingroup DD4HEP
0049      */
0050     class Counter {
0051     private:
0052       /// Reference counter value
0053       counter_t m_count = 0;
0054       /// Increment counter value
0055       counter_t m_tot = 0;
0056       /// Maximum number of simultaneous instances
0057       counter_t m_max = 0;
0058     public:
0059       /// Default constructor
0060       Counter() = default;
0061       /// Copy constructor
0062       Counter(const Counter& c) = default;
0063       /// Destructor
0064       ~Counter() = default;
0065       /// Increment counter
0066       void increment() {
0067         ++m_count;
0068         ++m_tot;
0069         m_max = std::max(m_max,m_count);
0070       }
0071       /// Decrement counter
0072       void decrement() {
0073         --m_count;
0074       }
0075       /// Access counter value
0076       counter_t value() const {
0077         return m_count;
0078       }
0079       /// Access counter value
0080       counter_t total() const {
0081         return m_tot;
0082       }
0083       /// Access maximum counter value
0084       counter_t maximum() const {
0085         return m_max;
0086       }
0087     };
0088   public:
0089     /// Standard Constructor - No need to call explicitly
0090     InstanceCount();
0091     /// Standard Destructor - No need to call explicitly
0092     virtual ~InstanceCount();
0093     /// Access counter object for local caching on optimizations
0094     static Counter* getCounter(const std::type_info& typ);
0095     /// Access counter object for local caching on optimizations
0096     static Counter* getCounter(const std::string& typ);
0097     /// Increment count according to type information
0098     template <class T> static void increment(T*) {
0099       increment(typeid(T));
0100     }
0101     /// Decrement count according to type information
0102     template <class T> static void decrement(T*) {
0103       decrement(typeid(T));
0104     }
0105     /// Access current counter
0106     template <class T> static counter_t get(T*) {
0107       return getCounter(typeid(T))->value();
0108     }
0109     /// Increment count according to type information
0110     static void increment(const std::type_info& typ);
0111     /// Decrement count according to type information
0112     static void decrement(const std::type_info& typ);
0113     /// Access current counter
0114     static counter_t get(const std::type_info& typ) {
0115       return getCounter(typ)->value();
0116     }
0117     /// Increment count according to string information
0118     static void increment(const std::string& typ);
0119     /// Decrement count according to string information
0120     static void decrement(const std::string& typ);
0121     /// Access current counter
0122     static counter_t get(const std::string& typ) {
0123       return getCounter(typ)->value();
0124     }
0125     /// Dump list of instance counters
0126     static void dump(int which = ALL);
0127     /// Clear list of instance counters
0128     static void clear(int which = ALL);
0129     /// Check if tracing is enabled.
0130     static bool doTrace();
0131     /// Enable/Disable tracing
0132     static void doTracing(bool value);
0133   };
0134 
0135   /// Helper class to count call stack depths of certain functions
0136   /**
0137    * Small class to count re-entrancy calls
0138    *
0139    *  \author  M.Frank
0140    *  \version 1.0
0141    *  \ingroup DD4HEP
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 } /* End namespace dd4hep             */
0150 #endif // DD4HEP_INSTANCECOUNT_H