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