00001 // -*- c++ -*- 00002 //------------------------------------------------------------------------------ 00003 // AutoPtr.h 00004 //------------------------------------------------------------------------------ 00005 // Copyright (C) 1999,2005 Vladislav Grinchenko 00006 // 00007 // This library is free software; you can redistribute it and/or 00008 // modify it under the terms of the GNU Library General Public 00009 // License as published by the Free Software Foundation; either 00010 // version 2 of the License, or (at your option) any later version. 00011 //------------------------------------------------------------------------------ 00012 #ifndef AUTO_PTR_H 00013 #define AUTO_PTR_H 00014 00015 #include <cstdlib> // NULL definition 00016 00023 namespace ASSA { 00024 00032 template<typename R> class AutoPtrRef { 00033 public: 00034 explicit AutoPtrRef (R* p_) : m_ptr (p_) { /* no-op */ } 00035 00036 R* m_ptr; 00037 }; 00038 00049 template<class X> class AutoPtr { 00050 private: 00052 X* m_ptr; 00053 00054 public: 00061 explicit AutoPtr (X* p_ = 0) : m_ptr (p_) { /* no-op */ } 00062 00067 AutoPtr (AutoPtr& a_) : m_ptr (a_.release ()) {/* no-op */ } 00068 00075 template<typename T> 00076 AutoPtr (AutoPtr<T>& a_) : m_ptr (a_.release ()) { /* no-op */ } 00077 00083 AutoPtr& operator= (AutoPtr& a_) { 00084 reset (a_.release ()); 00085 return *this; 00086 } 00087 00093 template<class T> AutoPtr& operator=(AutoPtr<T>& a_) { 00094 reset (a_.release ()); 00095 return *this; 00096 } 00097 00102 ~AutoPtr () { 00103 if (m_ptr) { 00104 delete m_ptr; 00105 } 00106 } 00107 00111 X& operator*() const { return *m_ptr; } 00112 00116 X* operator->() const { return m_ptr; } 00117 00123 X* get () const { return m_ptr; } 00124 00130 X* release () { 00131 X* tmp = m_ptr; 00132 m_ptr = NULL; 00133 return tmp; 00134 } 00135 00140 void reset (X* p_ = 0) { 00141 if (p_ != m_ptr) { 00142 delete m_ptr; 00143 m_ptr = p_; 00144 } 00145 } 00146 00159 AutoPtr (AutoPtrRef<X> ref_) : m_ptr (ref_.m_ptr) { /* no-op */ } 00160 00161 AutoPtr& operator=(AutoPtrRef<X> ref_) { 00162 if (ref_.m_ptr != get ()) { 00163 delete m_ptr; 00164 m_ptr = ref_.m_ptr; 00165 } 00166 return *this; 00167 } 00168 00169 template<typename T> 00170 operator AutoPtrRef<T> () { return AutoPtrRef<T> (release ()); } 00171 00172 template<typename T> 00173 operator AutoPtr<T> () { return AutoPtr<T> (release ()); } 00174 00176 }; 00177 00185 template<typename R> class AutoPtrArrayRef { 00186 public: 00187 explicit AutoPtrArrayRef (R* p_) : m_ptr (p_) { /* no-op */ } 00188 00189 R* m_ptr; 00190 }; 00191 00198 template<class X> class AutoPtrArray { 00199 private: 00201 X* m_ptr; 00202 00203 public: 00210 explicit AutoPtrArray (X* p_ = 0) : m_ptr (p_) { /* no-op */ } 00211 00216 AutoPtrArray (AutoPtrArray& a_) : m_ptr (a_.release ()) {/* no-op */ } 00217 00225 template<typename T> 00226 AutoPtrArray (AutoPtrArray<T>& a_) 00227 : m_ptr (a_.release ()) { /* no-op */ } 00228 00235 AutoPtrArray& operator= (AutoPtrArray& a_) { 00236 reset (a_.release ()); 00237 return *this; 00238 } 00239 00245 template<class T> 00246 AutoPtrArray& operator=(AutoPtrArray<T>& a_) { 00247 reset (a_.release ()); 00248 return *this; 00249 } 00250 00255 ~AutoPtrArray () { 00256 if (m_ptr) { 00257 delete [] m_ptr; 00258 } 00259 } 00260 00264 X& operator*() const { return *m_ptr; } 00265 00269 X* operator->() const { return m_ptr; } 00270 00274 X& operator[] (int i) const { 00275 X* array = get (); 00276 return (array [i]); 00277 } 00278 00284 X* get () const { return m_ptr; } 00285 00291 X* release () { 00292 X* tmp = m_ptr; 00293 m_ptr = NULL; 00294 return tmp; 00295 } 00296 00301 void reset (X* p_ = 0) { 00302 if (p_ != m_ptr) { 00303 delete [] m_ptr; 00304 m_ptr = p_; 00305 } 00306 } 00307 00320 AutoPtrArray (AutoPtrArrayRef<X> ref_) : m_ptr (ref_.m_ptr) { /* no-op */ } 00321 00322 AutoPtrArray& operator=(AutoPtrArrayRef<X> ref_) { 00323 if (ref_.m_ptr != get ()) { 00324 delete [] m_ptr; 00325 m_ptr = ref_.m_ptr; 00326 } 00327 return *this; 00328 } 00329 00330 template<typename T> 00331 operator AutoPtrArrayRef<T> () { return AutoPtrArrayRef<T> (release ()); } 00332 00333 template<typename T> 00334 operator AutoPtrArray<T> () { return AutoPtrArray<T> (release ()); } 00335 00337 }; 00338 00339 } // end namespace ASSA 00340 00341 #endif /* AUTO_PTR_H */ 00342 00343