blob: 08fc4bda1b7ea72cbde2c990f21d71d19f1c290b [file] [log] [blame]
Jeff Thompson3d613fd2013-10-15 15:39:04 -07001#ifndef NDNBOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED
2#define NDNBOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED
Jeff Thompsonf03af342013-08-07 10:32:25 -07003
4//
5// weak_ptr.hpp
6//
7// Copyright (c) 2001, 2002, 2003 Peter Dimov
8//
9// Distributed under the Boost Software License, Version 1.0. (See
10// accompanying file LICENSE_1_0.txt or copy at
11// http://www.boost.org/LICENSE_1_0.txt)
12//
13// See http://www.boost.org/libs/smart_ptr/weak_ptr.htm for documentation.
14//
15
16#include <memory> // boost.TR1 include order fix
17#include <ndnboost/smart_ptr/detail/shared_count.hpp>
18#include <ndnboost/smart_ptr/shared_ptr.hpp>
19
20namespace ndnboost
21{
22
23template<class T> class weak_ptr
24{
25private:
26
27 // Borland 5.5.1 specific workarounds
28 typedef weak_ptr<T> this_type;
29
30public:
31
32 typedef typename ndnboost::detail::sp_element< T >::type element_type;
33
Jeff Thompson3d613fd2013-10-15 15:39:04 -070034 weak_ptr() NDNBOOST_NOEXCEPT : px(0), pn() // never throws in 1.30+
Jeff Thompsonf03af342013-08-07 10:32:25 -070035 {
36 }
37
38// generated copy constructor, assignment, destructor are fine...
39
Jeff Thompson3d613fd2013-10-15 15:39:04 -070040#if !defined( NDNBOOST_NO_CXX11_RVALUE_REFERENCES )
Jeff Thompsonf03af342013-08-07 10:32:25 -070041
42// ... except in C++0x, move disables the implicit copy
43
Jeff Thompson3d613fd2013-10-15 15:39:04 -070044 weak_ptr( weak_ptr const & r ) NDNBOOST_NOEXCEPT : px( r.px ), pn( r.pn )
Jeff Thompsonf03af342013-08-07 10:32:25 -070045 {
46 }
47
Jeff Thompson3d613fd2013-10-15 15:39:04 -070048 weak_ptr & operator=( weak_ptr const & r ) NDNBOOST_NOEXCEPT
Jeff Thompsonf03af342013-08-07 10:32:25 -070049 {
50 px = r.px;
51 pn = r.pn;
52 return *this;
53 }
54
55#endif
56
57//
58// The "obvious" converting constructor implementation:
59//
60// template<class Y>
61// weak_ptr(weak_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
62// {
63// }
64//
65// has a serious problem.
66//
67// r.px may already have been invalidated. The px(r.px)
68// conversion may require access to *r.px (virtual inheritance).
69//
70// It is not possible to avoid spurious access violations since
71// in multithreaded programs r.px may be invalidated at any point.
72//
73
74 template<class Y>
Jeff Thompson3d613fd2013-10-15 15:39:04 -070075#if !defined( NDNBOOST_SP_NO_SP_CONVERTIBLE )
Jeff Thompsonf03af342013-08-07 10:32:25 -070076
77 weak_ptr( weak_ptr<Y> const & r, typename ndnboost::detail::sp_enable_if_convertible<Y,T>::type = ndnboost::detail::sp_empty() )
78
79#else
80
81 weak_ptr( weak_ptr<Y> const & r )
82
83#endif
Jeff Thompson3d613fd2013-10-15 15:39:04 -070084 NDNBOOST_NOEXCEPT : px(r.lock().get()), pn(r.pn)
Jeff Thompsonf03af342013-08-07 10:32:25 -070085 {
86 ndnboost::detail::sp_assert_convertible< Y, T >();
87 }
88
Jeff Thompson3d613fd2013-10-15 15:39:04 -070089#if !defined( NDNBOOST_NO_CXX11_RVALUE_REFERENCES )
Jeff Thompsonf03af342013-08-07 10:32:25 -070090
91 template<class Y>
Jeff Thompson3d613fd2013-10-15 15:39:04 -070092#if !defined( NDNBOOST_SP_NO_SP_CONVERTIBLE )
Jeff Thompsonf03af342013-08-07 10:32:25 -070093
94 weak_ptr( weak_ptr<Y> && r, typename ndnboost::detail::sp_enable_if_convertible<Y,T>::type = ndnboost::detail::sp_empty() )
95
96#else
97
98 weak_ptr( weak_ptr<Y> && r )
99
100#endif
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700101 NDNBOOST_NOEXCEPT : px( r.lock().get() ), pn( static_cast< ndnboost::detail::weak_count && >( r.pn ) )
Jeff Thompsonf03af342013-08-07 10:32:25 -0700102 {
103 ndnboost::detail::sp_assert_convertible< Y, T >();
104 r.px = 0;
105 }
106
107 // for better efficiency in the T == Y case
108 weak_ptr( weak_ptr && r )
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700109 NDNBOOST_NOEXCEPT : px( r.px ), pn( static_cast< ndnboost::detail::weak_count && >( r.pn ) )
Jeff Thompsonf03af342013-08-07 10:32:25 -0700110 {
111 r.px = 0;
112 }
113
114 // for better efficiency in the T == Y case
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700115 weak_ptr & operator=( weak_ptr && r ) NDNBOOST_NOEXCEPT
Jeff Thompsonf03af342013-08-07 10:32:25 -0700116 {
117 this_type( static_cast< weak_ptr && >( r ) ).swap( *this );
118 return *this;
119 }
120
121
122#endif
123
124 template<class Y>
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700125#if !defined( NDNBOOST_SP_NO_SP_CONVERTIBLE )
Jeff Thompsonf03af342013-08-07 10:32:25 -0700126
127 weak_ptr( shared_ptr<Y> const & r, typename ndnboost::detail::sp_enable_if_convertible<Y,T>::type = ndnboost::detail::sp_empty() )
128
129#else
130
131 weak_ptr( shared_ptr<Y> const & r )
132
133#endif
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700134 NDNBOOST_NOEXCEPT : px( r.px ), pn( r.pn )
Jeff Thompsonf03af342013-08-07 10:32:25 -0700135 {
136 ndnboost::detail::sp_assert_convertible< Y, T >();
137 }
138
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700139#if !defined(NDNBOOST_MSVC) || (NDNBOOST_MSVC >= 1300)
Jeff Thompsonf03af342013-08-07 10:32:25 -0700140
141 template<class Y>
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700142 weak_ptr & operator=( weak_ptr<Y> const & r ) NDNBOOST_NOEXCEPT
Jeff Thompsonf03af342013-08-07 10:32:25 -0700143 {
144 ndnboost::detail::sp_assert_convertible< Y, T >();
145
146 px = r.lock().get();
147 pn = r.pn;
148
149 return *this;
150 }
151
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700152#if !defined( NDNBOOST_NO_CXX11_RVALUE_REFERENCES )
Jeff Thompsonf03af342013-08-07 10:32:25 -0700153
154 template<class Y>
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700155 weak_ptr & operator=( weak_ptr<Y> && r ) NDNBOOST_NOEXCEPT
Jeff Thompsonf03af342013-08-07 10:32:25 -0700156 {
157 this_type( static_cast< weak_ptr<Y> && >( r ) ).swap( *this );
158 return *this;
159 }
160
161#endif
162
163 template<class Y>
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700164 weak_ptr & operator=( shared_ptr<Y> const & r ) NDNBOOST_NOEXCEPT
Jeff Thompsonf03af342013-08-07 10:32:25 -0700165 {
166 ndnboost::detail::sp_assert_convertible< Y, T >();
167
168 px = r.px;
169 pn = r.pn;
170
171 return *this;
172 }
173
174#endif
175
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700176 shared_ptr<T> lock() const NDNBOOST_NOEXCEPT
Jeff Thompsonf03af342013-08-07 10:32:25 -0700177 {
178 return shared_ptr<T>( *this, ndnboost::detail::sp_nothrow_tag() );
179 }
180
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700181 long use_count() const NDNBOOST_NOEXCEPT
Jeff Thompsonf03af342013-08-07 10:32:25 -0700182 {
183 return pn.use_count();
184 }
185
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700186 bool expired() const NDNBOOST_NOEXCEPT
Jeff Thompsonf03af342013-08-07 10:32:25 -0700187 {
188 return pn.use_count() == 0;
189 }
190
191 bool _empty() const // extension, not in std::weak_ptr
192 {
193 return pn.empty();
194 }
195
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700196 void reset() NDNBOOST_NOEXCEPT // never throws in 1.30+
Jeff Thompsonf03af342013-08-07 10:32:25 -0700197 {
198 this_type().swap(*this);
199 }
200
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700201 void swap(this_type & other) NDNBOOST_NOEXCEPT
Jeff Thompsonf03af342013-08-07 10:32:25 -0700202 {
203 std::swap(px, other.px);
204 pn.swap(other.pn);
205 }
206
207 template<typename Y>
208 void _internal_aliasing_assign(weak_ptr<Y> const & r, element_type * px2)
209 {
210 px = px2;
211 pn = r.pn;
212 }
213
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700214 template<class Y> bool owner_before( weak_ptr<Y> const & rhs ) const NDNBOOST_NOEXCEPT
Jeff Thompsonf03af342013-08-07 10:32:25 -0700215 {
216 return pn < rhs.pn;
217 }
218
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700219 template<class Y> bool owner_before( shared_ptr<Y> const & rhs ) const NDNBOOST_NOEXCEPT
Jeff Thompsonf03af342013-08-07 10:32:25 -0700220 {
221 return pn < rhs.pn;
222 }
223
224// Tasteless as this may seem, making all members public allows member templates
225// to work in the absence of member template friends. (Matthew Langston)
226
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700227#ifndef NDNBOOST_NO_MEMBER_TEMPLATE_FRIENDS
Jeff Thompsonf03af342013-08-07 10:32:25 -0700228
229private:
230
231 template<class Y> friend class weak_ptr;
232 template<class Y> friend class shared_ptr;
233
234#endif
235
236 element_type * px; // contained pointer
237 ndnboost::detail::weak_count pn; // reference counter
238
239}; // weak_ptr
240
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700241template<class T, class U> inline bool operator<(weak_ptr<T> const & a, weak_ptr<U> const & b) NDNBOOST_NOEXCEPT
Jeff Thompsonf03af342013-08-07 10:32:25 -0700242{
243 return a.owner_before( b );
244}
245
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700246template<class T> void swap(weak_ptr<T> & a, weak_ptr<T> & b) NDNBOOST_NOEXCEPT
Jeff Thompsonf03af342013-08-07 10:32:25 -0700247{
248 a.swap(b);
249}
250
251} // namespace ndnboost
252
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700253#endif // #ifndef NDNBOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED