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/smart_ptr/detail/sp_disable_deprecated.hpp>
19 #include <boost/detail/workaround.hpp>
21 #ifndef BOOST_NO_AUTO_PTR
22 # include <memory> // for std::auto_ptr
25 #if defined( BOOST_SP_DISABLE_DEPRECATED )
26 #pragma GCC diagnostic push
27 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
35 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
37 void sp_scalar_constructor_hook(void * p);
38 void sp_scalar_destructor_hook(void * p);
42 // scoped_ptr mimics a built-in pointer except that it guarantees deletion
43 // of the object pointed to, either on destruction of the scoped_ptr or via
44 // an explicit reset(). scoped_ptr is a simple solution for simple needs;
45 // use shared_ptr or std::auto_ptr if your needs are more complex.
47 template<class T> class scoped_ptr // noncopyable
53 scoped_ptr(scoped_ptr const &);
54 scoped_ptr & operator=(scoped_ptr const &);
56 typedef scoped_ptr<T> this_type;
58 void operator==( scoped_ptr const& ) const;
59 void operator!=( scoped_ptr const& ) const;
63 typedef T element_type;
65 explicit scoped_ptr( T * p = 0 ): px( p ) // never throws
67 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
68 boost::sp_scalar_constructor_hook( px );
72 #ifndef BOOST_NO_AUTO_PTR
74 explicit scoped_ptr( std::auto_ptr<T> p ) BOOST_NOEXCEPT : px( p.release() )
76 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
77 boost::sp_scalar_constructor_hook( px );
83 ~scoped_ptr() // never throws
85 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
86 boost::sp_scalar_destructor_hook( px );
88 boost::checked_delete( px );
91 void reset(T * p = 0) // never throws
93 BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors
94 this_type(p).swap(*this);
97 T & operator*() const // never throws
99 BOOST_ASSERT( px != 0 );
103 T * operator->() const // never throws
105 BOOST_ASSERT( px != 0 );
109 T * get() const BOOST_NOEXCEPT
114 // implicit conversion to "bool"
115 #include <boost/smart_ptr/detail/operator_bool.hpp>
117 void swap(scoped_ptr & b) BOOST_NOEXCEPT
125 #if !defined( BOOST_NO_CXX11_NULLPTR )
127 template<class T> inline bool operator==( scoped_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT
132 template<class T> inline bool operator==( boost::detail::sp_nullptr_t, scoped_ptr<T> const & p ) BOOST_NOEXCEPT
137 template<class T> inline bool operator!=( scoped_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT
142 template<class T> inline bool operator!=( boost::detail::sp_nullptr_t, scoped_ptr<T> const & p ) BOOST_NOEXCEPT
149 template<class T> inline void swap(scoped_ptr<T> & a, scoped_ptr<T> & b) BOOST_NOEXCEPT
154 // get_pointer(p) is a generic way to say p.get()
156 template<class T> inline T * get_pointer(scoped_ptr<T> const & p) BOOST_NOEXCEPT
163 #if defined( BOOST_SP_DISABLE_DEPRECATED )
164 #pragma GCC diagnostic pop
167 #endif // #ifndef BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED