home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / stlpt453.zip / STLport-4.5.3 / stlport / stl / _auto_ptr.h < prev    next >
C/C++ Source or Header  |  2001-11-09  |  3KB  |  130 lines

  1. /*
  2.  * Copyright (c) 1997-1999
  3.  * Silicon Graphics Computer Systems, Inc.
  4.  *
  5.  * Copyright (c) 1999 
  6.  * Boris Fomitchev
  7.  *
  8.  * This material is provided "as is", with absolutely no warranty expressed
  9.  * or implied. Any use is at your own risk.
  10.  *
  11.  * Permission to use or copy this software for any purpose is hereby granted 
  12.  * without fee, provided the above notices are retained on all copies.
  13.  * Permission to modify the code and to distribute modified code is granted,
  14.  * provided the above notices are retained, and a notice that the code was
  15.  * modified is included with the above copyright notice.
  16.  *
  17.  */
  18.  
  19. #ifndef _STLP_AUTO_PTR_H
  20. # define _STLP_AUTO_PTR_H
  21.  
  22. _STLP_BEGIN_NAMESPACE
  23. // implementation primitive
  24. class __ptr_base {
  25. public:
  26.   void* _M_p;
  27.   void  __set(const void* p) { _M_p = __CONST_CAST(void*,p); }
  28.   void  __set(void* p) { _M_p = p; }
  29. };
  30.  
  31. template <class _Tp> class auto_ptr_ref {
  32. public:
  33.   __ptr_base& _M_r;
  34.   _Tp* const _M_p;
  35.  
  36.   auto_ptr_ref(__ptr_base& __r, _Tp* __p) : _M_r(__r), _M_p(__p) {  }
  37.  
  38.   _Tp* release() const { _M_r.__set((void*)0); return _M_p; }
  39.  
  40. };
  41.  
  42. template<class _Tp> struct auto_ptr :  public __ptr_base {
  43.     
  44.   typedef _Tp element_type;
  45.   typedef auto_ptr<_Tp>           _Self;
  46.   
  47.   _Tp* release() {  
  48.     _Tp* __px = this->get(); 
  49.     this->_M_p = 0; 
  50.     return __px; 
  51.   }
  52.   
  53.   void reset(_Tp* __px=0) {
  54.     _Tp* __pt = this->get();
  55.     if (__px != __pt) 
  56.       delete __pt; 
  57.     this->__set(__px); 
  58.   }
  59.  
  60.   _Tp* get() const { return __REINTERPRET_CAST(_Tp*,__CONST_CAST(void*,_M_p)); } 
  61.  
  62. # if !defined (_STLP_NO_ARROW_OPERATOR)
  63.   _Tp* operator->() const { 
  64.     _STLP_VERBOSE_ASSERT(get()!=0, _StlMsg_AUTO_PTR_NULL)
  65.     return get(); 
  66.   }
  67. # endif
  68.   _Tp& operator*() const  { 
  69.     _STLP_VERBOSE_ASSERT(get()!=0, _StlMsg_AUTO_PTR_NULL)
  70.     return *get(); 
  71.   }
  72.   
  73.   auto_ptr() { this->_M_p = 0; }
  74.   
  75.   explicit auto_ptr(_Tp* __px) { this->__set(__px); }
  76.   
  77. #if defined (_STLP_MEMBER_TEMPLATES)
  78. # if !defined (_STLP_NO_TEMPLATE_CONVERSIONS)
  79.   template<class _Tp1> auto_ptr(auto_ptr<_Tp1>& __r) {
  80.     _Tp* __conversionCheck = __r.release();
  81.     this->__set(__conversionCheck);
  82.   }
  83. # endif    
  84.   template<class _Tp1> auto_ptr<_Tp>& operator=(auto_ptr<_Tp1>& __r) {
  85.     _Tp* __conversionCheck = __r.release();
  86.     reset(__conversionCheck);
  87.     return *this;
  88.   }
  89. #endif /* _STLP_MEMBER_TEMPLATES */
  90.   
  91.   auto_ptr(_Self& __r) { this->__set(__r.release()); }
  92.  
  93.   _Self& operator=(_Self& __r)  {
  94.     reset(__r.release());
  95.     return *this;
  96.   }
  97.  
  98.   ~auto_ptr() { /* boris : reset(0) might be better */ delete this->get(); }
  99.     
  100.   auto_ptr(auto_ptr_ref<_Tp> __r) {
  101.     this->__set(__r.release());
  102.   }
  103.     
  104.   _Self& operator=(auto_ptr_ref<_Tp> __r) {
  105.     reset(__r.release());
  106.     return *this;
  107.   }
  108.   
  109. # if defined(_STLP_MEMBER_TEMPLATES) && !defined(_STLP_NO_TEMPLATE_CONVERSIONS)
  110.   template<class _Tp1> operator auto_ptr_ref<_Tp1>() {
  111.     return auto_ptr_ref<_Tp1>(*this, this->get());
  112.   }
  113.   template<class _Tp1> operator auto_ptr<_Tp1>() {
  114.     return auto_ptr<_Tp1>(release());
  115.   }
  116. # else
  117.   operator auto_ptr_ref<_Tp>()
  118.   { return auto_ptr_ref<_Tp>(*this, this->get()); }
  119. # endif
  120.     
  121. };
  122. _STLP_END_NAMESPACE
  123.  
  124. #endif /* _STLP_AUTO_PTR_H */
  125.  
  126. // Local Variables:
  127. // mode:C++
  128. // End:
  129.  
  130.