_auto_ptr.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #ifndef _STLP_AUTO_PTR_H
  19. #define _STLP_AUTO_PTR_H
  20. _STLP_BEGIN_NAMESPACE
  21. // implementation primitive
  22. class __ptr_base {
  23. public:
  24. void* _M_p;
  25. void __set(const volatile void* p) { _M_p = __CONST_CAST(void*,p); }
  26. void __set(void* p) { _M_p = p; }
  27. };
  28. template <class _Tp>
  29. class auto_ptr_ref {
  30. public:
  31. __ptr_base& _M_r;
  32. _Tp* const _M_p;
  33. auto_ptr_ref(__ptr_base& __r, _Tp* __p) : _M_r(__r), _M_p(__p) { }
  34. _Tp* release() const { _M_r.__set(__STATIC_CAST(void*, 0)); return _M_p; }
  35. private:
  36. //explicitely defined as private to avoid warnings:
  37. typedef auto_ptr_ref<_Tp> _Self;
  38. _Self& operator = (_Self const&);
  39. };
  40. template<class _Tp>
  41. class auto_ptr : public __ptr_base {
  42. public:
  43. typedef _Tp element_type;
  44. typedef auto_ptr<_Tp> _Self;
  45. _Tp* release() _STLP_NOTHROW {
  46. _Tp* __px = this->get();
  47. this->_M_p = 0;
  48. return __px;
  49. }
  50. void reset(_Tp* __px = 0) _STLP_NOTHROW {
  51. _Tp* __pt = this->get();
  52. if (__px != __pt)
  53. delete __pt;
  54. this->__set(__px);
  55. }
  56. _Tp* get() const _STLP_NOTHROW
  57. #if !defined (__GNUC__) || (__GNUC__ > 2)
  58. { return __STATIC_CAST(_Tp*, _M_p); }
  59. #else
  60. { return __REINTERPRET_CAST(_Tp*, _M_p); }
  61. #endif
  62. #if !defined (_STLP_NO_ARROW_OPERATOR)
  63. _Tp* operator->() const _STLP_NOTHROW {
  64. _STLP_VERBOSE_ASSERT(get() != 0, _StlMsg_AUTO_PTR_NULL)
  65. return get();
  66. }
  67. #endif
  68. _Tp& operator*() const _STLP_NOTHROW {
  69. _STLP_VERBOSE_ASSERT(get() != 0, _StlMsg_AUTO_PTR_NULL)
  70. return *get();
  71. }
  72. explicit auto_ptr(_Tp* __px = 0) _STLP_NOTHROW { this->__set(__px); }
  73. #if defined (_STLP_MEMBER_TEMPLATES)
  74. # if !defined (_STLP_NO_TEMPLATE_CONVERSIONS)
  75. template<class _Tp1> auto_ptr(auto_ptr<_Tp1>& __r) _STLP_NOTHROW {
  76. _Tp* __conversionCheck = __r.release();
  77. this->__set(__conversionCheck);
  78. }
  79. # endif
  80. template<class _Tp1> auto_ptr<_Tp>& operator=(auto_ptr<_Tp1>& __r) _STLP_NOTHROW {
  81. _Tp* __conversionCheck = __r.release();
  82. reset(__conversionCheck);
  83. return *this;
  84. }
  85. #endif
  86. auto_ptr(_Self& __r) _STLP_NOTHROW { this->__set(__r.release()); }
  87. _Self& operator=(_Self& __r) _STLP_NOTHROW {
  88. reset(__r.release());
  89. return *this;
  90. }
  91. ~auto_ptr() _STLP_NOTHROW { /* boris : reset(0) might be better */ delete this->get(); }
  92. auto_ptr(auto_ptr_ref<_Tp> __r) _STLP_NOTHROW
  93. { this->__set(__r.release()); }
  94. _Self& operator=(auto_ptr_ref<_Tp> __r) _STLP_NOTHROW {
  95. reset(__r.release());
  96. return *this;
  97. }
  98. #if defined(_STLP_MEMBER_TEMPLATES) && !defined(_STLP_NO_TEMPLATE_CONVERSIONS)
  99. template<class _Tp1> operator auto_ptr_ref<_Tp1>() _STLP_NOTHROW
  100. { return auto_ptr_ref<_Tp1>(*this, this->get()); }
  101. template<class _Tp1> operator auto_ptr<_Tp1>() _STLP_NOTHROW
  102. { return auto_ptr<_Tp1>(release()); }
  103. #else
  104. operator auto_ptr_ref<_Tp>() _STLP_NOTHROW
  105. { return auto_ptr_ref<_Tp>(*this, this->get()); }
  106. #endif
  107. };
  108. _STLP_END_NAMESPACE
  109. #endif /* _STLP_AUTO_PTR_H */
  110. // Local Variables:
  111. // mode:C++
  112. // End: