CVS resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / kwsys / auto_ptr.hxx.in
blob104972c34df8aa0d3c643d2fb056f3f1dc0929d3
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)
30 #else
31 # define @KWSYS_NAMESPACE@_AUTO_PTR_REF 1
32 # define @KWSYS_NAMESPACE@_AUTO_PTR_CONST
33 # define @KWSYS_NAMESPACE@_AUTO_PTR_CAST(a) a
34 #endif
36 namespace @KWSYS_NAMESPACE@
39 template <class X> class auto_ptr;
41 #if @KWSYS_NAMESPACE@_AUTO_PTR_REF
42 namespace detail
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
49 Y* p_;
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
55 // infinately.
56 auto_ptr_ref(Y* p, int): p_(p) {}
59 #endif
61 /** C++98 Standard Section 20.4.5 - Template class auto_ptr. */
62 template <class X>
63 class auto_ptr
65 #if !@KWSYS_NAMESPACE@_AUTO_PTR_REF
66 template <typename Y>
67 static inline auto_ptr<Y>& cast(auto_ptr<Y> const& a)
68 { return const_cast<auto_ptr<Y>&>(a); }
69 #endif
71 /** The pointer to the object held. */
72 X* x_;
74 public:
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. */
80 template <class Y>
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. */
88 template <class Y>
89 auto_ptr& operator=(auto_ptr<Y> @KWSYS_NAMESPACE@_AUTO_PTR_CONST& a) throw()
91 this->reset(@KWSYS_NAMESPACE@_AUTO_PTR_CAST(a).release());
92 return *this;
95 /**
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
107 auto_ptr. */
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());
118 return *this;
121 /** Destruct and delete the object held. */
122 ~auto_ptr() throw()
124 // Assume object destructor is nothrow.
125 delete this->x_;
128 /** Dereference and return a reference to the object held. */
129 X& operator*() const throw()
131 return *this->x_;
134 /** Return a pointer to the object held. */
135 X* operator->() const throw()
137 return this->x_;
140 /** Return a pointer to the object held. */
141 X* get() const throw()
143 return this->x_;
146 /** Return a pointer to the object held and reset to hold no object.
147 This transfers ownership to the caller. */
148 X* release() throw()
150 X* x = this->x_;
151 this->x_ = 0;
152 return x;
155 /** Assume ownership of the given object. The object previously
156 held is deleted. */
157 void reset(X* p=0) throw()
159 if(this->x_ != p)
161 // Assume object destructor is nothrow.
162 delete this->x_;
163 this->x_ = p;
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
177 auto_ptr. */
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
184 assignment. */
185 auto_ptr& operator=(detail::auto_ptr_ref<X> r) throw()
187 this->reset(r.p_);
188 return *this;
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
193 another auto_ptr. */
194 template <class Y> operator detail::auto_ptr_ref<Y>() throw()
196 return detail::auto_ptr_ref<Y>(this->release(), 1);
198 #endif
201 } // namespace @KWSYS_NAMESPACE@
203 #endif