1 #ifndef APE_SMARTPTR_H 2 #define APE_SMARTPTR_H 3 4 // disable the operator -> on UDT warning 5 #ifdef _MSC_VER 6 #pragma warning(push) 7 #pragma warning(disable : 4284) 8 #endif 9 10 #include "NoWindows.h" 11 12 /************************************************************************************************* 13 CSmartPtr - a simple smart pointer class that can automatically initialize and free memory 14 note: (doesn't do garbage collection / reference counting because of the many pitfalls) 15 *************************************************************************************************/ 16 template <class TYPE> class CSmartPtr 17 { 18 public: 19 TYPE * m_pObject; 20 BOOL m_bArray; 21 BOOL m_bDelete; 22 23 CSmartPtr() 24 { 25 m_bDelete = TRUE; 26 m_pObject = NULL; 27 } 28 CSmartPtr(TYPE * a_pObject, BOOL a_bArray = FALSE, BOOL a_bDelete = TRUE) 29 { 30 m_bDelete = TRUE; 31 m_pObject = NULL; 32 Assign(a_pObject, a_bArray, a_bDelete); 33 } 34 35 ~CSmartPtr() 36 { 37 Delete(); 38 } 39 40 void Assign(TYPE * a_pObject, BOOL a_bArray = FALSE, BOOL a_bDelete = TRUE) 41 { 42 Delete(); 43 44 m_bDelete = a_bDelete; 45 m_bArray = a_bArray; 46 m_pObject = a_pObject; 47 } 48 49 void Delete() 50 { 51 if (m_bDelete && m_pObject) 52 { 53 if (m_bArray) 54 delete [] m_pObject; 55 else 56 delete m_pObject; 57 58 m_pObject = NULL; 59 } 60 } 61 62 void SetDelete(const BOOL a_bDelete) 63 { 64 m_bDelete = a_bDelete; 65 } 66 67 __inline TYPE * GetPtr() const 68 { 69 return m_pObject; 70 } 71 72 __inline operator TYPE * () const 73 { 74 return m_pObject; 75 } 76 77 __inline TYPE * operator ->() const 78 { 79 return m_pObject; 80 } 81 82 // declare assignment, but don't implement (compiler error if we try to use) 83 // that way we can't carelessly mix smart pointers and regular pointers 84 __inline void * operator =(void *) const; 85 }; 86 87 #ifdef _MSC_VER 88 #pragma warning(pop) 89 #endif // _MSC_VER 90 91 #endif // #ifndef APE_SMARTPTR_H 92