2006-03-17 00:04:05 +03:00
|
|
|
/*=========================================================================
|
|
|
|
|
|
|
|
Program: KWSys - Kitware System Library
|
|
|
|
Module: $RCSfile$
|
|
|
|
|
|
|
|
Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved.
|
|
|
|
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
|
|
|
|
|
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even
|
|
|
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
PURPOSE. See the above copyright notices for more information.
|
|
|
|
|
|
|
|
=========================================================================*/
|
|
|
|
#ifndef @KWSYS_NAMESPACE@_auto_ptr_hxx
|
|
|
|
#define @KWSYS_NAMESPACE@_auto_ptr_hxx
|
|
|
|
|
2007-03-03 23:43:31 +03:00
|
|
|
#include <@KWSYS_NAMESPACE@/Configure.hxx>
|
|
|
|
|
2007-03-10 00:58:08 +03:00
|
|
|
// The HP compiler and VS6 cannot handle the conversions necessary to use
|
2007-03-07 17:26:49 +03:00
|
|
|
// auto_ptr_ref to pass an auto_ptr returned from one function
|
|
|
|
// directly to another function as in use_auto_ptr(get_auto_ptr()).
|
2007-03-10 00:58:08 +03:00
|
|
|
// We instead use const_cast to achieve the syntax on those platforms.
|
2007-03-07 17:26:49 +03:00
|
|
|
// We do not use const_cast on other platforms to maintain the C++
|
|
|
|
// standard design and guarantee that if an auto_ptr is bound
|
|
|
|
// to a reference-to-const then ownership will be maintained.
|
2007-03-10 00:58:08 +03:00
|
|
|
#if defined(__HP_aCC) || (defined(_MSC_VER) && _MSC_VER <= 1200)
|
2007-03-07 17:26:49 +03:00
|
|
|
# define @KWSYS_NAMESPACE@_AUTO_PTR_REF 0
|
2007-03-10 00:58:08 +03:00
|
|
|
# define @KWSYS_NAMESPACE@_AUTO_PTR_CONST const
|
|
|
|
# define @KWSYS_NAMESPACE@_AUTO_PTR_CAST(a) cast(a)
|
2007-03-07 17:26:49 +03:00
|
|
|
#else
|
|
|
|
# define @KWSYS_NAMESPACE@_AUTO_PTR_REF 1
|
2007-03-10 00:58:08 +03:00
|
|
|
# define @KWSYS_NAMESPACE@_AUTO_PTR_CONST
|
|
|
|
# define @KWSYS_NAMESPACE@_AUTO_PTR_CAST(a) a
|
2007-03-07 17:26:49 +03:00
|
|
|
#endif
|
|
|
|
|
2006-03-17 00:04:05 +03:00
|
|
|
namespace @KWSYS_NAMESPACE@
|
|
|
|
{
|
|
|
|
|
2007-02-28 17:35:28 +03:00
|
|
|
template <class X> class auto_ptr;
|
|
|
|
|
2007-03-07 17:26:49 +03:00
|
|
|
#if @KWSYS_NAMESPACE@_AUTO_PTR_REF
|
2007-03-03 22:48:48 +03:00
|
|
|
namespace detail
|
|
|
|
{
|
2007-02-28 17:35:28 +03:00
|
|
|
// The auto_ptr_ref template is supposed to be a private member of
|
2007-03-03 22:48:48 +03:00
|
|
|
// auto_ptr but Borland 5.8 cannot handle it. Instead put it in
|
|
|
|
// a private namespace.
|
2007-02-28 17:35:28 +03:00
|
|
|
template <class Y> struct auto_ptr_ref
|
|
|
|
{
|
2007-03-03 23:05:52 +03:00
|
|
|
Y* p_;
|
2007-03-03 22:48:48 +03:00
|
|
|
|
|
|
|
// The extra constructor argument prevents implicit conversion to
|
|
|
|
// auto_ptr_ref from auto_ptr through the constructor. Normally
|
|
|
|
// this should be done with the explicit keyword but Borland 5.x
|
|
|
|
// generates code in the conversion operator to call itself
|
|
|
|
// infinately.
|
2007-03-03 23:05:52 +03:00
|
|
|
auto_ptr_ref(Y* p, int): p_(p) {}
|
2007-02-28 17:35:28 +03:00
|
|
|
};
|
2007-03-03 22:48:48 +03:00
|
|
|
}
|
2007-03-07 17:26:49 +03:00
|
|
|
#endif
|
2007-02-28 17:35:28 +03:00
|
|
|
|
2007-03-03 22:48:48 +03:00
|
|
|
/** C++98 Standard Section 20.4.5 - Template class auto_ptr. */
|
2006-03-17 00:04:05 +03:00
|
|
|
template <class X>
|
|
|
|
class auto_ptr
|
|
|
|
{
|
2007-03-10 00:58:08 +03:00
|
|
|
#if !@KWSYS_NAMESPACE@_AUTO_PTR_REF
|
|
|
|
template <typename Y>
|
|
|
|
static inline auto_ptr<Y>& cast(auto_ptr<Y> const& a)
|
|
|
|
{ return const_cast<auto_ptr<Y>&>(a); }
|
2007-03-07 17:26:49 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/** The pointer to the object held. */
|
2006-03-17 00:04:05 +03:00
|
|
|
X* x_;
|
2007-03-07 17:26:49 +03:00
|
|
|
|
2006-03-17 00:04:05 +03:00
|
|
|
public:
|
2007-03-03 22:48:48 +03:00
|
|
|
/** The type of object held by the auto_ptr. */
|
2006-03-17 00:04:05 +03:00
|
|
|
typedef X element_type;
|
|
|
|
|
2007-03-03 22:48:48 +03:00
|
|
|
/** Construct from an auto_ptr holding a compatible object. This
|
|
|
|
transfers ownership to the newly constructed auto_ptr. */
|
2006-03-17 00:04:05 +03:00
|
|
|
template <class Y>
|
2007-03-10 00:58:08 +03:00
|
|
|
auto_ptr(auto_ptr<Y> @KWSYS_NAMESPACE@_AUTO_PTR_CONST& a) throw():
|
|
|
|
x_(@KWSYS_NAMESPACE@_AUTO_PTR_CAST(a).release())
|
2007-03-03 22:48:48 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Assign from an auto_ptr holding a compatible object. This
|
|
|
|
transfers ownership to the left-hand-side of the assignment. */
|
2006-03-17 00:04:05 +03:00
|
|
|
template <class Y>
|
2007-03-10 00:58:08 +03:00
|
|
|
auto_ptr& operator=(auto_ptr<Y> @KWSYS_NAMESPACE@_AUTO_PTR_CONST& a) throw()
|
2007-03-03 22:48:48 +03:00
|
|
|
{
|
2007-03-10 00:58:08 +03:00
|
|
|
this->reset(@KWSYS_NAMESPACE@_AUTO_PTR_CAST(a).release());
|
2007-03-03 22:48:48 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Explicitly construct from a raw pointer. This is typically
|
|
|
|
* called with the result of operator new. For example:
|
|
|
|
*
|
|
|
|
* auto_ptr<X> ptr(new X());
|
|
|
|
*/
|
|
|
|
explicit auto_ptr(X* p=0) throw(): x_(p)
|
|
|
|
{
|
|
|
|
}
|
2007-03-10 00:58:08 +03:00
|
|
|
|
2007-03-03 22:48:48 +03:00
|
|
|
/** Construct from another auto_ptr holding an object of the same
|
|
|
|
type. This transfers ownership to the newly constructed
|
|
|
|
auto_ptr. */
|
2007-03-10 00:58:08 +03:00
|
|
|
auto_ptr(auto_ptr @KWSYS_NAMESPACE@_AUTO_PTR_CONST& a) throw():
|
|
|
|
x_(@KWSYS_NAMESPACE@_AUTO_PTR_CAST(a).release())
|
2007-03-03 22:48:48 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Assign from another auto_ptr holding an object of the same type.
|
|
|
|
This transfers ownership to the newly constructed auto_ptr. */
|
2007-03-10 00:58:08 +03:00
|
|
|
auto_ptr& operator=(auto_ptr @KWSYS_NAMESPACE@_AUTO_PTR_CONST& a) throw()
|
2007-03-03 22:48:48 +03:00
|
|
|
{
|
2007-03-10 00:58:08 +03:00
|
|
|
this->reset(@KWSYS_NAMESPACE@_AUTO_PTR_CAST(a).release());
|
2007-03-03 22:48:48 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Destruct and delete the object held. */
|
|
|
|
~auto_ptr() throw()
|
|
|
|
{
|
|
|
|
// Assume object destructor is nothrow.
|
|
|
|
delete this->x_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Dereference and return a reference to the object held. */
|
|
|
|
X& operator*() const throw()
|
|
|
|
{
|
|
|
|
return *this->x_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return a pointer to the object held. */
|
|
|
|
X* operator->() const throw()
|
|
|
|
{
|
|
|
|
return this->x_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return a pointer to the object held. */
|
|
|
|
X* get() const throw()
|
|
|
|
{
|
|
|
|
return this->x_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return a pointer to the object held and reset to hold no object.
|
|
|
|
This transfers ownership to the caller. */
|
|
|
|
X* release() throw()
|
|
|
|
{
|
|
|
|
X* x = this->x_;
|
|
|
|
this->x_ = 0;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Assume ownership of the given object. The object previously
|
|
|
|
held is deleted. */
|
|
|
|
void reset(X* p=0) throw()
|
|
|
|
{
|
|
|
|
if(this->x_ != p)
|
|
|
|
{
|
|
|
|
// Assume object destructor is nothrow.
|
|
|
|
delete this->x_;
|
|
|
|
this->x_ = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Convert to an auto_ptr holding an object of a compatible type.
|
|
|
|
This transfers ownership to the returned auto_ptr. */
|
|
|
|
template <class Y> operator auto_ptr<Y>() throw()
|
|
|
|
{
|
|
|
|
return auto_ptr<Y>(this->release());
|
|
|
|
}
|
|
|
|
|
2007-03-07 17:26:49 +03:00
|
|
|
#if @KWSYS_NAMESPACE@_AUTO_PTR_REF
|
|
|
|
/** Construct from an auto_ptr_ref. This is used when the
|
|
|
|
constructor argument is a call to a function returning an
|
|
|
|
auto_ptr. */
|
|
|
|
auto_ptr(detail::auto_ptr_ref<X> r) throw(): x_(r.p_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-03-03 22:48:48 +03:00
|
|
|
/** Assign from an auto_ptr_ref. This is used when a function
|
|
|
|
returning an auto_ptr is passed on the right-hand-side of an
|
|
|
|
assignment. */
|
|
|
|
auto_ptr& operator=(detail::auto_ptr_ref<X> r) throw()
|
|
|
|
{
|
2007-03-03 23:05:52 +03:00
|
|
|
this->reset(r.p_);
|
2007-03-03 22:48:48 +03:00
|
|
|
return *this;
|
|
|
|
}
|
2007-03-07 17:26:49 +03:00
|
|
|
|
|
|
|
/** Convert to an auto_ptr_ref. This is used when a function
|
|
|
|
returning an auto_ptr is the argument to the constructor of
|
|
|
|
another auto_ptr. */
|
|
|
|
template <class Y> operator detail::auto_ptr_ref<Y>() throw()
|
|
|
|
{
|
|
|
|
return detail::auto_ptr_ref<Y>(this->release(), 1);
|
|
|
|
}
|
|
|
|
#endif
|
2006-03-17 00:04:05 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace @KWSYS_NAMESPACE@
|
|
|
|
|
|
|
|
#endif
|