#ifndef _utl_CountedObject_h_ #define _utl_CountedObject_h_ /** \file Object that counts instances \author Lukas Nellen \version $Id$ \date 13 Jan 2004 */ static const char CVSId_utl_CountedObject[] = "$Id$"; #include namespace utl { /** \class CountedObject CountedObject.h "utl/CountedObject.h" \brief Mix-in class for counting creation and destruction of objects This template adds two global, static variables per type that is counted to count the calls to the constructor and destructor. To instrument the code using this class, you inherit privatly from this class \code class Object : private CountedObject { }; \endcode \author Lukas Nellen \date 13 Jan 2004 \ingroup stl */ template class CountedObject { public: /// The type of the object we are counting typedef T CountedType; /// The (integer) type used to hold the counter typedef CountedObjectRegistry::Counter Counter; /// number of calls to the constructor static Counter GetObjectsCreated() { return gfObjectsCreated; } /// number of calls to the destructor static Counter GetObjectsDestroyed() { return gfObjectsDestroyed; } /// difference of number of calls to constructor and destructor static Counter GetObjectsExisting() { return gfObjectsCreated - gfObjectsDestroyed; } static Counter GetObjectsCopied() { return gfObjectsCopied; } static Counter GetObjectsAssigned() { return gfObjectsAssigned; } protected: CountedObject() { if (!gfObjectsCreated++) CountedObjectRegistry::RegisterCountedObject >(); } CountedObject(const CountedObject& /*co*/) { if (!gfObjectsCreated++) CountedObjectRegistry::RegisterCountedObject >(); ++gfObjectsCopied; } virtual ~CountedObject() { ++gfObjectsDestroyed; } CountedObject& operator=(const CountedObject& /*co*/) { ++gfObjectsAssigned; return *this; } private: static Counter gfObjectsCreated; static Counter gfObjectsDestroyed; static Counter gfObjectsCopied; static Counter gfObjectsAssigned; }; template typename CountedObject::Counter CountedObject::gfObjectsCreated = 0; template typename CountedObject::Counter CountedObject::gfObjectsDestroyed = 0; template typename CountedObject::Counter CountedObject::gfObjectsCopied = 0; template typename CountedObject::Counter CountedObject::gfObjectsAssigned = 0; } // utl #endif // _utl_CountedObject_h_ // Configure (x)emacs for this file ... // Local Variables: // mode: c++ // compile-command: "make -C .. -k" // End: