shared_ptr.h: Add include guards.
[gcc.git] / libstdc++-v3 / include / bits / shared_ptr.h
1 // shared_ptr and weak_ptr implementation -*- 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 2, 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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 // shared_count.hpp
31 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
32
33 // shared_ptr.hpp
34 // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
35 // Copyright (C) 2001, 2002, 2003 Peter Dimov
36
37 // weak_ptr.hpp
38 // Copyright (C) 2001, 2002, 2003 Peter Dimov
39
40 // enable_shared_from_this.hpp
41 // Copyright (C) 2002 Peter Dimov
42
43 // Distributed under the Boost Software License, Version 1.0. (See
44 // accompanying file LICENSE_1_0.txt or copy at
45 // http://www.boost.org/LICENSE_1_0.txt)
46
47 // GCC Note: based on version 1.32.0 of the Boost library.
48
49 /** @file bits/shared_ptr.h
50 * This is an internal header file, included by other library headers.
51 * You should not attempt to use it directly.
52 */
53
54 #ifndef _SHARED_PTR_H
55 #define _SHARED_PTR_H 1
56
57 #ifndef __GXX_EXPERIMENTAL_CXX0X__
58 # include <c++0x_warning.h>
59 #endif
60
61 #if defined(_GLIBCXX_INCLUDE_AS_TR1)
62 # error C++0x header cannot be included from TR1 header
63 #endif
64
65 _GLIBCXX_BEGIN_NAMESPACE(std)
66
67 /**
68 * @addtogroup pointer_abstractions
69 * @{
70 */
71
72 // counted ptr with no deleter or allocator support
73 template<typename _Ptr, _Lock_policy _Lp>
74 class _Sp_counted_ptr
75 : public _Sp_counted_base<_Lp>
76 {
77 public:
78 _Sp_counted_ptr(_Ptr __p)
79 : _M_ptr(__p) { }
80
81 virtual void
82 _M_dispose() // nothrow
83 { delete _M_ptr; }
84
85 virtual void
86 _M_destroy() // nothrow
87 { delete this; }
88
89 virtual void*
90 _M_get_deleter(const std::type_info& __ti)
91 { return 0; }
92
93 _Sp_counted_ptr(const _Sp_counted_ptr&) = delete;
94 _Sp_counted_ptr& operator=(const _Sp_counted_ptr&) = delete;
95
96 protected:
97 _Ptr _M_ptr; // copy constructor must not throw
98 };
99
100 // support for custom deleter and/or allocator
101 template<typename _Ptr, typename _Deleter, typename _Alloc, _Lock_policy _Lp>
102 class _Sp_counted_deleter
103 : public _Sp_counted_ptr<_Ptr, _Lp>
104 {
105 typedef typename _Alloc::template
106 rebind<_Sp_counted_deleter>::other _My_alloc_type;
107
108 // Helper class that stores the Deleter and also acts as an allocator.
109 // Used to dispose of the owned pointer and the internal refcount
110 // Requires that copies of _Alloc can free each other's memory.
111 struct _My_Deleter
112 : public _My_alloc_type // copy constructor must not throw
113 {
114 _Deleter _M_del; // copy constructor must not throw
115 _My_Deleter(_Deleter __d, const _Alloc& __a)
116 : _My_alloc_type(__a), _M_del(__d) { }
117 };
118
119 protected:
120 typedef _Sp_counted_ptr<_Ptr, _Lp> _Base_type;
121
122 public:
123 /**
124 * @brief
125 * @pre __d(__p) must not throw.
126 */
127 _Sp_counted_deleter(_Ptr __p, _Deleter __d)
128 : _Base_type(__p), _M_del(__d, _Alloc()) { }
129
130 /**
131 * @brief
132 * @pre __d(__p) must not throw.
133 */
134 _Sp_counted_deleter(_Ptr __p, _Deleter __d, const _Alloc& __a)
135 : _Base_type(__p), _M_del(__d, __a) { }
136
137 virtual void
138 _M_dispose() // nothrow
139 { _M_del._M_del(_Base_type::_M_ptr); }
140
141 virtual void
142 _M_destroy() // nothrow
143 {
144 _My_alloc_type __a(_M_del);
145 this->~_Sp_counted_deleter();
146 __a.deallocate(this, 1);
147 }
148
149 virtual void*
150 _M_get_deleter(const std::type_info& __ti)
151 { return __ti == typeid(_Deleter) ? &_M_del._M_del : 0; }
152
153 protected:
154 _My_Deleter _M_del; // copy constructor must not throw
155 };
156
157 // helpers for make_shared / allocate_shared
158
159 template<typename _Tp>
160 struct _Sp_destroy_inplace
161 {
162 void operator()(_Tp* __p) const { if (__p) __p->~_Tp(); }
163 };
164
165 struct _Sp_make_shared_tag { };
166
167 template<typename _Tp, typename _Alloc, _Lock_policy _Lp>
168 class _Sp_counted_ptr_inplace
169 : public _Sp_counted_deleter<_Tp*, _Sp_destroy_inplace<_Tp>, _Alloc, _Lp>
170 {
171 typedef _Sp_counted_deleter<_Tp*, _Sp_destroy_inplace<_Tp>, _Alloc, _Lp>
172 _Base_type;
173
174 public:
175 _Sp_counted_ptr_inplace(_Alloc __a)
176 : _Base_type(static_cast<_Tp*>(0), _Sp_destroy_inplace<_Tp>(), __a)
177 , _M_storage()
178 {
179 void* __p = &_M_storage;
180 ::new (__p) _Tp(); // might throw
181 _Base_type::_Base_type::_M_ptr = static_cast<_Tp*>(__p);
182 }
183
184 template<typename... _Args>
185 _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
186 : _Base_type(static_cast<_Tp*>(0), _Sp_destroy_inplace<_Tp>(), __a)
187 , _M_storage()
188 {
189 void* __p = &_M_storage;
190 ::new (__p) _Tp(std::forward<_Args>(__args)...); // might throw
191 _Base_type::_Base_type::_M_ptr = static_cast<_Tp*>(__p);
192 }
193
194 // override because the allocator needs to know the dynamic type
195 virtual void
196 _M_destroy() // nothrow
197 {
198 typedef typename _Alloc::template
199 rebind<_Sp_counted_ptr_inplace>::other _My_alloc_type;
200 _My_alloc_type __a(_Base_type::_M_del);
201 this->~_Sp_counted_ptr_inplace();
202 __a.deallocate(this, 1);
203 }
204
205 // sneaky trick so __shared_ptr can get the managed pointer
206 virtual void*
207 _M_get_deleter(const std::type_info& __ti)
208 {
209 return __ti == typeid(_Sp_make_shared_tag)
210 ? static_cast<void*>(&_M_storage)
211 : _Base_type::_M_get_deleter(__ti);
212 }
213
214 private:
215 typename aligned_storage<sizeof(_Tp), alignment_of<_Tp>::value>::type
216 _M_storage;
217 };
218
219 template<_Lock_policy _Lp = __default_lock_policy>
220 class __weak_count;
221
222 template<_Lock_policy _Lp = __default_lock_policy>
223 class __shared_count
224 {
225 public:
226 __shared_count()
227 : _M_pi(0) // nothrow
228 { }
229
230 template<typename _Ptr>
231 __shared_count(_Ptr __p) : _M_pi(0)
232 {
233 __try
234 {
235 _M_pi = new _Sp_counted_ptr<_Ptr, _Lp>(__p);
236 }
237 __catch(...)
238 {
239 delete __p;
240 __throw_exception_again;
241 }
242 }
243
244 template<typename _Ptr, typename _Deleter>
245 __shared_count(_Ptr __p, _Deleter __d) : _M_pi(0)
246 {
247 // allocator's value_type doesn't matter, will rebind it anyway
248 typedef std::allocator<int> _Alloc;
249 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
250 typedef std::allocator<_Sp_cd_type> _Alloc2;
251 _Alloc2 __a2;
252 __try
253 {
254 _M_pi = __a2.allocate(1);
255 ::new(static_cast<void*>(_M_pi)) _Sp_cd_type(__p, __d);
256 }
257 __catch(...)
258 {
259 __d(__p); // Call _Deleter on __p.
260 if (_M_pi)
261 __a2.deallocate(static_cast<_Sp_cd_type*>(_M_pi), 1);
262 __throw_exception_again;
263 }
264 }
265
266 template<typename _Ptr, typename _Deleter, typename _Alloc>
267 __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
268 {
269 typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
270 typedef typename _Alloc::template rebind<_Sp_cd_type>::other _Alloc2;
271 _Alloc2 __a2(__a);
272 __try
273 {
274 _M_pi = __a2.allocate(1);
275 ::new(static_cast<void*>(_M_pi)) _Sp_cd_type(__p, __d, __a);
276 }
277 __catch(...)
278 {
279 __d(__p); // Call _Deleter on __p.
280 if (_M_pi)
281 __a2.deallocate(static_cast<_Sp_cd_type*>(_M_pi), 1);
282 __throw_exception_again;
283 }
284 }
285
286 template<typename _Tp, typename _Alloc, typename... _Args>
287 __shared_count(_Sp_make_shared_tag, _Tp*, _Alloc __a, _Args&&... __args)
288 : _M_pi(0)
289 {
290 typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
291 typedef typename _Alloc::template rebind<_Sp_cp_type>::other _Alloc2;
292 _Alloc2 __a2(__a);
293 __try
294 {
295 _M_pi = __a2.allocate(1);
296 ::new(static_cast<void*>(_M_pi)) _Sp_cp_type(__a,
297 std::forward<_Args>(__args)...);
298 }
299 __catch(...)
300 {
301 if (_M_pi)
302 __a2.deallocate(static_cast<_Sp_cp_type*>(_M_pi), 1);
303 __throw_exception_again;
304 }
305 }
306
307 #if _GLIBCXX_DEPRECATED
308 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
309 template<typename _Tp>
310 explicit
311 __shared_count(std::auto_ptr<_Tp>&& __r)
312 : _M_pi(new _Sp_counted_ptr<_Tp*, _Lp>(__r.get()))
313 { __r.release(); }
314 #endif
315
316 // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
317 template<typename _Tp, typename _Del>
318 explicit
319 __shared_count(std::unique_ptr<_Tp, _Del>&& __r)
320 : _M_pi(_S_create_from_up(std::move(__r)))
321 { __r.release(); }
322
323 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
324 explicit
325 __shared_count(const __weak_count<_Lp>& __r);
326
327 ~__shared_count() // nothrow
328 {
329 if (_M_pi != 0)
330 _M_pi->_M_release();
331 }
332
333 __shared_count(const __shared_count& __r)
334 : _M_pi(__r._M_pi) // nothrow
335 {
336 if (_M_pi != 0)
337 _M_pi->_M_add_ref_copy();
338 }
339
340 __shared_count&
341 operator=(const __shared_count& __r) // nothrow
342 {
343 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
344 if (__tmp != _M_pi)
345 {
346 if (__tmp != 0)
347 __tmp->_M_add_ref_copy();
348 if (_M_pi != 0)
349 _M_pi->_M_release();
350 _M_pi = __tmp;
351 }
352 return *this;
353 }
354
355 void
356 _M_swap(__shared_count& __r) // nothrow
357 {
358 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
359 __r._M_pi = _M_pi;
360 _M_pi = __tmp;
361 }
362
363 long
364 _M_get_use_count() const // nothrow
365 { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
366
367 bool
368 _M_unique() const // nothrow
369 { return this->_M_get_use_count() == 1; }
370
371 void*
372 _M_get_deleter(const std::type_info& __ti) const
373 { return _M_pi ? _M_pi->_M_get_deleter(__ti) : 0; }
374
375 bool
376 _M_less(const __shared_count& __rhs) const
377 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
378
379 bool
380 _M_less(const __weak_count<_Lp>& __rhs) const
381 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
382
383 // friend function injected into enclosing namespace and found by ADL
384 friend inline bool
385 operator==(const __shared_count& __a, const __shared_count& __b)
386 { return __a._M_pi == __b._M_pi; }
387
388 private:
389 friend class __weak_count<_Lp>;
390
391 template<typename _Tp, typename _Del>
392 static _Sp_counted_base<_Lp>*
393 _S_create_from_up(std::unique_ptr<_Tp, _Del>&& __r,
394 typename std::enable_if<!std::is_reference<_Del>::value>::type* = 0)
395 {
396 return new _Sp_counted_deleter<_Tp*, _Del, std::allocator<_Tp>,
397 _Lp>(__r.get(), __r.get_deleter());
398 }
399
400 template<typename _Tp, typename _Del>
401 static _Sp_counted_base<_Lp>*
402 _S_create_from_up(std::unique_ptr<_Tp, _Del>&& __r,
403 typename std::enable_if<std::is_reference<_Del>::value>::type* = 0)
404 {
405 typedef typename std::remove_reference<_Del>::type _Del1;
406 typedef std::reference_wrapper<_Del1> _Del2;
407 return new _Sp_counted_deleter<_Tp*, _Del2, std::allocator<_Tp>,
408 _Lp>(__r.get(), std::ref(__r.get_deleter()));
409 }
410
411 _Sp_counted_base<_Lp>* _M_pi;
412 };
413
414
415 template<_Lock_policy _Lp>
416 class __weak_count
417 {
418 public:
419 __weak_count()
420 : _M_pi(0) // nothrow
421 { }
422
423 __weak_count(const __shared_count<_Lp>& __r)
424 : _M_pi(__r._M_pi) // nothrow
425 {
426 if (_M_pi != 0)
427 _M_pi->_M_weak_add_ref();
428 }
429
430 __weak_count(const __weak_count<_Lp>& __r)
431 : _M_pi(__r._M_pi) // nothrow
432 {
433 if (_M_pi != 0)
434 _M_pi->_M_weak_add_ref();
435 }
436
437 ~__weak_count() // nothrow
438 {
439 if (_M_pi != 0)
440 _M_pi->_M_weak_release();
441 }
442
443 __weak_count<_Lp>&
444 operator=(const __shared_count<_Lp>& __r) // nothrow
445 {
446 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
447 if (__tmp != 0)
448 __tmp->_M_weak_add_ref();
449 if (_M_pi != 0)
450 _M_pi->_M_weak_release();
451 _M_pi = __tmp;
452 return *this;
453 }
454
455 __weak_count<_Lp>&
456 operator=(const __weak_count<_Lp>& __r) // nothrow
457 {
458 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
459 if (__tmp != 0)
460 __tmp->_M_weak_add_ref();
461 if (_M_pi != 0)
462 _M_pi->_M_weak_release();
463 _M_pi = __tmp;
464 return *this;
465 }
466
467 void
468 _M_swap(__weak_count<_Lp>& __r) // nothrow
469 {
470 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
471 __r._M_pi = _M_pi;
472 _M_pi = __tmp;
473 }
474
475 long
476 _M_get_use_count() const // nothrow
477 { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
478
479 bool
480 _M_less(const __weak_count& __rhs) const
481 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
482
483 bool
484 _M_less(const __shared_count<_Lp>& __rhs) const
485 { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
486
487 // friend function injected into enclosing namespace and found by ADL
488 friend inline bool
489 operator==(const __weak_count& __a, const __weak_count& __b)
490 { return __a._M_pi == __b._M_pi; }
491
492 private:
493 friend class __shared_count<_Lp>;
494
495 _Sp_counted_base<_Lp>* _M_pi;
496 };
497
498 // now that __weak_count is defined we can define this constructor:
499 template<_Lock_policy _Lp>
500 inline
501 __shared_count<_Lp>::
502 __shared_count(const __weak_count<_Lp>& __r)
503 : _M_pi(__r._M_pi)
504 {
505 if (_M_pi != 0)
506 _M_pi->_M_add_ref_lock();
507 else
508 __throw_bad_weak_ptr();
509 }
510
511 // Forward declarations.
512 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
513 class __shared_ptr;
514
515 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
516 class __weak_ptr;
517
518 template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
519 class __enable_shared_from_this;
520
521 template<typename _Tp>
522 class shared_ptr;
523
524 template<typename _Tp>
525 class weak_ptr;
526
527 template<typename _Tp>
528 class enable_shared_from_this;
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 /** @brief Construct an empty %__shared_ptr.
559 * @post use_count()==0 && get()==0
560 */
561 __shared_ptr()
562 : _M_ptr(0), _M_refcount() // never throws
563 { }
564
565 /** @brief Construct a %__shared_ptr that owns the pointer @a __p.
566 * @param __p A pointer that is convertible to element_type*.
567 * @post use_count() == 1 && get() == __p
568 * @throw std::bad_alloc, in which case @c delete @a __p is called.
569 */
570 template<typename _Tp1>
571 explicit
572 __shared_ptr(_Tp1* __p)
573 : _M_ptr(__p), _M_refcount(__p)
574 {
575 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
576 // __glibcxx_function_requires(_CompleteConcept<_Tp1*>)
577 __enable_shared_from_this_helper(_M_refcount, __p, __p);
578 }
579
580 //
581 // Requirements: _Deleter's copy constructor and destructor must
582 // not throw
583 //
584 // __shared_ptr will release __p by calling __d(__p)
585 //
586 /** @brief Construct a %__shared_ptr that owns the pointer @a __p
587 * and the deleter @a __d.
588 * @param __p A pointer.
589 * @param __d A deleter.
590 * @post use_count() == 1 && get() == __p
591 * @throw std::bad_alloc, in which case @a __d(__p) is called.
592 */
593 template<typename _Tp1, typename _Deleter>
594 __shared_ptr(_Tp1* __p, _Deleter __d)
595 : _M_ptr(__p), _M_refcount(__p, __d)
596 {
597 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
598 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
599 __enable_shared_from_this_helper(_M_refcount, __p, __p);
600 }
601
602 //
603 // Requirements: _Deleter's copy constructor and destructor must
604 // not throw _Alloc's copy constructor and destructor must not
605 // throw.
606 //
607 // __shared_ptr will release __p by calling __d(__p)
608 //
609 /** @brief Construct a %__shared_ptr that owns the pointer @a __p
610 * and the deleter @a __d.
611 * @param __p A pointer.
612 * @param __d A deleter.
613 * @param __a An allocator.
614 * @post use_count() == 1 && get() == __p
615 * @throw std::bad_alloc, in which case @a __d(__p) is called.
616 */
617 template<typename _Tp1, typename _Deleter, typename _Alloc>
618 __shared_ptr(_Tp1* __p, _Deleter __d, const _Alloc& __a)
619 : _M_ptr(__p), _M_refcount(__p, __d, __a)
620 {
621 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
622 // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
623 __enable_shared_from_this_helper(_M_refcount, __p, __p);
624 }
625
626 /** @brief Constructs a %__shared_ptr instance that stores @a __p
627 * and shares ownership with @a __r.
628 * @param __r A %__shared_ptr.
629 * @param __p A pointer that will remain valid while @a *__r is valid.
630 * @post get() == __p && use_count() == __r.use_count()
631 *
632 * This can be used to construct a @c shared_ptr to a sub-object
633 * of an object managed by an existing @c shared_ptr.
634 *
635 * @code
636 * shared_ptr< pair<int,int> > pii(new pair<int,int>());
637 * shared_ptr<int> pi(pii, &pii->first);
638 * assert(pii.use_count() == 2);
639 * @endcode
640 */
641 template<typename _Tp1>
642 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r, _Tp* __p)
643 : _M_ptr(__p), _M_refcount(__r._M_refcount) // never throws
644 { }
645
646 // generated copy constructor, assignment, destructor are fine.
647
648 /** @brief If @a __r is empty, constructs an empty %__shared_ptr;
649 * otherwise construct a %__shared_ptr that shares ownership
650 * with @a __r.
651 * @param __r A %__shared_ptr.
652 * @post get() == __r.get() && use_count() == __r.use_count()
653 */
654 template<typename _Tp1>
655 __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r)
656 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
657 { __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) }
658
659 /** @brief Move-constructs a %__shared_ptr instance from @a __r.
660 * @param __r A %__shared_ptr rvalue.
661 * @post *this contains the old value of @a __r, @a __r is empty.
662 */
663 __shared_ptr(__shared_ptr&& __r)
664 : _M_ptr(__r._M_ptr), _M_refcount() // never throws
665 {
666 _M_refcount._M_swap(__r._M_refcount);
667 __r._M_ptr = 0;
668 }
669
670 /** @brief Move-constructs a %__shared_ptr instance from @a __r.
671 * @param __r A %__shared_ptr rvalue.
672 * @post *this contains the old value of @a __r, @a __r is empty.
673 */
674 template<typename _Tp1>
675 __shared_ptr(__shared_ptr<_Tp1, _Lp>&& __r)
676 : _M_ptr(__r._M_ptr), _M_refcount() // never throws
677 {
678 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
679 _M_refcount._M_swap(__r._M_refcount);
680 __r._M_ptr = 0;
681 }
682
683 /** @brief Constructs a %__shared_ptr that shares ownership with @a __r
684 * and stores a copy of the pointer stored in @a __r.
685 * @param __r A weak_ptr.
686 * @post use_count() == __r.use_count()
687 * @throw bad_weak_ptr when __r.expired(),
688 * in which case the constructor has no effect.
689 */
690 template<typename _Tp1>
691 explicit
692 __shared_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
693 : _M_refcount(__r._M_refcount) // may throw
694 {
695 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
696 // It is now safe to copy __r._M_ptr, as _M_refcount(__r._M_refcount)
697 // did not throw.
698 _M_ptr = __r._M_ptr;
699 }
700
701 template<typename _Tp1, typename _Del>
702 explicit
703 __shared_ptr(const std::unique_ptr<_Tp1, _Del>&) = delete;
704
705 /**
706 * If an exception is thrown this constructor has no effect.
707 */
708 template<typename _Tp1, typename _Del>
709 explicit
710 __shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r)
711 : _M_ptr(__r.get()), _M_refcount()
712 {
713 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
714 _Tp1* __tmp = __r.get();
715 _M_refcount = __shared_count<_Lp>(std::move(__r));
716 __enable_shared_from_this_helper(_M_refcount, __tmp, __tmp);
717 }
718
719 #if _GLIBCXX_DEPRECATED
720 /**
721 * @post use_count() == 1 and __r.get() == 0
722 */
723 template<typename _Tp1>
724 explicit
725 __shared_ptr(std::auto_ptr<_Tp1>&& __r)
726 : _M_ptr(__r.get()), _M_refcount()
727 {
728 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
729 // TODO requires _Tp1 is complete, delete __r.release() well-formed
730 _Tp1* __tmp = __r.get();
731 _M_refcount = __shared_count<_Lp>(std::move(__r));
732 __enable_shared_from_this_helper(_M_refcount, __tmp, __tmp);
733 }
734 #endif
735
736 template<typename _Tp1>
737 __shared_ptr&
738 operator=(const __shared_ptr<_Tp1, _Lp>& __r) // never throws
739 {
740 _M_ptr = __r._M_ptr;
741 _M_refcount = __r._M_refcount; // __shared_count::op= doesn't throw
742 return *this;
743 }
744
745 #if _GLIBCXX_DEPRECATED
746 template<typename _Tp1>
747 __shared_ptr&
748 operator=(std::auto_ptr<_Tp1>&& __r)
749 {
750 __shared_ptr(std::move(__r)).swap(*this);
751 return *this;
752 }
753 #endif
754
755 __shared_ptr&
756 operator=(__shared_ptr&& __r)
757 {
758 __shared_ptr(std::move(__r)).swap(*this);
759 return *this;
760 }
761
762 template<class _Tp1>
763 __shared_ptr&
764 operator=(__shared_ptr<_Tp1, _Lp>&& __r)
765 {
766 __shared_ptr(std::move(__r)).swap(*this);
767 return *this;
768 }
769
770 template<typename _Tp1, typename _Del>
771 __shared_ptr&
772 operator=(const std::unique_ptr<_Tp1, _Del>& __r) = delete;
773
774 template<typename _Tp1, typename _Del>
775 __shared_ptr&
776 operator=(std::unique_ptr<_Tp1, _Del>&& __r)
777 {
778 __shared_ptr(std::move(__r)).swap(*this);
779 return *this;
780 }
781
782 void
783 reset() // never throws
784 { __shared_ptr().swap(*this); }
785
786 template<typename _Tp1>
787 void
788 reset(_Tp1* __p) // _Tp1 must be complete.
789 {
790 // Catch self-reset errors.
791 _GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != _M_ptr);
792 __shared_ptr(__p).swap(*this);
793 }
794
795 template<typename _Tp1, typename _Deleter>
796 void
797 reset(_Tp1* __p, _Deleter __d)
798 { __shared_ptr(__p, __d).swap(*this); }
799
800 template<typename _Tp1, typename _Deleter, typename _Alloc>
801 void
802 reset(_Tp1* __p, _Deleter __d, const _Alloc& __a)
803 { __shared_ptr(__p, __d, __a).swap(*this); }
804
805 // Allow class instantiation when _Tp is [cv-qual] void.
806 typename std::add_lvalue_reference<_Tp>::type
807 operator*() const // never throws
808 {
809 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
810 return *_M_ptr;
811 }
812
813 _Tp*
814 operator->() const // never throws
815 {
816 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
817 return _M_ptr;
818 }
819
820 _Tp*
821 get() const // never throws
822 { return _M_ptr; }
823
824 // Implicit conversion to "bool"
825 private:
826 typedef _Tp* __shared_ptr::*__unspecified_bool_type;
827
828 public:
829 operator __unspecified_bool_type() const // never throws
830 { return _M_ptr == 0 ? 0 : &__shared_ptr::_M_ptr; }
831
832 bool
833 unique() const // never throws
834 { return _M_refcount._M_unique(); }
835
836 long
837 use_count() const // never throws
838 { return _M_refcount._M_get_use_count(); }
839
840 void
841 swap(__shared_ptr<_Tp, _Lp>&& __other) // never throws
842 {
843 std::swap(_M_ptr, __other._M_ptr);
844 _M_refcount._M_swap(__other._M_refcount);
845 }
846
847 template<typename _Tp1>
848 bool
849 owner_before(__shared_ptr<_Tp1, _Lp> const& __rhs) const
850 { return _M_refcount._M_less(__rhs._M_refcount); }
851
852 template<typename _Tp1>
853 bool
854 owner_before(__weak_ptr<_Tp1, _Lp> const& __rhs) const
855 { return _M_refcount._M_less(__rhs._M_refcount); }
856
857 protected:
858 // This constructor is non-standard, it is used by allocate_shared.
859 template<typename _Alloc, typename... _Args>
860 __shared_ptr(_Sp_make_shared_tag __tag, _Alloc __a, _Args&&... __args)
861 : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a,
862 std::forward<_Args>(__args)...)
863 {
864 // _M_ptr needs to point to the newly constructed object.
865 // This relies on _Sp_counted_ptr_inplace::_M_get_deleter.
866 void* __p = _M_refcount._M_get_deleter(typeid(__tag));
867 _M_ptr = static_cast<_Tp*>(__p);
868 __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
869 }
870
871 template<typename _Tp1, _Lock_policy _Lp1, typename _Alloc,
872 typename... _Args>
873 friend __shared_ptr<_Tp1, _Lp1>
874 __allocate_shared(_Alloc __a, _Args&&... __args);
875
876 private:
877 void*
878 _M_get_deleter(const std::type_info& __ti) const
879 { return _M_refcount._M_get_deleter(__ti); }
880
881 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
882 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
883
884 template<typename _Del, typename _Tp1, _Lock_policy _Lp1>
885 friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&);
886
887 _Tp* _M_ptr; // Contained pointer.
888 __shared_count<_Lp> _M_refcount; // Reference counter.
889 };
890
891 // 20.8.13.2.7 shared_ptr comparisons
892 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
893 inline bool
894 operator==(const __shared_ptr<_Tp1, _Lp>& __a,
895 const __shared_ptr<_Tp2, _Lp>& __b)
896 { return __a.get() == __b.get(); }
897
898 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
899 inline bool
900 operator!=(const __shared_ptr<_Tp1, _Lp>& __a,
901 const __shared_ptr<_Tp2, _Lp>& __b)
902 { return __a.get() != __b.get(); }
903
904 template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
905 inline bool
906 operator<(const __shared_ptr<_Tp1, _Lp>& __a,
907 const __shared_ptr<_Tp2, _Lp>& __b)
908 { return __a.get() < __b.get(); }
909
910 template<typename _Sp>
911 struct _Sp_less : public binary_function<_Sp, _Sp, bool>
912 {
913 bool
914 operator()(const _Sp& __lhs, const _Sp& __rhs) const
915 {
916 return std::less<typename _Sp::element_type*>()(__lhs.get(),
917 __rhs.get());
918 }
919 };
920
921 template<typename _Tp, _Lock_policy _Lp>
922 struct less<__shared_ptr<_Tp, _Lp>>
923 : public _Sp_less<__shared_ptr<_Tp, _Lp>>
924 { };
925
926 // XXX LessThanComparable<_Tp> concept should provide >, >= and <=
927 template<typename _Tp, _Lock_policy _Lp>
928 inline bool
929 operator>(const __shared_ptr<_Tp, _Lp>& __a,
930 const __shared_ptr<_Tp, _Lp>& __b)
931 { return __a.get() > __b.get(); }
932
933 template<typename _Tp, _Lock_policy _Lp>
934 inline bool
935 operator>=(const __shared_ptr<_Tp, _Lp>& __a,
936 const __shared_ptr<_Tp, _Lp>& __b)
937 { return __a.get() >= __b.get(); }
938
939 template<typename _Tp, _Lock_policy _Lp>
940 inline bool
941 operator<=(const __shared_ptr<_Tp, _Lp>& __a,
942 const __shared_ptr<_Tp, _Lp>& __b)
943 { return __a.get() <= __b.get(); }
944
945 // 2.2.3.8 shared_ptr specialized algorithms.
946 template<typename _Tp, _Lock_policy _Lp>
947 inline void
948 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b)
949 { __a.swap(__b); }
950
951 template<typename _Tp, _Lock_policy _Lp>
952 inline void
953 swap(__shared_ptr<_Tp, _Lp>&& __a, __shared_ptr<_Tp, _Lp>& __b)
954 { __a.swap(__b); }
955
956 template<typename _Tp, _Lock_policy _Lp>
957 inline void
958 swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>&& __b)
959 { __a.swap(__b); }
960
961 // 2.2.3.9 shared_ptr casts
962 /** @warning The seemingly equivalent
963 * <code>shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))</code>
964 * will eventually result in undefined behaviour,
965 * attempting to delete the same object twice.
966 */
967 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
968 inline __shared_ptr<_Tp, _Lp>
969 static_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r)
970 { return __shared_ptr<_Tp, _Lp>(__r, static_cast<_Tp*>(__r.get())); }
971
972 /** @warning The seemingly equivalent
973 * <code>shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))</code>
974 * will eventually result in undefined behaviour,
975 * attempting to delete the same object twice.
976 */
977 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
978 inline __shared_ptr<_Tp, _Lp>
979 const_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r)
980 { return __shared_ptr<_Tp, _Lp>(__r, const_cast<_Tp*>(__r.get())); }
981
982 /** @warning The seemingly equivalent
983 * <code>shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))</code>
984 * will eventually result in undefined behaviour,
985 * attempting to delete the same object twice.
986 */
987 template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
988 inline __shared_ptr<_Tp, _Lp>
989 dynamic_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r)
990 {
991 if (_Tp* __p = dynamic_cast<_Tp*>(__r.get()))
992 return __shared_ptr<_Tp, _Lp>(__r, __p);
993 return __shared_ptr<_Tp, _Lp>();
994 }
995
996 // 2.2.3.7 shared_ptr I/O
997 template<typename _Ch, typename _Tr, typename _Tp, _Lock_policy _Lp>
998 std::basic_ostream<_Ch, _Tr>&
999 operator<<(std::basic_ostream<_Ch, _Tr>& __os,
1000 const __shared_ptr<_Tp, _Lp>& __p)
1001 {
1002 __os << __p.get();
1003 return __os;
1004 }
1005
1006 // 2.2.3.10 shared_ptr get_deleter (experimental)
1007 template<typename _Del, typename _Tp, _Lock_policy _Lp>
1008 inline _Del*
1009 get_deleter(const __shared_ptr<_Tp, _Lp>& __p)
1010 { return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del))); }
1011
1012
1013 template<typename _Tp, _Lock_policy _Lp>
1014 class __weak_ptr
1015 {
1016 public:
1017 typedef _Tp element_type;
1018
1019 __weak_ptr()
1020 : _M_ptr(0), _M_refcount() // never throws
1021 { }
1022
1023 // Generated copy constructor, assignment, destructor are fine.
1024
1025 // The "obvious" converting constructor implementation:
1026 //
1027 // template<typename _Tp1>
1028 // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
1029 // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
1030 // { }
1031 //
1032 // has a serious problem.
1033 //
1034 // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
1035 // conversion may require access to *__r._M_ptr (virtual inheritance).
1036 //
1037 // It is not possible to avoid spurious access violations since
1038 // in multithreaded programs __r._M_ptr may be invalidated at any point.
1039 template<typename _Tp1>
1040 __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
1041 : _M_refcount(__r._M_refcount) // never throws
1042 {
1043 __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
1044 _M_ptr = __r.lock().get();
1045 }
1046
1047 template<typename _Tp1>
1048 __weak_ptr(const __shared_ptr<_Tp1, _Lp>& __r)
1049 : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
1050 { __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) }
1051
1052 template<typename _Tp1>
1053 __weak_ptr&
1054 operator=(const __weak_ptr<_Tp1, _Lp>& __r) // never throws
1055 {
1056 _M_ptr = __r.lock().get();
1057 _M_refcount = __r._M_refcount;
1058 return *this;
1059 }
1060
1061 template<typename _Tp1>
1062 __weak_ptr&
1063 operator=(const __shared_ptr<_Tp1, _Lp>& __r) // never throws
1064 {
1065 _M_ptr = __r._M_ptr;
1066 _M_refcount = __r._M_refcount;
1067 return *this;
1068 }
1069
1070 __shared_ptr<_Tp, _Lp>
1071 lock() const // never throws
1072 {
1073 #ifdef __GTHREADS
1074 // Optimization: avoid throw overhead.
1075 if (expired())
1076 return __shared_ptr<element_type, _Lp>();
1077
1078 __try
1079 {
1080 return __shared_ptr<element_type, _Lp>(*this);
1081 }
1082 __catch(const bad_weak_ptr&)
1083 {
1084 // Q: How can we get here?
1085 // A: Another thread may have invalidated r after the
1086 // use_count test above.
1087 return __shared_ptr<element_type, _Lp>();
1088 }
1089
1090 #else
1091 // Optimization: avoid try/catch overhead when single threaded.
1092 return expired() ? __shared_ptr<element_type, _Lp>()
1093 : __shared_ptr<element_type, _Lp>(*this);
1094
1095 #endif
1096 } // XXX MT
1097
1098 long
1099 use_count() const // never throws
1100 { return _M_refcount._M_get_use_count(); }
1101
1102 bool
1103 expired() const // never throws
1104 { return _M_refcount._M_get_use_count() == 0; }
1105
1106 template<typename _Tp1>
1107 bool
1108 owner_before(const __shared_ptr<_Tp1, _Lp>& __rhs) const
1109 { return _M_refcount._M_less(__rhs._M_refcount); }
1110
1111 template<typename _Tp1>
1112 bool
1113 owner_before(const __weak_ptr<_Tp1, _Lp>& __rhs) const
1114 { return _M_refcount._M_less(__rhs._M_refcount); }
1115
1116 void
1117 reset() // never throws
1118 { __weak_ptr().swap(*this); }
1119
1120 void
1121 swap(__weak_ptr& __s) // never throws
1122 {
1123 std::swap(_M_ptr, __s._M_ptr);
1124 _M_refcount._M_swap(__s._M_refcount);
1125 }
1126
1127 // comparisons
1128 template<typename _Tp1>
1129 bool operator<(const __weak_ptr<_Tp1, _Lp>&) const = delete;
1130 template<typename _Tp1>
1131 bool operator<=(const __weak_ptr<_Tp1, _Lp>&) const = delete;
1132 template<typename _Tp1>
1133 bool operator>(const __weak_ptr<_Tp1, _Lp>&) const = delete;
1134 template<typename _Tp1>
1135 bool operator>=(const __weak_ptr<_Tp1, _Lp>&) const = delete;
1136
1137 private:
1138 // Used by __enable_shared_from_this.
1139 void
1140 _M_assign(_Tp* __ptr, const __shared_count<_Lp>& __refcount)
1141 {
1142 _M_ptr = __ptr;
1143 _M_refcount = __refcount;
1144 }
1145
1146 template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
1147 template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
1148 friend class __enable_shared_from_this<_Tp, _Lp>;
1149 friend class enable_shared_from_this<_Tp>;
1150
1151 _Tp* _M_ptr; // Contained pointer.
1152 __weak_count<_Lp> _M_refcount; // Reference counter.
1153 };
1154
1155 // 20.8.13.3.7 weak_ptr specialized algorithms.
1156 template<typename _Tp, _Lock_policy _Lp>
1157 inline void
1158 swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b)
1159 { __a.swap(__b); }
1160
1161 /// owner_less
1162 template<typename _Tp> struct owner_less;
1163
1164 template<typename _Tp, typename _Tp1>
1165 struct _Sp_owner_less : public binary_function<_Tp, _Tp, bool>
1166 {
1167 bool
1168 operator()(const _Tp& __lhs, const _Tp& __rhs) const
1169 { return __lhs.owner_before(__rhs); }
1170 bool
1171 operator()(const _Tp& __lhs, const _Tp1& __rhs) const
1172 { return __lhs.owner_before(__rhs); }
1173 bool
1174 operator()(const _Tp1& __lhs, const _Tp& __rhs) const
1175 { return __lhs.owner_before(__rhs); }
1176 };
1177
1178 template<typename _Tp, _Lock_policy _Lp>
1179 struct owner_less<__shared_ptr<_Tp, _Lp>>
1180 : public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
1181 { };
1182
1183 template<typename _Tp, _Lock_policy _Lp>
1184 struct owner_less<__weak_ptr<_Tp, _Lp>>
1185 : public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
1186 {
1187 };
1188
1189
1190 template<typename _Tp, _Lock_policy _Lp>
1191 class __enable_shared_from_this
1192 {
1193 protected:
1194 __enable_shared_from_this() { }
1195
1196 __enable_shared_from_this(const __enable_shared_from_this&) { }
1197
1198 __enable_shared_from_this&
1199 operator=(const __enable_shared_from_this&)
1200 { return *this; }
1201
1202 ~__enable_shared_from_this() { }
1203
1204 public:
1205 __shared_ptr<_Tp, _Lp>
1206 shared_from_this()
1207 { return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
1208
1209 __shared_ptr<const _Tp, _Lp>
1210 shared_from_this() const
1211 { return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
1212
1213 private:
1214 template<typename _Tp1>
1215 void
1216 _M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const
1217 { _M_weak_this._M_assign(__p, __n); }
1218
1219 template<typename _Tp1>
1220 friend void
1221 __enable_shared_from_this_helper(const __shared_count<_Lp>& __pn,
1222 const __enable_shared_from_this* __pe,
1223 const _Tp1* __px)
1224 {
1225 if (__pe != 0)
1226 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
1227 }
1228
1229 mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
1230 };
1231
1232 /**
1233 * @brief A smart pointer with reference-counted copy semantics.
1234 *
1235 * The object pointed to is deleted when the last shared_ptr pointing to
1236 * it is destroyed or reset.
1237 */
1238 template<typename _Tp>
1239 class shared_ptr
1240 : public __shared_ptr<_Tp>
1241 {
1242 public:
1243 shared_ptr()
1244 : __shared_ptr<_Tp>() { }
1245
1246 template<typename _Tp1>
1247 explicit
1248 shared_ptr(_Tp1* __p)
1249 : __shared_ptr<_Tp>(__p) { }
1250
1251 template<typename _Tp1, typename _Deleter>
1252 shared_ptr(_Tp1* __p, _Deleter __d)
1253 : __shared_ptr<_Tp>(__p, __d) { }
1254
1255 template<typename _Tp1, typename _Deleter, typename _Alloc>
1256 shared_ptr(_Tp1* __p, _Deleter __d, const _Alloc& __a)
1257 : __shared_ptr<_Tp>(__p, __d, __a) { }
1258
1259 // Aliasing constructor
1260 template<typename _Tp1>
1261 shared_ptr(const shared_ptr<_Tp1>& __r, _Tp* __p)
1262 : __shared_ptr<_Tp>(__r, __p) { }
1263
1264 template<typename _Tp1>
1265 shared_ptr(const shared_ptr<_Tp1>& __r)
1266 : __shared_ptr<_Tp>(__r) { }
1267
1268 shared_ptr(shared_ptr&& __r)
1269 : __shared_ptr<_Tp>(std::move(__r)) { }
1270
1271 template<typename _Tp1>
1272 shared_ptr(shared_ptr<_Tp1>&& __r)
1273 : __shared_ptr<_Tp>(std::move(__r)) { }
1274
1275 template<typename _Tp1>
1276 explicit
1277 shared_ptr(const weak_ptr<_Tp1>& __r)
1278 : __shared_ptr<_Tp>(__r) { }
1279
1280 #if _GLIBCXX_DEPRECATED
1281 template<typename _Tp1>
1282 explicit
1283 shared_ptr(std::auto_ptr<_Tp1>&& __r)
1284 : __shared_ptr<_Tp>(std::move(__r)) { }
1285 #endif
1286
1287 template<typename _Tp1, typename _Del>
1288 explicit
1289 shared_ptr(const std::unique_ptr<_Tp1, _Del>&) = delete;
1290
1291 template<typename _Tp1, typename _Del>
1292 explicit
1293 shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r)
1294 : __shared_ptr<_Tp>(std::move(__r)) { }
1295
1296 template<typename _Tp1>
1297 shared_ptr&
1298 operator=(const shared_ptr<_Tp1>& __r) // never throws
1299 {
1300 this->__shared_ptr<_Tp>::operator=(__r);
1301 return *this;
1302 }
1303
1304 #if _GLIBCXX_DEPRECATED
1305 template<typename _Tp1>
1306 shared_ptr&
1307 operator=(std::auto_ptr<_Tp1>&& __r)
1308 {
1309 this->__shared_ptr<_Tp>::operator=(std::move(__r));
1310 return *this;
1311 }
1312 #endif
1313
1314 shared_ptr&
1315 operator=(shared_ptr&& __r)
1316 {
1317 this->__shared_ptr<_Tp>::operator=(std::move(__r));
1318 return *this;
1319 }
1320
1321 template<class _Tp1>
1322 shared_ptr&
1323 operator=(shared_ptr<_Tp1>&& __r)
1324 {
1325 this->__shared_ptr<_Tp>::operator=(std::move(__r));
1326 return *this;
1327 }
1328
1329 template<typename _Tp1, typename _Del>
1330 shared_ptr&
1331 operator=(const std::unique_ptr<_Tp1, _Del>& __r) = delete;
1332
1333 template<typename _Tp1, typename _Del>
1334 shared_ptr&
1335 operator=(std::unique_ptr<_Tp1, _Del>&& __r)
1336 {
1337 this->__shared_ptr<_Tp>::operator=(std::move(__r));
1338 return *this;
1339 }
1340
1341 private:
1342 // This constructor is non-standard, it is used by allocate_shared.
1343 template<typename _Alloc, typename... _Args>
1344 shared_ptr(_Sp_make_shared_tag __tag, _Alloc __a, _Args&&... __args)
1345 : __shared_ptr<_Tp>(__tag, __a, std::forward<_Args>(__args)...)
1346 { }
1347
1348 template<typename _Tp1, typename _Alloc, typename... _Args>
1349 friend shared_ptr<_Tp1>
1350 allocate_shared(_Alloc __a, _Args&&... __args);
1351 };
1352
1353 // 20.8.13.2.7 shared_ptr comparisons
1354 template<typename _Tp1, typename _Tp2>
1355 inline bool
1356 operator==(const shared_ptr<_Tp1>& __a, const shared_ptr<_Tp2>& __b)
1357 { return __a.get() == __b.get(); }
1358
1359 template<typename _Tp1, typename _Tp2>
1360 inline bool
1361 operator!=(const shared_ptr<_Tp1>& __a, const shared_ptr<_Tp2>& __b)
1362 { return __a.get() != __b.get(); }
1363
1364 template<typename _Tp1, typename _Tp2>
1365 inline bool
1366 operator<(const shared_ptr<_Tp1>& __a, const shared_ptr<_Tp2>& __b)
1367 { return __a.get() < __b.get(); }
1368
1369 template<typename _Tp>
1370 struct less<shared_ptr<_Tp>>
1371 : public _Sp_less<shared_ptr<_Tp>>
1372 { };
1373
1374 // 20.8.13.2.9 shared_ptr specialized algorithms.
1375 template<typename _Tp>
1376 inline void
1377 swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>& __b)
1378 { __a.swap(__b); }
1379
1380 template<typename _Tp>
1381 inline void
1382 swap(shared_ptr<_Tp>&& __a, shared_ptr<_Tp>& __b)
1383 { __a.swap(__b); }
1384
1385 template<typename _Tp>
1386 inline void
1387 swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>&& __b)
1388 { __a.swap(__b); }
1389
1390 // 20.8.13.2.10 shared_ptr casts.
1391 template<typename _Tp, typename _Tp1>
1392 inline shared_ptr<_Tp>
1393 static_pointer_cast(const shared_ptr<_Tp1>& __r)
1394 { return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get())); }
1395
1396 template<typename _Tp, typename _Tp1>
1397 inline shared_ptr<_Tp>
1398 const_pointer_cast(const shared_ptr<_Tp1>& __r)
1399 { return shared_ptr<_Tp>(__r, const_cast<_Tp*>(__r.get())); }
1400
1401 template<typename _Tp, typename _Tp1>
1402 inline shared_ptr<_Tp>
1403 dynamic_pointer_cast(const shared_ptr<_Tp1>& __r)
1404 {
1405 if (_Tp* __p = dynamic_cast<_Tp*>(__r.get()))
1406 return shared_ptr<_Tp>(__r, __p);
1407 return shared_ptr<_Tp>();
1408 }
1409
1410
1411 /**
1412 * @brief A smart pointer with weak semantics.
1413 *
1414 * With forwarding constructors and assignment operators.
1415 */
1416 template<typename _Tp>
1417 class weak_ptr
1418 : public __weak_ptr<_Tp>
1419 {
1420 public:
1421 weak_ptr()
1422 : __weak_ptr<_Tp>() { }
1423
1424 template<typename _Tp1>
1425 weak_ptr(const weak_ptr<_Tp1>& __r)
1426 : __weak_ptr<_Tp>(__r) { }
1427
1428 template<typename _Tp1>
1429 weak_ptr(const shared_ptr<_Tp1>& __r)
1430 : __weak_ptr<_Tp>(__r) { }
1431
1432 template<typename _Tp1>
1433 weak_ptr&
1434 operator=(const weak_ptr<_Tp1>& __r) // never throws
1435 {
1436 this->__weak_ptr<_Tp>::operator=(__r);
1437 return *this;
1438 }
1439
1440 template<typename _Tp1>
1441 weak_ptr&
1442 operator=(const shared_ptr<_Tp1>& __r) // never throws
1443 {
1444 this->__weak_ptr<_Tp>::operator=(__r);
1445 return *this;
1446 }
1447
1448 shared_ptr<_Tp>
1449 lock() const // never throws
1450 {
1451 #ifdef __GTHREADS
1452 if (this->expired())
1453 return shared_ptr<_Tp>();
1454
1455 __try
1456 {
1457 return shared_ptr<_Tp>(*this);
1458 }
1459 __catch(const bad_weak_ptr&)
1460 {
1461 return shared_ptr<_Tp>();
1462 }
1463 #else
1464 return this->expired() ? shared_ptr<_Tp>()
1465 : shared_ptr<_Tp>(*this);
1466 #endif
1467 }
1468
1469 // comparisons
1470 template<typename _Tp1>
1471 bool operator<(const weak_ptr<_Tp1>&) const = delete;
1472 template<typename _Tp1>
1473 bool operator<=(const weak_ptr<_Tp1>&) const = delete;
1474 template<typename _Tp1>
1475 bool operator>(const weak_ptr<_Tp1>&) const = delete;
1476 template<typename _Tp1>
1477 bool operator>=(const weak_ptr<_Tp1>&) const = delete;
1478 };
1479
1480 // 20.8.13.3.7 weak_ptr specialized algorithms.
1481 template<typename _Tp>
1482 inline void
1483 swap(weak_ptr<_Tp>& __a, weak_ptr<_Tp>& __b)
1484 { __a.swap(__b); }
1485
1486 /// owner_less
1487 template<typename _Tp>
1488 struct owner_less<shared_ptr<_Tp>>
1489 : public _Sp_owner_less<shared_ptr<_Tp>, weak_ptr<_Tp>>
1490 { };
1491
1492 template<typename _Tp>
1493 struct owner_less<weak_ptr<_Tp>>
1494 : public _Sp_owner_less<weak_ptr<_Tp>, shared_ptr<_Tp>>
1495 { };
1496
1497 /**
1498 * @brief Base class allowing use of member function shared_from_this.
1499 */
1500 template<typename _Tp>
1501 class enable_shared_from_this
1502 {
1503 protected:
1504 enable_shared_from_this() { }
1505
1506 enable_shared_from_this(const enable_shared_from_this&) { }
1507
1508 enable_shared_from_this&
1509 operator=(const enable_shared_from_this&)
1510 { return *this; }
1511
1512 ~enable_shared_from_this() { }
1513
1514 public:
1515 shared_ptr<_Tp>
1516 shared_from_this()
1517 { return shared_ptr<_Tp>(this->_M_weak_this); }
1518
1519 shared_ptr<const _Tp>
1520 shared_from_this() const
1521 { return shared_ptr<const _Tp>(this->_M_weak_this); }
1522
1523 private:
1524 template<typename _Tp1>
1525 void
1526 _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const
1527 { _M_weak_this._M_assign(__p, __n); }
1528
1529 template<typename _Tp1>
1530 friend void
1531 __enable_shared_from_this_helper(const __shared_count<>& __pn,
1532 const enable_shared_from_this* __pe,
1533 const _Tp1* __px)
1534 {
1535 if (__pe != 0)
1536 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
1537 }
1538
1539 mutable weak_ptr<_Tp> _M_weak_this;
1540 };
1541
1542 template<typename _Tp, _Lock_policy _Lp, typename _Alloc, typename... _Args>
1543 inline __shared_ptr<_Tp, _Lp>
1544 __allocate_shared(_Alloc __a, _Args&&... __args)
1545 {
1546 return __shared_ptr<_Tp, _Lp>(_Sp_make_shared_tag(),
1547 std::forward<_Alloc>(__a), std::forward<_Args>(__args)...);
1548 }
1549
1550 template<typename _Tp, _Lock_policy _Lp, typename... _Args>
1551 inline __shared_ptr<_Tp, _Lp>
1552 __make_shared(_Args&&... __args)
1553 {
1554 typedef typename std::remove_const<_Tp>::type _Tp_nc;
1555 return __allocate_shared<_Tp, _Lp>(std::allocator<_Tp_nc>(),
1556 std::forward<_Args>(__args)...);
1557 }
1558
1559 /** @brief Create an object that is owned by a shared_ptr.
1560 * @param __a An allocator.
1561 * @param __args Arguments for the @a _Tp object's constructor.
1562 * @return A shared_ptr that owns the newly created object.
1563 * @throw An exception thrown from @a _Alloc::allocate or from the
1564 * constructor of @a _Tp.
1565 *
1566 * A copy of @a __a will be used to allocate memory for the shared_ptr
1567 * and the new object.
1568 */
1569 template<typename _Tp, typename _Alloc, typename... _Args>
1570 inline shared_ptr<_Tp>
1571 allocate_shared(_Alloc __a, _Args&&... __args)
1572 {
1573 return shared_ptr<_Tp>(_Sp_make_shared_tag(), std::forward<_Alloc>(__a),
1574 std::forward<_Args>(__args)...);
1575 }
1576
1577 /** @brief Create an object that is owned by a shared_ptr.
1578 * @param __args Arguments for the @a _Tp object's constructor.
1579 * @return A shared_ptr that owns the newly created object.
1580 * @throw std::bad_alloc, or an exception thrown from the
1581 * constructor of @a _Tp.
1582 */
1583 template<typename _Tp, typename... _Args>
1584 inline shared_ptr<_Tp>
1585 make_shared(_Args&&... __args)
1586 {
1587 typedef typename std::remove_const<_Tp>::type _Tp_nc;
1588 return allocate_shared<_Tp>(std::allocator<_Tp_nc>(),
1589 std::forward<_Args>(__args)...);
1590 }
1591
1592 // @} group pointer_abstractions
1593
1594 _GLIBCXX_END_NAMESPACE
1595
1596 #endif // _SHARED_PTR_H