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