common/autoptr.h

Go to the documentation of this file.
00001 /* autoptr.h: An auto pointer implementation
00002  * Nicked from gcc...
00003  */
00004 
00005 // Copyright (C) 2001 Free Software Foundation, Inc.
00006 //
00007 // This file is part of the GNU ISO C++ Library.  This library is free
00008 // software; you can redistribute it and/or modify it under the
00009 // terms of the GNU General Public License as published by the
00010 // Free Software Foundation; either version 2, or (at your option)
00011 // any later version.
00012 
00013 // This library is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 
00018 // You should have received a copy of the GNU General Public License along
00019 // with this library; see the file COPYING.  If not, write to the Free
00020 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00021 // USA.
00022 
00023 // As a special exception, you may use this file as part of a free software
00024 // library without restriction.  Specifically, if other files instantiate
00025 // templates or use macros or inline functions from this file, or you compile
00026 // this file and link it with other files to produce an executable, this
00027 // file does not by itself cause the resulting executable to be covered by
00028 // the GNU General Public License.  This exception does not however
00029 // invalidate any other reasons why the executable file might be covered by
00030 // the GNU General Public License.
00031 
00032 /*
00033  * Copyright (c) 1997-1999
00034  * Silicon Graphics Computer Systems, Inc.
00035  *
00036  * Permission to use, copy, modify, distribute and sell this software
00037  * and its documentation for any purpose is hereby granted without fee,
00038  * provided that the above copyright notice appear in all copies and
00039  * that both that copyright notice and this permission notice appear
00040  * in supporting documentation.  Silicon Graphics makes no
00041  * representations about the suitability of this software for any
00042  * purpose.  It is provided "as is" without express or implied warranty.
00043  *
00044  */
00045 
00046 #ifndef OM_HGUARD_AUTOPTR_H
00047 #define OM_HGUARD_AUTOPTR_H
00048 
00049 template<class _Tp1> struct AutoPtrRef {
00050    _Tp1* _M_ptr;
00051    AutoPtrRef(_Tp1* __p) : _M_ptr(__p) {}
00052 };
00053 
00054 template <class _Tp> class AutoPtr {
00055 private:
00056   _Tp* _M_ptr;
00057 
00058 public:
00059   typedef _Tp element_type;
00060 
00061   explicit AutoPtr(_Tp* __p = 0)  : _M_ptr(__p) {}
00062   AutoPtr(AutoPtr& __a)  : _M_ptr(__a.release()) {}
00063 
00064   template <class _Tp1> AutoPtr(AutoPtr<_Tp1>& __a) 
00065     : _M_ptr(__a.release()) {}
00066 
00067   AutoPtr& operator=(AutoPtr& __a)  {
00068     reset(__a.release());
00069     return *this;
00070   }
00071 
00072   template <class _Tp1>
00073   AutoPtr& operator=(AutoPtr<_Tp1>& __a)  {
00074     reset(__a.release());
00075     return *this;
00076   }
00077   
00078   // Note: The C++ standard says there is supposed to be an empty throw
00079   // specification here, but omitting it is standard conforming.  Its 
00080   // presence can be detected only if _Tp::~_Tp() throws, but (17.4.3.6/2)
00081   // this is prohibited.
00082   ~AutoPtr() { delete _M_ptr; }
00083  
00084   _Tp& operator*() const  {
00085     return *_M_ptr;
00086   }
00087   _Tp* operator->() const  {
00088     return _M_ptr;
00089   }
00090   _Tp* get() const  {
00091     return _M_ptr;
00092   }
00093   _Tp* release()  {
00094     _Tp* __tmp = _M_ptr;
00095     _M_ptr = 0;
00096     return __tmp;
00097   }
00098   void reset(_Tp* __p = 0)  {
00099     if (__p != _M_ptr) {
00100       delete _M_ptr;
00101       _M_ptr = __p;
00102     }    
00103   }
00104 
00105   // According to the C++ standard, these conversions are required.  Most
00106   // present-day compilers, however, do not enforce that requirement---and, 
00107   // in fact, most present-day compilers do not support the language 
00108   // features that these conversions rely on.
00109 public:
00110   AutoPtr(AutoPtrRef<_Tp> __ref) 
00111     : _M_ptr(__ref._M_ptr) {}
00112 
00113   AutoPtr& operator=(AutoPtrRef<_Tp> __ref)  {
00114     if (__ref._M_ptr != this->get()) {
00115       delete _M_ptr;
00116       _M_ptr = __ref._M_ptr;
00117     }
00118     return *this;
00119   }
00120 
00121   template <class _Tp1> operator AutoPtrRef<_Tp1>()  
00122     { return AutoPtrRef<_Tp>(this->release()); }
00123   template <class _Tp1> operator AutoPtr<_Tp1>() 
00124     { return AutoPtr<_Tp1>(this->release()); }
00125 };
00126 
00127 #endif /* OM_HGUARD_AUTOPTR_H */

Documentation for Xapian (version 1.0.10).
Generated on 24 Dec 2008 by Doxygen 1.5.2.