- Cal3D 0.11 API Reference -

refcounted.h

00001 #ifndef CAL_REF_COUNTED_H
00002 #define CAL_REF_COUNTED_H
00003 
00004 
00005 #include "cal3d/platform.h"
00006 
00007 
00008 namespace cal3d
00009 {
00010     
00011   template<typename T> class RefPtr;
00012     
00028   class CAL3D_API RefCounted
00029   {
00030     template<typename T> friend T* explicitIncRef(T* p);
00031     friend void explicitDecRef(RefCounted* p);
00032 
00033   protected:
00034     RefCounted()
00035       : m_refCount(0)
00036     {
00037     }
00038 
00046     virtual ~RefCounted()
00047     {
00048       assert(m_refCount == 0 && "_refCount nonzero in destructor");
00049     }
00050 
00051   // Must use RefPtr instead of manually calling incRef() and decRef().
00052   private:
00053     void incRef()
00054     {
00055       assert(m_refCount >= 0 && "_refCount is less than zero in incRef()!");
00056       ++m_refCount;
00057     }
00058 
00063     void decRef()
00064     {
00065       assert(m_refCount > 0 &&
00066              "_refCount is less than or equal to zero in decRef()!");
00067       if (--m_refCount == 0)
00068       {
00069         delete this;
00070       }
00071     }
00072 
00073   public:        
00074     int getRefCount() const
00075     {
00076       return m_refCount;
00077     }
00078 
00079   private:
00080     // Copying a RefCounted object must be done manually by the
00081     // subclass.  Otherwise the refCount gets copied too, and
00082     // that's Bad.
00083     RefCounted(const RefCounted& rhs);
00084     RefCounted& operator=(const RefCounted& rhs);
00085         
00086   private:
00087     int m_refCount;
00088   };
00089 
00090   template<typename T>
00091   T* explicitIncRef(T* p)
00092   {
00093     p->incRef();
00094     return p;
00095   }
00096 
00097   inline void explicitDecRef(RefCounted* p)
00098   {
00099     p->decRef();
00100   }
00101 
00102 }
00103 
00104 
00105 #endif

Generated at Thu Jun 29 19:03:59 2006 by The Cal3D Team with Doxygen 1.4.6