auto_ptr.hxx.in 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*============================================================================
  2. KWSys - Kitware System Library
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #ifndef @KWSYS_NAMESPACE@_auto_ptr_hxx
  11. #define @KWSYS_NAMESPACE@_auto_ptr_hxx
  12. #include <@KWSYS_NAMESPACE@/Configure.hxx>
  13. // The HP compiler and VS6 cannot handle the conversions necessary to use
  14. // auto_ptr_ref to pass an auto_ptr returned from one function
  15. // directly to another function as in use_auto_ptr(get_auto_ptr()).
  16. // We instead use const_cast to achieve the syntax on those platforms.
  17. // We do not use const_cast on other platforms to maintain the C++
  18. // standard design and guarantee that if an auto_ptr is bound
  19. // to a reference-to-const then ownership will be maintained.
  20. #if defined(__HP_aCC) || (defined(_MSC_VER) && _MSC_VER <= 1200)
  21. # define @KWSYS_NAMESPACE@_AUTO_PTR_REF 0
  22. # define @KWSYS_NAMESPACE@_AUTO_PTR_CONST const
  23. # define @KWSYS_NAMESPACE@_AUTO_PTR_CAST(a) cast(a)
  24. #else
  25. # define @KWSYS_NAMESPACE@_AUTO_PTR_REF 1
  26. # define @KWSYS_NAMESPACE@_AUTO_PTR_CONST
  27. # define @KWSYS_NAMESPACE@_AUTO_PTR_CAST(a) a
  28. #endif
  29. namespace @KWSYS_NAMESPACE@
  30. {
  31. template <class X> class auto_ptr;
  32. #if @KWSYS_NAMESPACE@_AUTO_PTR_REF
  33. namespace detail
  34. {
  35. // The auto_ptr_ref template is supposed to be a private member of
  36. // auto_ptr but Borland 5.8 cannot handle it. Instead put it in
  37. // a private namespace.
  38. template <class Y> struct auto_ptr_ref
  39. {
  40. Y* p_;
  41. // The extra constructor argument prevents implicit conversion to
  42. // auto_ptr_ref from auto_ptr through the constructor. Normally
  43. // this should be done with the explicit keyword but Borland 5.x
  44. // generates code in the conversion operator to call itself
  45. // infinately.
  46. auto_ptr_ref(Y* p, int): p_(p) {}
  47. };
  48. }
  49. #endif
  50. /** C++98 Standard Section 20.4.5 - Template class auto_ptr. */
  51. template <class X>
  52. class auto_ptr
  53. {
  54. #if !@KWSYS_NAMESPACE@_AUTO_PTR_REF
  55. template <typename Y>
  56. static inline auto_ptr<Y>& cast(auto_ptr<Y> const& a)
  57. { return const_cast<auto_ptr<Y>&>(a); }
  58. #endif
  59. /** The pointer to the object held. */
  60. X* x_;
  61. public:
  62. /** The type of object held by the auto_ptr. */
  63. typedef X element_type;
  64. /** Construct from an auto_ptr holding a compatible object. This
  65. transfers ownership to the newly constructed auto_ptr. */
  66. template <class Y>
  67. auto_ptr(auto_ptr<Y> @KWSYS_NAMESPACE@_AUTO_PTR_CONST& a) throw():
  68. x_(@KWSYS_NAMESPACE@_AUTO_PTR_CAST(a).release())
  69. {
  70. }
  71. /** Assign from an auto_ptr holding a compatible object. This
  72. transfers ownership to the left-hand-side of the assignment. */
  73. template <class Y>
  74. auto_ptr& operator=(auto_ptr<Y> @KWSYS_NAMESPACE@_AUTO_PTR_CONST& a) throw()
  75. {
  76. this->reset(@KWSYS_NAMESPACE@_AUTO_PTR_CAST(a).release());
  77. return *this;
  78. }
  79. /**
  80. * Explicitly construct from a raw pointer. This is typically
  81. * called with the result of operator new. For example:
  82. *
  83. * auto_ptr<X> ptr(new X());
  84. */
  85. explicit auto_ptr(X* p=0) throw(): x_(p)
  86. {
  87. }
  88. /** Construct from another auto_ptr holding an object of the same
  89. type. This transfers ownership to the newly constructed
  90. auto_ptr. */
  91. auto_ptr(auto_ptr @KWSYS_NAMESPACE@_AUTO_PTR_CONST& a) throw():
  92. x_(@KWSYS_NAMESPACE@_AUTO_PTR_CAST(a).release())
  93. {
  94. }
  95. /** Assign from another auto_ptr holding an object of the same type.
  96. This transfers ownership to the newly constructed auto_ptr. */
  97. auto_ptr& operator=(auto_ptr @KWSYS_NAMESPACE@_AUTO_PTR_CONST& a) throw()
  98. {
  99. this->reset(@KWSYS_NAMESPACE@_AUTO_PTR_CAST(a).release());
  100. return *this;
  101. }
  102. /** Destruct and delete the object held. */
  103. ~auto_ptr() throw()
  104. {
  105. // Assume object destructor is nothrow.
  106. delete this->x_;
  107. }
  108. /** Dereference and return a reference to the object held. */
  109. X& operator*() const throw()
  110. {
  111. return *this->x_;
  112. }
  113. /** Return a pointer to the object held. */
  114. X* operator->() const throw()
  115. {
  116. return this->x_;
  117. }
  118. /** Return a pointer to the object held. */
  119. X* get() const throw()
  120. {
  121. return this->x_;
  122. }
  123. /** Return a pointer to the object held and reset to hold no object.
  124. This transfers ownership to the caller. */
  125. X* release() throw()
  126. {
  127. X* x = this->x_;
  128. this->x_ = 0;
  129. return x;
  130. }
  131. /** Assume ownership of the given object. The object previously
  132. held is deleted. */
  133. void reset(X* p=0) throw()
  134. {
  135. if(this->x_ != p)
  136. {
  137. // Assume object destructor is nothrow.
  138. delete this->x_;
  139. this->x_ = p;
  140. }
  141. }
  142. /** Convert to an auto_ptr holding an object of a compatible type.
  143. This transfers ownership to the returned auto_ptr. */
  144. template <class Y> operator auto_ptr<Y>() throw()
  145. {
  146. return auto_ptr<Y>(this->release());
  147. }
  148. #if @KWSYS_NAMESPACE@_AUTO_PTR_REF
  149. /** Construct from an auto_ptr_ref. This is used when the
  150. constructor argument is a call to a function returning an
  151. auto_ptr. */
  152. auto_ptr(detail::auto_ptr_ref<X> r) throw(): x_(r.p_)
  153. {
  154. }
  155. /** Assign from an auto_ptr_ref. This is used when a function
  156. returning an auto_ptr is passed on the right-hand-side of an
  157. assignment. */
  158. auto_ptr& operator=(detail::auto_ptr_ref<X> r) throw()
  159. {
  160. this->reset(r.p_);
  161. return *this;
  162. }
  163. /** Convert to an auto_ptr_ref. This is used when a function
  164. returning an auto_ptr is the argument to the constructor of
  165. another auto_ptr. */
  166. template <class Y> operator detail::auto_ptr_ref<Y>() throw()
  167. {
  168. return detail::auto_ptr_ref<Y>(this->release(), 1);
  169. }
  170. #endif
  171. };
  172. } // namespace @KWSYS_NAMESPACE@
  173. #endif