00001 /* Copyright (C) 2004 Guido Simone 00002 * 00003 * This program is free software; you can redistribute it and/or modify 00004 * it under the terms of the GNU General Public License as published by 00005 * the Free Software Foundation; either version 2 of the License, or 00006 * (at your option) any later version. 00007 * 00008 * This program is distributed in the hope that it will be useful, 00009 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 * GNU General Public License for more details. 00012 00013 * You should have received a copy of the GNU General Public License 00014 * along with this program; if not, write to the Free Software 00015 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00016 */ 00017 00018 #ifndef GXLIB_GXAUTOPTR_H 00019 #define GXLIB_GXAUTOPTR_H 00020 00038 template<class T> 00039 class gxAutoPtr 00040 { 00041 public: 00046 gxAutoPtr( T * ptr = NULL ) 00047 { 00048 m_ptr = ptr; 00049 } 00050 00055 virtual ~gxAutoPtr() 00056 { 00057 clear(); 00058 } 00059 00063 T ** operator&() 00064 { 00065 return &m_ptr; 00066 } 00067 00072 T* operator->() { return m_ptr; } 00073 00078 bool operator==(gxAutoPtr<T> & rhs) 00079 { 00080 return m_ptr == rhs.m_ptr; 00081 } 00082 00087 bool operator==(T * rhs) 00088 { 00089 return m_ptr == rhs; 00090 } 00091 00097 gxAutoPtr & operator=( T * rhs ) 00098 { 00099 assign(rhs); 00100 return *this; 00101 } 00102 00108 operator T *() 00109 { 00110 return m_ptr; 00111 } 00112 void clear() 00113 { 00114 if ( m_ptr != NULL ) 00115 { 00116 delete m_ptr; 00117 m_ptr = NULL; 00118 } 00119 } 00120 00126 void assign( T * rhs ) 00127 { 00128 clear(); 00129 m_ptr = rhs; 00130 } 00131 00136 T* detach( ) 00137 { 00138 T * result = m_ptr; 00139 m_ptr = NULL; 00140 return result; 00141 } 00142 00143 T * m_ptr; 00144 00145 private: 00146 // do not implement 00147 gxAutoPtr( const gxAutoPtr & rhs); 00148 gxAutoPtr & operator=( const gxAutoPtr & rhs); 00149 }; 00150 00151 00152 #endif