mirror of
https://github.com/boostorg/smart_ptr.git
synced 2025-11-16 15:29:26 +01:00
@@ -40,7 +40,7 @@ template<class T> class scoped_ptr // noncopyable
|
||||
{
|
||||
private:
|
||||
|
||||
T * ptr;
|
||||
T * px;
|
||||
|
||||
scoped_ptr(scoped_ptr const &);
|
||||
scoped_ptr & operator=(scoped_ptr const &);
|
||||
@@ -54,19 +54,19 @@ public:
|
||||
|
||||
typedef T element_type;
|
||||
|
||||
explicit scoped_ptr(T * p = 0): ptr(p) // never throws
|
||||
explicit scoped_ptr( T * p = 0 ): px( p ) // never throws
|
||||
{
|
||||
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
|
||||
boost::sp_scalar_constructor_hook(ptr);
|
||||
boost::sp_scalar_constructor_hook( px );
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifndef BOOST_NO_AUTO_PTR
|
||||
|
||||
explicit scoped_ptr(std::auto_ptr<T> p): ptr(p.release()) // never throws
|
||||
explicit scoped_ptr( std::auto_ptr<T> p ): px( p.release() ) // never throws
|
||||
{
|
||||
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
|
||||
boost::sp_scalar_constructor_hook(ptr);
|
||||
boost::sp_scalar_constructor_hook( px );
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -75,71 +75,42 @@ public:
|
||||
~scoped_ptr() // never throws
|
||||
{
|
||||
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
|
||||
boost::sp_scalar_destructor_hook(ptr);
|
||||
boost::sp_scalar_destructor_hook( px );
|
||||
#endif
|
||||
boost::checked_delete(ptr);
|
||||
boost::checked_delete( px );
|
||||
}
|
||||
|
||||
void reset(T * p = 0) // never throws
|
||||
{
|
||||
BOOST_ASSERT(p == 0 || p != ptr); // catch self-reset errors
|
||||
BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors
|
||||
this_type(p).swap(*this);
|
||||
}
|
||||
|
||||
T & operator*() const // never throws
|
||||
{
|
||||
BOOST_ASSERT(ptr != 0);
|
||||
return *ptr;
|
||||
BOOST_ASSERT( px != 0 );
|
||||
return *px;
|
||||
}
|
||||
|
||||
T * operator->() const // never throws
|
||||
{
|
||||
BOOST_ASSERT(ptr != 0);
|
||||
return ptr;
|
||||
BOOST_ASSERT( px != 0 );
|
||||
return px;
|
||||
}
|
||||
|
||||
T * get() const // never throws
|
||||
{
|
||||
return ptr;
|
||||
return px;
|
||||
}
|
||||
|
||||
// implicit conversion to "bool"
|
||||
|
||||
#if defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x530)
|
||||
|
||||
operator bool () const
|
||||
{
|
||||
return ptr != 0;
|
||||
}
|
||||
|
||||
#elif defined(__MWERKS__) && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
|
||||
typedef T * (this_type::*unspecified_bool_type)() const;
|
||||
|
||||
operator unspecified_bool_type() const // never throws
|
||||
{
|
||||
return ptr == 0? 0: &this_type::get;
|
||||
}
|
||||
|
||||
#else
|
||||
typedef T * this_type::*unspecified_bool_type;
|
||||
|
||||
operator unspecified_bool_type() const // never throws
|
||||
{
|
||||
return ptr == 0? 0: &this_type::ptr;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
bool operator! () const // never throws
|
||||
{
|
||||
return ptr == 0;
|
||||
}
|
||||
// implicit conversion to "bool"
|
||||
#include <boost/smart_ptr/detail/operator_bool.hpp>
|
||||
|
||||
void swap(scoped_ptr & b) // never throws
|
||||
{
|
||||
T * tmp = b.ptr;
|
||||
b.ptr = ptr;
|
||||
ptr = tmp;
|
||||
T * tmp = b.px;
|
||||
b.px = px;
|
||||
px = tmp;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user