shared_ptr_base.h: Remove a few now redundant __glibcxx_function_requires(_Convertibl...
[gcc.git] / libstdc++-v3 / include / bits / shared_ptr_base.h
1 // shared_ptr and weak_ptr implementation details -*- C++ -*-
2
3 // Copyright (C) 2007, 2008, 2009, 2010 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 // 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 * You should not attempt to use it directly.
47 */
48
49 #ifndef _SHARED_PTR_BASE_H
50 #define _SHARED_PTR_BASE_H 1
51
52 _GLIBCXX_BEGIN_NAMESPACE(std)
53
54 // Forward declarations.
55 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
56 class __shared_ptr;
57
58 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
59 class __weak_ptr;
60
61 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
62 class __enable_shared_from_this;
63
64 template<typename _Tp>
65 class shared_ptr;
66
67 template<typename _Tp>
68 class weak_ptr;
69
70 template<typename _Tp>
71 struct owner_less;
72
73 template<typename _Tp>
74 class enable_shared_from_this;
75
76 template<_Lock_policy _Lp = __default_lock_policy>
77 class __weak_count;
78
79 template<_Lock_policy _Lp = __default_lock_policy>
80 class __shared_count;
81
82
83 // Counted ptr with no deleter or allocator support
84 template<typename _Ptr, _Lock_policy _Lp>
85 class _Sp_counted_ptr : public _Sp_counted_base<_Lp>
86 {
87 public:
88 explicit
89 _Sp_counted_ptr(_Ptr __p)
90 : _M_ptr(__p) { }
91
92 virtual void
93 _M_dispose() // nothrow
94 { delete _M_ptr; }
95
96 virtual void
97 _M_destroy() // nothrow
98 { delete this; }
99
100 virtual void*
101 _M_get_deleter(const std::type_info&)
102 { return 0; }
103
104 _Sp_counted_ptr(const _Sp_counted_ptr&) = delete;
105 _Sp_counted_ptr& operator=(const _Sp_counted_ptr&) = delete;
106
107 protected:
108 _Ptr _M_ptr; // copy constructor must not throw
109 };
110
111 template<>
112 inline void
113 _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() { }
114
115 template<>
116 inline void
117 _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() { }
118
119 template<>
120 inline void
121 _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() { }
122
123 // Support for custom deleter and/or allocator
124 template<typename _Ptr, typename _Deleter, typename _Alloc, _Lock_policy _Lp>
125 class _Sp_counted_deleter : public _Sp_counted_ptr<_Ptr, _Lp>
126 {
127 typedef typename _Alloc::template
128 rebind<_Sp_counted_deleter>::other _My_alloc_type;
129
130 // Helper class that stores the Deleter and also acts as an allocator.
131 // Used to dispose of the owned pointer and the internal refcount
132 // Requires that copies of _Alloc can free each other's memory.
133 struct _My_Deleter
134 : public _My_alloc_type // copy constructor must not throw
135 {
136 _Deleter _M_del; // copy constructor must not throw
137 _My_Deleter(_Deleter __d, const _Alloc& __a)
138 : _My_alloc_type(__a), _M_del(__d) { }
139 };
140
141 protected:
142 typedef _Sp_counted_ptr<_Ptr, _Lp> _Base_type;
143
144 public:
145 // __d(__p) must not throw.
146 _Sp_counted_deleter(_Ptr __p, _Deleter __d)
147 : _Base_type(__p), _M_del(__d, _Alloc()) { }
148
149 // __d(__p) must not throw.
150 _Sp_counted_deleter(_Ptr __p, _Deleter __d, const _Alloc& __a)
151 : _Base_type(__p), _M_del(__d, __a) { }
152
153 virtual void
154 _M_dispose() // nothrow
155 { _M_del._M_del(_Base_type::_M_ptr); }
156
157 virtual void
158 _M_destroy() // nothrow
159 {
160 _My_alloc_type __a(_M_del);
161 this->~_Sp_counted_deleter();
162 __a.deallocate(this, 1);
163 }
164
165 virtual void*
166 _M_get_deleter(const std::type_info& __ti)
167 {
168 #ifdef __GXX_RTTI
169 return __ti == typeid(_Deleter) ? &_M_del._M_del : 0;
170 #else
171 return 0;
172 #endif
173 }
174
175 protected:
176 _My_Deleter _M_del; // copy constructor must not throw
177 };
178
179 // helpers for make_shared / allocate_shared
180
181 template<typename _Tp>
182 struct _Sp_destroy_inplace
183 {
184 void operator()(_Tp* __p) const { if (__p) __p->~_Tp(); }
185 };
186
187 struct _Sp_make_shared_tag { };
188
189 template<typename _Tp, typename _Alloc, _Lock_policy _Lp>
190 class _Sp_counted_ptr_inplace
191 : public _Sp_counted_deleter<_Tp*, _Sp_destroy_inplace<_Tp>, _Alloc, _Lp>
192 {
193 typedef _Sp_counted_deleter<_Tp*, _Sp_destroy_inplace<_Tp>, _Alloc, _Lp>
194 _Base_type;
195
196 public:
197 explicit
198 _Sp_counted_ptr_inplace(_Alloc __a)
199 : _Base_type(static_cast<_Tp*>(0), _Sp_destroy_inplace<_Tp>(), __a)
200 , _M_storage()
201 {
202 void* __p = &_M_storage;
203 ::new (__p) _Tp(); // might throw
204 _Base_type::_Base_type::_M_ptr = static_cast<_Tp*>(__p);
205 }
206
207 template<typename... _Args>
208 _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
209 : _Base_type(static_cast<_Tp*>(0), _Sp_destroy_inplace<_Tp>(), __a)
210 , _M_storage()
211 {
212 void* __p = &_M_storage;
213 ::new (__p) _Tp(std::forward<_Args>(__args)...); // might throw
214 _Base_type::_Base_type::_M_ptr = static_cast<_Tp*>(__p);
215 }
216
217 // Override because the allocator needs to know the dynamic type
218 virtual void
219 _M_destroy() // nothrow
220 {
221 typedef typename _Alloc::template
222 rebind<_Sp_counted_ptr_inplace>::other _My_alloc_type;
223 _My_alloc_type __a(_Base_type::_M_del);
224 this->~_Sp_counted_ptr_inplace();
225 __a.deallocate(this, 1);
226 }
227
228 // Sneaky trick so __shared_ptr can get the managed pointer
229 virtual void*
230 _M_get_deleter(const std::type_info& __ti)
231 {
232 #ifdef __GXX_RTTI
233 return __ti == typeid(_Sp_make_shared_tag)
234 ? static_cast<void*>(&_M_storage)
235 : _Base_type::_M_get_deleter(__ti);
236 #else
237 return 0;
238 #endif
239 }
240
241 private:
242 typename aligned_storage<sizeof(_Tp), alignment_of<_Tp>::value>::type
243 _M_storage;
244 };
245
246 template<_Lock_policy _Lp>
247 class __shared_count
248 {
249 public:
250 __shared_count() : _M_pi(0) // nothrow
251 { }
252
253 template<typename _Ptr>
254 explicit
255 __shared_count(_Ptr __p) : _M_pi(0)
256 {
257 __try
258 {
259 _M_pi = new _Sp_counted_ptr<_Ptr, _Lp>(__p);
260 }
261 __catch(...)
262 {
263 delete __p;
264 __throw_exception_again;
265 }
266 }
267
268 template<typename _Ptr, typename _Deleter>
269 __shared_count(_Ptr __p, _Deleter __d) : _M_pi(0)
270 {
271 // The allocator's value_type doesn't matter, will rebind it anyway.
272 typedef std::allocator<int> _Alloc;
273 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
274 typedef std::allocator<_Sp_cd_type> _Alloc2;
275 _Alloc2 __a2;
276 __try
277 {
278 _M_pi = __a2.allocate(1);
279 ::new(static_cast<void*>(_M_pi)) _Sp_cd_type(__p, __d);
280 }
281 __catch(...)
282 {
283 __d(__p); // Call _Deleter on __p.
284 if (_M_pi)
285 __a2.deallocate(static_cast<_Sp_cd_type*>(_M_pi), 1);
286 __throw_exception_again;
287 }
288 }
289
290 template<typename _Ptr, typename _Deleter, typename _Alloc>
291 __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
292 {
293 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
294 typedef typename _Alloc::template rebind<_Sp_cd_type>::other _Alloc2;
295 _Alloc2 __a2(__a);
296 __try
297 {
298 _M_pi = __a2.allocate(1);
299 ::new(static_cast<void*>(_M_pi)) _Sp_cd_type(__p, __d, __a);
300 }
301 __catch(...)
302 {
303 __d(__p); // Call _Deleter on __p.
304 if (_M_pi)
305 __a2.deallocate(static_cast<_Sp_cd_type*>(_M_pi), 1);
306 __throw_exception_again;
307 }
308 }
309
310 template<typename _Tp, typename _Alloc, typename... _Args>
311 __shared_count(_Sp_make_shared_tag, _Tp*, _Alloc __a, _Args&&... __args)
312 : _M_pi(0)
313 {
314 typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
315 typedef typename _Alloc::template rebind<_Sp_cp_type>::other _Alloc2;
316 _Alloc2 __a2(__a);
317 __try
318 {
319 _M_pi = __a2.allocate(1);
320 ::new(static_cast<void*>(_M_pi)) _Sp_cp_type(__a,
321 std::forward<_Args>(__args)...);
322 }
323 __catch(...)
324 {
325 if (_M_pi)
326 __a2.deallocate(static_cast<_Sp_cp_type*>(_M_pi), 1);
327 __throw_exception_again;
328 }
329 }
330
331 #if _GLIBCXX_DEPRECATED
332 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
333 template<typename _Tp>
334 explicit
335 __shared_count(std::auto_ptr<_Tp>&& __r)
336 : _M_pi(new _Sp_counted_ptr<_Tp*, _Lp>(__r.get()))
337 { __r.release(); }
338 #endif
339
340 // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
341 template<typename _Tp, typename _Del>
342 explicit
343 __shared_count(std::unique_ptr<_Tp, _Del>&& __r)
344 : _M_pi(_S_create_from_up(std::move(__r)))
345 { __r.release(); }
346
347 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
348 explicit __shared_count(const __weak_count<_Lp>& __r);
349
350 ~__shared_count() // nothrow
351 {
352 if (_M_pi != 0)
353 _M_pi->_M_release();
354 }
355
356 __shared_count(const __shared_count& __r)
357 : _M_pi(__r._M_pi) // nothrow
358 {
359 if (_M_pi != 0)
360 _M_pi->_M_add_ref_copy();
361 }
362
363 __shared_count&
364 operator=(const __shared_count& __r) // nothrow
365 {
366 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
367 if (__tmp != _M_pi)
368 {
369 if (__tmp != 0)
370 __tmp->_M_add_ref_copy();
371 if (_M_pi != 0)
372 _M_pi->_M_release();
373 _M_pi = __tmp;
374 }
375 return *this;
376 }
377
378 void
379 _M_swap(__shared_count& __r) // nothrow
380 {
381 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
382 __r._M_pi = _M_pi;
383 _M_pi = __tmp;
384 }
385
386 long
387 _M_get_use_count() const // nothrow
388 { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
389
390 bool
391 _M_unique() const // nothrow
392 { return this->_M_get_use_count() == 1; }
393
394 void*
395 _M_get_deleter(const std::type_info& __ti) const
396 { return _M_pi ? _M_pi->_M_get_deleter(__ti) : 0; }
397
398 bool
399 _M_less(const __shared_count& __rhs) const
400 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
401
402 bool
403 _M_less(const __weak_count<_Lp>& __rhs) const
404 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
405
406 // Friend function injected into enclosing namespace and found by ADL
407 friend inline bool
408 operator==(const __shared_count& __a, const __shared_count& __b)
409 { return __a._M_pi == __b._M_pi; }
410
411 private:
412 friend class __weak_count<_Lp>;
413
414 template<typename _Tp, typename _Del>
415 static _Sp_counted_base<_Lp>*
416 _S_create_from_up(std::unique_ptr<_Tp, _Del>&& __r,
417 typename std::enable_if<!std::is_reference<_Del>::value>::type* = 0)
418 {
419 return new _Sp_counted_deleter<_Tp*, _Del, std::allocator<_Tp>,
420 _Lp>(__r.get(), __r.get_deleter());
421 }
422
423 template<typename _Tp, typename _Del>
424 static _Sp_counted_base<_Lp>*
425 _S_create_from_up(std::unique_ptr<_Tp, _Del>&& __r,
426 typename std::enable_if<std::is_reference<_Del>::value>::type* = 0)
427 {
428 typedef typename std::remove_reference<_Del>::type _Del1;
429 typedef std::reference_wrapper<_Del1> _Del2;
430 return new _Sp_counted_deleter<_Tp*, _Del2, std::allocator<_Tp>,
431 _Lp>(__r.get(), std::ref(__r.get_deleter()));
432 }
433
434 _Sp_counted_base<_Lp>* _M_pi;
435 };
436
437
438 template<_Lock_policy _Lp>
439 class __weak_count
440 {
441 public:
442 __weak_count() : _M_pi(0) // nothrow
443 { }
444
445 __weak_count(const __shared_count<_Lp>& __r) : _M_pi(__r._M_pi) // nothrow
446 {
447 if (_M_pi != 0)
448 _M_pi->_M_weak_add_ref();
449 }
450
451 __weak_count(const __weak_count<_Lp>& __r) : _M_pi(__r._M_pi) // nothrow
452 {
453 if (_M_pi != 0)
454 _M_pi->_M_weak_add_ref();
455 }
456
457 ~__weak_count() // nothrow
458 {
459 if (_M_pi != 0)
460 _M_pi->_M_weak_release();
461 }
462
463 __weak_count<_Lp>&
464 operator=(const __shared_count<_Lp>& __r) // nothrow
465 {
466 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
467 if (__tmp != 0)
468 __tmp->_M_weak_add_ref();
469 if (_M_pi != 0)
470 _M_pi->_M_weak_release();
471 _M_pi = __tmp;
472 return *this;
473 }
474
475 __weak_count<_Lp>&
476 operator=(const __weak_count<_Lp>& __r) // nothrow
477 {
478 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
479 if (__tmp != 0)
480 __tmp->_M_weak_add_ref();
481 if (_M_pi != 0)
482 _M_pi->_M_weak_release();
483 _M_pi = __tmp;
484 return *this;
485 }
486
487 void
488 _M_swap(__weak_count<_Lp>& __r) // nothrow
489 {
490 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
491 __r._M_pi = _M_pi;
492 _M_pi = __tmp;
493 }
494
495 long
496 _M_get_use_count() const // nothrow
497 { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
498
499 bool
500 _M_less(const __weak_count& __rhs) const
501 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
502
503 bool
504 _M_less(const __shared_count<_Lp>& __rhs) const
505 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
506
507 // Friend function injected into enclosing namespace and found by ADL
508 friend inline bool
509 operator==(const __weak_count& __a, const __weak_count& __b)
510 { return __a._M_pi == __b._M_pi; }
511
512 private:
513 friend class __shared_count<_Lp>;
514
515 _Sp_counted_base<_Lp>* _M_pi;
516 };
517
518 // Now that __weak_count is defined we can define this constructor:
519 template<_Lock_policy _Lp>
520 inline __shared_count<_Lp>:: __shared_count(const __weak_count<_Lp>& __r)
521 : _M_pi(__r._M_pi)
522 {
523 if (_M_pi != 0)
524 _M_pi->_M_add_ref_lock();
525 else
526 __throw_bad_weak_ptr();
527 }
528
529
530 // Support for enable_shared_from_this.
531
532 // Friend of __enable_shared_from_this.
533 template<_Lock_policy _Lp, typename _Tp1, typename _Tp2>
534 void
535 __enable_shared_from_this_helper(const __shared_count<_Lp>&,
536 const __enable_shared_from_this<_Tp1,
537 _Lp>*, const _Tp2*);
538
539 // Friend of enable_shared_from_this.
540 template<typename _Tp1, typename _Tp2>
541 void
542 __enable_shared_from_this_helper(const __shared_count<>&,
543 const enable_shared_from_this<_Tp1>*,
544 const _Tp2*);
545
546 template<_Lock_policy _Lp>
547 inline void
548 __enable_shared_from_this_helper(const __shared_count<_Lp>&, ...)
549 { }
550
551
552 template<typename _Tp, _Lock_policy _Lp>
553 class __shared_ptr
554 {
555 public:
556 typedef _Tp element_type;
557
558 __shared_ptr() : _M_ptr(0), _M_refcount() // never throws
559 { }
560
561 template<typename _Tp1>
562 explicit __shared_ptr(_Tp1* __p) : _M_ptr(__p), _M_refcount(__p)
563 {
564 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
565 static_assert( sizeof(_Tp1) > 0, "incomplete type" );
566 __enable_shared_from_this_helper(_M_refcount, __p, __p);
567 }
568
569 template<typename _Tp1, typename _Deleter>
570 __shared_ptr(_Tp1* __p, _Deleter __d)
571 : _M_ptr(__p), _M_refcount(__p, __d)
572 {
573 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
574 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
575 __enable_shared_from_this_helper(_M_refcount, __p, __p);
576 }
577
578 template<typename _Tp1, typename _Deleter, typename _Alloc>
579 __shared_ptr(_Tp1* __p, _Deleter __d, const _Alloc& __a)
580 : _M_ptr(__p), _M_refcount(__p, __d, __a)
581 {
582 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
583 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
584 __enable_shared_from_this_helper(_M_refcount, __p, __p);
585 }
586
587 template<typename _Deleter>
588 __shared_ptr(nullptr_t __p, _Deleter __d)
589 : _M_ptr(0), _M_refcount(__p, __d)
590 { }
591
592 template<typename _Deleter, typename _Alloc>
593 __shared_ptr(nullptr_t __p, _Deleter __d, const _Alloc& __a)
594 : _M_ptr(0), _M_refcount(__p, __d, __a)
595 { }
596
597 template<typename _Tp1>
598 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r, _Tp* __p)
599 : _M_ptr(__p), _M_refcount(__r._M_refcount) // never throws
600 { }
601
602 // generated copy constructor, assignment, destructor are fine.
603
604 template<typename _Tp1, typename = typename
605 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
606 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r)
607 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
608 { }
609
610 __shared_ptr(__shared_ptr&& __r)
611 : _M_ptr(__r._M_ptr), _M_refcount() // never throws
612 {
613 _M_refcount._M_swap(__r._M_refcount);
614 __r._M_ptr = 0;
615 }
616
617 template<typename _Tp1, typename = typename
618 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
619 __shared_ptr(__shared_ptr<_Tp1, _Lp>&& __r)
620 : _M_ptr(__r._M_ptr), _M_refcount() // never throws
621 {
622 _M_refcount._M_swap(__r._M_refcount);
623 __r._M_ptr = 0;
624 }
625
626 template<typename _Tp1>
627 explicit __shared_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
628 : _M_refcount(__r._M_refcount) // may throw
629 {
630 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
631
632 // It is now safe to copy __r._M_ptr, as
633 // _M_refcount(__r._M_refcount) did not throw.
634 _M_ptr = __r._M_ptr;
635 }
636
637 // If an exception is thrown this constructor has no effect.
638 template<typename _Tp1, typename _Del>
639 __shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r)
640 : _M_ptr(__r.get()), _M_refcount()
641 {
642 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
643 _Tp1* __tmp = __r.get();
644 _M_refcount = __shared_count<_Lp>(std::move(__r));
645 __enable_shared_from_this_helper(_M_refcount, __tmp, __tmp);
646 }
647
648 #if _GLIBCXX_DEPRECATED
649 // Postcondition: use_count() == 1 and __r.get() == 0
650 template<typename _Tp1>
651 __shared_ptr(std::auto_ptr<_Tp1>&& __r)
652 : _M_ptr(__r.get()), _M_refcount()
653 {
654 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
655 static_assert( sizeof(_Tp1) > 0, "incomplete type" );
656 _Tp1* __tmp = __r.get();
657 _M_refcount = __shared_count<_Lp>(std::move(__r));
658 __enable_shared_from_this_helper(_M_refcount, __tmp, __tmp);
659 }
660 #endif
661
662 /* TODO: use delegating constructor */
663 __shared_ptr(nullptr_t) : _M_ptr(0), _M_refcount() // never throws
664 { }
665
666 template<typename _Tp1>
667 __shared_ptr&
668 operator=(const __shared_ptr<_Tp1, _Lp>& __r) // never throws
669 {
670 _M_ptr = __r._M_ptr;
671 _M_refcount = __r._M_refcount; // __shared_count::op= doesn't throw
672 return *this;
673 }
674
675 #if _GLIBCXX_DEPRECATED
676 template<typename _Tp1>
677 __shared_ptr&
678 operator=(std::auto_ptr<_Tp1>&& __r)
679 {
680 __shared_ptr(std::move(__r)).swap(*this);
681 return *this;
682 }
683 #endif
684
685 __shared_ptr&
686 operator=(__shared_ptr&& __r)
687 {
688 __shared_ptr(std::move(__r)).swap(*this);
689 return *this;
690 }
691
692 template<class _Tp1>
693 __shared_ptr&
694 operator=(__shared_ptr<_Tp1, _Lp>&& __r)
695 {
696 __shared_ptr(std::move(__r)).swap(*this);
697 return *this;
698 }
699
700 template<typename _Tp1, typename _Del>
701 __shared_ptr&
702 operator=(std::unique_ptr<_Tp1, _Del>&& __r)
703 {
704 __shared_ptr(std::move(__r)).swap(*this);
705 return *this;
706 }
707
708 void
709 reset() // never throws
710 { __shared_ptr().swap(*this); }
711
712 template<typename _Tp1>
713 void
714 reset(_Tp1* __p) // _Tp1 must be complete.
715 {
716 // Catch self-reset errors.
717 _GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != _M_ptr);
718 __shared_ptr(__p).swap(*this);
719 }
720
721 template<typename _Tp1, typename _Deleter>
722 void
723 reset(_Tp1* __p, _Deleter __d)
724 { __shared_ptr(__p, __d).swap(*this); }
725
726 template<typename _Tp1, typename _Deleter, typename _Alloc>
727 void
728 reset(_Tp1* __p, _Deleter __d, const _Alloc& __a)
729 { __shared_ptr(__p, __d, __a).swap(*this); }
730
731 // Allow class instantiation when _Tp is [cv-qual] void.
732 typename std::add_lvalue_reference<_Tp>::type
733 operator*() const // never throws
734 {
735 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
736 return *_M_ptr;
737 }
738
739 _Tp*
740 operator->() const // never throws
741 {
742 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
743 return _M_ptr;
744 }
745
746 _Tp*
747 get() const // never throws
748 { return _M_ptr; }
749
750 explicit operator bool() const // never throws
751 { return _M_ptr == 0 ? false : true; }
752
753 bool
754 unique() const // never throws
755 { return _M_refcount._M_unique(); }
756
757 long
758 use_count() const // never throws
759 { return _M_refcount._M_get_use_count(); }
760
761 void
762 swap(__shared_ptr<_Tp, _Lp>& __other) // never throws
763 {
764 std::swap(_M_ptr, __other._M_ptr);
765 _M_refcount._M_swap(__other._M_refcount);
766 }
767
768 template<typename _Tp1>
769 bool
770 owner_before(__shared_ptr<_Tp1, _Lp> const& __rhs) const
771 { return _M_refcount._M_less(__rhs._M_refcount); }
772
773 template<typename _Tp1>
774 bool
775 owner_before(__weak_ptr<_Tp1, _Lp> const& __rhs) const
776 { return _M_refcount._M_less(__rhs._M_refcount); }
777
778 #ifdef __GXX_RTTI
779 protected:
780 // This constructor is non-standard, it is used by allocate_shared.
781 template<typename _Alloc, typename... _Args>
782 __shared_ptr(_Sp_make_shared_tag __tag, _Alloc __a, _Args&&... __args)
783 : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a,
784 std::forward<_Args>(__args)...)
785 {
786 // _M_ptr needs to point to the newly constructed object.
787 // This relies on _Sp_counted_ptr_inplace::_M_get_deleter.
788 void* __p = _M_refcount._M_get_deleter(typeid(__tag));
789 _M_ptr = static_cast<_Tp*>(__p);
790 __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
791 }
792 #else
793 template<typename _Alloc>
794 struct _Deleter
795 {
796 void operator()(_Tp* __ptr)
797 {
798 _M_alloc.destroy(__ptr);
799 _M_alloc.deallocate(__ptr, 1);
800 }
801 _Alloc _M_alloc;
802 };
803
804 template<typename _Alloc, typename... _Args>
805 __shared_ptr(_Sp_make_shared_tag __tag, _Alloc __a, _Args&&... __args)
806 : _M_ptr(), _M_refcount()
807 {
808 typedef typename _Alloc::template rebind<_Tp>::other _Alloc2;
809 _Deleter<_Alloc2> __del = { _Alloc2(__a) };
810 _M_ptr = __del._M_alloc.allocate(1);
811 __try
812 {
813 __del._M_alloc.construct(_M_ptr, std::forward<_Args>(__args)...);
814 }
815 __catch(...)
816 {
817 __del._M_alloc.deallocate(_M_ptr, 1);
818 __throw_exception_again;
819 }
820 __shared_count<_Lp> __count(_M_ptr, __del, __del._M_alloc);
821 _M_refcount._M_swap(__count);
822 __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
823 }
824 #endif
825
826 template<typename _Tp1, _Lock_policy _Lp1, typename _Alloc,
827 typename... _Args>
828 friend __shared_ptr<_Tp1, _Lp1>
829 __allocate_shared(_Alloc __a, _Args&&... __args);
830
831 private:
832 void*
833 _M_get_deleter(const std::type_info& __ti) const
834 { return _M_refcount._M_get_deleter(__ti); }
835
836 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
837 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
838
839 template<typename _Del, typename _Tp1, _Lock_policy _Lp1>
840 friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&);
841
842 _Tp* _M_ptr; // Contained pointer.
843 __shared_count<_Lp> _M_refcount; // Reference counter.
844 };
845
846
847 // 20.8.13.2.7 shared_ptr comparisons
848 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
849 inline bool
850 operator==(const __shared_ptr<_Tp1, _Lp>& __a,
851 const __shared_ptr<_Tp2, _Lp>& __b)
852 { return __a.get() == __b.get(); }
853
854 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
855 inline bool
856 operator!=(const __shared_ptr<_Tp1, _Lp>& __a,
857 const __shared_ptr<_Tp2, _Lp>& __b)
858 { return __a.get() != __b.get(); }
859
860 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
861 inline bool
862 operator<(const __shared_ptr<_Tp1, _Lp>& __a,
863 const __shared_ptr<_Tp2, _Lp>& __b)
864 { return __a.get() < __b.get(); }
865
866 template<typename _Sp>
867 struct _Sp_less : public binary_function<_Sp, _Sp, bool>
868 {
869 bool
870 operator()(const _Sp& __lhs, const _Sp& __rhs) const
871 {
872 typedef typename _Sp::element_type element_type;
873 return std::less<element_type*>()(__lhs.get(), __rhs.get());
874 }
875 };
876
877 template<typename _Tp, _Lock_policy _Lp>
878 struct less<__shared_ptr<_Tp, _Lp>>
879 : public _Sp_less<__shared_ptr<_Tp, _Lp>>
880 { };
881
882 // XXX LessThanComparable<_Tp> concept should provide >, >= and <=
883 template<typename _Tp, _Lock_policy _Lp>
884 inline bool
885 operator>(const __shared_ptr<_Tp, _Lp>& __a,
886 const __shared_ptr<_Tp, _Lp>& __b)
887 { return __a.get() > __b.get(); }
888
889 template<typename _Tp, _Lock_policy _Lp>
890 inline bool
891 operator>=(const __shared_ptr<_Tp, _Lp>& __a,
892 const __shared_ptr<_Tp, _Lp>& __b)
893 { return __a.get() >= __b.get(); }
894
895 template<typename _Tp, _Lock_policy _Lp>
896 inline bool
897 operator<=(const __shared_ptr<_Tp, _Lp>& __a,
898 const __shared_ptr<_Tp, _Lp>& __b)
899 { return __a.get() <= __b.get(); }
900
901 // 2.2.3.8 shared_ptr specialized algorithms.
902 template<typename _Tp, _Lock_policy _Lp>
903 inline void
904 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b)
905 { __a.swap(__b); }
906
907 // 2.2.3.9 shared_ptr casts
908
909 // The seemingly equivalent code:
910 // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
911 // will eventually result in undefined behaviour, attempting to
912 // delete the same object twice.
913 /// static_pointer_cast
914 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
915 inline __shared_ptr<_Tp, _Lp>
916 static_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r)
917 { return __shared_ptr<_Tp, _Lp>(__r, static_cast<_Tp*>(__r.get())); }
918
919 // The seemingly equivalent code:
920 // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
921 // will eventually result in undefined behaviour, attempting to
922 // delete the same object twice.
923 /// const_pointer_cast
924 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
925 inline __shared_ptr<_Tp, _Lp>
926 const_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r)
927 { return __shared_ptr<_Tp, _Lp>(__r, const_cast<_Tp*>(__r.get())); }
928
929 // The seemingly equivalent code:
930 // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
931 // will eventually result in undefined behaviour, attempting to
932 // delete the same object twice.
933 /// dynamic_pointer_cast
934 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
935 inline __shared_ptr<_Tp, _Lp>
936 dynamic_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r)
937 {
938 if (_Tp* __p = dynamic_cast<_Tp*>(__r.get()))
939 return __shared_ptr<_Tp, _Lp>(__r, __p);
940 return __shared_ptr<_Tp, _Lp>();
941 }
942
943
944 template<typename _Tp, _Lock_policy _Lp>
945 class __weak_ptr
946 {
947 public:
948 typedef _Tp element_type;
949
950 __weak_ptr() : _M_ptr(0), _M_refcount() // never throws
951 { }
952
953 // Generated copy constructor, assignment, destructor are fine.
954
955 // The "obvious" converting constructor implementation:
956 //
957 // template<typename _Tp1>
958 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
959 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
960 // { }
961 //
962 // has a serious problem.
963 //
964 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
965 // conversion may require access to *__r._M_ptr (virtual inheritance).
966 //
967 // It is not possible to avoid spurious access violations since
968 // in multithreaded programs __r._M_ptr may be invalidated at any point.
969 template<typename _Tp1, typename = typename
970 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
971 __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
972 : _M_refcount(__r._M_refcount) // never throws
973 { _M_ptr = __r.lock().get(); }
974
975 template<typename _Tp1, typename = typename
976 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
977 __weak_ptr(const __shared_ptr<_Tp1, _Lp>& __r)
978 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
979 { }
980
981 template<typename _Tp1>
982 __weak_ptr&
983 operator=(const __weak_ptr<_Tp1, _Lp>& __r) // never throws
984 {
985 _M_ptr = __r.lock().get();
986 _M_refcount = __r._M_refcount;
987 return *this;
988 }
989
990 template<typename _Tp1>
991 __weak_ptr&
992 operator=(const __shared_ptr<_Tp1, _Lp>& __r) // never throws
993 {
994 _M_ptr = __r._M_ptr;
995 _M_refcount = __r._M_refcount;
996 return *this;
997 }
998
999 __shared_ptr<_Tp, _Lp>
1000 lock() const // never throws
1001 {
1002 #ifdef __GTHREADS
1003 // Optimization: avoid throw overhead.
1004 if (expired())
1005 return __shared_ptr<element_type, _Lp>();
1006
1007 __try
1008 {
1009 return __shared_ptr<element_type, _Lp>(*this);
1010 }
1011 __catch(const bad_weak_ptr&)
1012 {
1013 // Q: How can we get here?
1014 // A: Another thread may have invalidated r after the
1015 // use_count test above.
1016 return __shared_ptr<element_type, _Lp>();
1017 }
1018
1019 #else
1020 // Optimization: avoid try/catch overhead when single threaded.
1021 return expired() ? __shared_ptr<element_type, _Lp>()
1022 : __shared_ptr<element_type, _Lp>(*this);
1023
1024 #endif
1025 } // XXX MT
1026
1027 long
1028 use_count() const // never throws
1029 { return _M_refcount._M_get_use_count(); }
1030
1031 bool
1032 expired() const // never throws
1033 { return _M_refcount._M_get_use_count() == 0; }
1034
1035 template<typename _Tp1>
1036 bool
1037 owner_before(const __shared_ptr<_Tp1, _Lp>& __rhs) const
1038 { return _M_refcount._M_less(__rhs._M_refcount); }
1039
1040 template<typename _Tp1>
1041 bool
1042 owner_before(const __weak_ptr<_Tp1, _Lp>& __rhs) const
1043 { return _M_refcount._M_less(__rhs._M_refcount); }
1044
1045 void
1046 reset() // never throws
1047 { __weak_ptr().swap(*this); }
1048
1049 void
1050 swap(__weak_ptr& __s) // never throws
1051 {
1052 std::swap(_M_ptr, __s._M_ptr);
1053 _M_refcount._M_swap(__s._M_refcount);
1054 }
1055
1056 private:
1057 // Used by __enable_shared_from_this.
1058 void
1059 _M_assign(_Tp* __ptr, const __shared_count<_Lp>& __refcount)
1060 {
1061 _M_ptr = __ptr;
1062 _M_refcount = __refcount;
1063 }
1064
1065 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
1066 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
1067 friend class __enable_shared_from_this<_Tp, _Lp>;
1068 friend class enable_shared_from_this<_Tp>;
1069
1070 _Tp* _M_ptr; // Contained pointer.
1071 __weak_count<_Lp> _M_refcount; // Reference counter.
1072 };
1073
1074 // 20.8.13.3.7 weak_ptr specialized algorithms.
1075 template<typename _Tp, _Lock_policy _Lp>
1076 inline void
1077 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b)
1078 { __a.swap(__b); }
1079
1080 template<typename _Tp, typename _Tp1>
1081 struct _Sp_owner_less : public binary_function<_Tp, _Tp, bool>
1082 {
1083 bool
1084 operator()(const _Tp& __lhs, const _Tp& __rhs) const
1085 { return __lhs.owner_before(__rhs); }
1086
1087 bool
1088 operator()(const _Tp& __lhs, const _Tp1& __rhs) const
1089 { return __lhs.owner_before(__rhs); }
1090
1091 bool
1092 operator()(const _Tp1& __lhs, const _Tp& __rhs) const
1093 { return __lhs.owner_before(__rhs); }
1094 };
1095
1096 template<typename _Tp, _Lock_policy _Lp>
1097 struct owner_less<__shared_ptr<_Tp, _Lp>>
1098 : public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
1099 { };
1100
1101 template<typename _Tp, _Lock_policy _Lp>
1102 struct owner_less<__weak_ptr<_Tp, _Lp>>
1103 : public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
1104 { };
1105
1106
1107 template<typename _Tp, _Lock_policy _Lp>
1108 class __enable_shared_from_this
1109 {
1110 protected:
1111 __enable_shared_from_this() { }
1112
1113 __enable_shared_from_this(const __enable_shared_from_this&) { }
1114
1115 __enable_shared_from_this&
1116 operator=(const __enable_shared_from_this&)
1117 { return *this; }
1118
1119 ~__enable_shared_from_this() { }
1120
1121 public:
1122 __shared_ptr<_Tp, _Lp>
1123 shared_from_this()
1124 { return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
1125
1126 __shared_ptr<const _Tp, _Lp>
1127 shared_from_this() const
1128 { return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
1129
1130 private:
1131 template<typename _Tp1>
1132 void
1133 _M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const
1134 { _M_weak_this._M_assign(__p, __n); }
1135
1136 template<typename _Tp1>
1137 friend void
1138 __enable_shared_from_this_helper(const __shared_count<_Lp>& __pn,
1139 const __enable_shared_from_this* __pe,
1140 const _Tp1* __px)
1141 {
1142 if (__pe != 0)
1143 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
1144 }
1145
1146 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
1147 };
1148
1149
1150 template<typename _Tp, _Lock_policy _Lp, typename _Alloc, typename... _Args>
1151 inline __shared_ptr<_Tp, _Lp>
1152 __allocate_shared(_Alloc __a, _Args&&... __args)
1153 {
1154 return __shared_ptr<_Tp, _Lp>(_Sp_make_shared_tag(),
1155 std::forward<_Alloc>(__a), std::forward<_Args>(__args)...);
1156 }
1157
1158 template<typename _Tp, _Lock_policy _Lp, typename... _Args>
1159 inline __shared_ptr<_Tp, _Lp>
1160 __make_shared(_Args&&... __args)
1161 {
1162 typedef typename std::remove_const<_Tp>::type _Tp_nc;
1163 return __allocate_shared<_Tp, _Lp>(std::allocator<_Tp_nc>(),
1164 std::forward<_Args>(__args)...);
1165 }
1166
1167 /// std::hash specialization for __shared_ptr.
1168 template<typename _Tp, _Lock_policy _Lp>
1169 struct hash<__shared_ptr<_Tp, _Lp>>
1170 : public std::unary_function<__shared_ptr<_Tp, _Lp>, size_t>
1171 {
1172 size_t
1173 operator()(const __shared_ptr<_Tp, _Lp>& __s) const
1174 { return std::hash<_Tp*>()(__s.get()); }
1175 };
1176
1177 _GLIBCXX_END_NAMESPACE
1178
1179 #endif // _SHARED_PTR_BASE_H