_auto_ptr.h

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> class auto_ptr_ref {
00032 public:
00033   __ptr_base& _M_r;
00034   _Tp* const _M_p;
00035 
00036   auto_ptr_ref(__ptr_base& __r, _Tp* __p) : _M_r(__r), _M_p(__p) {  }
00037 
00038   _Tp* release() const { _M_r.__set((void*)0); return _M_p; }
00039 
00040 };
00041 
00042 template<class _Tp> struct auto_ptr :  public __ptr_base {
00043         
00044   typedef _Tp element_type;
00045   typedef auto_ptr<_Tp>           _Self;
00046   
00047   _Tp* release() {  
00048     _Tp* __px = this->get(); 
00049     this->_M_p = 0; 
00050     return __px; 
00051   }
00052   
00053   void reset(_Tp* __px=0) {
00054     _Tp* __pt = this->get();
00055     if (__px != __pt) 
00056       delete __pt; 
00057     this->__set(__px); 
00058   }
00059 
00060   _Tp* get() const { return __REINTERPRET_CAST(_Tp*,__CONST_CAST(void*,_M_p)); } 
00061 
00062 # if !defined (_STLP_NO_ARROW_OPERATOR)
00063   _Tp* operator->() const { 
00064     _STLP_VERBOSE_ASSERT(get()!=0, _StlMsg_AUTO_PTR_NULL)
00065     return get(); 
00066   }
00067 # endif
00068   _Tp& operator*() const  { 
00069     _STLP_VERBOSE_ASSERT(get()!=0, _StlMsg_AUTO_PTR_NULL)
00070     return *get(); 
00071   }
00072   
00073   auto_ptr() { this->_M_p = 0; }
00074   
00075   explicit auto_ptr(_Tp* __px) { this->__set(__px); }
00076   
00077 #if defined (_STLP_MEMBER_TEMPLATES)
00078 # if !defined (_STLP_NO_TEMPLATE_CONVERSIONS)
00079   template<class _Tp1> auto_ptr(auto_ptr<_Tp1>& __r) {
00080     _Tp* __conversionCheck = __r.release();
00081     this->__set(__conversionCheck);
00082   }
00083 # endif 
00084   template<class _Tp1> auto_ptr<_Tp>& operator=(auto_ptr<_Tp1>& __r) {
00085     _Tp* __conversionCheck = __r.release();
00086     reset(__conversionCheck);
00087     return *this;
00088   }
00089 #endif /* _STLP_MEMBER_TEMPLATES */
00090   
00091   auto_ptr(_Self& __r) { this->__set(__r.release()); }
00092 
00093   _Self& operator=(_Self& __r)  {
00094     reset(__r.release());
00095     return *this;
00096   }
00097 
00098   ~auto_ptr() { /* boris : reset(0) might be better */ delete this->get(); }
00099         
00100   auto_ptr(auto_ptr_ref<_Tp> __r) {
00101     this->__set(__r.release());
00102   }
00103         
00104   _Self& operator=(auto_ptr_ref<_Tp> __r) {
00105     reset(__r.release());
00106     return *this;
00107   }
00108   
00109 # if defined(_STLP_MEMBER_TEMPLATES) && !defined(_STLP_NO_TEMPLATE_CONVERSIONS)
00110   template<class _Tp1> operator auto_ptr_ref<_Tp1>() {
00111     return auto_ptr_ref<_Tp1>(*this, this->get());
00112   }
00113   template<class _Tp1> operator auto_ptr<_Tp1>() {
00114     return auto_ptr<_Tp1>(release());
00115   }
00116 # else
00117   operator auto_ptr_ref<_Tp>()
00118   { return auto_ptr_ref<_Tp>(*this, this->get()); }
00119 # endif
00120         
00121 };
00122 _STLP_END_NAMESPACE
00123 
00124 #endif /* _STLP_AUTO_PTR_H */
00125 
00126 // Local Variables:
00127 // mode:C++
00128 // End:
00129 

Generated on Mon Jun 5 10:20:45 2006 for Intelligence.kdevelop by  doxygen 1.4.6