shared_ptr.h (operator>, [...]): Add, per DR 1401.
[gcc.git] / libstdc++-v3 / include / bits / shared_ptr.h
1 // shared_ptr and weak_ptr implementation -*- C++ -*-
2
3 // Copyright (C) 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 // GCC Note: Based on files from version 1.32.0 of the Boost library.
26
27 // shared_count.hpp
28 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
29
30 // shared_ptr.hpp
31 // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
32 // Copyright (C) 2001, 2002, 2003 Peter Dimov
33
34 // weak_ptr.hpp
35 // Copyright (C) 2001, 2002, 2003 Peter Dimov
36
37 // enable_shared_from_this.hpp
38 // Copyright (C) 2002 Peter Dimov
39
40 // Distributed under the Boost Software License, Version 1.0. (See
41 // accompanying file LICENSE_1_0.txt or copy at
42 // http://www.boost.org/LICENSE_1_0.txt)
43
44 /** @file bits/shared_ptr.h
45 * This is an internal header file, included by other library headers.
46 * Do not attempt to use it directly. @headername{memory}
47 */
48
49 #ifndef _SHARED_PTR_H
50 #define _SHARED_PTR_H 1
51
52 #include <bits/shared_ptr_base.h>
53
54 namespace std _GLIBCXX_VISIBILITY(default)
55 {
56 _GLIBCXX_BEGIN_NAMESPACE_VERSION
57
58 /**
59 * @addtogroup pointer_abstractions
60 * @{
61 */
62
63 /// 2.2.3.7 shared_ptr I/O
64 template<typename _Ch, typename _Tr, typename _Tp, _Lock_policy _Lp>
65 inline std::basic_ostream<_Ch, _Tr>&
66 operator<<(std::basic_ostream<_Ch, _Tr>& __os,
67 const __shared_ptr<_Tp, _Lp>& __p)
68 {
69 __os << __p.get();
70 return __os;
71 }
72
73 /// 2.2.3.10 shared_ptr get_deleter (experimental)
74 template<typename _Del, typename _Tp, _Lock_policy _Lp>
75 inline _Del*
76 get_deleter(const __shared_ptr<_Tp, _Lp>& __p)
77 {
78 #ifdef __GXX_RTTI
79 return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del)));
80 #else
81 return 0;
82 #endif
83 }
84
85
86 /**
87 * @brief A smart pointer with reference-counted copy semantics.
88 *
89 * The object pointed to is deleted when the last shared_ptr pointing to
90 * it is destroyed or reset.
91 */
92 template<typename _Tp>
93 class shared_ptr : public __shared_ptr<_Tp>
94 {
95 public:
96 /**
97 * @brief Construct an empty %shared_ptr.
98 * @post use_count()==0 && get()==0
99 */
100 constexpr shared_ptr()
101 : __shared_ptr<_Tp>() { }
102
103 /**
104 * @brief Construct a %shared_ptr that owns the pointer @a __p.
105 * @param __p A pointer that is convertible to element_type*.
106 * @post use_count() == 1 && get() == __p
107 * @throw std::bad_alloc, in which case @c delete @a __p is called.
108 */
109 template<typename _Tp1>
110 explicit shared_ptr(_Tp1* __p)
111 : __shared_ptr<_Tp>(__p) { }
112
113 /**
114 * @brief Construct a %shared_ptr that owns the pointer @a __p
115 * and the deleter @a __d.
116 * @param __p A pointer.
117 * @param __d A deleter.
118 * @post use_count() == 1 && get() == __p
119 * @throw std::bad_alloc, in which case @a __d(__p) is called.
120 *
121 * Requirements: _Deleter's copy constructor and destructor must
122 * not throw
123 *
124 * __shared_ptr will release __p by calling __d(__p)
125 */
126 template<typename _Tp1, typename _Deleter>
127 shared_ptr(_Tp1* __p, _Deleter __d)
128 : __shared_ptr<_Tp>(__p, __d) { }
129
130 /**
131 * @brief Construct a %shared_ptr that owns a null pointer
132 * and the deleter @a __d.
133 * @param __p A null pointer constant.
134 * @param __d A deleter.
135 * @post use_count() == 1 && get() == __p
136 * @throw std::bad_alloc, in which case @a __d(__p) is called.
137 *
138 * Requirements: _Deleter's copy constructor and destructor must
139 * not throw
140 *
141 * The last owner will call __d(__p)
142 */
143 template<typename _Deleter>
144 shared_ptr(nullptr_t __p, _Deleter __d)
145 : __shared_ptr<_Tp>(__p, __d) { }
146
147 /**
148 * @brief Construct a %shared_ptr that owns the pointer @a __p
149 * and the deleter @a __d.
150 * @param __p A pointer.
151 * @param __d A deleter.
152 * @param __a An allocator.
153 * @post use_count() == 1 && get() == __p
154 * @throw std::bad_alloc, in which case @a __d(__p) is called.
155 *
156 * Requirements: _Deleter's copy constructor and destructor must
157 * not throw _Alloc's copy constructor and destructor must not
158 * throw.
159 *
160 * __shared_ptr will release __p by calling __d(__p)
161 */
162 template<typename _Tp1, typename _Deleter, typename _Alloc>
163 shared_ptr(_Tp1* __p, _Deleter __d, _Alloc __a)
164 : __shared_ptr<_Tp>(__p, __d, std::move(__a)) { }
165
166 /**
167 * @brief Construct a %shared_ptr that owns a null pointer
168 * and the deleter @a __d.
169 * @param __p A null pointer constant.
170 * @param __d A deleter.
171 * @param __a An allocator.
172 * @post use_count() == 1 && get() == __p
173 * @throw std::bad_alloc, in which case @a __d(__p) is called.
174 *
175 * Requirements: _Deleter's copy constructor and destructor must
176 * not throw _Alloc's copy constructor and destructor must not
177 * throw.
178 *
179 * The last owner will call __d(__p)
180 */
181 template<typename _Deleter, typename _Alloc>
182 shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
183 : __shared_ptr<_Tp>(__p, __d, std::move(__a)) { }
184
185 // Aliasing constructor
186
187 /**
188 * @brief Constructs a %shared_ptr instance that stores @a __p
189 * and shares ownership with @a __r.
190 * @param __r A %shared_ptr.
191 * @param __p A pointer that will remain valid while @a *__r is valid.
192 * @post get() == __p && use_count() == __r.use_count()
193 *
194 * This can be used to construct a @c shared_ptr to a sub-object
195 * of an object managed by an existing @c shared_ptr.
196 *
197 * @code
198 * shared_ptr< pair<int,int> > pii(new pair<int,int>());
199 * shared_ptr<int> pi(pii, &pii->first);
200 * assert(pii.use_count() == 2);
201 * @endcode
202 */
203 template<typename _Tp1>
204 shared_ptr(const shared_ptr<_Tp1>& __r, _Tp* __p)
205 : __shared_ptr<_Tp>(__r, __p) { }
206
207 /**
208 * @brief If @a __r is empty, constructs an empty %shared_ptr;
209 * otherwise construct a %shared_ptr that shares ownership
210 * with @a __r.
211 * @param __r A %shared_ptr.
212 * @post get() == __r.get() && use_count() == __r.use_count()
213 */
214 template<typename _Tp1, typename = typename
215 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
216 shared_ptr(const shared_ptr<_Tp1>& __r)
217 : __shared_ptr<_Tp>(__r) { }
218
219 /**
220 * @brief Move-constructs a %shared_ptr instance from @a __r.
221 * @param __r A %shared_ptr rvalue.
222 * @post *this contains the old value of @a __r, @a __r is empty.
223 */
224 shared_ptr(shared_ptr&& __r)
225 : __shared_ptr<_Tp>(std::move(__r)) { }
226
227 /**
228 * @brief Move-constructs a %shared_ptr instance from @a __r.
229 * @param __r A %shared_ptr rvalue.
230 * @post *this contains the old value of @a __r, @a __r is empty.
231 */
232 template<typename _Tp1, typename = typename
233 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
234 shared_ptr(shared_ptr<_Tp1>&& __r)
235 : __shared_ptr<_Tp>(std::move(__r)) { }
236
237 /**
238 * @brief Constructs a %shared_ptr that shares ownership with @a __r
239 * and stores a copy of the pointer stored in @a __r.
240 * @param __r A weak_ptr.
241 * @post use_count() == __r.use_count()
242 * @throw bad_weak_ptr when __r.expired(),
243 * in which case the constructor has no effect.
244 */
245 template<typename _Tp1>
246 explicit shared_ptr(const weak_ptr<_Tp1>& __r)
247 : __shared_ptr<_Tp>(__r) { }
248
249 #if _GLIBCXX_USE_DEPRECATED
250 template<typename _Tp1>
251 shared_ptr(std::auto_ptr<_Tp1>&& __r)
252 : __shared_ptr<_Tp>(std::move(__r)) { }
253 #endif
254
255 template<typename _Tp1, typename _Del>
256 shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r)
257 : __shared_ptr<_Tp>(std::move(__r)) { }
258
259 /**
260 * @brief Construct an empty %shared_ptr.
261 * @param __p A null pointer constant.
262 * @post use_count() == 0 && get() == nullptr
263 */
264 constexpr shared_ptr(nullptr_t __p)
265 : __shared_ptr<_Tp>(__p) { }
266
267 template<typename _Tp1>
268 shared_ptr&
269 operator=(const shared_ptr<_Tp1>& __r) // never throws
270 {
271 this->__shared_ptr<_Tp>::operator=(__r);
272 return *this;
273 }
274
275 #if _GLIBCXX_USE_DEPRECATED
276 template<typename _Tp1>
277 shared_ptr&
278 operator=(std::auto_ptr<_Tp1>&& __r)
279 {
280 this->__shared_ptr<_Tp>::operator=(std::move(__r));
281 return *this;
282 }
283 #endif
284
285 shared_ptr&
286 operator=(shared_ptr&& __r)
287 {
288 this->__shared_ptr<_Tp>::operator=(std::move(__r));
289 return *this;
290 }
291
292 template<class _Tp1>
293 shared_ptr&
294 operator=(shared_ptr<_Tp1>&& __r)
295 {
296 this->__shared_ptr<_Tp>::operator=(std::move(__r));
297 return *this;
298 }
299
300 template<typename _Tp1, typename _Del>
301 shared_ptr&
302 operator=(std::unique_ptr<_Tp1, _Del>&& __r)
303 {
304 this->__shared_ptr<_Tp>::operator=(std::move(__r));
305 return *this;
306 }
307
308 private:
309 // This constructor is non-standard, it is used by allocate_shared.
310 template<typename _Alloc, typename... _Args>
311 shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a,
312 _Args&&... __args)
313 : __shared_ptr<_Tp>(__tag, __a, std::forward<_Args>(__args)...)
314 { }
315
316 template<typename _Tp1, typename _Alloc, typename... _Args>
317 friend shared_ptr<_Tp1>
318 allocate_shared(const _Alloc& __a, _Args&&... __args);
319 };
320
321 // 20.8.13.2.7 shared_ptr comparisons
322 template<typename _Tp1, typename _Tp2>
323 inline bool
324 operator==(const shared_ptr<_Tp1>& __a,
325 const shared_ptr<_Tp2>& __b) noexcept
326 { return __a.get() == __b.get(); }
327
328 template<typename _Tp>
329 inline bool
330 operator==(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
331 { return !__a; }
332
333 template<typename _Tp>
334 inline bool
335 operator==(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
336 { return !__a; }
337
338 template<typename _Tp1, typename _Tp2>
339 inline bool
340 operator!=(const shared_ptr<_Tp1>& __a,
341 const shared_ptr<_Tp2>& __b) noexcept
342 { return __a.get() != __b.get(); }
343
344 template<typename _Tp>
345 inline bool
346 operator!=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
347 { return (bool)__a; }
348
349 template<typename _Tp>
350 inline bool
351 operator!=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
352 { return (bool)__a; }
353
354 template<typename _Tp1, typename _Tp2>
355 inline bool
356 operator<(const shared_ptr<_Tp1>& __a,
357 const shared_ptr<_Tp2>& __b) noexcept
358 {
359 typedef typename std::common_type<_Tp1*, _Tp2*>::type _CT;
360 return std::less<_CT>()(__a.get(), __b.get());
361 }
362
363 template<typename _Tp>
364 inline bool
365 operator<(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
366 { return std::less<_Tp*>()(__a.get(), nullptr); }
367
368 template<typename _Tp>
369 inline bool
370 operator<(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
371 { return std::less<_Tp*>()(nullptr, __a.get()); }
372
373 template<typename _Tp1, typename _Tp2>
374 inline bool
375 operator<=(const shared_ptr<_Tp1>& __a,
376 const shared_ptr<_Tp2>& __b) noexcept
377 { return !(__b < __a); }
378
379 template<typename _Tp>
380 inline bool
381 operator<=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
382 { return !(nullptr < __a); }
383
384 template<typename _Tp>
385 inline bool
386 operator<=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
387 { return !(__a < nullptr); }
388
389 template<typename _Tp1, typename _Tp2>
390 inline bool
391 operator>(const shared_ptr<_Tp1>& __a,
392 const shared_ptr<_Tp2>& __b) noexcept
393 { return (__b < __a); }
394
395 template<typename _Tp>
396 inline bool
397 operator>(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
398 { return std::less<_Tp*>()(nullptr, __a.get()); }
399
400 template<typename _Tp>
401 inline bool
402 operator>(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
403 { return std::less<_Tp*>()(__a.get(), nullptr); }
404
405 template<typename _Tp1, typename _Tp2>
406 inline bool
407 operator>=(const shared_ptr<_Tp1>& __a,
408 const shared_ptr<_Tp2>& __b) noexcept
409 { return !(__a < __b); }
410
411 template<typename _Tp>
412 inline bool
413 operator>=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
414 { return !(__a < nullptr); }
415
416 template<typename _Tp>
417 inline bool
418 operator>=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
419 { return !(nullptr < __a); }
420
421 template<typename _Tp>
422 struct less<shared_ptr<_Tp>> : public _Sp_less<shared_ptr<_Tp>>
423 { };
424
425 // 20.8.13.2.9 shared_ptr specialized algorithms.
426 template<typename _Tp>
427 inline void
428 swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>& __b)
429 { __a.swap(__b); }
430
431 // 20.8.13.2.10 shared_ptr casts.
432 template<typename _Tp, typename _Tp1>
433 inline shared_ptr<_Tp>
434 static_pointer_cast(const shared_ptr<_Tp1>& __r)
435 { return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get())); }
436
437 template<typename _Tp, typename _Tp1>
438 inline shared_ptr<_Tp>
439 const_pointer_cast(const shared_ptr<_Tp1>& __r)
440 { return shared_ptr<_Tp>(__r, const_cast<_Tp*>(__r.get())); }
441
442 template<typename _Tp, typename _Tp1>
443 inline shared_ptr<_Tp>
444 dynamic_pointer_cast(const shared_ptr<_Tp1>& __r)
445 {
446 if (_Tp* __p = dynamic_cast<_Tp*>(__r.get()))
447 return shared_ptr<_Tp>(__r, __p);
448 return shared_ptr<_Tp>();
449 }
450
451
452 /**
453 * @brief A smart pointer with weak semantics.
454 *
455 * With forwarding constructors and assignment operators.
456 */
457 template<typename _Tp>
458 class weak_ptr : public __weak_ptr<_Tp>
459 {
460 public:
461 constexpr weak_ptr()
462 : __weak_ptr<_Tp>() { }
463
464 template<typename _Tp1, typename = typename
465 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
466 weak_ptr(const weak_ptr<_Tp1>& __r)
467 : __weak_ptr<_Tp>(__r) { }
468
469 template<typename _Tp1, typename = typename
470 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
471 weak_ptr(const shared_ptr<_Tp1>& __r)
472 : __weak_ptr<_Tp>(__r) { }
473
474 template<typename _Tp1>
475 weak_ptr&
476 operator=(const weak_ptr<_Tp1>& __r) // never throws
477 {
478 this->__weak_ptr<_Tp>::operator=(__r);
479 return *this;
480 }
481
482 template<typename _Tp1>
483 weak_ptr&
484 operator=(const shared_ptr<_Tp1>& __r) // never throws
485 {
486 this->__weak_ptr<_Tp>::operator=(__r);
487 return *this;
488 }
489
490 shared_ptr<_Tp>
491 lock() const // never throws
492 {
493 #ifdef __GTHREADS
494 if (this->expired())
495 return shared_ptr<_Tp>();
496
497 __try
498 {
499 return shared_ptr<_Tp>(*this);
500 }
501 __catch(const bad_weak_ptr&)
502 {
503 return shared_ptr<_Tp>();
504 }
505 #else
506 return this->expired() ? shared_ptr<_Tp>() : shared_ptr<_Tp>(*this);
507 #endif
508 }
509 };
510
511 // 20.8.13.3.7 weak_ptr specialized algorithms.
512 template<typename _Tp>
513 inline void
514 swap(weak_ptr<_Tp>& __a, weak_ptr<_Tp>& __b)
515 { __a.swap(__b); }
516
517
518 /// Primary template owner_less
519 template<typename _Tp>
520 struct owner_less;
521
522 /// Partial specialization of owner_less for shared_ptr.
523 template<typename _Tp>
524 struct owner_less<shared_ptr<_Tp>>
525 : public _Sp_owner_less<shared_ptr<_Tp>, weak_ptr<_Tp>>
526 { };
527
528 /// Partial specialization of owner_less for weak_ptr.
529 template<typename _Tp>
530 struct owner_less<weak_ptr<_Tp>>
531 : public _Sp_owner_less<weak_ptr<_Tp>, shared_ptr<_Tp>>
532 { };
533
534 /**
535 * @brief Base class allowing use of member function shared_from_this.
536 */
537 template<typename _Tp>
538 class enable_shared_from_this
539 {
540 protected:
541 constexpr enable_shared_from_this() { }
542
543 enable_shared_from_this(const enable_shared_from_this&) { }
544
545 enable_shared_from_this&
546 operator=(const enable_shared_from_this&)
547 { return *this; }
548
549 ~enable_shared_from_this() { }
550
551 public:
552 shared_ptr<_Tp>
553 shared_from_this()
554 { return shared_ptr<_Tp>(this->_M_weak_this); }
555
556 shared_ptr<const _Tp>
557 shared_from_this() const
558 { return shared_ptr<const _Tp>(this->_M_weak_this); }
559
560 private:
561 template<typename _Tp1>
562 void
563 _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const
564 { _M_weak_this._M_assign(__p, __n); }
565
566 template<typename _Tp1>
567 friend void
568 __enable_shared_from_this_helper(const __shared_count<>& __pn,
569 const enable_shared_from_this* __pe,
570 const _Tp1* __px)
571 {
572 if (__pe != 0)
573 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
574 }
575
576 mutable weak_ptr<_Tp> _M_weak_this;
577 };
578
579 /**
580 * @brief Create an object that is owned by a shared_ptr.
581 * @param __a An allocator.
582 * @param __args Arguments for the @a _Tp object's constructor.
583 * @return A shared_ptr that owns the newly created object.
584 * @throw An exception thrown from @a _Alloc::allocate or from the
585 * constructor of @a _Tp.
586 *
587 * A copy of @a __a will be used to allocate memory for the shared_ptr
588 * and the new object.
589 */
590 template<typename _Tp, typename _Alloc, typename... _Args>
591 inline shared_ptr<_Tp>
592 allocate_shared(const _Alloc& __a, _Args&&... __args)
593 {
594 return shared_ptr<_Tp>(_Sp_make_shared_tag(), __a,
595 std::forward<_Args>(__args)...);
596 }
597
598 /**
599 * @brief Create an object that is owned by a shared_ptr.
600 * @param __args Arguments for the @a _Tp object's constructor.
601 * @return A shared_ptr that owns the newly created object.
602 * @throw std::bad_alloc, or an exception thrown from the
603 * constructor of @a _Tp.
604 */
605 template<typename _Tp, typename... _Args>
606 inline shared_ptr<_Tp>
607 make_shared(_Args&&... __args)
608 {
609 typedef typename std::remove_const<_Tp>::type _Tp_nc;
610 return allocate_shared<_Tp>(std::allocator<_Tp_nc>(),
611 std::forward<_Args>(__args)...);
612 }
613
614 /// std::hash specialization for shared_ptr.
615 template<typename _Tp>
616 struct hash<shared_ptr<_Tp>>
617 : public std::unary_function<shared_ptr<_Tp>, size_t>
618 {
619 size_t
620 operator()(const shared_ptr<_Tp>& __s) const
621 { return std::hash<_Tp*>()(__s.get()); }
622 };
623
624 // @} group pointer_abstractions
625
626 _GLIBCXX_END_NAMESPACE_VERSION
627 } // namespace
628
629 #endif // _SHARED_PTR_H