auto_ptr.hxx.in 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*=========================================================================
  2. Program: KWSys - Kitware System Library
  3. Module: $RCSfile$
  4. Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved.
  5. See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even
  7. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  8. PURPOSE. See the above copyright notices for more information.
  9. =========================================================================*/
  10. #ifndef @KWSYS_NAMESPACE@_auto_ptr_hxx
  11. #define @KWSYS_NAMESPACE@_auto_ptr_hxx
  12. namespace @KWSYS_NAMESPACE@
  13. {
  14. // C++98 Standard Section 20.4.5 - Template class auto_ptr.
  15. template <class X>
  16. class auto_ptr
  17. {
  18. template <class Y> struct auto_ptr_ref
  19. {
  20. auto_ptr<Y>& p_;
  21. explicit auto_ptr_ref(auto_ptr<Y>& p): p_(p) {}
  22. };
  23. X* x_;
  24. public:
  25. typedef X element_type;
  26. template <class Y>
  27. auto_ptr(auto_ptr<Y>& a) throw(): x_(a.release()) {}
  28. template <class Y>
  29. auto_ptr& operator=(auto_ptr<Y>& a) throw()
  30. { reset(a.release()); return *this; }
  31. explicit auto_ptr(X* p=0) throw(): x_(p) {}
  32. auto_ptr(auto_ptr& a) throw(): x_(a.release()) {}
  33. auto_ptr& operator=(auto_ptr& a) throw() { reset(a.release()); return *this; }
  34. ~auto_ptr() throw() { delete get(); }
  35. X& operator*() const throw() { return *get(); }
  36. X* operator->() const throw() { return get(); }
  37. X* get() const throw() { return x_; }
  38. X* release() throw() { X* x = x_; x_ = 0; return x; }
  39. void reset(X* p=0) throw() { if(get() != p) { delete get(); x_ = p; } }
  40. auto_ptr(auto_ptr_ref<X> r) throw(): x_(r.p_.release()) {}
  41. template <class Y> operator auto_ptr_ref<Y>() throw() { return auto_ptr_ref<Y>(*this); }
  42. template <class Y> operator auto_ptr<Y>() throw() { return release(); }
  43. auto_ptr& operator=(auto_ptr_ref<X> r) throw() { x_ = r.p_.release(); return *this; }
  44. };
  45. } // namespace @KWSYS_NAMESPACE@
  46. #endif