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