auto_ptr.hxx.in 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. template <class X> class auto_ptr;
  15. // The auto_ptr_ref template is supposed to be a private member of
  16. // auto_ptr but Borland 5.8 cannot handle it.
  17. template <class Y> struct auto_ptr_ref
  18. {
  19. auto_ptr<Y>& p_;
  20. explicit auto_ptr_ref(auto_ptr<Y>& p): p_(p) {}
  21. };
  22. // C++98 Standard Section 20.4.5 - Template class auto_ptr.
  23. template <class X>
  24. class auto_ptr
  25. {
  26. X* x_;
  27. public:
  28. typedef X element_type;
  29. template <class Y>
  30. auto_ptr(auto_ptr<Y>& a) throw(): x_(a.release()) {}
  31. template <class Y>
  32. auto_ptr& operator=(auto_ptr<Y>& a) throw()
  33. { reset(a.release()); return *this; }
  34. explicit auto_ptr(X* p=0) throw(): x_(p) {}
  35. auto_ptr(auto_ptr& a) throw(): x_(a.release()) {}
  36. auto_ptr& operator=(auto_ptr& a) throw() { reset(a.release()); return *this; }
  37. ~auto_ptr() throw() { delete get(); }
  38. X& operator*() const throw() { return *get(); }
  39. X* operator->() const throw() { return get(); }
  40. X* get() const throw() { return x_; }
  41. X* release() throw() { X* x = x_; x_ = 0; return x; }
  42. void reset(X* p=0) throw() { if(get() != p) { delete get(); x_ = p; } }
  43. auto_ptr(auto_ptr_ref<X> r) throw(): x_(r.p_.release()) {}
  44. template <class Y> operator auto_ptr_ref<Y>() throw() { return auto_ptr_ref<Y>(*this); }
  45. template <class Y> operator auto_ptr<Y>() throw() { return release(); }
  46. auto_ptr& operator=(auto_ptr_ref<X> r) throw() { reset(r.p_.release()); return *this; }
  47. };
  48. } // namespace @KWSYS_NAMESPACE@
  49. #endif