shared_ptr.h (hash<shared_ptr>): Add.
[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>
605 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r)
606 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
607 { __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) }
608
609 __shared_ptr(__shared_ptr&& __r)
610 : _M_ptr(__r._M_ptr), _M_refcount() // never throws
611 {
612 _M_refcount._M_swap(__r._M_refcount);
613 __r._M_ptr = 0;
614 }
615
616 template<typename _Tp1>
617 __shared_ptr(__shared_ptr<_Tp1, _Lp>&& __r)
618 : _M_ptr(__r._M_ptr), _M_refcount() // never throws
619 {
620 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
621 _M_refcount._M_swap(__r._M_refcount);
622 __r._M_ptr = 0;
623 }
624
625 template<typename _Tp1>
626 explicit __shared_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
627 : _M_refcount(__r._M_refcount) // may throw
628 {
629 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
630
631 // It is now safe to copy __r._M_ptr, as
632 // _M_refcount(__r._M_refcount) did not throw.
633 _M_ptr = __r._M_ptr;
634 }
635
636 // If an exception is thrown this constructor has no effect.
637 template<typename _Tp1, typename _Del>
638 __shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r)
639 : _M_ptr(__r.get()), _M_refcount()
640 {
641 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
642 _Tp1* __tmp = __r.get();
643 _M_refcount = __shared_count<_Lp>(std::move(__r));
644 __enable_shared_from_this_helper(_M_refcount, __tmp, __tmp);
645 }
646
647 #if _GLIBCXX_DEPRECATED
648 // Postcondition: use_count() == 1 and __r.get() == 0
649 template<typename _Tp1>
650 __shared_ptr(std::auto_ptr<_Tp1>&& __r)
651 : _M_ptr(__r.get()), _M_refcount()
652 {
653 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
654 static_assert( sizeof(_Tp1) > 0, "incomplete type" );
655 _Tp1* __tmp = __r.get();
656 _M_refcount = __shared_count<_Lp>(std::move(__r));
657 __enable_shared_from_this_helper(_M_refcount, __tmp, __tmp);
658 }
659 #endif
660
661 /* TODO: use delegating constructor */
662 __shared_ptr(nullptr_t) : _M_ptr(0), _M_refcount() // never throws
663 { }
664
665 template<typename _Tp1>
666 __shared_ptr&
667 operator=(const __shared_ptr<_Tp1, _Lp>& __r) // never throws
668 {
669 _M_ptr = __r._M_ptr;
670 _M_refcount = __r._M_refcount; // __shared_count::op= doesn't throw
671 return *this;
672 }
673
674 #if _GLIBCXX_DEPRECATED
675 template<typename _Tp1>
676 __shared_ptr&
677 operator=(std::auto_ptr<_Tp1>&& __r)
678 {
679 __shared_ptr(std::move(__r)).swap(*this);
680 return *this;
681 }
682 #endif
683
684 __shared_ptr&
685 operator=(__shared_ptr&& __r)
686 {
687 __shared_ptr(std::move(__r)).swap(*this);
688 return *this;
689 }
690
691 template<class _Tp1>
692 __shared_ptr&
693 operator=(__shared_ptr<_Tp1, _Lp>&& __r)
694 {
695 __shared_ptr(std::move(__r)).swap(*this);
696 return *this;
697 }
698
699 template<typename _Tp1, typename _Del>
700 __shared_ptr&
701 operator=(std::unique_ptr<_Tp1, _Del>&& __r)
702 {
703 __shared_ptr(std::move(__r)).swap(*this);
704 return *this;
705 }
706
707 void
708 reset() // never throws
709 { __shared_ptr().swap(*this); }
710
711 template<typename _Tp1>
712 void
713 reset(_Tp1* __p) // _Tp1 must be complete.
714 {
715 // Catch self-reset errors.
716 _GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != _M_ptr);
717 __shared_ptr(__p).swap(*this);
718 }
719
720 template<typename _Tp1, typename _Deleter>
721 void
722 reset(_Tp1* __p, _Deleter __d)
723 { __shared_ptr(__p, __d).swap(*this); }
724
725 template<typename _Tp1, typename _Deleter, typename _Alloc>
726 void
727 reset(_Tp1* __p, _Deleter __d, const _Alloc& __a)
728 { __shared_ptr(__p, __d, __a).swap(*this); }
729
730 // Allow class instantiation when _Tp is [cv-qual] void.
731 typename std::add_lvalue_reference<_Tp>::type
732 operator*() const // never throws
733 {
734 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
735 return *_M_ptr;
736 }
737
738 _Tp*
739 operator->() const // never throws
740 {
741 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
742 return _M_ptr;
743 }
744
745 _Tp*
746 get() const // never throws
747 { return _M_ptr; }
748
749 explicit operator bool() const // never throws
750 { return _M_ptr == 0 ? false : true; }
751
752 bool
753 unique() const // never throws
754 { return _M_refcount._M_unique(); }
755
756 long
757 use_count() const // never throws
758 { return _M_refcount._M_get_use_count(); }
759
760 void
761 swap(__shared_ptr<_Tp, _Lp>& __other) // never throws
762 {
763 std::swap(_M_ptr, __other._M_ptr);
764 _M_refcount._M_swap(__other._M_refcount);
765 }
766
767 template<typename _Tp1>
768 bool
769 owner_before(__shared_ptr<_Tp1, _Lp> const& __rhs) const
770 { return _M_refcount._M_less(__rhs._M_refcount); }
771
772 template<typename _Tp1>
773 bool
774 owner_before(__weak_ptr<_Tp1, _Lp> const& __rhs) const
775 { return _M_refcount._M_less(__rhs._M_refcount); }
776
777 #ifdef __GXX_RTTI
778 protected:
779 // This constructor is non-standard, it is used by allocate_shared.
780 template<typename _Alloc, typename... _Args>
781 __shared_ptr(_Sp_make_shared_tag __tag, _Alloc __a, _Args&&... __args)
782 : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a,
783 std::forward<_Args>(__args)...)
784 {
785 // _M_ptr needs to point to the newly constructed object.
786 // This relies on _Sp_counted_ptr_inplace::_M_get_deleter.
787 void* __p = _M_refcount._M_get_deleter(typeid(__tag));
788 _M_ptr = static_cast<_Tp*>(__p);
789 __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
790 }
791 #else
792 template<typename _Alloc>
793 struct _Deleter
794 {
795 void operator()(_Tp* __ptr)
796 {
797 _M_alloc.destroy(__ptr);
798 _M_alloc.deallocate(__ptr, 1);
799 }
800 _Alloc _M_alloc;
801 };
802
803 template<typename _Alloc, typename... _Args>
804 __shared_ptr(_Sp_make_shared_tag __tag, _Alloc __a, _Args&&... __args)
805 : _M_ptr(), _M_refcount()
806 {
807 typedef typename _Alloc::template rebind<_Tp>::other _Alloc2;
808 _Deleter<_Alloc2> __del = { _Alloc2(__a) };
809 _M_ptr = __del._M_alloc.allocate(1);
810 __try
811 {
812 __del._M_alloc.construct(_M_ptr, std::forward<_Args>(__args)...);
813 }
814 __catch(...)
815 {
816 __del._M_alloc.deallocate(_M_ptr, 1);
817 __throw_exception_again;
818 }
819 __shared_count<_Lp> __count(_M_ptr, __del, __del._M_alloc);
820 _M_refcount._M_swap(__count);
821 __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
822 }
823 #endif
824
825 template<typename _Tp1, _Lock_policy _Lp1, typename _Alloc,
826 typename... _Args>
827 friend __shared_ptr<_Tp1, _Lp1>
828 __allocate_shared(_Alloc __a, _Args&&... __args);
829
830 private:
831 void*
832 _M_get_deleter(const std::type_info& __ti) const
833 { return _M_refcount._M_get_deleter(__ti); }
834
835 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
836 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
837
838 template<typename _Del, typename _Tp1, _Lock_policy _Lp1>
839 friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&);
840
841 _Tp* _M_ptr; // Contained pointer.
842 __shared_count<_Lp> _M_refcount; // Reference counter.
843 };
844
845
846 // 20.8.13.2.7 shared_ptr comparisons
847 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
848 inline bool
849 operator==(const __shared_ptr<_Tp1, _Lp>& __a,
850 const __shared_ptr<_Tp2, _Lp>& __b)
851 { return __a.get() == __b.get(); }
852
853 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
854 inline bool
855 operator!=(const __shared_ptr<_Tp1, _Lp>& __a,
856 const __shared_ptr<_Tp2, _Lp>& __b)
857 { return __a.get() != __b.get(); }
858
859 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
860 inline bool
861 operator<(const __shared_ptr<_Tp1, _Lp>& __a,
862 const __shared_ptr<_Tp2, _Lp>& __b)
863 { return __a.get() < __b.get(); }
864
865 template<typename _Sp>
866 struct _Sp_less : public binary_function<_Sp, _Sp, bool>
867 {
868 bool
869 operator()(const _Sp& __lhs, const _Sp& __rhs) const
870 {
871 typedef typename _Sp::element_type element_type;
872 return std::less<element_type*>()(__lhs.get(), __rhs.get());
873 }
874 };
875
876 template<typename _Tp, _Lock_policy _Lp>
877 struct less<__shared_ptr<_Tp, _Lp>>
878 : public _Sp_less<__shared_ptr<_Tp, _Lp>>
879 { };
880
881 // XXX LessThanComparable<_Tp> concept should provide >, >= and <=
882 template<typename _Tp, _Lock_policy _Lp>
883 inline bool
884 operator>(const __shared_ptr<_Tp, _Lp>& __a,
885 const __shared_ptr<_Tp, _Lp>& __b)
886 { return __a.get() > __b.get(); }
887
888 template<typename _Tp, _Lock_policy _Lp>
889 inline bool
890 operator>=(const __shared_ptr<_Tp, _Lp>& __a,
891 const __shared_ptr<_Tp, _Lp>& __b)
892 { return __a.get() >= __b.get(); }
893
894 template<typename _Tp, _Lock_policy _Lp>
895 inline bool
896 operator<=(const __shared_ptr<_Tp, _Lp>& __a,
897 const __shared_ptr<_Tp, _Lp>& __b)
898 { return __a.get() <= __b.get(); }
899
900 // 2.2.3.8 shared_ptr specialized algorithms.
901 template<typename _Tp, _Lock_policy _Lp>
902 inline void
903 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b)
904 { __a.swap(__b); }
905
906 // 2.2.3.9 shared_ptr casts
907
908 // The seemingly equivalent code:
909 // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
910 // will eventually result in undefined behaviour, attempting to
911 // delete the same object twice.
912 /// static_pointer_cast
913 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
914 inline __shared_ptr<_Tp, _Lp>
915 static_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r)
916 { return __shared_ptr<_Tp, _Lp>(__r, static_cast<_Tp*>(__r.get())); }
917
918 // The seemingly equivalent code:
919 // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
920 // will eventually result in undefined behaviour, attempting to
921 // delete the same object twice.
922 /// const_pointer_cast
923 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
924 inline __shared_ptr<_Tp, _Lp>
925 const_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r)
926 { return __shared_ptr<_Tp, _Lp>(__r, const_cast<_Tp*>(__r.get())); }
927
928 // The seemingly equivalent code:
929 // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
930 // will eventually result in undefined behaviour, attempting to
931 // delete the same object twice.
932 /// dynamic_pointer_cast
933 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
934 inline __shared_ptr<_Tp, _Lp>
935 dynamic_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r)
936 {
937 if (_Tp* __p = dynamic_cast<_Tp*>(__r.get()))
938 return __shared_ptr<_Tp, _Lp>(__r, __p);
939 return __shared_ptr<_Tp, _Lp>();
940 }
941
942
943 template<typename _Tp, _Lock_policy _Lp>
944 class __weak_ptr
945 {
946 public:
947 typedef _Tp element_type;
948
949 __weak_ptr() : _M_ptr(0), _M_refcount() // never throws
950 { }
951
952 // Generated copy constructor, assignment, destructor are fine.
953
954 // The "obvious" converting constructor implementation:
955 //
956 // template<typename _Tp1>
957 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
958 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
959 // { }
960 //
961 // has a serious problem.
962 //
963 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
964 // conversion may require access to *__r._M_ptr (virtual inheritance).
965 //
966 // It is not possible to avoid spurious access violations since
967 // in multithreaded programs __r._M_ptr may be invalidated at any point.
968 template<typename _Tp1>
969 __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
970 : _M_refcount(__r._M_refcount) // never throws
971 {
972 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
973 _M_ptr = __r.lock().get();
974 }
975
976 template<typename _Tp1>
977 __weak_ptr(const __shared_ptr<_Tp1, _Lp>& __r)
978 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
979 { __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) }
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