]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/experimental/bits/shared_ptr.h
* include/experimental/bits/shared_ptr.h: Tweak comments.
[thirdparty/gcc.git] / libstdc++-v3 / include / experimental / bits / shared_ptr.h
CommitLineData
930d5602
FY
1// Experimental shared_ptr with array support -*- C++ -*-
2
3// Copyright (C) 2015 Free Software Foundation, Inc.
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/** @file experimental/bits/shared_ptr.h
26 * This is an internal header file, included by other library headers.
011b25e4 27 * Do not attempt to use it directly. @headername{experimental/memory}
930d5602
FY
28 */
29
30#ifndef _GLIBCXX_EXPERIMENTAL_SHARED_PTR_H
31#define _GLIBCXX_EXPERIMENTAL_SHARED_PTR_H 1
32
33#pragma GCC system_header
34
35#if __cplusplus <= 201103L
36# include <bits/c++14_warning.h>
37#else
38
39#include <memory>
40#include <experimental/type_traits>
41
42namespace std _GLIBCXX_VISIBILITY(default)
43{
44namespace experimental
45{
46inline namespace fundamentals_v2
47{
48_GLIBCXX_BEGIN_NAMESPACE_VERSION
49 template<typename _Tp> class enable_shared_from_this;
50_GLIBCXX_END_NAMESPACE_VERSION
51} // namespace fundamentals_v2
52} // namespace experimental
53
54#define __cpp_lib_experimental_shared_ptr_arrays 201406
55
56_GLIBCXX_BEGIN_NAMESPACE_VERSION
57
58 /*
59 * The specification of std::experimental::shared_ptr is slightly different
011b25e4 60 * to std::shared_ptr (specifically in terms of "compatible" pointers) so
930d5602
FY
61 * to implement std::experimental::shared_ptr without too much duplication
62 * we make it derive from a partial specialization of std::__shared_ptr
63 * using a special tag type, __libfund_v1.
64 *
65 * There are two partial specializations for the tag type, supporting the
66 * different interfaces of the array and non-array forms.
67 */
68
69 template <typename _Tp, bool = is_array<_Tp>::value>
70 struct __libfund_v1 { using type = _Tp; };
71
72 // helper for _Compatible
73 template<typename _From_type, typename _To_type>
74 struct __sp_compatible
75 : is_convertible<_From_type*, _To_type*>::type
76 { };
77
78 template<size_t _Nm, typename _Tp>
79 struct __sp_compatible<_Tp[_Nm], _Tp[]>
80 : true_type
81 { };
82
83 template<size_t _Nm, typename _Tp>
84 struct __sp_compatible<_Tp[_Nm], const _Tp[]>
85 : true_type
86 { };
87
88 // Partial specialization for base class of experimental::shared_ptr<T>
89 // (i.e. the non-array form of experimental::shared_ptr)
90 template<typename _Tp, _Lock_policy _Lp>
91 class __shared_ptr<__libfund_v1<_Tp, false>, _Lp>
92 : private __shared_ptr<_Tp, _Lp>
93 {
94 template<typename _Tp1>
95 using _Compatible
96 = enable_if_t<__sp_compatible<_Tp1, _Tp>::value>;
97
98 using _Base_type = __shared_ptr<_Tp>;
99
100 _Base_type& _M_get_base() { return *this;}
101 const _Base_type& _M_get_base() const { return *this;}
102
103 public:
104 using element_type = _Tp;
105
106 constexpr __shared_ptr() noexcept = default;
107
108 template<typename _Tp1>
109 explicit __shared_ptr(_Tp1* __p)
110 : _Base_type(__p)
111 { }
112
113 template<typename _Tp1, typename _Deleter>
114 __shared_ptr(_Tp1* __p, _Deleter __d)
115 : _Base_type(__p, __d)
116 { }
117
118 template<typename _Tp1, typename _Deleter, typename _Alloc>
119 __shared_ptr(_Tp1* __p, _Deleter __d, _Alloc __a)
120 : _Base_type(__p, __d, __a)
121 { }
122
123 template<typename _Deleter>
124 __shared_ptr(nullptr_t __p, _Deleter __d)
125 : _Base_type(__p, __d)
126 { }
127
128 template<typename _Deleter, typename _Alloc>
129 __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
130 : _Base_type(__p, __d, __a)
131 { }
132
133 template<typename _Tp1>
134 __shared_ptr(const __shared_ptr<__libfund_v1<_Tp1>, _Lp>& __r,
135 element_type* __p) noexcept
136 : _Base_type(__r._M_get_base(), __p)
137 { }
138
139 __shared_ptr(const __shared_ptr&) noexcept = default;
140 __shared_ptr(__shared_ptr&&) noexcept = default;
141 __shared_ptr& operator=(const __shared_ptr&) noexcept = default;
142 __shared_ptr& operator=(__shared_ptr&&) noexcept = default;
143 ~__shared_ptr() = default;
144
145 template<typename _Tp1, typename = _Compatible<_Tp1>>
146 __shared_ptr(const __shared_ptr<__libfund_v1<_Tp1>, _Lp>& __r) noexcept
147 : _Base_type(__r._M_get_base())
148 { }
149
150 template<typename _Tp1, typename = _Compatible<_Tp1>>
151 __shared_ptr(__shared_ptr<__libfund_v1<_Tp1>, _Lp>&& __r) noexcept
152 : _Base_type(std::move((__r._M_get_base())))
153 { }
154
155 template<typename _Tp1>
156 explicit __shared_ptr(const __weak_ptr<__libfund_v1<_Tp1>, _Lp>& __r)
157 : _Base_type(__r._M_get_base())
158 { }
159
160 template<typename _Tp1, typename _Del, typename
161 = _Compatible<remove_pointer_t<
162 typename unique_ptr<_Tp1, _Del>::pointer>>>
163 __shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r)
164 : _Base_type(std::move(__r))
165 { }
166
167#if _GLIBCXX_USE_DEPRECATED
168 // Postcondition: use_count() == 1 and __r.get() == 0
169 template<typename _Tp1>
170 __shared_ptr(std::auto_ptr<_Tp1>&& __r)
171 : _Base_type(std::move(__r))
172 { }
173#endif
174
175 constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { }
176
177 // reset
178 void
179 reset() noexcept
180 { __shared_ptr(nullptr).swap(*this); }
181
182 template<typename _Tp1>
183 void
184 reset(_Tp1* __p)
185 {
186 _GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != get());
187 __shared_ptr(__p).swap(*this);
188 }
189
190 template<typename _Tp1, typename _Deleter>
191 void
192 reset(_Tp1* __p, _Deleter __d)
193 { __shared_ptr(__p, __d).swap(*this); }
194
195 template<typename _Tp1, typename _Deleter, typename _Alloc>
196 void
197 reset(_Tp1* __p, _Deleter __d, _Alloc __a)
198 { __shared_ptr(__p, __d, std::move(__a)).swap(*this); }
199
200 using _Base_type::operator*;
201 using _Base_type::operator->;
202
203 template<typename _Tp1>
204 __shared_ptr&
205 operator=(const __shared_ptr<__libfund_v1<_Tp1>, _Lp>& __r) noexcept
206 {
207 _Base_type::operator=(__r._M_get_base());
208 return *this;
209 }
210
211 template<class _Tp1>
212 __shared_ptr&
213 operator=(__shared_ptr<__libfund_v1<_Tp1>, _Lp>&& __r) noexcept
214 {
215 _Base_type::operator=(std::move(__r._M_get_base()));
216 return *this;
217 }
218
219 template<typename _Tp1>
220 __shared_ptr&
221 operator=(std::unique_ptr<_Tp1>&& __r)
222 {
223 _Base_type::operator=(std::move(__r));
224 return *this;
225 }
226
227#if _GLIBCXX_USE_DEPRECATED
228 template<typename _Tp1>
229 __shared_ptr&
230 operator=(std::auto_ptr<_Tp1>&& __r)
231 {
232 _Base_type::operator=(std::move(__r));
233 return *this;
234 }
235#endif
236
237 void
238 swap(__shared_ptr& __other) noexcept
239 { _Base_type::swap(__other); }
240
241 template<typename _Tp1>
242 bool
243 owner_before(__shared_ptr<__libfund_v1<_Tp1>, _Lp> const& __rhs) const
244 { return _Base_type::owner_before(__rhs._M_get_base()); }
245
246 template<typename _Tp1>
247 bool
248 owner_before(__weak_ptr<__libfund_v1<_Tp1>, _Lp> const& __rhs) const
249 { return _Base_type::owner_before(__rhs._M_get_base()); }
250
251 using _Base_type::operator bool;
252 using _Base_type::get;
253 using _Base_type::unique;
254 using _Base_type::use_count;
255
256 protected:
257
258 // make_shared not yet support for shared_ptr_arrays
259 //template<typename _Alloc, typename... _Args>
260 // __shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a,
261 // _Args&&... __args)
262 // : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a,
263 // std::forward<_Args>(__args)...)
264 // {
265 // void* __p = _M_refcount._M_get_deleter(typeid(__tag));
266 // _M_ptr = static_cast<_Tp*>(__p);
267 // __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
268 // }
269
270 // __weak_ptr::lock()
271 __shared_ptr(const __weak_ptr<__libfund_v1<_Tp>, _Lp>& __r,
272 std::nothrow_t)
273 : _Base_type(__r._M_get_base(), std::nothrow)
274 { }
275
276 private:
277 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
278 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
279
280 // TODO
281 template<typename _Del, typename _Tp1, _Lock_policy _Lp1>
282 friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&) noexcept;
283 };
284
285 // Partial specialization for base class of experimental::shared_ptr<T[N]>
286 // and experimental::shared_ptr<T[]> (i.e. the array forms).
287 template<typename _Tp, _Lock_policy _Lp>
288 class __shared_ptr<__libfund_v1<_Tp, true>, _Lp>
289 : private __shared_ptr<remove_extent_t<_Tp>, _Lp>
290 {
291 public:
292 using element_type = remove_extent_t<_Tp>;
293
294 private:
295 struct _Array_Deleter
296 {
297 void
298 operator()(element_type const *__p) const
299 { delete [] __p; }
300 };
301
302 struct _Normal_Deleter
303 {
304 void
305 operator()(element_type const *__p) const
306 { delete __p; }
307 };
308
309 template<typename _Tp1>
310 using _Compatible
311 = enable_if_t<__sp_compatible<_Tp1, _Tp>::value>;
312
313 using _Deleter_type
314 = conditional_t<is_array<_Tp>::value, _Array_Deleter, _Normal_Deleter>;
315
316 using _Base_type = __shared_ptr<element_type>;
317
318 _Base_type& _M_get_base() { return *this;}
319 const _Base_type& _M_get_base() const { return *this;}
320
321 public:
322 constexpr __shared_ptr() noexcept
323 : _Base_type()
324 { }
325
326 template<typename _Tp1>
327 explicit __shared_ptr(_Tp1* __p)
328 : _Base_type(__p, _Deleter_type())
329 { }
330
331 template<typename _Tp1, typename _Deleter>
332 __shared_ptr(_Tp1* __p, _Deleter __d)
333 : _Base_type(__p, __d)
334 { }
335
336 template<typename _Tp1, typename _Deleter, typename _Alloc>
337 __shared_ptr(_Tp1* __p, _Deleter __d, _Alloc __a)
338 : _Base_type(__p, __d, __a)
339 { }
340
341 template<typename _Deleter>
342 __shared_ptr(nullptr_t __p, _Deleter __d)
343 : _Base_type(__p, __d)
344 { }
345
346 template<typename _Deleter, typename _Alloc>
347 __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
348 : _Base_type(__p, __d, __a)
349 { }
350
351 template<typename _Tp1>
352 __shared_ptr(const __shared_ptr<__libfund_v1<_Tp1>, _Lp>& __r,
353 element_type* __p) noexcept
354 : _Base_type(__r._M_get_base(), __p)
355 { }
356
357 __shared_ptr(const __shared_ptr&) noexcept = default;
358 __shared_ptr(__shared_ptr&&) noexcept = default;
359 __shared_ptr& operator=(const __shared_ptr&) noexcept = default;
360 __shared_ptr& operator=(__shared_ptr&&) noexcept = default;
361 ~__shared_ptr() = default;
362
363 template<typename _Tp1, typename = _Compatible<_Tp1>>
364 __shared_ptr(const __shared_ptr<__libfund_v1<_Tp1>, _Lp>& __r) noexcept
365 : _Base_type(__r._M_get_base())
366 { }
367
368 template<typename _Tp1, typename = _Compatible<_Tp1>>
369 __shared_ptr(__shared_ptr<__libfund_v1<_Tp1>, _Lp>&& __r) noexcept
370 : _Base_type(std::move((__r._M_get_base())))
371 { }
372
373 template<typename _Tp1>
374 explicit __shared_ptr(const __weak_ptr<__libfund_v1<_Tp1>, _Lp>& __r)
375 : _Base_type(__r._M_get_base())
376 { }
377
378 template<typename _Tp1, typename _Del, typename
379 = _Compatible<remove_pointer_t<
380 typename unique_ptr<_Tp1, _Del>::pointer>>>
381 __shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r)
382 : _Base_type(std::move(__r))
383 { }
384
385#if _GLIBCXX_USE_DEPRECATED
386 // Postcondition: use_count() == 1 and __r.get() == 0
387 template<typename _Tp1>
388 __shared_ptr(std::auto_ptr<_Tp1>&& __r)
389 : _Base_type(std::move(__r))
390 { }
391#endif
392
393 constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { }
394
395 // reset
396 void
397 reset() noexcept
398 { __shared_ptr(nullptr).swap(*this); }
399
400 template<typename _Tp1>
401 void
402 reset(_Tp1* __p)
403 {
404 _GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != get());
405 __shared_ptr(__p, _Deleter_type()).swap(*this);
406 }
407
408 template<typename _Tp1, typename _Deleter>
409 void
410 reset(_Tp1* __p, _Deleter __d)
411 { __shared_ptr(__p, __d).swap(*this); }
412
413 template<typename _Tp1, typename _Deleter, typename _Alloc>
414 void
415 reset(_Tp1* __p, _Deleter __d, _Alloc __a)
416 { __shared_ptr(__p, __d, std::move(__a)).swap(*this); }
417
418 element_type&
419 operator[](ptrdiff_t i) const noexcept
420 {
421 _GLIBCXX_DEBUG_ASSERT(get() != 0 && i >= 0);
422 return get()[i];
423 }
424
425 template<typename _Tp1>
426 __shared_ptr&
427 operator=(const __shared_ptr<__libfund_v1<_Tp1>, _Lp>& __r) noexcept
428 {
429 _Base_type::operator=(__r._M_get_base());
430 return *this;
431 }
432
433 template<class _Tp1>
434 __shared_ptr&
435 operator=(__shared_ptr<__libfund_v1<_Tp1>, _Lp>&& __r) noexcept
436 {
437 _Base_type::operator=(std::move(__r._M_get_base()));
438 return *this;
439 }
440
441 template<typename _Tp1>
442 __shared_ptr&
443 operator=(std::unique_ptr<_Tp1>&& __r)
444 {
445 _Base_type::operator=(std::move(__r));
446 return *this;
447 }
448
449#if _GLIBCXX_USE_DEPRECATED
450 template<typename _Tp1>
451 __shared_ptr&
452 operator=(std::auto_ptr<_Tp1>&& __r)
453 {
454 _Base_type::operator=(std::move(__r));
455 return *this;
456 }
457#endif
458
459 void
460 swap(__shared_ptr& __other) noexcept
461 { _Base_type::swap(__other); }
462
463 template<typename _Tp1>
464 bool
465 owner_before(__shared_ptr<__libfund_v1<_Tp1>, _Lp> const& __rhs) const
466 { return _Base_type::owner_before(__rhs._M_get_base()); }
467
468 template<typename _Tp1>
469 bool
470 owner_before(__weak_ptr<__libfund_v1<_Tp1>, _Lp> const& __rhs) const
471 { return _Base_type::owner_before(__rhs._M_get_base()); }
472
473 using _Base_type::operator bool;
474 using _Base_type::get;
475 using _Base_type::unique;
476 using _Base_type::use_count;
477
478 protected:
479
480 // make_shared not yet support for shared_ptr_arrays
481 //template<typename _Alloc, typename... _Args>
482 // __shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a,
483 // _Args&&... __args)
484 // : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a,
485 // std::forward<_Args>(__args)...)
486 // {
487 // void* __p = _M_refcount._M_get_deleter(typeid(__tag));
488 // _M_ptr = static_cast<_Tp*>(__p);
489 // __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
490 // }
491
492 // __weak_ptr::lock()
493 __shared_ptr(const __weak_ptr<__libfund_v1<_Tp>, _Lp>& __r,
494 std::nothrow_t)
495 : _Base_type(__r._M_get_base(), std::nothrow)
496 { }
497
498 private:
499 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
500 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
501
502 // TODO
503 template<typename _Del, typename _Tp1, _Lock_policy _Lp1>
504 friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&) noexcept;
505 };
506
507 // weak_ptr specialization for __shared_ptr array
508 template<typename _Tp, _Lock_policy _Lp>
509 class __weak_ptr<__libfund_v1<_Tp>, _Lp>
510 : __weak_ptr<remove_extent_t<_Tp>, _Lp>
511 {
512 template<typename _Tp1>
513 using _Compatible = enable_if_t<__sp_compatible<_Tp1, _Tp>::value>;
514
515 using _Base_type = __weak_ptr<remove_extent_t<_Tp>>;
516
517 _Base_type& _M_get_base() { return *this;}
518 const _Base_type& _M_get_base() const { return *this; }
519
520 public:
521 using element_type = remove_extent_t<_Tp>;
522
523 constexpr __weak_ptr() noexcept
524 : _Base_type()
525 { }
526
527 __weak_ptr(const __weak_ptr&) noexcept = default;
528
529 ~__weak_ptr() = default;
530
531 template<typename _Tp1, typename = _Compatible<_Tp1>>
532 __weak_ptr(const __weak_ptr<__libfund_v1<_Tp1>, _Lp>& __r) noexcept
533 : _Base_type(__r._M_get_base())
534 { }
535
536 template<typename _Tp1, typename = _Compatible<_Tp1>>
537 __weak_ptr(const __shared_ptr<__libfund_v1<_Tp1>, _Lp>& __r) noexcept
538 : _Base_type(__r._M_get_base())
539 { }
540
541 __weak_ptr(__weak_ptr&& __r) noexcept
542 : _Base_type(std::move(__r))
543 { }
544
545 template<typename _Tp1, typename = _Compatible<_Tp1>>
546 __weak_ptr(__weak_ptr<__libfund_v1<_Tp1>, _Lp>&& __r) noexcept
547 : _Base_type(std::move(__r._M_get_base()))
548 { }
549
550 __weak_ptr&
551 operator=(const __weak_ptr& __r) noexcept = default;
552
553 template<typename _Tp1>
554 __weak_ptr&
555 operator=(const __weak_ptr<__libfund_v1<_Tp1>, _Lp>& __r) noexcept
556 {
557 this->_Base_type::operator=(__r._M_get_base());
558 return *this;
559 }
560
561 template<typename _Tp1>
562 __weak_ptr&
563 operator=(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
564 {
565 this->_Base_type::operator=(__r._M_get_base());
566 return *this;
567 }
568
569 __weak_ptr&
570 operator=(__weak_ptr&& __r) noexcept
571 {
572 this->_Base_type::operator=(std::move(__r));
573 return *this;
574 }
575
576 template<typename _Tp1>
577 __weak_ptr&
578 operator=(__weak_ptr<_Tp1, _Lp>&& __r) noexcept
579 {
580 this->_Base_type::operator=(std::move(__r._M_get_base()));
581 return *this;
582 }
583
584 void
585 swap(__weak_ptr& __other) noexcept
586 { this->_Base_type::swap(__other); }
587
588 template<typename _Tp1>
589 bool
590 owner_before(const __shared_ptr<__libfund_v1<_Tp1>, _Lp>& __rhs) const
591 { return _Base_type::owner_before(__rhs._M_get_base()); }
592
593 template<typename _Tp1>
594 bool
595 owner_before(const __weak_ptr<__libfund_v1<_Tp1>, _Lp>& __rhs) const
596 { return _Base_type::owner_before(__rhs._M_get_base()); }
597
598 __shared_ptr<__libfund_v1<_Tp>, _Lp>
599 lock() const noexcept // should not be element_type
600 { return __shared_ptr<__libfund_v1<_Tp>, _Lp>(*this, std::nothrow); }
601
602 using _Base_type::use_count;
603 using _Base_type::expired;
604 using _Base_type::reset;
605
606 private:
607 // Used by __enable_shared_from_this.
608 void
609 _M_assign(element_type* __ptr,
610 const __shared_count<_Lp>& __refcount) noexcept
611 { this->_Base_type::_M_assign(__ptr, __refcount); }
612
613 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
614 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
615 friend class __enable_shared_from_this<_Tp, _Lp>;
616 friend class experimental::enable_shared_from_this<_Tp>;
617 friend class enable_shared_from_this<_Tp>;
618 };
619
620_GLIBCXX_END_NAMESPACE_VERSION
621
622namespace experimental
623{
624inline namespace fundamentals_v2
625{
626_GLIBCXX_BEGIN_NAMESPACE_VERSION
627
628 // 8.2.1
629
630 template<typename _Tp> class shared_ptr;
631 template<typename _Tp> class weak_ptr;
632
633 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
634 using __shared_ptr = std::__shared_ptr<__libfund_v1<_Tp>, _Lp>;
635
636 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
637 using __weak_ptr = std::__weak_ptr<__libfund_v1<_Tp>, _Lp>;
638
639 template<typename _Tp>
640 class shared_ptr : public __shared_ptr<_Tp>
641 {
642 template<typename _Tp1>
643 using _Compatible = enable_if_t<__sp_compatible<_Tp1, _Tp>::value>;
644
645 using _Base_type = __shared_ptr<_Tp>;
646
647 public:
648 using element_type = typename _Base_type::element_type;
649
650 // 8.2.1.1, shared_ptr constructors
651 constexpr shared_ptr() noexcept = default;
652
653 template<typename _Tp1>
654 explicit shared_ptr(_Tp1* __p) : _Base_type(__p) { }
655
656 template<typename _Tp1, typename _Deleter>
657 shared_ptr(_Tp1* __p, _Deleter __d)
658 : _Base_type(__p, __d) { }
659
660 template<typename _Tp1, typename _Deleter, typename _Alloc>
661 shared_ptr(_Tp1* __p, _Deleter __d, _Alloc __a)
662 : _Base_type(__p, __d, __a) { }
663
664 template<typename _Deleter>
665 shared_ptr(nullptr_t __p, _Deleter __d)
666 : _Base_type(__p, __d) { }
667
668 template<typename _Deleter, typename _Alloc>
669 shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
670 : _Base_type(__p, __d, __a) { }
671
672 template<typename _Tp1>
673 shared_ptr(const shared_ptr<_Tp1>& __r, element_type* __p) noexcept
674 : _Base_type(__r, __p) { }
675
676 shared_ptr(const shared_ptr& __r) noexcept
677 : _Base_type(__r) { }
678
679 template<typename _Tp1, typename = _Compatible<_Tp1>>
680 shared_ptr(const shared_ptr<_Tp1>& __r) noexcept
681 : _Base_type(__r) { }
682
683 shared_ptr(const shared_ptr<_Tp>&& __r) noexcept
684 : _Base_type(std::move(__r)) { }
685
686 template<typename _Tp1, typename = _Compatible<_Tp1>>
687 shared_ptr(shared_ptr<_Tp1>&& __r) noexcept
688 : _Base_type(std::move(__r)) { }
689
690 template<typename _Tp1>
691 explicit shared_ptr(const weak_ptr<_Tp1>& __r)
692 : _Base_type(__r) { }
693
694#if _GLIBCXX_USE_DEPRECATED
695 template<typename _Tp1>
696 shared_ptr(std::auto_ptr<_Tp1>&& __r)
697 : _Base_type() { } // TODO
698#endif
699
700 template<typename _Tp1, typename _Del, typename
701 = _Compatible<remove_pointer_t<
702 typename unique_ptr<_Tp1, _Del>::pointer>>>
703 shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r)
704 : _Base_type(std::move(__r)) { }
705
706 constexpr shared_ptr(nullptr_t __p)
707 : _Base_type(__p) { }
708
709 // C++14 §20.8.2.2
710 ~shared_ptr() = default;
711
712 // C++14 §20.8.2.3
713 shared_ptr& operator=(const shared_ptr&) noexcept = default;
714
715 template <typename _Tp1>
716 shared_ptr&
717 operator=(const shared_ptr<_Tp1>& __r) noexcept
718 {
719 _Base_type::operator=(__r);
720 return *this;
721 }
722
723 shared_ptr&
724 operator=(shared_ptr&& __r) noexcept
725 {
726 _Base_type::operator=(std::move(__r));
727 return *this;
728 }
729
730 template <typename _Tp1>
731 shared_ptr&
732 operator=(shared_ptr<_Tp1>&& __r) noexcept
733 {
734 _Base_type::operator=(std::move(__r));
735 return *this;
736 }
737
738#if _GLIBCXX_USE_DEPRECATED
739 template<typename _Tp1>
740 shared_ptr&
741 operator=(std::auto_ptr<_Tp1>&& __r)
742 {
743 __shared_ptr<_Tp>::operator=(std::move(__r));
744 return *this;
745 }
746#endif
747
748 template <typename _Tp1, typename _Del>
749 shared_ptr&
750 operator=(unique_ptr<_Tp1, _Del>&& __r)
751 {
752 _Base_type::operator=(std::move(__r));
753 return *this;
754 }
755
756 // C++14 §20.8.2.2.4
757 // swap & reset
758 // 8.2.1.2 shared_ptr observers
759 // in __shared_ptr
760
761 private:
762 template<typename _Alloc, typename... _Args>
763 shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a,
764 _Args&&... __args)
765 : _Base_type(__tag, __a, std::forward<_Args>(__args)...)
766 { }
767
768 template<typename _Tp1, typename _Alloc, typename... _Args>
769 friend shared_ptr<_Tp1>
770 allocate_shared(const _Alloc& __a, _Args&&... __args);
771
772 shared_ptr(const weak_ptr<_Tp>& __r, std::nothrow_t)
773 : _Base_type(__r, std::nothrow) { }
774
775 friend class weak_ptr<_Tp>;
776 };
777
778 // C++14 §20.8.2.2.7 //DOING
779 template<typename _Tp1, typename _Tp2>
780 bool operator==(const shared_ptr<_Tp1>& __a,
781 const shared_ptr<_Tp2>& __b) noexcept
782 { return __a.get() == __b.get(); }
783
784 template<typename _Tp>
785 inline bool
786 operator==(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
787 { return !__a; }
788
789 template<typename _Tp>
790 inline bool
791 operator==(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
792 { return !__a; }
793
794 template<typename _Tp1, typename _Tp2>
795 inline bool
796 operator!=(const shared_ptr<_Tp1>& __a,
797 const shared_ptr<_Tp2>& __b) noexcept
798 { return __a.get() != __b.get(); }
799
800 template<typename _Tp>
801 inline bool
802 operator!=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
803 { return (bool)__a; }
804
805 template<typename _Tp>
806 inline bool
807 operator!=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
808 { return (bool)__a; }
809
810 template<typename _Tp1, typename _Tp2>
811 inline bool
812 operator<(const shared_ptr<_Tp1>& __a,
813 const shared_ptr<_Tp2>& __b) noexcept
814 {
815 using __elem_t1 = typename shared_ptr<_Tp1>::element_type;
816 using __elem_t2 = typename shared_ptr<_Tp2>::element_type;
817 using _CT = common_type_t<__elem_t1*, __elem_t2*>;
818 return std::less<_CT>()(__a.get(), __b.get());
819 }
820
821 template<typename _Tp>
822 inline bool
823 operator<(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
824 {
825 using __elem_t = typename shared_ptr<_Tp>::element_type;
826 return std::less<__elem_t>()(__a.get(), nullptr);
827 }
828
829 template<typename _Tp>
830 inline bool
831 operator<(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
832 {
833 using __elem_t = typename shared_ptr<_Tp>::element_type;
834 return std::less<__elem_t*>()(nullptr, __a.get());
835 }
836
837 template<typename _Tp1, typename _Tp2>
838 inline bool
839 operator<=(const shared_ptr<_Tp1>& __a,
840 const shared_ptr<_Tp2>& __b) noexcept
841 { return !(__b < __a); }
842
843 template<typename _Tp>
844 inline bool
845 operator<=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
846 { return !(nullptr < __a); }
847
848 template<typename _Tp>
849 inline bool
850 operator<=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
851 { return !(__a < nullptr); }
852
853 template<typename _Tp1, typename _Tp2>
854 inline bool
855 operator>(const shared_ptr<_Tp1>& __a,
856 const shared_ptr<_Tp2>& __b) noexcept
857 { return (__b < __a); }
858
859 template<typename _Tp>
860 inline bool
861 operator>(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
862 {
863 using __elem_t = typename shared_ptr<_Tp>::element_type;
864 return std::less<__elem_t*>()(nullptr, __a.get());
865 }
866
867 template<typename _Tp>
868 inline bool
869 operator>(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
870 {
871 using __elem_t = typename shared_ptr<_Tp>::element_type;
872 return std::less<__elem_t*>()(__a.get(), nullptr);
873 }
874
875 template<typename _Tp1, typename _Tp2>
876 inline bool
877 operator>=(const shared_ptr<_Tp1>& __a,
878 const shared_ptr<_Tp2>& __b) noexcept
879 { return !(__a < __b); }
880
881 template<typename _Tp>
882 inline bool
883 operator>=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
884 { return !(__a < nullptr); }
885
886 template<typename _Tp>
887 inline bool
888 operator>=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
889 { return !(nullptr < __a); }
890
891 // C++14 §20.8.2.2.8
892 template<typename _Tp>
893 inline void
894 swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>& __b) noexcept
895 { __a.swap(__b); }
896
897 // 8.2.1.3, shared_ptr casts
898 template<typename _Tp, typename _Tp1>
899 inline shared_ptr<_Tp>
900 static_pointer_cast(const shared_ptr<_Tp1>& __r) noexcept
901 {
902 using __elem_t = typename shared_ptr<_Tp>::element_type;
903 return shared_ptr<_Tp>(__r, static_cast<__elem_t*>(__r.get()));
904 }
905
906 template<typename _Tp, typename _Tp1>
907 inline shared_ptr<_Tp>
908 dynamic_pointer_cast(const shared_ptr<_Tp1>& __r) noexcept
909 {
910 using __elem_t = typename shared_ptr<_Tp>::element_type;
911 if (_Tp* __p = dynamic_cast<__elem_t*>(__r.get()))
912 return shared_ptr<_Tp>(__r, __p);
913 return shared_ptr<_Tp>();
914 }
915
916 template<typename _Tp, typename _Tp1>
917 inline shared_ptr<_Tp>
918 const_pointer_cast(const shared_ptr<_Tp1>& __r) noexcept
919 {
920 using __elem_t = typename shared_ptr<_Tp>::element_type;
921 return shared_ptr<_Tp>(__r, const_cast<__elem_t*>(__r.get()));
922 }
923
924 template<typename _Tp, typename _Tp1>
925 inline shared_ptr<_Tp>
926 reinterpret_pointer_cast(const shared_ptr<_Tp1>& __r) noexcept
927 {
928 using __elem_t = typename shared_ptr<_Tp>::element_type;
929 return shared_ptr<_Tp>(__r, reinterpret_cast<__elem_t*>(__r.get()));
930 }
931
932 // C++14 §20.8.2.3
933 template<typename _Tp>
934 class weak_ptr : public __weak_ptr<_Tp>
935 {
936 template<typename _Tp1>
937 using _Compatible = enable_if_t<__sp_compatible<_Tp1, _Tp>::value>;
938
939 using _Base_type = __weak_ptr<_Tp>;
940
941 public:
942 constexpr weak_ptr() noexcept = default;
943
944 template<typename _Tp1, typename = _Compatible<_Tp1>>
945 weak_ptr(const shared_ptr<_Tp1>& __r) noexcept
946 : _Base_type(__r) { }
947
948 weak_ptr(const weak_ptr&) noexcept = default;
949
950 template<typename _Tp1, typename = _Compatible<_Tp1>>
951 weak_ptr(const weak_ptr<_Tp1>& __r) noexcept
952 : _Base_type(__r) { }
953
954 weak_ptr(weak_ptr&&) noexcept = default;
955
956 template<typename _Tp1, typename = _Compatible<_Tp1>>
957 weak_ptr(weak_ptr<_Tp1>&& __r) noexcept
958 : _Base_type(std::move(__r)) { }
959
960 weak_ptr&
961 operator=(const weak_ptr& __r) noexcept = default;
962
963 template<typename _Tp1>
964 weak_ptr&
965 operator=(const weak_ptr<_Tp1>& __r) noexcept
966 {
967 this->_Base_type::operator=(__r);
968 return *this;
969 }
970
971 template<typename _Tp1>
972 weak_ptr&
973 operator=(const shared_ptr<_Tp1>& __r) noexcept
974 {
975 this->_Base_type::operator=(__r);
976 return *this;
977 }
978
979 weak_ptr&
980 operator=(weak_ptr&& __r) noexcept = default;
981
982 template<typename _Tp1>
983 weak_ptr&
984 operator=(weak_ptr<_Tp1>&& __r) noexcept
985 {
986 this->_Base_type::operator=(std::move(__r));
987 return *this;
988 }
989
990 shared_ptr<_Tp>
991 lock() const noexcept
992 { return shared_ptr<_Tp>(*this, std::nothrow); }
993
994 friend class enable_shared_from_this<_Tp>;
995 };
996
997 // C++14 §20.8.2.3.6
998 template<typename _Tp>
999 inline void
1000 swap(weak_ptr<_Tp>& __a, weak_ptr<_Tp>& __b) noexcept
1001 { __a.swap(__b); }
1002
1003 /// C++14 §20.8.2.2.10
1004 template<typename _Del, typename _Tp, _Lock_policy _Lp>
1005 inline _Del*
1006 get_deleter(const __shared_ptr<_Tp, _Lp>& __p) noexcept
1007 { return std::get_deleter<_Del>(__p); }
1008
1009 // C++14 §20.8.2.2.11
1010 template<typename _Ch, typename _Tr, typename _Tp, _Lock_policy _Lp>
1011 inline std::basic_ostream<_Ch, _Tr>&
1012 operator<<(std::basic_ostream<_Ch, _Tr>& __os,
1013 const __shared_ptr<_Tp, _Lp>& __p)
1014 {
1015 __os << __p.get();
1016 return __os;
1017 }
1018
1019 // C++14 §20.8.2.4
1020 template<typename _Tp = void> class owner_less;
1021
1022 /// Partial specialization of owner_less for shared_ptr.
1023 template<typename _Tp>
1024 struct owner_less<shared_ptr<_Tp>>
1025 : public _Sp_owner_less<shared_ptr<_Tp>, weak_ptr<_Tp>>
1026 { };
1027
1028 /// Partial specialization of owner_less for weak_ptr.
1029 template<typename _Tp>
1030 struct owner_less<weak_ptr<_Tp>>
1031 : public _Sp_owner_less<weak_ptr<_Tp>, shared_ptr<_Tp>>
1032 { };
1033
1034 template<>
1035 class owner_less<void>
1036 {
1037 template<typename _Tp, typename _Up>
1038 bool
1039 operator()(shared_ptr<_Tp> const& __lhs,
1040 shared_ptr<_Up> const& __rhs) const
1041 { return __lhs.owner_before(__rhs); }
1042
1043 template<typename _Tp, typename _Up>
1044 bool
1045 operator()(shared_ptr<_Tp> const& __lhs,
1046 weak_ptr<_Up> const& __rhs) const
1047 { return __lhs.owner_before(__rhs); }
1048
1049 template<typename _Tp, typename _Up>
1050 bool
1051 operator()(weak_ptr<_Tp> const& __lhs,
1052 shared_ptr<_Up> const& __rhs) const
1053 { return __lhs.owner_before(__rhs); }
1054
1055 template<typename _Tp, typename _Up>
1056 bool
1057 operator()(weak_ptr<_Tp> const& __lhs,
1058 weak_ptr<_Up> const& __rhs) const
1059 { return __lhs.owner_before(__rhs); }
1060
1061 typedef void is_transparent;
1062 };
1063
1064 // C++14 §20.8.2.6
1065 template<typename _Tp>
1066 inline bool
1067 atomic_is_lock_free(const shared_ptr<_Tp>* __p)
1068 { return std::atomic_is_lock_free<_Tp, __default_lock_policy>(__p); }
1069
1070 template<typename _Tp>
1071 shared_ptr<_Tp> atomic_load(const shared_ptr<_Tp>* __p)
1072 { return std::atomic_load<_Tp>(__p); }
1073
1074 template<typename _Tp>
1075 shared_ptr<_Tp>
1076 atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order __mo)
1077 { return std::atomic_load_explicit<_Tp>(__p, __mo); }
1078
1079 template<typename _Tp>
1080 void atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
1081 { return std::atomic_store<_Tp>(__p, __r); }
1082
1083 template<typename _Tp>
1084 shared_ptr<_Tp>
1085 atomic_store_explicit(const shared_ptr<_Tp>* __p,
1086 shared_ptr<_Tp> __r,
1087 memory_order __mo)
1088 { return std::atomic_store_explicit<_Tp>(__p, __r, __mo); }
1089
1090 template<typename _Tp>
1091 void atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
1092 { return std::atomic_exchange<_Tp>(__p, __r); }
1093
1094 template<typename _Tp>
1095 shared_ptr<_Tp>
1096 atomic_exchange_explicit(const shared_ptr<_Tp>* __p,
1097 shared_ptr<_Tp> __r,
1098 memory_order __mo)
1099 { return std::atomic_exchange_explicit<_Tp>(__p, __r, __mo); }
1100
1101 template<typename _Tp>
1102 bool atomic_compare_exchange_weak(shared_ptr<_Tp>* __p,
1103 shared_ptr<_Tp>* __v,
1104 shared_ptr<_Tp> __w)
1105 { return std::atomic_compare_exchange_weak<_Tp>(__p, __v, __w); }
1106
1107 template<typename _Tp>
1108 bool atomic_compare_exchange_strong(shared_ptr<_Tp>* __p,
1109 shared_ptr<_Tp>* __v,
1110 shared_ptr<_Tp> __w)
1111 { return std::atomic_compare_exchange_strong<_Tp>(__p, __v, __w); }
1112
1113 template<typename _Tp>
1114 bool atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p,
1115 shared_ptr<_Tp>* __v,
1116 shared_ptr<_Tp> __w,
1117 memory_order __success,
1118 memory_order __failure)
1119 { return std::atomic_compare_exchange_weak_explicit<_Tp>(__p, __v, __w,
1120 __success,
1121 __failure); }
1122
1123 template<typename _Tp>
1124 bool atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p,
1125 shared_ptr<_Tp>* __v,
1126 shared_ptr<_Tp> __w,
1127 memory_order __success,
1128 memory_order __failure)
1129 { return std::atomic_compare_exchange_strong_explicit<_Tp>(__p, __v, __w,
1130 __success,
1131 __failure); }
1132
1133 //enable_shared_from_this
1134 template<typename _Tp>
1135 class enable_shared_from_this
1136 {
1137 protected:
1138 constexpr enable_shared_from_this() noexcept { }
1139
1140 enable_shared_from_this(const enable_shared_from_this&) noexcept { }
1141
1142 enable_shared_from_this&
1143 operator=(const enable_shared_from_this&) noexcept
1144 { return *this; }
1145
1146 ~enable_shared_from_this() { }
1147
1148 public:
1149 shared_ptr<_Tp>
1150 shared_from_this()
1151 { return shared_ptr<_Tp>(this->_M_weak_this); }
1152
1153 shared_ptr<const _Tp>
1154 shared_from_this() const
1155 { return shared_ptr<const _Tp>(this->_M_weak_this); }
1156
1157 private:
1158 template<typename _Tp1>
1159 void
1160 _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const noexcept
1161 { _M_weak_this._M_assign(__p, __n); }
1162
1163 template<typename _Tp1>
1164 friend void
1165 __enable_shared_from_this_helper(const __shared_count<>& __pn,
1166 const enable_shared_from_this* __pe,
1167 const _Tp1* __px) noexcept
1168 {
1169 if(__pe != 0)
1170 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
1171 }
1172
1173 mutable weak_ptr<_Tp> _M_weak_this;
1174 };
1175
1176_GLIBCXX_END_NAMESPACE_VERSION
1177} // namespace fundamentals_v2
1178} // namespace experimental
1179
1180_GLIBCXX_BEGIN_NAMESPACE_VERSION
1181
1182 /// std::hash specialization for shared_ptr.
1183 template<typename _Tp>
1184 struct hash<experimental::shared_ptr<_Tp>>
1185 : public __hash_base<size_t, experimental::shared_ptr<_Tp>>
1186 {
1187 size_t
1188 operator()(const experimental::shared_ptr<_Tp>& __s) const noexcept
1189 { return std::hash<_Tp*>()(__s.get()); }
1190 };
1191
1192_GLIBCXX_END_NAMESPACE_VERSION
1193} // namespace std
1194
1195#endif // __cplusplus <= 201103L
1196
1197#endif // _GLIBCXX_EXPERIMENTAL_SHARED_PTR_H