Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 1 | #ifndef NDNBOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED |
| 2 | #define NDNBOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED |
Jeff Thompson | f03af34 | 2013-08-07 10:32:25 -0700 | [diff] [blame] | 3 | |
| 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 | |
| 20 | namespace ndnboost |
| 21 | { |
| 22 | |
| 23 | template<class T> class weak_ptr |
| 24 | { |
| 25 | private: |
| 26 | |
| 27 | // Borland 5.5.1 specific workarounds |
| 28 | typedef weak_ptr<T> this_type; |
| 29 | |
| 30 | public: |
| 31 | |
| 32 | typedef typename ndnboost::detail::sp_element< T >::type element_type; |
| 33 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 34 | weak_ptr() NDNBOOST_NOEXCEPT : px(0), pn() // never throws in 1.30+ |
Jeff Thompson | f03af34 | 2013-08-07 10:32:25 -0700 | [diff] [blame] | 35 | { |
| 36 | } |
| 37 | |
| 38 | // generated copy constructor, assignment, destructor are fine... |
| 39 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 40 | #if !defined( NDNBOOST_NO_CXX11_RVALUE_REFERENCES ) |
Jeff Thompson | f03af34 | 2013-08-07 10:32:25 -0700 | [diff] [blame] | 41 | |
| 42 | // ... except in C++0x, move disables the implicit copy |
| 43 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 44 | weak_ptr( weak_ptr const & r ) NDNBOOST_NOEXCEPT : px( r.px ), pn( r.pn ) |
Jeff Thompson | f03af34 | 2013-08-07 10:32:25 -0700 | [diff] [blame] | 45 | { |
| 46 | } |
| 47 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 48 | weak_ptr & operator=( weak_ptr const & r ) NDNBOOST_NOEXCEPT |
Jeff Thompson | f03af34 | 2013-08-07 10:32:25 -0700 | [diff] [blame] | 49 | { |
| 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 Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 75 | #if !defined( NDNBOOST_SP_NO_SP_CONVERTIBLE ) |
Jeff Thompson | f03af34 | 2013-08-07 10:32:25 -0700 | [diff] [blame] | 76 | |
| 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 Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 84 | NDNBOOST_NOEXCEPT : px(r.lock().get()), pn(r.pn) |
Jeff Thompson | f03af34 | 2013-08-07 10:32:25 -0700 | [diff] [blame] | 85 | { |
| 86 | ndnboost::detail::sp_assert_convertible< Y, T >(); |
| 87 | } |
| 88 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 89 | #if !defined( NDNBOOST_NO_CXX11_RVALUE_REFERENCES ) |
Jeff Thompson | f03af34 | 2013-08-07 10:32:25 -0700 | [diff] [blame] | 90 | |
| 91 | template<class Y> |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 92 | #if !defined( NDNBOOST_SP_NO_SP_CONVERTIBLE ) |
Jeff Thompson | f03af34 | 2013-08-07 10:32:25 -0700 | [diff] [blame] | 93 | |
| 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 Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 101 | NDNBOOST_NOEXCEPT : px( r.lock().get() ), pn( static_cast< ndnboost::detail::weak_count && >( r.pn ) ) |
Jeff Thompson | f03af34 | 2013-08-07 10:32:25 -0700 | [diff] [blame] | 102 | { |
| 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 Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 109 | NDNBOOST_NOEXCEPT : px( r.px ), pn( static_cast< ndnboost::detail::weak_count && >( r.pn ) ) |
Jeff Thompson | f03af34 | 2013-08-07 10:32:25 -0700 | [diff] [blame] | 110 | { |
| 111 | r.px = 0; |
| 112 | } |
| 113 | |
| 114 | // for better efficiency in the T == Y case |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 115 | weak_ptr & operator=( weak_ptr && r ) NDNBOOST_NOEXCEPT |
Jeff Thompson | f03af34 | 2013-08-07 10:32:25 -0700 | [diff] [blame] | 116 | { |
| 117 | this_type( static_cast< weak_ptr && >( r ) ).swap( *this ); |
| 118 | return *this; |
| 119 | } |
| 120 | |
| 121 | |
| 122 | #endif |
| 123 | |
| 124 | template<class Y> |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 125 | #if !defined( NDNBOOST_SP_NO_SP_CONVERTIBLE ) |
Jeff Thompson | f03af34 | 2013-08-07 10:32:25 -0700 | [diff] [blame] | 126 | |
| 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 Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 134 | NDNBOOST_NOEXCEPT : px( r.px ), pn( r.pn ) |
Jeff Thompson | f03af34 | 2013-08-07 10:32:25 -0700 | [diff] [blame] | 135 | { |
| 136 | ndnboost::detail::sp_assert_convertible< Y, T >(); |
| 137 | } |
| 138 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 139 | #if !defined(NDNBOOST_MSVC) || (NDNBOOST_MSVC >= 1300) |
Jeff Thompson | f03af34 | 2013-08-07 10:32:25 -0700 | [diff] [blame] | 140 | |
| 141 | template<class Y> |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 142 | weak_ptr & operator=( weak_ptr<Y> const & r ) NDNBOOST_NOEXCEPT |
Jeff Thompson | f03af34 | 2013-08-07 10:32:25 -0700 | [diff] [blame] | 143 | { |
| 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 Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 152 | #if !defined( NDNBOOST_NO_CXX11_RVALUE_REFERENCES ) |
Jeff Thompson | f03af34 | 2013-08-07 10:32:25 -0700 | [diff] [blame] | 153 | |
| 154 | template<class Y> |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 155 | weak_ptr & operator=( weak_ptr<Y> && r ) NDNBOOST_NOEXCEPT |
Jeff Thompson | f03af34 | 2013-08-07 10:32:25 -0700 | [diff] [blame] | 156 | { |
| 157 | this_type( static_cast< weak_ptr<Y> && >( r ) ).swap( *this ); |
| 158 | return *this; |
| 159 | } |
| 160 | |
| 161 | #endif |
| 162 | |
| 163 | template<class Y> |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 164 | weak_ptr & operator=( shared_ptr<Y> const & r ) NDNBOOST_NOEXCEPT |
Jeff Thompson | f03af34 | 2013-08-07 10:32:25 -0700 | [diff] [blame] | 165 | { |
| 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 Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 176 | shared_ptr<T> lock() const NDNBOOST_NOEXCEPT |
Jeff Thompson | f03af34 | 2013-08-07 10:32:25 -0700 | [diff] [blame] | 177 | { |
| 178 | return shared_ptr<T>( *this, ndnboost::detail::sp_nothrow_tag() ); |
| 179 | } |
| 180 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 181 | long use_count() const NDNBOOST_NOEXCEPT |
Jeff Thompson | f03af34 | 2013-08-07 10:32:25 -0700 | [diff] [blame] | 182 | { |
| 183 | return pn.use_count(); |
| 184 | } |
| 185 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 186 | bool expired() const NDNBOOST_NOEXCEPT |
Jeff Thompson | f03af34 | 2013-08-07 10:32:25 -0700 | [diff] [blame] | 187 | { |
| 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 Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 196 | void reset() NDNBOOST_NOEXCEPT // never throws in 1.30+ |
Jeff Thompson | f03af34 | 2013-08-07 10:32:25 -0700 | [diff] [blame] | 197 | { |
| 198 | this_type().swap(*this); |
| 199 | } |
| 200 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 201 | void swap(this_type & other) NDNBOOST_NOEXCEPT |
Jeff Thompson | f03af34 | 2013-08-07 10:32:25 -0700 | [diff] [blame] | 202 | { |
| 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 Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 214 | template<class Y> bool owner_before( weak_ptr<Y> const & rhs ) const NDNBOOST_NOEXCEPT |
Jeff Thompson | f03af34 | 2013-08-07 10:32:25 -0700 | [diff] [blame] | 215 | { |
| 216 | return pn < rhs.pn; |
| 217 | } |
| 218 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 219 | template<class Y> bool owner_before( shared_ptr<Y> const & rhs ) const NDNBOOST_NOEXCEPT |
Jeff Thompson | f03af34 | 2013-08-07 10:32:25 -0700 | [diff] [blame] | 220 | { |
| 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 Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 227 | #ifndef NDNBOOST_NO_MEMBER_TEMPLATE_FRIENDS |
Jeff Thompson | f03af34 | 2013-08-07 10:32:25 -0700 | [diff] [blame] | 228 | |
| 229 | private: |
| 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 Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 241 | template<class T, class U> inline bool operator<(weak_ptr<T> const & a, weak_ptr<U> const & b) NDNBOOST_NOEXCEPT |
Jeff Thompson | f03af34 | 2013-08-07 10:32:25 -0700 | [diff] [blame] | 242 | { |
| 243 | return a.owner_before( b ); |
| 244 | } |
| 245 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 246 | template<class T> void swap(weak_ptr<T> & a, weak_ptr<T> & b) NDNBOOST_NOEXCEPT |
Jeff Thompson | f03af34 | 2013-08-07 10:32:25 -0700 | [diff] [blame] | 247 | { |
| 248 | a.swap(b); |
| 249 | } |
| 250 | |
| 251 | } // namespace ndnboost |
| 252 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 253 | #endif // #ifndef NDNBOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED |