Chris@16: #ifndef BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED Chris@16: #define BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED Chris@16: Chris@16: // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999. Chris@16: // Copyright (c) 2001, 2002 Peter Dimov Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // http://www.boost.org/libs/smart_ptr/scoped_ptr.htm Chris@16: // Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #ifndef BOOST_NO_AUTO_PTR Chris@16: # include // for std::auto_ptr Chris@16: #endif Chris@16: Chris@16: namespace boost Chris@16: { Chris@16: Chris@16: // Debug hooks Chris@16: Chris@16: #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS) Chris@16: Chris@16: void sp_scalar_constructor_hook(void * p); Chris@16: void sp_scalar_destructor_hook(void * p); Chris@16: Chris@16: #endif Chris@16: Chris@16: // scoped_ptr mimics a built-in pointer except that it guarantees deletion Chris@16: // of the object pointed to, either on destruction of the scoped_ptr or via Chris@16: // an explicit reset(). scoped_ptr is a simple solution for simple needs; Chris@16: // use shared_ptr or std::auto_ptr if your needs are more complex. Chris@16: Chris@16: template class scoped_ptr // noncopyable Chris@16: { Chris@16: private: Chris@16: Chris@16: T * px; Chris@16: Chris@16: scoped_ptr(scoped_ptr const &); Chris@16: scoped_ptr & operator=(scoped_ptr const &); Chris@16: Chris@16: typedef scoped_ptr this_type; Chris@16: Chris@16: void operator==( scoped_ptr const& ) const; Chris@16: void operator!=( scoped_ptr const& ) const; Chris@16: Chris@16: public: Chris@16: Chris@16: typedef T element_type; Chris@16: Chris@16: explicit scoped_ptr( T * p = 0 ): px( p ) // never throws Chris@16: { Chris@16: #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS) Chris@16: boost::sp_scalar_constructor_hook( px ); Chris@16: #endif Chris@16: } Chris@16: Chris@16: #ifndef BOOST_NO_AUTO_PTR Chris@16: Chris@16: explicit scoped_ptr( std::auto_ptr p ) BOOST_NOEXCEPT : px( p.release() ) Chris@16: { Chris@16: #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS) Chris@16: boost::sp_scalar_constructor_hook( px ); Chris@16: #endif Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: ~scoped_ptr() // never throws Chris@16: { Chris@16: #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS) Chris@16: boost::sp_scalar_destructor_hook( px ); Chris@16: #endif Chris@16: boost::checked_delete( px ); Chris@16: } Chris@16: Chris@16: void reset(T * p = 0) // never throws Chris@16: { Chris@16: BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors Chris@16: this_type(p).swap(*this); Chris@16: } Chris@16: Chris@16: T & operator*() const // never throws Chris@16: { Chris@16: BOOST_ASSERT( px != 0 ); Chris@16: return *px; Chris@16: } Chris@16: Chris@16: T * operator->() const // never throws Chris@16: { Chris@16: BOOST_ASSERT( px != 0 ); Chris@16: return px; Chris@16: } Chris@16: Chris@16: T * get() const BOOST_NOEXCEPT Chris@16: { Chris@16: return px; Chris@16: } Chris@16: Chris@16: // implicit conversion to "bool" Chris@16: #include Chris@16: Chris@16: void swap(scoped_ptr & b) BOOST_NOEXCEPT Chris@16: { Chris@16: T * tmp = b.px; Chris@16: b.px = px; Chris@16: px = tmp; Chris@16: } Chris@16: }; Chris@16: Chris@16: #if !defined( BOOST_NO_CXX11_NULLPTR ) Chris@16: Chris@16: template inline bool operator==( scoped_ptr const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT Chris@16: { Chris@16: return p.get() == 0; Chris@16: } Chris@16: Chris@16: template inline bool operator==( boost::detail::sp_nullptr_t, scoped_ptr const & p ) BOOST_NOEXCEPT Chris@16: { Chris@16: return p.get() == 0; Chris@16: } Chris@16: Chris@16: template inline bool operator!=( scoped_ptr const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT Chris@16: { Chris@16: return p.get() != 0; Chris@16: } Chris@16: Chris@16: template inline bool operator!=( boost::detail::sp_nullptr_t, scoped_ptr const & p ) BOOST_NOEXCEPT Chris@16: { Chris@16: return p.get() != 0; Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: template inline void swap(scoped_ptr & a, scoped_ptr & b) BOOST_NOEXCEPT Chris@16: { Chris@16: a.swap(b); Chris@16: } Chris@16: Chris@16: // get_pointer(p) is a generic way to say p.get() Chris@16: Chris@16: template inline T * get_pointer(scoped_ptr const & p) BOOST_NOEXCEPT Chris@16: { Chris@16: return p.get(); Chris@16: } Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #endif // #ifndef BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED