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