1 #ifndef BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED
2 #define BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED
4 // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
5 // Copyright (c) 2001, 2002 Peter Dimov
7 // Distributed under the Boost Software License, Version 1.0. (See
8 // accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
11 // http://www.boost.org/libs/smart_ptr/scoped_ptr.htm
14 #include <boost/config.hpp>
15 #include <boost/assert.hpp>
16 #include <boost/checked_delete.hpp>
17 #include <boost/smart_ptr/detail/sp_nullptr_t.hpp>
18 #include <boost/detail/workaround.hpp>
20 #ifndef BOOST_NO_AUTO_PTR
21 # include <memory> // for std::auto_ptr
29 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
31 void sp_scalar_constructor_hook(void * p);
32 void sp_scalar_destructor_hook(void * p);
36 // scoped_ptr mimics a built-in pointer except that it guarantees deletion
37 // of the object pointed to, either on destruction of the scoped_ptr or via
38 // an explicit reset(). scoped_ptr is a simple solution for simple needs;
39 // use shared_ptr or std::auto_ptr if your needs are more complex.
41 template<class T> class scoped_ptr // noncopyable
47 scoped_ptr(scoped_ptr const &);
48 scoped_ptr & operator=(scoped_ptr const &);
50 typedef scoped_ptr<T> this_type;
52 void operator==( scoped_ptr const& ) const;
53 void operator!=( scoped_ptr const& ) const;
57 typedef T element_type;
59 explicit scoped_ptr( T * p = 0 ): px( p ) // never throws
61 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
62 boost::sp_scalar_constructor_hook( px );
66 #ifndef BOOST_NO_AUTO_PTR
68 explicit scoped_ptr( std::auto_ptr<T> p ) BOOST_NOEXCEPT : px( p.release() )
70 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
71 boost::sp_scalar_constructor_hook( px );
77 ~scoped_ptr() // never throws
79 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
80 boost::sp_scalar_destructor_hook( px );
82 boost::checked_delete( px );
85 void reset(T * p = 0) // never throws
87 BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors
88 this_type(p).swap(*this);
91 T & operator*() const // never throws
93 BOOST_ASSERT( px != 0 );
97 T * operator->() const // never throws
99 BOOST_ASSERT( px != 0 );
103 T * get() const BOOST_NOEXCEPT
108 // implicit conversion to "bool"
109 #include <boost/smart_ptr/detail/operator_bool.hpp>
111 void swap(scoped_ptr & b) BOOST_NOEXCEPT
119 #if !defined( BOOST_NO_CXX11_NULLPTR )
121 template<class T> inline bool operator==( scoped_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT
126 template<class T> inline bool operator==( boost::detail::sp_nullptr_t, scoped_ptr<T> const & p ) BOOST_NOEXCEPT
131 template<class T> inline bool operator!=( scoped_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT
136 template<class T> inline bool operator!=( boost::detail::sp_nullptr_t, scoped_ptr<T> const & p ) BOOST_NOEXCEPT
143 template<class T> inline void swap(scoped_ptr<T> & a, scoped_ptr<T> & b) BOOST_NOEXCEPT
148 // get_pointer(p) is a generic way to say p.get()
150 template<class T> inline T * get_pointer(scoped_ptr<T> const & p) BOOST_NOEXCEPT
157 #endif // #ifndef BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED