Main Page | Modules | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

RefCounted.h

Go to the documentation of this file.
00001 #ifndef REFCOUNTED_H
00002 #define REFCOUNTED_H
00003 #ifdef _DEBUG
00004 #include <sstream>
00005 #include <iomanip>
00006 #endif
00007 
00008 
00009 template<typename T>
00010 class RefCounted
00011 {
00012 public:
00013         RefCounted(T* ptr=NULL):m_ptr(ptr),m_nRefs(new size_t(1))
00014         {
00015         }
00016         RefCounted(const RefCounted& other):m_nRefs(NULL)
00017         {
00018                 operator=(other);
00019         }
00020         ~RefCounted()
00021         {
00022                 release();
00023         }
00024         void operator=(const RefCounted& other)
00025         {
00026                 if(this!=&other)
00027                 {
00028                         release();
00029                         m_nRefs=other.m_nRefs;
00030                         ++(*m_nRefs);
00031                         m_ptr=other.m_ptr;
00032                 }
00033         }
00034         operator T*()
00035         {
00036                 return m_ptr;
00037         }
00038         operator const T*()const
00039         {
00040                 return m_ptr;
00041         }
00042         T* ptr()
00043         {
00044                 return m_ptr;
00045         }
00046         const T* ptr()const
00047         {
00048                 return m_ptr;
00049         }
00050         T* operator->()
00051         {
00052                 return m_ptr;
00053         }
00054         const T* operator->()const
00055         {
00056                 return m_ptr;
00057         }
00058 private:
00059         void release()
00060         {
00061                 if(m_nRefs && !(--(*m_nRefs)))
00062                 {
00063                         delete m_ptr;
00064                         delete m_nRefs;
00065                         m_nRefs=NULL;
00066                 }
00067         }
00068         T* m_ptr;
00069         mutable size_t* m_nRefs;
00070 };
00071 
00072 #endif//REFCOUNTED_H

Generated on Thu Jun 16 00:13:14 2005 for Netlib for Abuse! by  doxygen 1.4.3