ENH: Enabled support for use_auto_ptr(get_auto_ptr()) syntax on HP compiler.

This commit is contained in:
Brad King 2007-03-07 09:26:49 -05:00
parent c0ee369379
commit 4148fedbf0
2 changed files with 47 additions and 21 deletions

View File

@ -16,11 +16,25 @@
#include <@KWSYS_NAMESPACE@/Configure.hxx> #include <@KWSYS_NAMESPACE@/Configure.hxx>
// The HP compiler cannot handle the conversions necessary to use
// auto_ptr_ref to pass an auto_ptr returned from one function
// directly to another function as in use_auto_ptr(get_auto_ptr()).
// We instead use const_cast to achieve the syntax on that platform.
// 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.
#if defined(__HP_aCC)
# define @KWSYS_NAMESPACE@_AUTO_PTR_REF 0
#else
# define @KWSYS_NAMESPACE@_AUTO_PTR_REF 1
#endif
namespace @KWSYS_NAMESPACE@ namespace @KWSYS_NAMESPACE@
{ {
template <class X> class auto_ptr; template <class X> class auto_ptr;
#if @KWSYS_NAMESPACE@_AUTO_PTR_REF
namespace detail namespace detail
{ {
// The auto_ptr_ref template is supposed to be a private member of // The auto_ptr_ref template is supposed to be a private member of
@ -38,12 +52,24 @@ template <class Y> struct auto_ptr_ref
auto_ptr_ref(Y* p, int): p_(p) {} auto_ptr_ref(Y* p, int): p_(p) {}
}; };
} }
#endif
/** C++98 Standard Section 20.4.5 - Template class auto_ptr. */ /** C++98 Standard Section 20.4.5 - Template class auto_ptr. */
template <class X> template <class X>
class auto_ptr class auto_ptr
{ {
#if @KWSYS_NAMESPACE@_AUTO_PTR_REF
typedef auto_ptr auto_ptr_source;
static inline auto_ptr& cast(auto_ptr_source& a) { return a; }
#else
typedef auto_ptr const auto_ptr_source;
static inline auto_ptr& cast(auto_ptr_source& a)
{ return const_cast<auto_ptr&>(a); }
#endif
/** The pointer to the object held. */
X* x_; X* x_;
public: public:
/** The type of object held by the auto_ptr. */ /** The type of object held by the auto_ptr. */
typedef X element_type; typedef X element_type;
@ -73,19 +99,19 @@ public:
explicit auto_ptr(X* p=0) throw(): x_(p) explicit auto_ptr(X* p=0) throw(): x_(p)
{ {
} }
/** Construct from another auto_ptr holding an object of the same /** Construct from another auto_ptr holding an object of the same
type. This transfers ownership to the newly constructed type. This transfers ownership to the newly constructed
auto_ptr. */ auto_ptr. */
auto_ptr(auto_ptr& a) throw(): x_(a.release()) auto_ptr(auto_ptr_source& a) throw(): x_(cast(a).release())
{ {
} }
/** Assign from another auto_ptr holding an object of the same type. /** Assign from another auto_ptr holding an object of the same type.
This transfers ownership to the newly constructed auto_ptr. */ This transfers ownership to the newly constructed auto_ptr. */
auto_ptr& operator=(auto_ptr& a) throw() auto_ptr& operator=(auto_ptr_source& a) throw()
{ {
this->reset(a.release()); this->reset(cast(a).release());
return *this; return *this;
} }
@ -135,21 +161,6 @@ public:
} }
} }
/** 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_)
{
}
/** 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);
}
/** Convert to an auto_ptr holding an object of a compatible type. /** Convert to an auto_ptr holding an object of a compatible type.
This transfers ownership to the returned auto_ptr. */ This transfers ownership to the returned auto_ptr. */
template <class Y> operator auto_ptr<Y>() throw() template <class Y> operator auto_ptr<Y>() throw()
@ -157,6 +168,14 @@ public:
return auto_ptr<Y>(this->release()); return auto_ptr<Y>(this->release());
} }
#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_)
{
}
/** Assign from an auto_ptr_ref. This is used when a function /** 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 returning an auto_ptr is passed on the right-hand-side of an
assignment. */ assignment. */
@ -165,6 +184,15 @@ public:
this->reset(r.p_); this->reset(r.p_);
return *this; return *this;
} }
/** 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
}; };
} // namespace @KWSYS_NAMESPACE@ } // namespace @KWSYS_NAMESPACE@

View File

@ -115,14 +115,12 @@ int testAutoPtr(int, char*[])
"auto_ptr did not release ownership to called function"); "auto_ptr did not release ownership to called function");
} }
#if !defined(__HP_aCC)
{ {
int received = function_call(generate_auto_ptr_A()); int received = function_call(generate_auto_ptr_A());
ASSERT(received, ASSERT(received,
"auto_ptr in called function did not take ownership " "auto_ptr in called function did not take ownership "
"from factory function"); "from factory function");
} }
#endif
#if 0 #if 0
// Is this allowed by the standard? // Is this allowed by the standard?