111
|
1 // shared_ptr and weak_ptr implementation details -*- C++ -*-
|
|
2
|
145
|
3 // Copyright (C) 2007-2020 Free Software Foundation, Inc.
|
111
|
4 //
|
|
5 // This file is part of the GNU ISO C++ Library. This library is free
|
|
6 // software; you can redistribute it and/or modify it under the
|
|
7 // terms of the GNU General Public License as published by the
|
|
8 // Free Software Foundation; either version 3, or (at your option)
|
|
9 // any later version.
|
|
10
|
|
11 // This library is distributed in the hope that it will be useful,
|
|
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 // GNU General Public License for more details.
|
|
15
|
|
16 // Under Section 7 of GPL version 3, you are granted additional
|
|
17 // permissions described in the GCC Runtime Library Exception, version
|
|
18 // 3.1, as published by the Free Software Foundation.
|
|
19
|
|
20 // You should have received a copy of the GNU General Public License and
|
|
21 // a copy of the GCC Runtime Library Exception along with this program;
|
|
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
|
|
23 // <http://www.gnu.org/licenses/>.
|
|
24
|
|
25 // GCC Note: Based on files from version 1.32.0 of the Boost library.
|
|
26
|
|
27 // shared_count.hpp
|
|
28 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
|
|
29
|
|
30 // shared_ptr.hpp
|
|
31 // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
|
|
32 // Copyright (C) 2001, 2002, 2003 Peter Dimov
|
|
33
|
|
34 // weak_ptr.hpp
|
|
35 // Copyright (C) 2001, 2002, 2003 Peter Dimov
|
|
36
|
|
37 // enable_shared_from_this.hpp
|
|
38 // Copyright (C) 2002 Peter Dimov
|
|
39
|
|
40 // Distributed under the Boost Software License, Version 1.0. (See
|
|
41 // accompanying file LICENSE_1_0.txt or copy at
|
|
42 // http://www.boost.org/LICENSE_1_0.txt)
|
|
43
|
|
44 /** @file bits/shared_ptr_base.h
|
|
45 * This is an internal header file, included by other library headers.
|
|
46 * Do not attempt to use it directly. @headername{memory}
|
|
47 */
|
|
48
|
|
49 #ifndef _SHARED_PTR_BASE_H
|
|
50 #define _SHARED_PTR_BASE_H 1
|
|
51
|
131
|
52 #include <typeinfo>
|
111
|
53 #include <bits/allocated_ptr.h>
|
|
54 #include <bits/refwrap.h>
|
|
55 #include <bits/stl_function.h>
|
|
56 #include <ext/aligned_buffer.h>
|
|
57
|
|
58 namespace std _GLIBCXX_VISIBILITY(default)
|
|
59 {
|
|
60 _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
|
61
|
|
62 #if _GLIBCXX_USE_DEPRECATED
|
|
63 #pragma GCC diagnostic push
|
|
64 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
|
65 template<typename> class auto_ptr;
|
|
66 #pragma GCC diagnostic pop
|
|
67 #endif
|
|
68
|
|
69 /**
|
|
70 * @brief Exception possibly thrown by @c shared_ptr.
|
|
71 * @ingroup exceptions
|
|
72 */
|
|
73 class bad_weak_ptr : public std::exception
|
|
74 {
|
|
75 public:
|
|
76 virtual char const* what() const noexcept;
|
|
77
|
|
78 virtual ~bad_weak_ptr() noexcept;
|
|
79 };
|
|
80
|
|
81 // Substitute for bad_weak_ptr object in the case of -fno-exceptions.
|
|
82 inline void
|
|
83 __throw_bad_weak_ptr()
|
|
84 { _GLIBCXX_THROW_OR_ABORT(bad_weak_ptr()); }
|
|
85
|
|
86 using __gnu_cxx::_Lock_policy;
|
|
87 using __gnu_cxx::__default_lock_policy;
|
|
88 using __gnu_cxx::_S_single;
|
|
89 using __gnu_cxx::_S_mutex;
|
|
90 using __gnu_cxx::_S_atomic;
|
|
91
|
|
92 // Empty helper class except when the template argument is _S_mutex.
|
|
93 template<_Lock_policy _Lp>
|
|
94 class _Mutex_base
|
|
95 {
|
|
96 protected:
|
|
97 // The atomic policy uses fully-fenced builtins, single doesn't care.
|
|
98 enum { _S_need_barriers = 0 };
|
|
99 };
|
|
100
|
|
101 template<>
|
|
102 class _Mutex_base<_S_mutex>
|
|
103 : public __gnu_cxx::__mutex
|
|
104 {
|
|
105 protected:
|
|
106 // This policy is used when atomic builtins are not available.
|
|
107 // The replacement atomic operations might not have the necessary
|
|
108 // memory barriers.
|
|
109 enum { _S_need_barriers = 1 };
|
|
110 };
|
|
111
|
|
112 template<_Lock_policy _Lp = __default_lock_policy>
|
|
113 class _Sp_counted_base
|
|
114 : public _Mutex_base<_Lp>
|
|
115 {
|
|
116 public:
|
|
117 _Sp_counted_base() noexcept
|
|
118 : _M_use_count(1), _M_weak_count(1) { }
|
|
119
|
|
120 virtual
|
|
121 ~_Sp_counted_base() noexcept
|
|
122 { }
|
|
123
|
|
124 // Called when _M_use_count drops to zero, to release the resources
|
|
125 // managed by *this.
|
|
126 virtual void
|
|
127 _M_dispose() noexcept = 0;
|
|
128
|
|
129 // Called when _M_weak_count drops to zero.
|
|
130 virtual void
|
|
131 _M_destroy() noexcept
|
|
132 { delete this; }
|
|
133
|
|
134 virtual void*
|
|
135 _M_get_deleter(const std::type_info&) noexcept = 0;
|
|
136
|
|
137 void
|
|
138 _M_add_ref_copy()
|
|
139 { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); }
|
|
140
|
|
141 void
|
|
142 _M_add_ref_lock();
|
|
143
|
|
144 bool
|
|
145 _M_add_ref_lock_nothrow();
|
|
146
|
|
147 void
|
|
148 _M_release() noexcept
|
|
149 {
|
|
150 // Be race-detector-friendly. For more info see bits/c++config.
|
|
151 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
|
|
152 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
|
|
153 {
|
|
154 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
|
|
155 _M_dispose();
|
|
156 // There must be a memory barrier between dispose() and destroy()
|
|
157 // to ensure that the effects of dispose() are observed in the
|
|
158 // thread that runs destroy().
|
|
159 // See http://gcc.gnu.org/ml/libstdc++/2005-11/msg00136.html
|
|
160 if (_Mutex_base<_Lp>::_S_need_barriers)
|
|
161 {
|
|
162 __atomic_thread_fence (__ATOMIC_ACQ_REL);
|
|
163 }
|
|
164
|
|
165 // Be race-detector-friendly. For more info see bits/c++config.
|
|
166 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
|
|
167 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count,
|
|
168 -1) == 1)
|
|
169 {
|
|
170 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
|
|
171 _M_destroy();
|
|
172 }
|
|
173 }
|
|
174 }
|
|
175
|
|
176 void
|
|
177 _M_weak_add_ref() noexcept
|
|
178 { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); }
|
|
179
|
|
180 void
|
|
181 _M_weak_release() noexcept
|
|
182 {
|
|
183 // Be race-detector-friendly. For more info see bits/c++config.
|
|
184 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
|
|
185 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1)
|
|
186 {
|
|
187 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
|
|
188 if (_Mutex_base<_Lp>::_S_need_barriers)
|
|
189 {
|
|
190 // See _M_release(),
|
|
191 // destroy() must observe results of dispose()
|
|
192 __atomic_thread_fence (__ATOMIC_ACQ_REL);
|
|
193 }
|
|
194 _M_destroy();
|
|
195 }
|
|
196 }
|
|
197
|
|
198 long
|
|
199 _M_get_use_count() const noexcept
|
|
200 {
|
|
201 // No memory barrier is used here so there is no synchronization
|
|
202 // with other threads.
|
|
203 return __atomic_load_n(&_M_use_count, __ATOMIC_RELAXED);
|
|
204 }
|
|
205
|
|
206 private:
|
|
207 _Sp_counted_base(_Sp_counted_base const&) = delete;
|
|
208 _Sp_counted_base& operator=(_Sp_counted_base const&) = delete;
|
|
209
|
|
210 _Atomic_word _M_use_count; // #shared
|
|
211 _Atomic_word _M_weak_count; // #weak + (#shared != 0)
|
|
212 };
|
|
213
|
|
214 template<>
|
|
215 inline void
|
|
216 _Sp_counted_base<_S_single>::
|
|
217 _M_add_ref_lock()
|
|
218 {
|
|
219 if (_M_use_count == 0)
|
|
220 __throw_bad_weak_ptr();
|
|
221 ++_M_use_count;
|
|
222 }
|
|
223
|
|
224 template<>
|
|
225 inline void
|
|
226 _Sp_counted_base<_S_mutex>::
|
|
227 _M_add_ref_lock()
|
|
228 {
|
|
229 __gnu_cxx::__scoped_lock sentry(*this);
|
|
230 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
|
|
231 {
|
|
232 _M_use_count = 0;
|
|
233 __throw_bad_weak_ptr();
|
|
234 }
|
|
235 }
|
|
236
|
|
237 template<>
|
|
238 inline void
|
|
239 _Sp_counted_base<_S_atomic>::
|
|
240 _M_add_ref_lock()
|
|
241 {
|
|
242 // Perform lock-free add-if-not-zero operation.
|
|
243 _Atomic_word __count = _M_get_use_count();
|
|
244 do
|
|
245 {
|
|
246 if (__count == 0)
|
|
247 __throw_bad_weak_ptr();
|
|
248 // Replace the current counter value with the old value + 1, as
|
|
249 // long as it's not changed meanwhile.
|
|
250 }
|
|
251 while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
|
|
252 true, __ATOMIC_ACQ_REL,
|
|
253 __ATOMIC_RELAXED));
|
|
254 }
|
|
255
|
|
256 template<>
|
|
257 inline bool
|
|
258 _Sp_counted_base<_S_single>::
|
|
259 _M_add_ref_lock_nothrow()
|
|
260 {
|
|
261 if (_M_use_count == 0)
|
|
262 return false;
|
|
263 ++_M_use_count;
|
|
264 return true;
|
|
265 }
|
|
266
|
|
267 template<>
|
|
268 inline bool
|
|
269 _Sp_counted_base<_S_mutex>::
|
|
270 _M_add_ref_lock_nothrow()
|
|
271 {
|
|
272 __gnu_cxx::__scoped_lock sentry(*this);
|
|
273 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
|
|
274 {
|
|
275 _M_use_count = 0;
|
|
276 return false;
|
|
277 }
|
|
278 return true;
|
|
279 }
|
|
280
|
|
281 template<>
|
|
282 inline bool
|
|
283 _Sp_counted_base<_S_atomic>::
|
|
284 _M_add_ref_lock_nothrow()
|
|
285 {
|
|
286 // Perform lock-free add-if-not-zero operation.
|
|
287 _Atomic_word __count = _M_get_use_count();
|
|
288 do
|
|
289 {
|
|
290 if (__count == 0)
|
|
291 return false;
|
|
292 // Replace the current counter value with the old value + 1, as
|
|
293 // long as it's not changed meanwhile.
|
|
294 }
|
|
295 while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
|
|
296 true, __ATOMIC_ACQ_REL,
|
|
297 __ATOMIC_RELAXED));
|
|
298 return true;
|
|
299 }
|
|
300
|
|
301 template<>
|
|
302 inline void
|
|
303 _Sp_counted_base<_S_single>::_M_add_ref_copy()
|
|
304 { ++_M_use_count; }
|
|
305
|
|
306 template<>
|
|
307 inline void
|
|
308 _Sp_counted_base<_S_single>::_M_release() noexcept
|
|
309 {
|
|
310 if (--_M_use_count == 0)
|
|
311 {
|
|
312 _M_dispose();
|
|
313 if (--_M_weak_count == 0)
|
|
314 _M_destroy();
|
|
315 }
|
|
316 }
|
|
317
|
|
318 template<>
|
|
319 inline void
|
|
320 _Sp_counted_base<_S_single>::_M_weak_add_ref() noexcept
|
|
321 { ++_M_weak_count; }
|
|
322
|
|
323 template<>
|
|
324 inline void
|
|
325 _Sp_counted_base<_S_single>::_M_weak_release() noexcept
|
|
326 {
|
|
327 if (--_M_weak_count == 0)
|
|
328 _M_destroy();
|
|
329 }
|
|
330
|
|
331 template<>
|
|
332 inline long
|
|
333 _Sp_counted_base<_S_single>::_M_get_use_count() const noexcept
|
|
334 { return _M_use_count; }
|
|
335
|
|
336
|
|
337 // Forward declarations.
|
|
338 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
|
|
339 class __shared_ptr;
|
|
340
|
|
341 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
|
|
342 class __weak_ptr;
|
|
343
|
|
344 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
|
|
345 class __enable_shared_from_this;
|
|
346
|
|
347 template<typename _Tp>
|
|
348 class shared_ptr;
|
|
349
|
|
350 template<typename _Tp>
|
|
351 class weak_ptr;
|
|
352
|
|
353 template<typename _Tp>
|
|
354 struct owner_less;
|
|
355
|
|
356 template<typename _Tp>
|
|
357 class enable_shared_from_this;
|
|
358
|
|
359 template<_Lock_policy _Lp = __default_lock_policy>
|
|
360 class __weak_count;
|
|
361
|
|
362 template<_Lock_policy _Lp = __default_lock_policy>
|
|
363 class __shared_count;
|
|
364
|
|
365
|
|
366 // Counted ptr with no deleter or allocator support
|
|
367 template<typename _Ptr, _Lock_policy _Lp>
|
|
368 class _Sp_counted_ptr final : public _Sp_counted_base<_Lp>
|
|
369 {
|
|
370 public:
|
|
371 explicit
|
|
372 _Sp_counted_ptr(_Ptr __p) noexcept
|
|
373 : _M_ptr(__p) { }
|
|
374
|
|
375 virtual void
|
|
376 _M_dispose() noexcept
|
|
377 { delete _M_ptr; }
|
|
378
|
|
379 virtual void
|
|
380 _M_destroy() noexcept
|
|
381 { delete this; }
|
|
382
|
|
383 virtual void*
|
|
384 _M_get_deleter(const std::type_info&) noexcept
|
|
385 { return nullptr; }
|
|
386
|
|
387 _Sp_counted_ptr(const _Sp_counted_ptr&) = delete;
|
|
388 _Sp_counted_ptr& operator=(const _Sp_counted_ptr&) = delete;
|
|
389
|
|
390 private:
|
|
391 _Ptr _M_ptr;
|
|
392 };
|
|
393
|
|
394 template<>
|
|
395 inline void
|
|
396 _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() noexcept { }
|
|
397
|
|
398 template<>
|
|
399 inline void
|
|
400 _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() noexcept { }
|
|
401
|
|
402 template<>
|
|
403 inline void
|
|
404 _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() noexcept { }
|
|
405
|
|
406 template<int _Nm, typename _Tp,
|
|
407 bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
|
|
408 struct _Sp_ebo_helper;
|
|
409
|
|
410 /// Specialization using EBO.
|
|
411 template<int _Nm, typename _Tp>
|
|
412 struct _Sp_ebo_helper<_Nm, _Tp, true> : private _Tp
|
|
413 {
|
|
414 explicit _Sp_ebo_helper(const _Tp& __tp) : _Tp(__tp) { }
|
|
415 explicit _Sp_ebo_helper(_Tp&& __tp) : _Tp(std::move(__tp)) { }
|
|
416
|
|
417 static _Tp&
|
|
418 _S_get(_Sp_ebo_helper& __eboh) { return static_cast<_Tp&>(__eboh); }
|
|
419 };
|
|
420
|
|
421 /// Specialization not using EBO.
|
|
422 template<int _Nm, typename _Tp>
|
|
423 struct _Sp_ebo_helper<_Nm, _Tp, false>
|
|
424 {
|
|
425 explicit _Sp_ebo_helper(const _Tp& __tp) : _M_tp(__tp) { }
|
|
426 explicit _Sp_ebo_helper(_Tp&& __tp) : _M_tp(std::move(__tp)) { }
|
|
427
|
|
428 static _Tp&
|
|
429 _S_get(_Sp_ebo_helper& __eboh)
|
|
430 { return __eboh._M_tp; }
|
|
431
|
|
432 private:
|
|
433 _Tp _M_tp;
|
|
434 };
|
|
435
|
|
436 // Support for custom deleter and/or allocator
|
|
437 template<typename _Ptr, typename _Deleter, typename _Alloc, _Lock_policy _Lp>
|
|
438 class _Sp_counted_deleter final : public _Sp_counted_base<_Lp>
|
|
439 {
|
|
440 class _Impl : _Sp_ebo_helper<0, _Deleter>, _Sp_ebo_helper<1, _Alloc>
|
|
441 {
|
|
442 typedef _Sp_ebo_helper<0, _Deleter> _Del_base;
|
|
443 typedef _Sp_ebo_helper<1, _Alloc> _Alloc_base;
|
|
444
|
|
445 public:
|
|
446 _Impl(_Ptr __p, _Deleter __d, const _Alloc& __a) noexcept
|
|
447 : _M_ptr(__p), _Del_base(std::move(__d)), _Alloc_base(__a)
|
|
448 { }
|
|
449
|
|
450 _Deleter& _M_del() noexcept { return _Del_base::_S_get(*this); }
|
|
451 _Alloc& _M_alloc() noexcept { return _Alloc_base::_S_get(*this); }
|
|
452
|
|
453 _Ptr _M_ptr;
|
|
454 };
|
|
455
|
|
456 public:
|
|
457 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_deleter>;
|
|
458
|
|
459 // __d(__p) must not throw.
|
|
460 _Sp_counted_deleter(_Ptr __p, _Deleter __d) noexcept
|
|
461 : _M_impl(__p, std::move(__d), _Alloc()) { }
|
|
462
|
|
463 // __d(__p) must not throw.
|
|
464 _Sp_counted_deleter(_Ptr __p, _Deleter __d, const _Alloc& __a) noexcept
|
|
465 : _M_impl(__p, std::move(__d), __a) { }
|
|
466
|
|
467 ~_Sp_counted_deleter() noexcept { }
|
|
468
|
|
469 virtual void
|
|
470 _M_dispose() noexcept
|
|
471 { _M_impl._M_del()(_M_impl._M_ptr); }
|
|
472
|
|
473 virtual void
|
|
474 _M_destroy() noexcept
|
|
475 {
|
|
476 __allocator_type __a(_M_impl._M_alloc());
|
|
477 __allocated_ptr<__allocator_type> __guard_ptr{ __a, this };
|
|
478 this->~_Sp_counted_deleter();
|
|
479 }
|
|
480
|
|
481 virtual void*
|
|
482 _M_get_deleter(const std::type_info& __ti) noexcept
|
|
483 {
|
|
484 #if __cpp_rtti
|
|
485 // _GLIBCXX_RESOLVE_LIB_DEFECTS
|
|
486 // 2400. shared_ptr's get_deleter() should use addressof()
|
|
487 return __ti == typeid(_Deleter)
|
|
488 ? std::__addressof(_M_impl._M_del())
|
|
489 : nullptr;
|
|
490 #else
|
|
491 return nullptr;
|
|
492 #endif
|
|
493 }
|
|
494
|
|
495 private:
|
|
496 _Impl _M_impl;
|
|
497 };
|
|
498
|
|
499 // helpers for make_shared / allocate_shared
|
|
500
|
|
501 struct _Sp_make_shared_tag
|
|
502 {
|
|
503 private:
|
|
504 template<typename _Tp, typename _Alloc, _Lock_policy _Lp>
|
|
505 friend class _Sp_counted_ptr_inplace;
|
|
506
|
|
507 static const type_info&
|
131
|
508 _S_ti() noexcept _GLIBCXX_VISIBILITY(default)
|
111
|
509 {
|
145
|
510 alignas(type_info) static constexpr char __tag[sizeof(type_info)] = { };
|
111
|
511 return reinterpret_cast<const type_info&>(__tag);
|
|
512 }
|
145
|
513
|
|
514 static bool _S_eq(const type_info&) noexcept;
|
111
|
515 };
|
|
516
|
145
|
517 template<typename _Alloc>
|
|
518 struct _Sp_alloc_shared_tag
|
|
519 {
|
|
520 const _Alloc& _M_a;
|
|
521 };
|
|
522
|
111
|
523 template<typename _Tp, typename _Alloc, _Lock_policy _Lp>
|
|
524 class _Sp_counted_ptr_inplace final : public _Sp_counted_base<_Lp>
|
|
525 {
|
|
526 class _Impl : _Sp_ebo_helper<0, _Alloc>
|
|
527 {
|
|
528 typedef _Sp_ebo_helper<0, _Alloc> _A_base;
|
|
529
|
|
530 public:
|
|
531 explicit _Impl(_Alloc __a) noexcept : _A_base(__a) { }
|
|
532
|
|
533 _Alloc& _M_alloc() noexcept { return _A_base::_S_get(*this); }
|
|
534
|
|
535 __gnu_cxx::__aligned_buffer<_Tp> _M_storage;
|
|
536 };
|
|
537
|
|
538 public:
|
|
539 using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>;
|
|
540
|
131
|
541 // Alloc parameter is not a reference so doesn't alias anything in __args
|
111
|
542 template<typename... _Args>
|
|
543 _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
|
|
544 : _M_impl(__a)
|
|
545 {
|
|
546 // _GLIBCXX_RESOLVE_LIB_DEFECTS
|
|
547 // 2070. allocate_shared should use allocator_traits<A>::construct
|
|
548 allocator_traits<_Alloc>::construct(__a, _M_ptr(),
|
|
549 std::forward<_Args>(__args)...); // might throw
|
|
550 }
|
|
551
|
|
552 ~_Sp_counted_ptr_inplace() noexcept { }
|
|
553
|
|
554 virtual void
|
|
555 _M_dispose() noexcept
|
|
556 {
|
|
557 allocator_traits<_Alloc>::destroy(_M_impl._M_alloc(), _M_ptr());
|
|
558 }
|
|
559
|
|
560 // Override because the allocator needs to know the dynamic type
|
|
561 virtual void
|
|
562 _M_destroy() noexcept
|
|
563 {
|
|
564 __allocator_type __a(_M_impl._M_alloc());
|
|
565 __allocated_ptr<__allocator_type> __guard_ptr{ __a, this };
|
|
566 this->~_Sp_counted_ptr_inplace();
|
|
567 }
|
|
568
|
145
|
569 private:
|
|
570 friend class __shared_count<_Lp>; // To be able to call _M_ptr().
|
|
571
|
|
572 // No longer used, but code compiled against old libstdc++ headers
|
|
573 // might still call it from __shared_ptr ctor to get the pointer out.
|
111
|
574 virtual void*
|
145
|
575 _M_get_deleter(const std::type_info& __ti) noexcept override
|
111
|
576 {
|
145
|
577 auto __ptr = const_cast<typename remove_cv<_Tp>::type*>(_M_ptr());
|
|
578 // Check for the fake type_info first, so we don't try to access it
|
|
579 // as a real type_info object. Otherwise, check if it's the real
|
|
580 // type_info for this class. With RTTI enabled we can check directly,
|
|
581 // or call a library function to do it.
|
|
582 if (&__ti == &_Sp_make_shared_tag::_S_ti()
|
|
583 ||
|
111
|
584 #if __cpp_rtti
|
145
|
585 __ti == typeid(_Sp_make_shared_tag)
|
111
|
586 #else
|
145
|
587 _Sp_make_shared_tag::_S_eq(__ti)
|
111
|
588 #endif
|
145
|
589 )
|
|
590 return __ptr;
|
111
|
591 return nullptr;
|
|
592 }
|
|
593
|
|
594 _Tp* _M_ptr() noexcept { return _M_impl._M_storage._M_ptr(); }
|
|
595
|
|
596 _Impl _M_impl;
|
|
597 };
|
|
598
|
|
599 // The default deleter for shared_ptr<T[]> and shared_ptr<T[N]>.
|
|
600 struct __sp_array_delete
|
|
601 {
|
|
602 template<typename _Yp>
|
|
603 void operator()(_Yp* __p) const { delete[] __p; }
|
|
604 };
|
|
605
|
|
606 template<_Lock_policy _Lp>
|
|
607 class __shared_count
|
|
608 {
|
145
|
609 template<typename _Tp>
|
|
610 struct __not_alloc_shared_tag { using type = void; };
|
|
611
|
|
612 template<typename _Tp>
|
|
613 struct __not_alloc_shared_tag<_Sp_alloc_shared_tag<_Tp>> { };
|
|
614
|
111
|
615 public:
|
|
616 constexpr __shared_count() noexcept : _M_pi(0)
|
|
617 { }
|
|
618
|
|
619 template<typename _Ptr>
|
|
620 explicit
|
|
621 __shared_count(_Ptr __p) : _M_pi(0)
|
|
622 {
|
|
623 __try
|
|
624 {
|
|
625 _M_pi = new _Sp_counted_ptr<_Ptr, _Lp>(__p);
|
|
626 }
|
|
627 __catch(...)
|
|
628 {
|
|
629 delete __p;
|
|
630 __throw_exception_again;
|
|
631 }
|
|
632 }
|
|
633
|
|
634 template<typename _Ptr>
|
|
635 __shared_count(_Ptr __p, /* is_array = */ false_type)
|
|
636 : __shared_count(__p)
|
|
637 { }
|
|
638
|
|
639 template<typename _Ptr>
|
|
640 __shared_count(_Ptr __p, /* is_array = */ true_type)
|
|
641 : __shared_count(__p, __sp_array_delete{}, allocator<void>())
|
|
642 { }
|
|
643
|
145
|
644 template<typename _Ptr, typename _Deleter,
|
|
645 typename = typename __not_alloc_shared_tag<_Deleter>::type>
|
111
|
646 __shared_count(_Ptr __p, _Deleter __d)
|
|
647 : __shared_count(__p, std::move(__d), allocator<void>())
|
|
648 { }
|
|
649
|
145
|
650 template<typename _Ptr, typename _Deleter, typename _Alloc,
|
|
651 typename = typename __not_alloc_shared_tag<_Deleter>::type>
|
111
|
652 __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
|
|
653 {
|
|
654 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
|
|
655 __try
|
|
656 {
|
|
657 typename _Sp_cd_type::__allocator_type __a2(__a);
|
|
658 auto __guard = std::__allocate_guarded(__a2);
|
|
659 _Sp_cd_type* __mem = __guard.get();
|
|
660 ::new (__mem) _Sp_cd_type(__p, std::move(__d), std::move(__a));
|
|
661 _M_pi = __mem;
|
|
662 __guard = nullptr;
|
|
663 }
|
|
664 __catch(...)
|
|
665 {
|
|
666 __d(__p); // Call _Deleter on __p.
|
|
667 __throw_exception_again;
|
|
668 }
|
|
669 }
|
|
670
|
|
671 template<typename _Tp, typename _Alloc, typename... _Args>
|
145
|
672 __shared_count(_Tp*& __p, _Sp_alloc_shared_tag<_Alloc> __a,
|
111
|
673 _Args&&... __args)
|
|
674 {
|
|
675 typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
|
145
|
676 typename _Sp_cp_type::__allocator_type __a2(__a._M_a);
|
111
|
677 auto __guard = std::__allocate_guarded(__a2);
|
|
678 _Sp_cp_type* __mem = __guard.get();
|
145
|
679 auto __pi = ::new (__mem)
|
|
680 _Sp_cp_type(__a._M_a, std::forward<_Args>(__args)...);
|
111
|
681 __guard = nullptr;
|
145
|
682 _M_pi = __pi;
|
|
683 __p = __pi->_M_ptr();
|
111
|
684 }
|
|
685
|
|
686 #if _GLIBCXX_USE_DEPRECATED
|
|
687 #pragma GCC diagnostic push
|
|
688 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
|
689 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
|
|
690 template<typename _Tp>
|
|
691 explicit
|
|
692 __shared_count(std::auto_ptr<_Tp>&& __r);
|
|
693 #pragma GCC diagnostic pop
|
|
694 #endif
|
|
695
|
|
696 // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
|
|
697 template<typename _Tp, typename _Del>
|
|
698 explicit
|
|
699 __shared_count(std::unique_ptr<_Tp, _Del>&& __r) : _M_pi(0)
|
|
700 {
|
|
701 // _GLIBCXX_RESOLVE_LIB_DEFECTS
|
|
702 // 2415. Inconsistency between unique_ptr and shared_ptr
|
|
703 if (__r.get() == nullptr)
|
|
704 return;
|
|
705
|
|
706 using _Ptr = typename unique_ptr<_Tp, _Del>::pointer;
|
|
707 using _Del2 = typename conditional<is_reference<_Del>::value,
|
|
708 reference_wrapper<typename remove_reference<_Del>::type>,
|
|
709 _Del>::type;
|
|
710 using _Sp_cd_type
|
|
711 = _Sp_counted_deleter<_Ptr, _Del2, allocator<void>, _Lp>;
|
|
712 using _Alloc = allocator<_Sp_cd_type>;
|
|
713 using _Alloc_traits = allocator_traits<_Alloc>;
|
|
714 _Alloc __a;
|
|
715 _Sp_cd_type* __mem = _Alloc_traits::allocate(__a, 1);
|
|
716 _Alloc_traits::construct(__a, __mem, __r.release(),
|
|
717 __r.get_deleter()); // non-throwing
|
|
718 _M_pi = __mem;
|
|
719 }
|
|
720
|
|
721 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
|
|
722 explicit __shared_count(const __weak_count<_Lp>& __r);
|
|
723
|
|
724 // Does not throw if __r._M_get_use_count() == 0, caller must check.
|
|
725 explicit __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t);
|
|
726
|
|
727 ~__shared_count() noexcept
|
|
728 {
|
|
729 if (_M_pi != nullptr)
|
|
730 _M_pi->_M_release();
|
|
731 }
|
|
732
|
|
733 __shared_count(const __shared_count& __r) noexcept
|
|
734 : _M_pi(__r._M_pi)
|
|
735 {
|
|
736 if (_M_pi != 0)
|
|
737 _M_pi->_M_add_ref_copy();
|
|
738 }
|
|
739
|
|
740 __shared_count&
|
|
741 operator=(const __shared_count& __r) noexcept
|
|
742 {
|
|
743 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
|
|
744 if (__tmp != _M_pi)
|
|
745 {
|
|
746 if (__tmp != 0)
|
|
747 __tmp->_M_add_ref_copy();
|
|
748 if (_M_pi != 0)
|
|
749 _M_pi->_M_release();
|
|
750 _M_pi = __tmp;
|
|
751 }
|
|
752 return *this;
|
|
753 }
|
|
754
|
|
755 void
|
|
756 _M_swap(__shared_count& __r) noexcept
|
|
757 {
|
|
758 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
|
|
759 __r._M_pi = _M_pi;
|
|
760 _M_pi = __tmp;
|
|
761 }
|
|
762
|
|
763 long
|
|
764 _M_get_use_count() const noexcept
|
|
765 { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
|
|
766
|
|
767 bool
|
|
768 _M_unique() const noexcept
|
|
769 { return this->_M_get_use_count() == 1; }
|
|
770
|
|
771 void*
|
|
772 _M_get_deleter(const std::type_info& __ti) const noexcept
|
|
773 { return _M_pi ? _M_pi->_M_get_deleter(__ti) : nullptr; }
|
|
774
|
|
775 bool
|
|
776 _M_less(const __shared_count& __rhs) const noexcept
|
|
777 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
|
|
778
|
|
779 bool
|
|
780 _M_less(const __weak_count<_Lp>& __rhs) const noexcept
|
|
781 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
|
|
782
|
|
783 // Friend function injected into enclosing namespace and found by ADL
|
|
784 friend inline bool
|
|
785 operator==(const __shared_count& __a, const __shared_count& __b) noexcept
|
|
786 { return __a._M_pi == __b._M_pi; }
|
|
787
|
|
788 private:
|
|
789 friend class __weak_count<_Lp>;
|
|
790
|
|
791 _Sp_counted_base<_Lp>* _M_pi;
|
|
792 };
|
|
793
|
|
794
|
|
795 template<_Lock_policy _Lp>
|
|
796 class __weak_count
|
|
797 {
|
|
798 public:
|
|
799 constexpr __weak_count() noexcept : _M_pi(nullptr)
|
|
800 { }
|
|
801
|
|
802 __weak_count(const __shared_count<_Lp>& __r) noexcept
|
|
803 : _M_pi(__r._M_pi)
|
|
804 {
|
|
805 if (_M_pi != nullptr)
|
|
806 _M_pi->_M_weak_add_ref();
|
|
807 }
|
|
808
|
|
809 __weak_count(const __weak_count& __r) noexcept
|
|
810 : _M_pi(__r._M_pi)
|
|
811 {
|
|
812 if (_M_pi != nullptr)
|
|
813 _M_pi->_M_weak_add_ref();
|
|
814 }
|
|
815
|
|
816 __weak_count(__weak_count&& __r) noexcept
|
|
817 : _M_pi(__r._M_pi)
|
|
818 { __r._M_pi = nullptr; }
|
|
819
|
|
820 ~__weak_count() noexcept
|
|
821 {
|
|
822 if (_M_pi != nullptr)
|
|
823 _M_pi->_M_weak_release();
|
|
824 }
|
|
825
|
|
826 __weak_count&
|
|
827 operator=(const __shared_count<_Lp>& __r) noexcept
|
|
828 {
|
|
829 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
|
|
830 if (__tmp != nullptr)
|
|
831 __tmp->_M_weak_add_ref();
|
|
832 if (_M_pi != nullptr)
|
|
833 _M_pi->_M_weak_release();
|
|
834 _M_pi = __tmp;
|
|
835 return *this;
|
|
836 }
|
|
837
|
|
838 __weak_count&
|
|
839 operator=(const __weak_count& __r) noexcept
|
|
840 {
|
|
841 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
|
|
842 if (__tmp != nullptr)
|
|
843 __tmp->_M_weak_add_ref();
|
|
844 if (_M_pi != nullptr)
|
|
845 _M_pi->_M_weak_release();
|
|
846 _M_pi = __tmp;
|
|
847 return *this;
|
|
848 }
|
|
849
|
|
850 __weak_count&
|
|
851 operator=(__weak_count&& __r) noexcept
|
|
852 {
|
|
853 if (_M_pi != nullptr)
|
|
854 _M_pi->_M_weak_release();
|
|
855 _M_pi = __r._M_pi;
|
|
856 __r._M_pi = nullptr;
|
|
857 return *this;
|
|
858 }
|
|
859
|
|
860 void
|
|
861 _M_swap(__weak_count& __r) noexcept
|
|
862 {
|
|
863 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
|
|
864 __r._M_pi = _M_pi;
|
|
865 _M_pi = __tmp;
|
|
866 }
|
|
867
|
|
868 long
|
|
869 _M_get_use_count() const noexcept
|
|
870 { return _M_pi != nullptr ? _M_pi->_M_get_use_count() : 0; }
|
|
871
|
|
872 bool
|
|
873 _M_less(const __weak_count& __rhs) const noexcept
|
|
874 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
|
|
875
|
|
876 bool
|
|
877 _M_less(const __shared_count<_Lp>& __rhs) const noexcept
|
|
878 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
|
|
879
|
|
880 // Friend function injected into enclosing namespace and found by ADL
|
|
881 friend inline bool
|
|
882 operator==(const __weak_count& __a, const __weak_count& __b) noexcept
|
|
883 { return __a._M_pi == __b._M_pi; }
|
|
884
|
|
885 private:
|
|
886 friend class __shared_count<_Lp>;
|
|
887
|
|
888 _Sp_counted_base<_Lp>* _M_pi;
|
|
889 };
|
|
890
|
|
891 // Now that __weak_count is defined we can define this constructor:
|
|
892 template<_Lock_policy _Lp>
|
|
893 inline
|
|
894 __shared_count<_Lp>::__shared_count(const __weak_count<_Lp>& __r)
|
|
895 : _M_pi(__r._M_pi)
|
|
896 {
|
|
897 if (_M_pi != nullptr)
|
|
898 _M_pi->_M_add_ref_lock();
|
|
899 else
|
|
900 __throw_bad_weak_ptr();
|
|
901 }
|
|
902
|
|
903 // Now that __weak_count is defined we can define this constructor:
|
|
904 template<_Lock_policy _Lp>
|
|
905 inline
|
|
906 __shared_count<_Lp>::
|
|
907 __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t)
|
|
908 : _M_pi(__r._M_pi)
|
|
909 {
|
|
910 if (_M_pi != nullptr)
|
|
911 if (!_M_pi->_M_add_ref_lock_nothrow())
|
|
912 _M_pi = nullptr;
|
|
913 }
|
|
914
|
145
|
915 #define __cpp_lib_shared_ptr_arrays 201611L
|
111
|
916
|
|
917 // Helper traits for shared_ptr of array:
|
|
918
|
|
919 // A pointer type Y* is said to be compatible with a pointer type T* when
|
|
920 // either Y* is convertible to T* or Y is U[N] and T is U cv [].
|
|
921 template<typename _Yp_ptr, typename _Tp_ptr>
|
|
922 struct __sp_compatible_with
|
|
923 : false_type
|
|
924 { };
|
|
925
|
|
926 template<typename _Yp, typename _Tp>
|
|
927 struct __sp_compatible_with<_Yp*, _Tp*>
|
|
928 : is_convertible<_Yp*, _Tp*>::type
|
|
929 { };
|
|
930
|
|
931 template<typename _Up, size_t _Nm>
|
|
932 struct __sp_compatible_with<_Up(*)[_Nm], _Up(*)[]>
|
|
933 : true_type
|
|
934 { };
|
|
935
|
|
936 template<typename _Up, size_t _Nm>
|
|
937 struct __sp_compatible_with<_Up(*)[_Nm], const _Up(*)[]>
|
|
938 : true_type
|
|
939 { };
|
|
940
|
|
941 template<typename _Up, size_t _Nm>
|
|
942 struct __sp_compatible_with<_Up(*)[_Nm], volatile _Up(*)[]>
|
|
943 : true_type
|
|
944 { };
|
|
945
|
|
946 template<typename _Up, size_t _Nm>
|
|
947 struct __sp_compatible_with<_Up(*)[_Nm], const volatile _Up(*)[]>
|
|
948 : true_type
|
|
949 { };
|
|
950
|
|
951 // Test conversion from Y(*)[N] to U(*)[N] without forming invalid type Y[N].
|
|
952 template<typename _Up, size_t _Nm, typename _Yp, typename = void>
|
|
953 struct __sp_is_constructible_arrN
|
|
954 : false_type
|
|
955 { };
|
|
956
|
|
957 template<typename _Up, size_t _Nm, typename _Yp>
|
|
958 struct __sp_is_constructible_arrN<_Up, _Nm, _Yp, __void_t<_Yp[_Nm]>>
|
|
959 : is_convertible<_Yp(*)[_Nm], _Up(*)[_Nm]>::type
|
|
960 { };
|
|
961
|
|
962 // Test conversion from Y(*)[] to U(*)[] without forming invalid type Y[].
|
|
963 template<typename _Up, typename _Yp, typename = void>
|
|
964 struct __sp_is_constructible_arr
|
|
965 : false_type
|
|
966 { };
|
|
967
|
|
968 template<typename _Up, typename _Yp>
|
|
969 struct __sp_is_constructible_arr<_Up, _Yp, __void_t<_Yp[]>>
|
|
970 : is_convertible<_Yp(*)[], _Up(*)[]>::type
|
|
971 { };
|
|
972
|
|
973 // Trait to check if shared_ptr<T> can be constructed from Y*.
|
|
974 template<typename _Tp, typename _Yp>
|
|
975 struct __sp_is_constructible;
|
|
976
|
|
977 // When T is U[N], Y(*)[N] shall be convertible to T*;
|
|
978 template<typename _Up, size_t _Nm, typename _Yp>
|
|
979 struct __sp_is_constructible<_Up[_Nm], _Yp>
|
|
980 : __sp_is_constructible_arrN<_Up, _Nm, _Yp>::type
|
|
981 { };
|
|
982
|
|
983 // when T is U[], Y(*)[] shall be convertible to T*;
|
|
984 template<typename _Up, typename _Yp>
|
|
985 struct __sp_is_constructible<_Up[], _Yp>
|
|
986 : __sp_is_constructible_arr<_Up, _Yp>::type
|
|
987 { };
|
|
988
|
|
989 // otherwise, Y* shall be convertible to T*.
|
|
990 template<typename _Tp, typename _Yp>
|
|
991 struct __sp_is_constructible
|
|
992 : is_convertible<_Yp*, _Tp*>::type
|
|
993 { };
|
|
994
|
|
995
|
|
996 // Define operator* and operator-> for shared_ptr<T>.
|
|
997 template<typename _Tp, _Lock_policy _Lp,
|
|
998 bool = is_array<_Tp>::value, bool = is_void<_Tp>::value>
|
|
999 class __shared_ptr_access
|
|
1000 {
|
|
1001 public:
|
|
1002 using element_type = _Tp;
|
|
1003
|
|
1004 element_type&
|
|
1005 operator*() const noexcept
|
|
1006 {
|
|
1007 __glibcxx_assert(_M_get() != nullptr);
|
|
1008 return *_M_get();
|
|
1009 }
|
|
1010
|
|
1011 element_type*
|
|
1012 operator->() const noexcept
|
|
1013 {
|
|
1014 _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr);
|
|
1015 return _M_get();
|
|
1016 }
|
|
1017
|
|
1018 private:
|
|
1019 element_type*
|
|
1020 _M_get() const noexcept
|
|
1021 { return static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); }
|
|
1022 };
|
|
1023
|
|
1024 // Define operator-> for shared_ptr<cv void>.
|
|
1025 template<typename _Tp, _Lock_policy _Lp>
|
|
1026 class __shared_ptr_access<_Tp, _Lp, false, true>
|
|
1027 {
|
|
1028 public:
|
|
1029 using element_type = _Tp;
|
|
1030
|
|
1031 element_type*
|
|
1032 operator->() const noexcept
|
|
1033 {
|
|
1034 auto __ptr = static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get();
|
|
1035 _GLIBCXX_DEBUG_PEDASSERT(__ptr != nullptr);
|
|
1036 return __ptr;
|
|
1037 }
|
|
1038 };
|
|
1039
|
|
1040 // Define operator[] for shared_ptr<T[]> and shared_ptr<T[N]>.
|
|
1041 template<typename _Tp, _Lock_policy _Lp>
|
|
1042 class __shared_ptr_access<_Tp, _Lp, true, false>
|
|
1043 {
|
|
1044 public:
|
|
1045 using element_type = typename remove_extent<_Tp>::type;
|
|
1046
|
|
1047 #if __cplusplus <= 201402L
|
|
1048 [[__deprecated__("shared_ptr<T[]>::operator* is absent from C++17")]]
|
|
1049 element_type&
|
|
1050 operator*() const noexcept
|
|
1051 {
|
|
1052 __glibcxx_assert(_M_get() != nullptr);
|
|
1053 return *_M_get();
|
|
1054 }
|
|
1055
|
|
1056 [[__deprecated__("shared_ptr<T[]>::operator-> is absent from C++17")]]
|
|
1057 element_type*
|
|
1058 operator->() const noexcept
|
|
1059 {
|
|
1060 _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr);
|
|
1061 return _M_get();
|
|
1062 }
|
|
1063 #endif
|
|
1064
|
|
1065 element_type&
|
|
1066 operator[](ptrdiff_t __i) const
|
|
1067 {
|
|
1068 __glibcxx_assert(_M_get() != nullptr);
|
|
1069 __glibcxx_assert(!extent<_Tp>::value || __i < extent<_Tp>::value);
|
|
1070 return _M_get()[__i];
|
|
1071 }
|
|
1072
|
|
1073 private:
|
|
1074 element_type*
|
|
1075 _M_get() const noexcept
|
|
1076 { return static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); }
|
|
1077 };
|
|
1078
|
|
1079 template<typename _Tp, _Lock_policy _Lp>
|
|
1080 class __shared_ptr
|
|
1081 : public __shared_ptr_access<_Tp, _Lp>
|
|
1082 {
|
|
1083 public:
|
|
1084 using element_type = typename remove_extent<_Tp>::type;
|
|
1085
|
|
1086 private:
|
|
1087 // Constraint for taking ownership of a pointer of type _Yp*:
|
|
1088 template<typename _Yp>
|
|
1089 using _SafeConv
|
|
1090 = typename enable_if<__sp_is_constructible<_Tp, _Yp>::value>::type;
|
|
1091
|
|
1092 // Constraint for construction from shared_ptr and weak_ptr:
|
|
1093 template<typename _Yp, typename _Res = void>
|
|
1094 using _Compatible = typename
|
|
1095 enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
|
|
1096
|
|
1097 // Constraint for assignment from shared_ptr and weak_ptr:
|
|
1098 template<typename _Yp>
|
|
1099 using _Assignable = _Compatible<_Yp, __shared_ptr&>;
|
|
1100
|
|
1101 // Constraint for construction from unique_ptr:
|
|
1102 template<typename _Yp, typename _Del, typename _Res = void,
|
|
1103 typename _Ptr = typename unique_ptr<_Yp, _Del>::pointer>
|
|
1104 using _UniqCompatible = typename enable_if<__and_<
|
|
1105 __sp_compatible_with<_Yp*, _Tp*>, is_convertible<_Ptr, element_type*>
|
|
1106 >::value, _Res>::type;
|
|
1107
|
|
1108 // Constraint for assignment from unique_ptr:
|
|
1109 template<typename _Yp, typename _Del>
|
|
1110 using _UniqAssignable = _UniqCompatible<_Yp, _Del, __shared_ptr&>;
|
|
1111
|
|
1112 public:
|
|
1113
|
|
1114 #if __cplusplus > 201402L
|
|
1115 using weak_type = __weak_ptr<_Tp, _Lp>;
|
|
1116 #endif
|
|
1117
|
|
1118 constexpr __shared_ptr() noexcept
|
|
1119 : _M_ptr(0), _M_refcount()
|
|
1120 { }
|
|
1121
|
|
1122 template<typename _Yp, typename = _SafeConv<_Yp>>
|
|
1123 explicit
|
|
1124 __shared_ptr(_Yp* __p)
|
|
1125 : _M_ptr(__p), _M_refcount(__p, typename is_array<_Tp>::type())
|
|
1126 {
|
|
1127 static_assert( !is_void<_Yp>::value, "incomplete type" );
|
|
1128 static_assert( sizeof(_Yp) > 0, "incomplete type" );
|
|
1129 _M_enable_shared_from_this_with(__p);
|
|
1130 }
|
|
1131
|
|
1132 template<typename _Yp, typename _Deleter, typename = _SafeConv<_Yp>>
|
|
1133 __shared_ptr(_Yp* __p, _Deleter __d)
|
|
1134 : _M_ptr(__p), _M_refcount(__p, std::move(__d))
|
|
1135 {
|
|
1136 static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
|
|
1137 "deleter expression d(p) is well-formed");
|
|
1138 _M_enable_shared_from_this_with(__p);
|
|
1139 }
|
|
1140
|
|
1141 template<typename _Yp, typename _Deleter, typename _Alloc,
|
|
1142 typename = _SafeConv<_Yp>>
|
|
1143 __shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a)
|
|
1144 : _M_ptr(__p), _M_refcount(__p, std::move(__d), std::move(__a))
|
|
1145 {
|
|
1146 static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
|
|
1147 "deleter expression d(p) is well-formed");
|
|
1148 _M_enable_shared_from_this_with(__p);
|
|
1149 }
|
|
1150
|
|
1151 template<typename _Deleter>
|
|
1152 __shared_ptr(nullptr_t __p, _Deleter __d)
|
|
1153 : _M_ptr(0), _M_refcount(__p, std::move(__d))
|
|
1154 { }
|
|
1155
|
|
1156 template<typename _Deleter, typename _Alloc>
|
|
1157 __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
|
|
1158 : _M_ptr(0), _M_refcount(__p, std::move(__d), std::move(__a))
|
|
1159 { }
|
|
1160
|
145
|
1161 // Aliasing constructor
|
111
|
1162 template<typename _Yp>
|
|
1163 __shared_ptr(const __shared_ptr<_Yp, _Lp>& __r,
|
|
1164 element_type* __p) noexcept
|
|
1165 : _M_ptr(__p), _M_refcount(__r._M_refcount) // never throws
|
|
1166 { }
|
|
1167
|
145
|
1168 // Aliasing constructor
|
|
1169 template<typename _Yp>
|
|
1170 __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r,
|
|
1171 element_type* __p) noexcept
|
|
1172 : _M_ptr(__p), _M_refcount()
|
|
1173 {
|
|
1174 _M_refcount._M_swap(__r._M_refcount);
|
|
1175 __r._M_ptr = 0;
|
|
1176 }
|
|
1177
|
111
|
1178 __shared_ptr(const __shared_ptr&) noexcept = default;
|
|
1179 __shared_ptr& operator=(const __shared_ptr&) noexcept = default;
|
|
1180 ~__shared_ptr() = default;
|
|
1181
|
|
1182 template<typename _Yp, typename = _Compatible<_Yp>>
|
|
1183 __shared_ptr(const __shared_ptr<_Yp, _Lp>& __r) noexcept
|
|
1184 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
|
|
1185 { }
|
|
1186
|
|
1187 __shared_ptr(__shared_ptr&& __r) noexcept
|
|
1188 : _M_ptr(__r._M_ptr), _M_refcount()
|
|
1189 {
|
|
1190 _M_refcount._M_swap(__r._M_refcount);
|
|
1191 __r._M_ptr = 0;
|
|
1192 }
|
|
1193
|
|
1194 template<typename _Yp, typename = _Compatible<_Yp>>
|
|
1195 __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r) noexcept
|
|
1196 : _M_ptr(__r._M_ptr), _M_refcount()
|
|
1197 {
|
|
1198 _M_refcount._M_swap(__r._M_refcount);
|
|
1199 __r._M_ptr = 0;
|
|
1200 }
|
|
1201
|
|
1202 template<typename _Yp, typename = _Compatible<_Yp>>
|
|
1203 explicit __shared_ptr(const __weak_ptr<_Yp, _Lp>& __r)
|
|
1204 : _M_refcount(__r._M_refcount) // may throw
|
|
1205 {
|
|
1206 // It is now safe to copy __r._M_ptr, as
|
|
1207 // _M_refcount(__r._M_refcount) did not throw.
|
|
1208 _M_ptr = __r._M_ptr;
|
|
1209 }
|
|
1210
|
|
1211 // If an exception is thrown this constructor has no effect.
|
|
1212 template<typename _Yp, typename _Del,
|
|
1213 typename = _UniqCompatible<_Yp, _Del>>
|
|
1214 __shared_ptr(unique_ptr<_Yp, _Del>&& __r)
|
|
1215 : _M_ptr(__r.get()), _M_refcount()
|
|
1216 {
|
|
1217 auto __raw = __to_address(__r.get());
|
|
1218 _M_refcount = __shared_count<_Lp>(std::move(__r));
|
|
1219 _M_enable_shared_from_this_with(__raw);
|
|
1220 }
|
|
1221
|
|
1222 #if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED
|
|
1223 protected:
|
|
1224 // If an exception is thrown this constructor has no effect.
|
|
1225 template<typename _Tp1, typename _Del,
|
|
1226 typename enable_if<__and_<
|
|
1227 __not_<is_array<_Tp>>, is_array<_Tp1>,
|
|
1228 is_convertible<typename unique_ptr<_Tp1, _Del>::pointer, _Tp*>
|
|
1229 >::value, bool>::type = true>
|
|
1230 __shared_ptr(unique_ptr<_Tp1, _Del>&& __r, __sp_array_delete)
|
|
1231 : _M_ptr(__r.get()), _M_refcount()
|
|
1232 {
|
|
1233 auto __raw = __to_address(__r.get());
|
|
1234 _M_refcount = __shared_count<_Lp>(std::move(__r));
|
|
1235 _M_enable_shared_from_this_with(__raw);
|
|
1236 }
|
|
1237 public:
|
|
1238 #endif
|
|
1239
|
|
1240 #if _GLIBCXX_USE_DEPRECATED
|
|
1241 #pragma GCC diagnostic push
|
|
1242 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
|
1243 // Postcondition: use_count() == 1 and __r.get() == 0
|
|
1244 template<typename _Yp, typename = _Compatible<_Yp>>
|
|
1245 __shared_ptr(auto_ptr<_Yp>&& __r);
|
|
1246 #pragma GCC diagnostic pop
|
|
1247 #endif
|
|
1248
|
|
1249 constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { }
|
|
1250
|
|
1251 template<typename _Yp>
|
|
1252 _Assignable<_Yp>
|
|
1253 operator=(const __shared_ptr<_Yp, _Lp>& __r) noexcept
|
|
1254 {
|
|
1255 _M_ptr = __r._M_ptr;
|
|
1256 _M_refcount = __r._M_refcount; // __shared_count::op= doesn't throw
|
|
1257 return *this;
|
|
1258 }
|
|
1259
|
|
1260 #if _GLIBCXX_USE_DEPRECATED
|
|
1261 #pragma GCC diagnostic push
|
|
1262 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
|
1263 template<typename _Yp>
|
|
1264 _Assignable<_Yp>
|
|
1265 operator=(auto_ptr<_Yp>&& __r)
|
|
1266 {
|
|
1267 __shared_ptr(std::move(__r)).swap(*this);
|
|
1268 return *this;
|
|
1269 }
|
|
1270 #pragma GCC diagnostic pop
|
|
1271 #endif
|
|
1272
|
|
1273 __shared_ptr&
|
|
1274 operator=(__shared_ptr&& __r) noexcept
|
|
1275 {
|
|
1276 __shared_ptr(std::move(__r)).swap(*this);
|
|
1277 return *this;
|
|
1278 }
|
|
1279
|
|
1280 template<class _Yp>
|
|
1281 _Assignable<_Yp>
|
|
1282 operator=(__shared_ptr<_Yp, _Lp>&& __r) noexcept
|
|
1283 {
|
|
1284 __shared_ptr(std::move(__r)).swap(*this);
|
|
1285 return *this;
|
|
1286 }
|
|
1287
|
|
1288 template<typename _Yp, typename _Del>
|
|
1289 _UniqAssignable<_Yp, _Del>
|
|
1290 operator=(unique_ptr<_Yp, _Del>&& __r)
|
|
1291 {
|
|
1292 __shared_ptr(std::move(__r)).swap(*this);
|
|
1293 return *this;
|
|
1294 }
|
|
1295
|
|
1296 void
|
|
1297 reset() noexcept
|
|
1298 { __shared_ptr().swap(*this); }
|
|
1299
|
|
1300 template<typename _Yp>
|
|
1301 _SafeConv<_Yp>
|
|
1302 reset(_Yp* __p) // _Yp must be complete.
|
|
1303 {
|
|
1304 // Catch self-reset errors.
|
|
1305 __glibcxx_assert(__p == 0 || __p != _M_ptr);
|
|
1306 __shared_ptr(__p).swap(*this);
|
|
1307 }
|
|
1308
|
|
1309 template<typename _Yp, typename _Deleter>
|
|
1310 _SafeConv<_Yp>
|
|
1311 reset(_Yp* __p, _Deleter __d)
|
|
1312 { __shared_ptr(__p, std::move(__d)).swap(*this); }
|
|
1313
|
|
1314 template<typename _Yp, typename _Deleter, typename _Alloc>
|
|
1315 _SafeConv<_Yp>
|
|
1316 reset(_Yp* __p, _Deleter __d, _Alloc __a)
|
|
1317 { __shared_ptr(__p, std::move(__d), std::move(__a)).swap(*this); }
|
|
1318
|
145
|
1319 /// Return the stored pointer.
|
111
|
1320 element_type*
|
|
1321 get() const noexcept
|
|
1322 { return _M_ptr; }
|
|
1323
|
145
|
1324 /// Return true if the stored pointer is not null.
|
111
|
1325 explicit operator bool() const // never throws
|
|
1326 { return _M_ptr == 0 ? false : true; }
|
|
1327
|
145
|
1328 /// Return true if use_count() == 1.
|
111
|
1329 bool
|
|
1330 unique() const noexcept
|
|
1331 { return _M_refcount._M_unique(); }
|
|
1332
|
145
|
1333 /// If *this owns a pointer, return the number of owners, otherwise zero.
|
111
|
1334 long
|
|
1335 use_count() const noexcept
|
|
1336 { return _M_refcount._M_get_use_count(); }
|
|
1337
|
145
|
1338 /// Exchange both the owned pointer and the stored pointer.
|
111
|
1339 void
|
|
1340 swap(__shared_ptr<_Tp, _Lp>& __other) noexcept
|
|
1341 {
|
|
1342 std::swap(_M_ptr, __other._M_ptr);
|
|
1343 _M_refcount._M_swap(__other._M_refcount);
|
|
1344 }
|
|
1345
|
145
|
1346 /** @brief Define an ordering based on ownership.
|
|
1347 *
|
|
1348 * This function defines a strict weak ordering between two shared_ptr
|
|
1349 * or weak_ptr objects, such that one object is less than the other
|
|
1350 * unless they share ownership of the same pointer, or are both empty.
|
|
1351 * @{
|
|
1352 */
|
111
|
1353 template<typename _Tp1>
|
|
1354 bool
|
|
1355 owner_before(__shared_ptr<_Tp1, _Lp> const& __rhs) const noexcept
|
|
1356 { return _M_refcount._M_less(__rhs._M_refcount); }
|
|
1357
|
|
1358 template<typename _Tp1>
|
|
1359 bool
|
|
1360 owner_before(__weak_ptr<_Tp1, _Lp> const& __rhs) const noexcept
|
|
1361 { return _M_refcount._M_less(__rhs._M_refcount); }
|
145
|
1362 // @}
|
111
|
1363
|
|
1364 protected:
|
|
1365 // This constructor is non-standard, it is used by allocate_shared.
|
|
1366 template<typename _Alloc, typename... _Args>
|
145
|
1367 __shared_ptr(_Sp_alloc_shared_tag<_Alloc> __tag, _Args&&... __args)
|
|
1368 : _M_ptr(), _M_refcount(_M_ptr, __tag, std::forward<_Args>(__args)...)
|
|
1369 { _M_enable_shared_from_this_with(_M_ptr); }
|
111
|
1370
|
|
1371 template<typename _Tp1, _Lock_policy _Lp1, typename _Alloc,
|
|
1372 typename... _Args>
|
|
1373 friend __shared_ptr<_Tp1, _Lp1>
|
|
1374 __allocate_shared(const _Alloc& __a, _Args&&... __args);
|
|
1375
|
|
1376 // This constructor is used by __weak_ptr::lock() and
|
|
1377 // shared_ptr::shared_ptr(const weak_ptr&, std::nothrow_t).
|
|
1378 __shared_ptr(const __weak_ptr<_Tp, _Lp>& __r, std::nothrow_t)
|
|
1379 : _M_refcount(__r._M_refcount, std::nothrow)
|
|
1380 {
|
|
1381 _M_ptr = _M_refcount._M_get_use_count() ? __r._M_ptr : nullptr;
|
|
1382 }
|
|
1383
|
|
1384 friend class __weak_ptr<_Tp, _Lp>;
|
|
1385
|
|
1386 private:
|
|
1387
|
|
1388 template<typename _Yp>
|
|
1389 using __esft_base_t = decltype(__enable_shared_from_this_base(
|
|
1390 std::declval<const __shared_count<_Lp>&>(),
|
|
1391 std::declval<_Yp*>()));
|
|
1392
|
|
1393 // Detect an accessible and unambiguous enable_shared_from_this base.
|
|
1394 template<typename _Yp, typename = void>
|
|
1395 struct __has_esft_base
|
|
1396 : false_type { };
|
|
1397
|
|
1398 template<typename _Yp>
|
|
1399 struct __has_esft_base<_Yp, __void_t<__esft_base_t<_Yp>>>
|
|
1400 : __not_<is_array<_Tp>> { }; // No enable shared_from_this for arrays
|
|
1401
|
|
1402 template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
|
|
1403 typename enable_if<__has_esft_base<_Yp2>::value>::type
|
|
1404 _M_enable_shared_from_this_with(_Yp* __p) noexcept
|
|
1405 {
|
|
1406 if (auto __base = __enable_shared_from_this_base(_M_refcount, __p))
|
|
1407 __base->_M_weak_assign(const_cast<_Yp2*>(__p), _M_refcount);
|
|
1408 }
|
|
1409
|
|
1410 template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
|
|
1411 typename enable_if<!__has_esft_base<_Yp2>::value>::type
|
|
1412 _M_enable_shared_from_this_with(_Yp*) noexcept
|
|
1413 { }
|
|
1414
|
|
1415 void*
|
|
1416 _M_get_deleter(const std::type_info& __ti) const noexcept
|
|
1417 { return _M_refcount._M_get_deleter(__ti); }
|
|
1418
|
|
1419 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
|
|
1420 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
|
|
1421
|
|
1422 template<typename _Del, typename _Tp1, _Lock_policy _Lp1>
|
|
1423 friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&) noexcept;
|
|
1424
|
|
1425 template<typename _Del, typename _Tp1>
|
|
1426 friend _Del* get_deleter(const shared_ptr<_Tp1>&) noexcept;
|
|
1427
|
|
1428 element_type* _M_ptr; // Contained pointer.
|
|
1429 __shared_count<_Lp> _M_refcount; // Reference counter.
|
|
1430 };
|
|
1431
|
|
1432
|
|
1433 // 20.7.2.2.7 shared_ptr comparisons
|
|
1434 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
|
|
1435 inline bool
|
|
1436 operator==(const __shared_ptr<_Tp1, _Lp>& __a,
|
|
1437 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
|
|
1438 { return __a.get() == __b.get(); }
|
|
1439
|
|
1440 template<typename _Tp, _Lock_policy _Lp>
|
|
1441 inline bool
|
|
1442 operator==(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
|
|
1443 { return !__a; }
|
|
1444
|
|
1445 template<typename _Tp, _Lock_policy _Lp>
|
|
1446 inline bool
|
|
1447 operator==(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
|
|
1448 { return !__a; }
|
|
1449
|
|
1450 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
|
|
1451 inline bool
|
|
1452 operator!=(const __shared_ptr<_Tp1, _Lp>& __a,
|
|
1453 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
|
|
1454 { return __a.get() != __b.get(); }
|
|
1455
|
|
1456 template<typename _Tp, _Lock_policy _Lp>
|
|
1457 inline bool
|
|
1458 operator!=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
|
|
1459 { return (bool)__a; }
|
|
1460
|
|
1461 template<typename _Tp, _Lock_policy _Lp>
|
|
1462 inline bool
|
|
1463 operator!=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
|
|
1464 { return (bool)__a; }
|
|
1465
|
|
1466 template<typename _Tp, typename _Up, _Lock_policy _Lp>
|
|
1467 inline bool
|
|
1468 operator<(const __shared_ptr<_Tp, _Lp>& __a,
|
|
1469 const __shared_ptr<_Up, _Lp>& __b) noexcept
|
|
1470 {
|
|
1471 using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type;
|
|
1472 using _Up_elt = typename __shared_ptr<_Up, _Lp>::element_type;
|
|
1473 using _Vp = typename common_type<_Tp_elt*, _Up_elt*>::type;
|
|
1474 return less<_Vp>()(__a.get(), __b.get());
|
|
1475 }
|
|
1476
|
|
1477 template<typename _Tp, _Lock_policy _Lp>
|
|
1478 inline bool
|
|
1479 operator<(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
|
|
1480 {
|
|
1481 using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type;
|
|
1482 return less<_Tp_elt*>()(__a.get(), nullptr);
|
|
1483 }
|
|
1484
|
|
1485 template<typename _Tp, _Lock_policy _Lp>
|
|
1486 inline bool
|
|
1487 operator<(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
|
|
1488 {
|
|
1489 using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type;
|
|
1490 return less<_Tp_elt*>()(nullptr, __a.get());
|
|
1491 }
|
|
1492
|
|
1493 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
|
|
1494 inline bool
|
|
1495 operator<=(const __shared_ptr<_Tp1, _Lp>& __a,
|
|
1496 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
|
|
1497 { return !(__b < __a); }
|
|
1498
|
|
1499 template<typename _Tp, _Lock_policy _Lp>
|
|
1500 inline bool
|
|
1501 operator<=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
|
|
1502 { return !(nullptr < __a); }
|
|
1503
|
|
1504 template<typename _Tp, _Lock_policy _Lp>
|
|
1505 inline bool
|
|
1506 operator<=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
|
|
1507 { return !(__a < nullptr); }
|
|
1508
|
|
1509 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
|
|
1510 inline bool
|
|
1511 operator>(const __shared_ptr<_Tp1, _Lp>& __a,
|
|
1512 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
|
|
1513 { return (__b < __a); }
|
|
1514
|
|
1515 template<typename _Tp, _Lock_policy _Lp>
|
|
1516 inline bool
|
|
1517 operator>(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
|
|
1518 { return nullptr < __a; }
|
|
1519
|
|
1520 template<typename _Tp, _Lock_policy _Lp>
|
|
1521 inline bool
|
|
1522 operator>(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
|
|
1523 { return __a < nullptr; }
|
|
1524
|
|
1525 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
|
|
1526 inline bool
|
|
1527 operator>=(const __shared_ptr<_Tp1, _Lp>& __a,
|
|
1528 const __shared_ptr<_Tp2, _Lp>& __b) noexcept
|
|
1529 { return !(__a < __b); }
|
|
1530
|
|
1531 template<typename _Tp, _Lock_policy _Lp>
|
|
1532 inline bool
|
|
1533 operator>=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
|
|
1534 { return !(__a < nullptr); }
|
|
1535
|
|
1536 template<typename _Tp, _Lock_policy _Lp>
|
|
1537 inline bool
|
|
1538 operator>=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
|
|
1539 { return !(nullptr < __a); }
|
|
1540
|
|
1541 // 20.7.2.2.8 shared_ptr specialized algorithms.
|
|
1542 template<typename _Tp, _Lock_policy _Lp>
|
|
1543 inline void
|
|
1544 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b) noexcept
|
|
1545 { __a.swap(__b); }
|
|
1546
|
|
1547 // 20.7.2.2.9 shared_ptr casts
|
|
1548
|
|
1549 // The seemingly equivalent code:
|
|
1550 // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
|
|
1551 // will eventually result in undefined behaviour, attempting to
|
|
1552 // delete the same object twice.
|
|
1553 /// static_pointer_cast
|
|
1554 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
|
|
1555 inline __shared_ptr<_Tp, _Lp>
|
|
1556 static_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
|
|
1557 {
|
|
1558 using _Sp = __shared_ptr<_Tp, _Lp>;
|
|
1559 return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get()));
|
|
1560 }
|
|
1561
|
|
1562 // The seemingly equivalent code:
|
|
1563 // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
|
|
1564 // will eventually result in undefined behaviour, attempting to
|
|
1565 // delete the same object twice.
|
|
1566 /// const_pointer_cast
|
|
1567 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
|
|
1568 inline __shared_ptr<_Tp, _Lp>
|
|
1569 const_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
|
|
1570 {
|
|
1571 using _Sp = __shared_ptr<_Tp, _Lp>;
|
|
1572 return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get()));
|
|
1573 }
|
|
1574
|
|
1575 // The seemingly equivalent code:
|
|
1576 // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
|
|
1577 // will eventually result in undefined behaviour, attempting to
|
|
1578 // delete the same object twice.
|
|
1579 /// dynamic_pointer_cast
|
|
1580 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
|
|
1581 inline __shared_ptr<_Tp, _Lp>
|
|
1582 dynamic_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
|
|
1583 {
|
|
1584 using _Sp = __shared_ptr<_Tp, _Lp>;
|
|
1585 if (auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get()))
|
|
1586 return _Sp(__r, __p);
|
|
1587 return _Sp();
|
|
1588 }
|
|
1589
|
|
1590 #if __cplusplus > 201402L
|
|
1591 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
|
|
1592 inline __shared_ptr<_Tp, _Lp>
|
|
1593 reinterpret_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
|
|
1594 {
|
|
1595 using _Sp = __shared_ptr<_Tp, _Lp>;
|
|
1596 return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get()));
|
|
1597 }
|
|
1598 #endif
|
|
1599
|
|
1600 template<typename _Tp, _Lock_policy _Lp>
|
|
1601 class __weak_ptr
|
|
1602 {
|
|
1603 template<typename _Yp, typename _Res = void>
|
|
1604 using _Compatible = typename
|
|
1605 enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type;
|
|
1606
|
|
1607 // Constraint for assignment from shared_ptr and weak_ptr:
|
|
1608 template<typename _Yp>
|
|
1609 using _Assignable = _Compatible<_Yp, __weak_ptr&>;
|
|
1610
|
|
1611 public:
|
|
1612 using element_type = typename remove_extent<_Tp>::type;
|
|
1613
|
|
1614 constexpr __weak_ptr() noexcept
|
|
1615 : _M_ptr(nullptr), _M_refcount()
|
|
1616 { }
|
|
1617
|
|
1618 __weak_ptr(const __weak_ptr&) noexcept = default;
|
|
1619
|
|
1620 ~__weak_ptr() = default;
|
|
1621
|
|
1622 // The "obvious" converting constructor implementation:
|
|
1623 //
|
|
1624 // template<typename _Tp1>
|
|
1625 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
|
|
1626 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
|
|
1627 // { }
|
|
1628 //
|
|
1629 // has a serious problem.
|
|
1630 //
|
|
1631 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
|
|
1632 // conversion may require access to *__r._M_ptr (virtual inheritance).
|
|
1633 //
|
|
1634 // It is not possible to avoid spurious access violations since
|
|
1635 // in multithreaded programs __r._M_ptr may be invalidated at any point.
|
|
1636 template<typename _Yp, typename = _Compatible<_Yp>>
|
|
1637 __weak_ptr(const __weak_ptr<_Yp, _Lp>& __r) noexcept
|
|
1638 : _M_refcount(__r._M_refcount)
|
|
1639 { _M_ptr = __r.lock().get(); }
|
|
1640
|
|
1641 template<typename _Yp, typename = _Compatible<_Yp>>
|
|
1642 __weak_ptr(const __shared_ptr<_Yp, _Lp>& __r) noexcept
|
|
1643 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
|
|
1644 { }
|
|
1645
|
|
1646 __weak_ptr(__weak_ptr&& __r) noexcept
|
|
1647 : _M_ptr(__r._M_ptr), _M_refcount(std::move(__r._M_refcount))
|
|
1648 { __r._M_ptr = nullptr; }
|
|
1649
|
|
1650 template<typename _Yp, typename = _Compatible<_Yp>>
|
|
1651 __weak_ptr(__weak_ptr<_Yp, _Lp>&& __r) noexcept
|
|
1652 : _M_ptr(__r.lock().get()), _M_refcount(std::move(__r._M_refcount))
|
|
1653 { __r._M_ptr = nullptr; }
|
|
1654
|
|
1655 __weak_ptr&
|
|
1656 operator=(const __weak_ptr& __r) noexcept = default;
|
|
1657
|
|
1658 template<typename _Yp>
|
|
1659 _Assignable<_Yp>
|
|
1660 operator=(const __weak_ptr<_Yp, _Lp>& __r) noexcept
|
|
1661 {
|
|
1662 _M_ptr = __r.lock().get();
|
|
1663 _M_refcount = __r._M_refcount;
|
|
1664 return *this;
|
|
1665 }
|
|
1666
|
|
1667 template<typename _Yp>
|
|
1668 _Assignable<_Yp>
|
|
1669 operator=(const __shared_ptr<_Yp, _Lp>& __r) noexcept
|
|
1670 {
|
|
1671 _M_ptr = __r._M_ptr;
|
|
1672 _M_refcount = __r._M_refcount;
|
|
1673 return *this;
|
|
1674 }
|
|
1675
|
|
1676 __weak_ptr&
|
|
1677 operator=(__weak_ptr&& __r) noexcept
|
|
1678 {
|
|
1679 _M_ptr = __r._M_ptr;
|
|
1680 _M_refcount = std::move(__r._M_refcount);
|
|
1681 __r._M_ptr = nullptr;
|
|
1682 return *this;
|
|
1683 }
|
|
1684
|
|
1685 template<typename _Yp>
|
|
1686 _Assignable<_Yp>
|
|
1687 operator=(__weak_ptr<_Yp, _Lp>&& __r) noexcept
|
|
1688 {
|
|
1689 _M_ptr = __r.lock().get();
|
|
1690 _M_refcount = std::move(__r._M_refcount);
|
|
1691 __r._M_ptr = nullptr;
|
|
1692 return *this;
|
|
1693 }
|
|
1694
|
|
1695 __shared_ptr<_Tp, _Lp>
|
|
1696 lock() const noexcept
|
|
1697 { return __shared_ptr<element_type, _Lp>(*this, std::nothrow); }
|
|
1698
|
|
1699 long
|
|
1700 use_count() const noexcept
|
|
1701 { return _M_refcount._M_get_use_count(); }
|
|
1702
|
|
1703 bool
|
|
1704 expired() const noexcept
|
|
1705 { return _M_refcount._M_get_use_count() == 0; }
|
|
1706
|
|
1707 template<typename _Tp1>
|
|
1708 bool
|
|
1709 owner_before(const __shared_ptr<_Tp1, _Lp>& __rhs) const noexcept
|
|
1710 { return _M_refcount._M_less(__rhs._M_refcount); }
|
|
1711
|
|
1712 template<typename _Tp1>
|
|
1713 bool
|
|
1714 owner_before(const __weak_ptr<_Tp1, _Lp>& __rhs) const noexcept
|
|
1715 { return _M_refcount._M_less(__rhs._M_refcount); }
|
|
1716
|
|
1717 void
|
|
1718 reset() noexcept
|
|
1719 { __weak_ptr().swap(*this); }
|
|
1720
|
|
1721 void
|
|
1722 swap(__weak_ptr& __s) noexcept
|
|
1723 {
|
|
1724 std::swap(_M_ptr, __s._M_ptr);
|
|
1725 _M_refcount._M_swap(__s._M_refcount);
|
|
1726 }
|
|
1727
|
|
1728 private:
|
|
1729 // Used by __enable_shared_from_this.
|
|
1730 void
|
|
1731 _M_assign(_Tp* __ptr, const __shared_count<_Lp>& __refcount) noexcept
|
|
1732 {
|
|
1733 if (use_count() == 0)
|
|
1734 {
|
|
1735 _M_ptr = __ptr;
|
|
1736 _M_refcount = __refcount;
|
|
1737 }
|
|
1738 }
|
|
1739
|
|
1740 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
|
|
1741 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
|
|
1742 friend class __enable_shared_from_this<_Tp, _Lp>;
|
|
1743 friend class enable_shared_from_this<_Tp>;
|
|
1744
|
|
1745 element_type* _M_ptr; // Contained pointer.
|
|
1746 __weak_count<_Lp> _M_refcount; // Reference counter.
|
|
1747 };
|
|
1748
|
|
1749 // 20.7.2.3.6 weak_ptr specialized algorithms.
|
|
1750 template<typename _Tp, _Lock_policy _Lp>
|
|
1751 inline void
|
|
1752 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b) noexcept
|
|
1753 { __a.swap(__b); }
|
|
1754
|
|
1755 template<typename _Tp, typename _Tp1>
|
|
1756 struct _Sp_owner_less : public binary_function<_Tp, _Tp, bool>
|
|
1757 {
|
|
1758 bool
|
|
1759 operator()(const _Tp& __lhs, const _Tp& __rhs) const noexcept
|
|
1760 { return __lhs.owner_before(__rhs); }
|
|
1761
|
|
1762 bool
|
|
1763 operator()(const _Tp& __lhs, const _Tp1& __rhs) const noexcept
|
|
1764 { return __lhs.owner_before(__rhs); }
|
|
1765
|
|
1766 bool
|
|
1767 operator()(const _Tp1& __lhs, const _Tp& __rhs) const noexcept
|
|
1768 { return __lhs.owner_before(__rhs); }
|
|
1769 };
|
|
1770
|
|
1771 template<>
|
|
1772 struct _Sp_owner_less<void, void>
|
|
1773 {
|
|
1774 template<typename _Tp, typename _Up>
|
|
1775 auto
|
|
1776 operator()(const _Tp& __lhs, const _Up& __rhs) const noexcept
|
|
1777 -> decltype(__lhs.owner_before(__rhs))
|
|
1778 { return __lhs.owner_before(__rhs); }
|
|
1779
|
|
1780 using is_transparent = void;
|
|
1781 };
|
|
1782
|
|
1783 template<typename _Tp, _Lock_policy _Lp>
|
|
1784 struct owner_less<__shared_ptr<_Tp, _Lp>>
|
|
1785 : public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
|
|
1786 { };
|
|
1787
|
|
1788 template<typename _Tp, _Lock_policy _Lp>
|
|
1789 struct owner_less<__weak_ptr<_Tp, _Lp>>
|
|
1790 : public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
|
|
1791 { };
|
|
1792
|
|
1793
|
|
1794 template<typename _Tp, _Lock_policy _Lp>
|
|
1795 class __enable_shared_from_this
|
|
1796 {
|
|
1797 protected:
|
|
1798 constexpr __enable_shared_from_this() noexcept { }
|
|
1799
|
|
1800 __enable_shared_from_this(const __enable_shared_from_this&) noexcept { }
|
|
1801
|
|
1802 __enable_shared_from_this&
|
|
1803 operator=(const __enable_shared_from_this&) noexcept
|
|
1804 { return *this; }
|
|
1805
|
|
1806 ~__enable_shared_from_this() { }
|
|
1807
|
|
1808 public:
|
|
1809 __shared_ptr<_Tp, _Lp>
|
|
1810 shared_from_this()
|
|
1811 { return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
|
|
1812
|
|
1813 __shared_ptr<const _Tp, _Lp>
|
|
1814 shared_from_this() const
|
|
1815 { return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
|
|
1816
|
|
1817 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
|
|
1818 __weak_ptr<_Tp, _Lp>
|
|
1819 weak_from_this() noexcept
|
|
1820 { return this->_M_weak_this; }
|
|
1821
|
|
1822 __weak_ptr<const _Tp, _Lp>
|
|
1823 weak_from_this() const noexcept
|
|
1824 { return this->_M_weak_this; }
|
|
1825 #endif
|
|
1826
|
|
1827 private:
|
|
1828 template<typename _Tp1>
|
|
1829 void
|
|
1830 _M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const noexcept
|
|
1831 { _M_weak_this._M_assign(__p, __n); }
|
|
1832
|
|
1833 friend const __enable_shared_from_this*
|
|
1834 __enable_shared_from_this_base(const __shared_count<_Lp>&,
|
|
1835 const __enable_shared_from_this* __p)
|
|
1836 { return __p; }
|
|
1837
|
|
1838 template<typename, _Lock_policy>
|
|
1839 friend class __shared_ptr;
|
|
1840
|
|
1841 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
|
|
1842 };
|
|
1843
|
145
|
1844 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy,
|
|
1845 typename _Alloc, typename... _Args>
|
111
|
1846 inline __shared_ptr<_Tp, _Lp>
|
|
1847 __allocate_shared(const _Alloc& __a, _Args&&... __args)
|
|
1848 {
|
145
|
1849 return __shared_ptr<_Tp, _Lp>(_Sp_alloc_shared_tag<_Alloc>{__a},
|
111
|
1850 std::forward<_Args>(__args)...);
|
|
1851 }
|
|
1852
|
145
|
1853 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy,
|
|
1854 typename... _Args>
|
111
|
1855 inline __shared_ptr<_Tp, _Lp>
|
|
1856 __make_shared(_Args&&... __args)
|
|
1857 {
|
|
1858 typedef typename std::remove_const<_Tp>::type _Tp_nc;
|
|
1859 return std::__allocate_shared<_Tp, _Lp>(std::allocator<_Tp_nc>(),
|
|
1860 std::forward<_Args>(__args)...);
|
|
1861 }
|
|
1862
|
|
1863 /// std::hash specialization for __shared_ptr.
|
|
1864 template<typename _Tp, _Lock_policy _Lp>
|
|
1865 struct hash<__shared_ptr<_Tp, _Lp>>
|
|
1866 : public __hash_base<size_t, __shared_ptr<_Tp, _Lp>>
|
|
1867 {
|
|
1868 size_t
|
|
1869 operator()(const __shared_ptr<_Tp, _Lp>& __s) const noexcept
|
|
1870 {
|
|
1871 return hash<typename __shared_ptr<_Tp, _Lp>::element_type*>()(
|
|
1872 __s.get());
|
|
1873 }
|
|
1874 };
|
|
1875
|
|
1876 _GLIBCXX_END_NAMESPACE_VERSION
|
|
1877 } // namespace
|
|
1878
|
|
1879 #endif // _SHARED_PTR_BASE_H
|