1#ifndef BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED
2#define BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED
3
4// (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
5// Copyright (c) 2001, 2002 Peter Dimov
6//
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)
10//
11// See http://www.boost.org/libs/smart_ptr/ for documentation.
12
13#include <boost/smart_ptr/detail/requires_cxx11.hpp>
14#include <boost/config.hpp>
15#include <boost/assert.hpp>
16#include <boost/core/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/smart_ptr/detail/sp_noexcept.hpp>
20#include <boost/config/workaround.hpp>
21
22#ifndef BOOST_NO_AUTO_PTR
23# include <memory> // for std::auto_ptr
24#endif
25
26#if defined( BOOST_SP_DISABLE_DEPRECATED )
27#pragma GCC diagnostic push
28#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
29#endif
30
31namespace boost
32{
33
34// Debug hooks
35
36#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
37
38void sp_scalar_constructor_hook(void * p);
39void sp_scalar_destructor_hook(void * p);
40
41#endif
42
43// scoped_ptr mimics a built-in pointer except that it guarantees deletion
44// of the object pointed to, either on destruction of the scoped_ptr or via
45// an explicit reset(). scoped_ptr is a simple solution for simple needs;
46// use shared_ptr or std::auto_ptr if your needs are more complex.
47
48template<class T> class scoped_ptr // noncopyable
49{
50private:
51
52 T * px;
53
54 scoped_ptr(scoped_ptr const &);
55 scoped_ptr & operator=(scoped_ptr const &);
56
57 typedef scoped_ptr<T> this_type;
58
59 void operator==( scoped_ptr const& ) const;
60 void operator!=( scoped_ptr const& ) const;
61
62public:
63
64 typedef T element_type;
65
66 explicit scoped_ptr( T * p = 0 ) BOOST_SP_NOEXCEPT : px( p )
67 {
68#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
69 boost::sp_scalar_constructor_hook( px );
70#endif
71 }
72
73#ifndef BOOST_NO_AUTO_PTR
74
75 explicit scoped_ptr( std::auto_ptr<T> p ) BOOST_SP_NOEXCEPT : px( p.release() )
76 {
77#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
78 boost::sp_scalar_constructor_hook( px );
79#endif
80 }
81
82#endif
83
84 ~scoped_ptr() BOOST_SP_NOEXCEPT
85 {
86#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
87 boost::sp_scalar_destructor_hook( px );
88#endif
89 boost::checked_delete( px );
90 }
91
92 void reset(T * p = 0) BOOST_SP_NOEXCEPT_WITH_ASSERT
93 {
94 BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors
95 this_type(p).swap(*this);
96 }
97
98 T & operator*() const BOOST_SP_NOEXCEPT_WITH_ASSERT
99 {
100 BOOST_ASSERT( px != 0 );
101 return *px;
102 }
103
104 T * operator->() const BOOST_SP_NOEXCEPT_WITH_ASSERT
105 {
106 BOOST_ASSERT( px != 0 );
107 return px;
108 }
109
110 T * get() const BOOST_SP_NOEXCEPT
111 {
112 return px;
113 }
114
115// implicit conversion to "bool"
116#include <boost/smart_ptr/detail/operator_bool.hpp>
117
118 void swap(scoped_ptr & b) BOOST_SP_NOEXCEPT
119 {
120 T * tmp = b.px;
121 b.px = px;
122 px = tmp;
123 }
124};
125
126#if !defined( BOOST_NO_CXX11_NULLPTR )
127
128template<class T> inline bool operator==( scoped_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT
129{
130 return p.get() == 0;
131}
132
133template<class T> inline bool operator==( boost::detail::sp_nullptr_t, scoped_ptr<T> const & p ) BOOST_SP_NOEXCEPT
134{
135 return p.get() == 0;
136}
137
138template<class T> inline bool operator!=( scoped_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT
139{
140 return p.get() != 0;
141}
142
143template<class T> inline bool operator!=( boost::detail::sp_nullptr_t, scoped_ptr<T> const & p ) BOOST_SP_NOEXCEPT
144{
145 return p.get() != 0;
146}
147
148#endif
149
150template<class T> inline void swap(scoped_ptr<T> & a, scoped_ptr<T> & b) BOOST_SP_NOEXCEPT
151{
152 a.swap(b);
153}
154
155// get_pointer(p) is a generic way to say p.get()
156
157template<class T> inline T * get_pointer(scoped_ptr<T> const & p) BOOST_SP_NOEXCEPT
158{
159 return p.get();
160}
161
162} // namespace boost
163
164#if defined( BOOST_SP_DISABLE_DEPRECATED )
165#pragma GCC diagnostic pop
166#endif
167
168#endif // #ifndef BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED
169

source code of boost/libs/smart_ptr/include/boost/smart_ptr/scoped_ptr.hpp