1 /*=========================================================================
3 Program: KWSys - Kitware System Library
4 Module: $RCSfile: auto_ptr.hxx.in,v $
6 Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved.
7 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the above copyright notices for more information.
13 =========================================================================*/
14 #ifndef @KWSYS_NAMESPACE@_auto_ptr_hxx
15 #define @KWSYS_NAMESPACE@_auto_ptr_hxx
17 #include <@KWSYS_NAMESPACE@/Configure.hxx>
19 // The HP compiler and VS6 cannot handle the conversions necessary to use
20 // auto_ptr_ref to pass an auto_ptr returned from one function
21 // directly to another function as in use_auto_ptr(get_auto_ptr()).
22 // We instead use const_cast to achieve the syntax on those platforms.
23 // We do not use const_cast on other platforms to maintain the C++
24 // standard design and guarantee that if an auto_ptr is bound
25 // to a reference-to-const then ownership will be maintained.
26 #if defined(__HP_aCC) || (defined(_MSC_VER) && _MSC_VER <= 1200)
27 # define @KWSYS_NAMESPACE@_AUTO_PTR_REF 0
28 # define @KWSYS_NAMESPACE@_AUTO_PTR_CONST const
29 # define @KWSYS_NAMESPACE@_AUTO_PTR_CAST(a) cast(a)
31 # define @KWSYS_NAMESPACE@_AUTO_PTR_REF 1
32 # define @KWSYS_NAMESPACE@_AUTO_PTR_CONST
33 # define @KWSYS_NAMESPACE@_AUTO_PTR_CAST(a) a
36 namespace @KWSYS_NAMESPACE@
39 template <class X
> class auto_ptr
;
41 #if @KWSYS_NAMESPACE@_AUTO_PTR_REF
44 // The auto_ptr_ref template is supposed to be a private member of
45 // auto_ptr but Borland 5.8 cannot handle it. Instead put it in
46 // a private namespace.
47 template <class Y
> struct auto_ptr_ref
51 // The extra constructor argument prevents implicit conversion to
52 // auto_ptr_ref from auto_ptr through the constructor. Normally
53 // this should be done with the explicit keyword but Borland 5.x
54 // generates code in the conversion operator to call itself
56 auto_ptr_ref(Y
* p
, int): p_(p
) {}
61 /** C++98 Standard Section 20.4.5 - Template class auto_ptr. */
65 #if !@KWSYS_NAMESPACE@_AUTO_PTR_REF
67 static inline auto_ptr
<Y
>& cast(auto_ptr
<Y
> const& a
)
68 { return const_cast<auto_ptr
<Y
>&>(a
); }
71 /** The pointer to the object held. */
75 /** The type of object held by the auto_ptr. */
76 typedef X element_type
;
78 /** Construct from an auto_ptr holding a compatible object. This
79 transfers ownership to the newly constructed auto_ptr. */
81 auto_ptr(auto_ptr
<Y
> @KWSYS_NAMESPACE@_AUTO_PTR_CONST
& a
) throw():
82 x_(@KWSYS_NAMESPACE@
_AUTO_PTR_CAST(a
).release())
86 /** Assign from an auto_ptr holding a compatible object. This
87 transfers ownership to the left-hand-side of the assignment. */
89 auto_ptr
& operator=(auto_ptr
<Y
> @KWSYS_NAMESPACE@_AUTO_PTR_CONST
& a
) throw()
91 this->reset(@KWSYS_NAMESPACE@
_AUTO_PTR_CAST(a
).release());
96 * Explicitly construct from a raw pointer. This is typically
97 * called with the result of operator new. For example:
99 * auto_ptr<X> ptr(new X());
101 explicit auto_ptr(X
* p
=0) throw(): x_(p
)
105 /** Construct from another auto_ptr holding an object of the same
106 type. This transfers ownership to the newly constructed
108 auto_ptr(auto_ptr @KWSYS_NAMESPACE@_AUTO_PTR_CONST
& a
) throw():
109 x_(@KWSYS_NAMESPACE@
_AUTO_PTR_CAST(a
).release())
113 /** Assign from another auto_ptr holding an object of the same type.
114 This transfers ownership to the newly constructed auto_ptr. */
115 auto_ptr
& operator=(auto_ptr @KWSYS_NAMESPACE@_AUTO_PTR_CONST
& a
) throw()
117 this->reset(@KWSYS_NAMESPACE@
_AUTO_PTR_CAST(a
).release());
121 /** Destruct and delete the object held. */
124 // Assume object destructor is nothrow.
128 /** Dereference and return a reference to the object held. */
129 X
& operator*() const throw()
134 /** Return a pointer to the object held. */
135 X
* operator->() const throw()
140 /** Return a pointer to the object held. */
141 X
* get() const throw()
146 /** Return a pointer to the object held and reset to hold no object.
147 This transfers ownership to the caller. */
155 /** Assume ownership of the given object. The object previously
157 void reset(X
* p
=0) throw()
161 // Assume object destructor is nothrow.
167 /** Convert to an auto_ptr holding an object of a compatible type.
168 This transfers ownership to the returned auto_ptr. */
169 template <class Y
> operator auto_ptr
<Y
>() throw()
171 return auto_ptr
<Y
>(this->release());
174 #if @KWSYS_NAMESPACE@_AUTO_PTR_REF
175 /** Construct from an auto_ptr_ref. This is used when the
176 constructor argument is a call to a function returning an
178 auto_ptr(detail::auto_ptr_ref
<X
> r
) throw(): x_(r
.p_
)
182 /** Assign from an auto_ptr_ref. This is used when a function
183 returning an auto_ptr is passed on the right-hand-side of an
185 auto_ptr
& operator=(detail::auto_ptr_ref
<X
> r
) throw()
191 /** Convert to an auto_ptr_ref. This is used when a function
192 returning an auto_ptr is the argument to the constructor of
194 template <class Y
> operator detail::auto_ptr_ref
<Y
>() throw()
196 return detail::auto_ptr_ref
<Y
>(this->release(), 1);
201 } // namespace @KWSYS_NAMESPACE@