00001 /****************************************************************************\ 00002 * * 00003 * 22.05.2005 * 00004 * TSmartPointer - Header File * 00005 * * 00006 * (c) Raphael Charwot * 00007 * Thx to Photon * 00008 * * 00009 \****************************************************************************/ 00010 00011 #ifndef __TSMARTPOINTER_H__ 00012 #define __TSMARTPOINTER_H__ 00013 00014 /****************************************************************************\ 00015 * REQUIRED HEADERS * 00016 \****************************************************************************/ 00017 #include <Definitions.h> 00018 00019 #include <Memory/mmgr.h> 00020 00021 /****************************************************************************\ 00022 * DEFINITIONS * 00023 \****************************************************************************/ 00024 00025 /****************************************************************************\ 00026 * CLASS * 00027 \****************************************************************************/ 00028 template<class T> 00029 class TSmartPointer 00030 { 00031 /**************************************************************************\ 00032 * PUBLIC CLASS FUNCTIONS * 00033 \**************************************************************************/ 00034 public: 00035 TSmartPointer() : m_pObject(NULL) {}; 00036 TSmartPointer(T *pObject) : m_pObject(pObject) {AddRef();}; 00037 TSmartPointer(const TSmartPointer &other) : m_pObject(other.m_pObject) {AddRef();} 00038 00039 virtual ~TSmartPointer() {Release();} 00040 00041 TSmartPointer &operator =(const TSmartPointer &other) 00042 { 00043 return *this = other.m_pObject; 00044 } 00045 00046 TSmartPointer &operator =(T *pObject) 00047 { 00048 if(m_pObject != pObject) 00049 { 00050 Release(); 00051 m_pObject = pObject; 00052 AddRef(); 00053 } 00054 return *this; 00055 } 00056 00057 T &operator *() const {assert(m_pObject && "Error: NULL SmartPointer dereferenced! (*)"); return *m_pObject;} 00058 T *operator ->() const {assert(m_pObject && "Error: NULL SmartPointer dereferenced! (->)"); return m_pObject;} 00059 00060 operator T*() const {return m_pObject;} 00061 operator const T*() const {return m_pObject;} 00062 00063 operator bool () const {return m_pObject != 0;} 00064 00065 bool operator !() const {return !(m_pObject);} 00066 bool operator ==(const TSmartPointer &other) const {return m_pObject == other.m_pObject;}; 00067 bool operator ==(const T &other) const {return m_pObject == pObject;}; 00068 bool operator !=(const TSmartPointer &other) const {return m_pObject != other.m_pObject;}; 00069 bool operator !=(const T &other) const {return m_pObject != pObject;}; 00070 00071 /**************************************************************************\ 00072 * PRIVATE CLASS FUNCTIONS * 00073 \**************************************************************************/ 00074 private: 00075 void AddRef() 00076 { 00077 if(m_pObject) 00078 { 00079 m_pObject->AddRef(); 00080 } 00081 } 00082 00083 void Release() 00084 { 00085 if(m_pObject) 00086 { 00087 if(m_pObject->Release()) 00088 { 00089 delete m_pObject; 00090 m_pObject = NULL; 00091 } 00092 } 00093 } 00094 /**************************************************************************\ 00095 * PRIVATE CLASS MEMBERS * 00096 \**************************************************************************/ 00097 private: 00098 T *m_pObject; 00099 }; 00100 00101 #endif