Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

auto_array_ptr.h

Go to the documentation of this file.
00001 /*  auto_array_ptr.h - auto_ptr for arrays
00002     Copyright (C) 2001-2004 Mark Weaver
00003     Written by Mark Weaver <mark@npsl.co.uk>
00004 
00005     Part of the Open-Win32 library.
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public
00017     License along with this library; if not, write to the
00018     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019     Boston, MA  02111-1307, USA.
00020 */
00021 
00026 #ifndef OW32_auto_array_ptr_h
00027 #define OW32_auto_array_ptr_h
00028 
00029 namespace OW32
00030 {
00031 
00032 template<class _Tp1> struct auto_array_ptr_ref {
00033   _Tp1* _M_ptr;
00034   auto_array_ptr_ref(_Tp1* __p) : _M_ptr(__p) {}
00035 };
00036 
00040 template <class _Tp> class auto_array_ptr {
00041 private:
00042   _Tp* _M_ptr;
00043 
00044 public:
00045   typedef _Tp element_type;
00046 
00047   explicit auto_array_ptr(_Tp* __p = 0) throw() : _M_ptr(__p) {}
00048   auto_array_ptr(auto_array_ptr& __a) throw() : _M_ptr(__a.release()) {}
00049 
00050 #if 0 // member templates not supported
00051   template <class _Tp1> auto_array_ptr(auto_array_ptr<_Tp1>& __a) throw()
00052     : _M_ptr(__a.release()) {}
00053 #endif
00054 
00055   auto_array_ptr& operator=(auto_array_ptr& __a) throw() {
00056     if (&__a != this) {
00057       delete [] _M_ptr;
00058       _M_ptr = __a.release();
00059     }
00060     return *this;
00061   }
00062 
00063 #if 0 // member templates not supported
00064   template <class _Tp1>
00065   auto_array_ptr& operator=(auto_array_ptr<_Tp1>& __a) throw() {
00066     if (__a.get() != this->get()) {
00067       delete [] _M_ptr;
00068       _M_ptr = __a.release();
00069     }
00070     return *this;
00071   }
00072 #endif
00073 
00074   // Note: The C++ standard says there is supposed to be an empty throw
00075   // specification here, but omitting it is standard conforming.  Its 
00076   // presence can be detected only if _Tp::~_Tp() throws, but (17.4.3.6/2)
00077   // this is prohibited.
00078   ~auto_array_ptr() { delete [] _M_ptr; }
00079 
00080   _Tp& operator*() const throw() {
00081     return *_M_ptr;
00082   }
00083   _Tp& operator[](size_t index) throw() {
00084       return _M_ptr[index];
00085   }
00086   const _Tp& operator[](size_t index) const throw() {
00087       return _M_ptr[index];
00088   }
00089 /*  _Tp* operator->() const throw() {
00090     return _M_ptr;
00091   }*/
00092   _Tp* get() const throw() {
00093     return _M_ptr;
00094   }
00095   _Tp* release() throw() {
00096     _Tp* __tmp = _M_ptr;
00097     _M_ptr = 0;
00098     return __tmp;
00099   }
00100   void reset(_Tp* __p = 0) throw() {
00101     if (__p != _M_ptr) {
00102       delete [] _M_ptr;
00103       _M_ptr = __p;
00104     }
00105   }
00106 
00107   // According to the C++ standard, these conversions are required.  Most
00108   // present-day compilers, however, do not enforce that requirement---and, 
00109   // in fact, most present-day compilers do not support the language 
00110   // features that these conversions rely on.
00111 
00112 #if 0 // member templates not supported
00113 public:
00114   auto_array_ptr(auto_array_ptr_ref<_Tp> __ref) throw()
00115     : _M_ptr(__ref._M_ptr) {}
00116 
00117   auto_array_ptr& operator=(auto_array_ptr_ref<_Tp> __ref) throw() {
00118     if (__ref._M_ptr != this->get()) {
00119       delete [] _M_ptr;
00120       _M_ptr = __ref._M_ptr;
00121     }
00122     return *this;
00123   }
00124 
00125   template <class _Tp1> operator auto_array_ptr_ref<_Tp1>() throw() 
00126     { return auto_array_ptr_ref<_Tp1>(this->release()); }
00127   template <class _Tp1> operator auto_array_ptr<_Tp1>() throw()
00128     { return auto_array_ptr<_Tp1>(this->release()); }
00129 #endif
00130 };
00131 
00132 } // namespace OW32
00133 
00134 #endif // OW32_auto_array_ptr_h

Generated on Sun Jun 5 01:29:17 2005 for OW32 by  doxygen 1.3.9.1