/home/ntakagi/work/STLport-5.1.5/stlport/stl/_auto_ptr.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1997-1999
00003  * Silicon Graphics Computer Systems, Inc.
00004  *
00005  * Copyright (c) 1999
00006  * Boris Fomitchev
00007  *
00008  * This material is provided "as is", with absolutely no warranty expressed
00009  * or implied. Any use is at your own risk.
00010  *
00011  * Permission to use or copy this software for any purpose is hereby granted
00012  * without fee, provided the above notices are retained on all copies.
00013  * Permission to modify the code and to distribute modified code is granted,
00014  * provided the above notices are retained, and a notice that the code was
00015  * modified is included with the above copyright notice.
00016  *
00017  */
00018 
00019 #ifndef _STLP_AUTO_PTR_H
00020 # define _STLP_AUTO_PTR_H
00021 
00022 _STLP_BEGIN_NAMESPACE
00023 // implementation primitive
00024 class __ptr_base {
00025 public:
00026   void* _M_p;
00027   void  __set(const void* p) { _M_p = __CONST_CAST(void*,p); }
00028   void  __set(void* p) { _M_p = p; }
00029 };
00030 
00031 template <class _Tp>
00032 class auto_ptr_ref {
00033 public:
00034   __ptr_base& _M_r;
00035   _Tp* const _M_p;
00036 
00037   auto_ptr_ref(__ptr_base& __r, _Tp* __p) : _M_r(__r), _M_p(__p) {  }
00038 
00039   _Tp* release() const { _M_r.__set((void*)0); return _M_p; }
00040 
00041 private:
00042   //explicitely defined as private to avoid warnings:
00043   typedef auto_ptr_ref<_Tp> _Self;
00044   _Self& operator = (_Self const&);
00045 };
00046 
00047 template<class _Tp>
00048 class auto_ptr :  public __ptr_base {
00049 public:
00050   typedef _Tp element_type;
00051   typedef auto_ptr<_Tp> _Self;
00052 
00053   _Tp* release() _STLP_NOTHROW {
00054     _Tp* __px = this->get();
00055     this->_M_p = 0;
00056     return __px;
00057   }
00058 
00059   void reset(_Tp* __px = 0) _STLP_NOTHROW {
00060     _Tp* __pt = this->get();
00061     if (__px != __pt)
00062       delete __pt;
00063     this->__set(__px);
00064   }
00065 
00066   _Tp* get() const _STLP_NOTHROW
00067   { return __REINTERPRET_CAST(_Tp*,__CONST_CAST(void*,_M_p)); }
00068 
00069 #if !defined (_STLP_NO_ARROW_OPERATOR)
00070   _Tp* operator->() const _STLP_NOTHROW {
00071     _STLP_VERBOSE_ASSERT(get()!=0, _StlMsg_AUTO_PTR_NULL)
00072     return get();
00073   }
00074 #endif
00075   _Tp& operator*() const _STLP_NOTHROW {
00076     _STLP_VERBOSE_ASSERT(get()!= 0, _StlMsg_AUTO_PTR_NULL)
00077     return *get();
00078   }
00079 
00080   explicit auto_ptr(_Tp* __px = 0) _STLP_NOTHROW { this->__set(__px); }
00081 
00082 #if defined (_STLP_MEMBER_TEMPLATES)
00083 #  if !defined (_STLP_NO_TEMPLATE_CONVERSIONS)
00084   template<class _Tp1> auto_ptr(auto_ptr<_Tp1>& __r) _STLP_NOTHROW {
00085     _Tp* __conversionCheck = __r.release();
00086     this->__set(__conversionCheck);
00087   }
00088 #  endif
00089   template<class _Tp1> auto_ptr<_Tp>& operator=(auto_ptr<_Tp1>& __r) _STLP_NOTHROW {
00090     _Tp* __conversionCheck = __r.release();
00091     reset(__conversionCheck);
00092     return *this;
00093   }
00094 #endif
00095 
00096   auto_ptr(_Self& __r) _STLP_NOTHROW { this->__set(__r.release()); }
00097 
00098   _Self& operator=(_Self& __r) _STLP_NOTHROW {
00099     reset(__r.release());
00100     return *this;
00101   }
00102 
00103   ~auto_ptr() _STLP_NOTHROW { /* boris : reset(0) might be better */ delete this->get(); }
00104 
00105   auto_ptr(auto_ptr_ref<_Tp> __r) _STLP_NOTHROW
00106   { this->__set(__r.release()); }
00107 
00108   _Self& operator=(auto_ptr_ref<_Tp> __r) _STLP_NOTHROW {
00109     reset(__r.release());
00110     return *this;
00111   }
00112 
00113 #if defined(_STLP_MEMBER_TEMPLATES) && !defined(_STLP_NO_TEMPLATE_CONVERSIONS)
00114   template<class _Tp1> operator auto_ptr_ref<_Tp1>() _STLP_NOTHROW
00115   { return auto_ptr_ref<_Tp1>(*this, this->get()); }
00116   template<class _Tp1> operator auto_ptr<_Tp1>() _STLP_NOTHROW
00117   { return auto_ptr<_Tp1>(release()); }
00118 #else
00119   operator auto_ptr_ref<_Tp>() _STLP_NOTHROW
00120   { return auto_ptr_ref<_Tp>(*this, this->get()); }
00121 #endif
00122 };
00123 _STLP_END_NAMESPACE
00124 
00125 #endif /* _STLP_AUTO_PTR_H */
00126 
00127 // Local Variables:
00128 // mode:C++
00129 // End:



Generated on Mon Mar 10 15:32:18 2008 by  doxygen 1.5.1