Merge from mainline (165734:167278).
[official-gcc/graphite-test-results.git] / libstdc++-v3 / include / bits / shared_ptr.h
blob0e6f7a6d0b34ea21ceb07dc91ddc8eeecb71c00f
1 // shared_ptr and weak_ptr implementation -*- C++ -*-
3 // Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
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.
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.
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/>.
25 // GCC Note: Based on files from version 1.32.0 of the Boost library.
27 // shared_count.hpp
28 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
30 // shared_ptr.hpp
31 // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
32 // Copyright (C) 2001, 2002, 2003 Peter Dimov
34 // weak_ptr.hpp
35 // Copyright (C) 2001, 2002, 2003 Peter Dimov
37 // enable_shared_from_this.hpp
38 // Copyright (C) 2002 Peter Dimov
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)
44 /** @file bits/shared_ptr.h
45 * This is an internal header file, included by other library headers.
46 * You should not attempt to use it directly.
49 #ifndef _SHARED_PTR_H
50 #define _SHARED_PTR_H 1
52 #include <bits/shared_ptr_base.h>
54 _GLIBCXX_BEGIN_NAMESPACE(std)
56 /**
57 * @addtogroup pointer_abstractions
58 * @{
61 /// 2.2.3.7 shared_ptr I/O
62 template<typename _Ch, typename _Tr, typename _Tp, _Lock_policy _Lp>
63 inline std::basic_ostream<_Ch, _Tr>&
64 operator<<(std::basic_ostream<_Ch, _Tr>& __os,
65 const __shared_ptr<_Tp, _Lp>& __p)
67 __os << __p.get();
68 return __os;
71 /// 2.2.3.10 shared_ptr get_deleter (experimental)
72 template<typename _Del, typename _Tp, _Lock_policy _Lp>
73 inline _Del*
74 get_deleter(const __shared_ptr<_Tp, _Lp>& __p)
76 #ifdef __GXX_RTTI
77 return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del)));
78 #else
79 return 0;
80 #endif
84 /**
85 * @brief A smart pointer with reference-counted copy semantics.
87 * The object pointed to is deleted when the last shared_ptr pointing to
88 * it is destroyed or reset.
90 template<typename _Tp>
91 class shared_ptr : public __shared_ptr<_Tp>
93 public:
94 /**
95 * @brief Construct an empty %shared_ptr.
96 * @post use_count()==0 && get()==0
98 constexpr shared_ptr()
99 : __shared_ptr<_Tp>() { }
102 * @brief Construct a %shared_ptr that owns the pointer @a __p.
103 * @param __p A pointer that is convertible to element_type*.
104 * @post use_count() == 1 && get() == __p
105 * @throw std::bad_alloc, in which case @c delete @a __p is called.
107 template<typename _Tp1>
108 explicit shared_ptr(_Tp1* __p)
109 : __shared_ptr<_Tp>(__p) { }
112 * @brief Construct a %shared_ptr that owns the pointer @a __p
113 * and the deleter @a __d.
114 * @param __p A pointer.
115 * @param __d A deleter.
116 * @post use_count() == 1 && get() == __p
117 * @throw std::bad_alloc, in which case @a __d(__p) is called.
119 * Requirements: _Deleter's copy constructor and destructor must
120 * not throw
122 * __shared_ptr will release __p by calling __d(__p)
124 template<typename _Tp1, typename _Deleter>
125 shared_ptr(_Tp1* __p, _Deleter __d) : __shared_ptr<_Tp>(__p, __d) { }
128 * @brief Construct a %shared_ptr that owns a null pointer
129 * and the deleter @a __d.
130 * @param __p A null pointer constant.
131 * @param __d A deleter.
132 * @post use_count() == 1 && get() == __p
133 * @throw std::bad_alloc, in which case @a __d(__p) is called.
135 * Requirements: _Deleter's copy constructor and destructor must
136 * not throw
138 * The last owner will call __d(__p)
140 template<typename _Deleter>
141 shared_ptr(nullptr_t __p, _Deleter __d)
142 : __shared_ptr<_Tp>(__p, __d) { }
145 * @brief Construct a %shared_ptr that owns the pointer @a __p
146 * and the deleter @a __d.
147 * @param __p A pointer.
148 * @param __d A deleter.
149 * @param __a An allocator.
150 * @post use_count() == 1 && get() == __p
151 * @throw std::bad_alloc, in which case @a __d(__p) is called.
153 * Requirements: _Deleter's copy constructor and destructor must
154 * not throw _Alloc's copy constructor and destructor must not
155 * throw.
157 * __shared_ptr will release __p by calling __d(__p)
159 template<typename _Tp1, typename _Deleter, typename _Alloc>
160 shared_ptr(_Tp1* __p, _Deleter __d, const _Alloc& __a)
161 : __shared_ptr<_Tp>(__p, __d, __a) { }
164 * @brief Construct a %shared_ptr that owns a null pointer
165 * and the deleter @a __d.
166 * @param __p A null pointer constant.
167 * @param __d A deleter.
168 * @param __a An allocator.
169 * @post use_count() == 1 && get() == __p
170 * @throw std::bad_alloc, in which case @a __d(__p) is called.
172 * Requirements: _Deleter's copy constructor and destructor must
173 * not throw _Alloc's copy constructor and destructor must not
174 * throw.
176 * The last owner will call __d(__p)
178 template<typename _Deleter, typename _Alloc>
179 shared_ptr(nullptr_t __p, _Deleter __d, const _Alloc& __a)
180 : __shared_ptr<_Tp>(__p, __d, __a) { }
182 // Aliasing constructor
185 * @brief Constructs a %shared_ptr instance that stores @a __p
186 * and shares ownership with @a __r.
187 * @param __r A %shared_ptr.
188 * @param __p A pointer that will remain valid while @a *__r is valid.
189 * @post get() == __p && use_count() == __r.use_count()
191 * This can be used to construct a @c shared_ptr to a sub-object
192 * of an object managed by an existing @c shared_ptr.
194 * @code
195 * shared_ptr< pair<int,int> > pii(new pair<int,int>());
196 * shared_ptr<int> pi(pii, &pii->first);
197 * assert(pii.use_count() == 2);
198 * @endcode
200 template<typename _Tp1>
201 shared_ptr(const shared_ptr<_Tp1>& __r, _Tp* __p)
202 : __shared_ptr<_Tp>(__r, __p) { }
205 * @brief If @a __r is empty, constructs an empty %shared_ptr;
206 * otherwise construct a %shared_ptr that shares ownership
207 * with @a __r.
208 * @param __r A %shared_ptr.
209 * @post get() == __r.get() && use_count() == __r.use_count()
211 template<typename _Tp1, typename = typename
212 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
213 shared_ptr(const shared_ptr<_Tp1>& __r)
214 : __shared_ptr<_Tp>(__r) { }
217 * @brief Move-constructs a %shared_ptr instance from @a __r.
218 * @param __r A %shared_ptr rvalue.
219 * @post *this contains the old value of @a __r, @a __r is empty.
221 shared_ptr(shared_ptr&& __r)
222 : __shared_ptr<_Tp>(std::move(__r)) { }
225 * @brief Move-constructs a %shared_ptr instance from @a __r.
226 * @param __r A %shared_ptr rvalue.
227 * @post *this contains the old value of @a __r, @a __r is empty.
229 template<typename _Tp1, typename = typename
230 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
231 shared_ptr(shared_ptr<_Tp1>&& __r)
232 : __shared_ptr<_Tp>(std::move(__r)) { }
235 * @brief Constructs a %shared_ptr that shares ownership with @a __r
236 * and stores a copy of the pointer stored in @a __r.
237 * @param __r A weak_ptr.
238 * @post use_count() == __r.use_count()
239 * @throw bad_weak_ptr when __r.expired(),
240 * in which case the constructor has no effect.
242 template<typename _Tp1>
243 explicit shared_ptr(const weak_ptr<_Tp1>& __r)
244 : __shared_ptr<_Tp>(__r) { }
246 #if _GLIBCXX_DEPRECATED
247 template<typename _Tp1>
248 shared_ptr(std::auto_ptr<_Tp1>&& __r)
249 : __shared_ptr<_Tp>(std::move(__r)) { }
250 #endif
252 template<typename _Tp1, typename _Del>
253 shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r)
254 : __shared_ptr<_Tp>(std::move(__r)) { }
257 * @brief Construct an empty %shared_ptr.
258 * @param __p A null pointer constant.
259 * @post use_count() == 0 && get() == nullptr
261 constexpr shared_ptr(nullptr_t __p)
262 : __shared_ptr<_Tp>(__p) { }
264 template<typename _Tp1>
265 shared_ptr&
266 operator=(const shared_ptr<_Tp1>& __r) // never throws
268 this->__shared_ptr<_Tp>::operator=(__r);
269 return *this;
272 #if _GLIBCXX_DEPRECATED
273 template<typename _Tp1>
274 shared_ptr&
275 operator=(std::auto_ptr<_Tp1>&& __r)
277 this->__shared_ptr<_Tp>::operator=(std::move(__r));
278 return *this;
280 #endif
282 shared_ptr&
283 operator=(shared_ptr&& __r)
285 this->__shared_ptr<_Tp>::operator=(std::move(__r));
286 return *this;
289 template<class _Tp1>
290 shared_ptr&
291 operator=(shared_ptr<_Tp1>&& __r)
293 this->__shared_ptr<_Tp>::operator=(std::move(__r));
294 return *this;
297 template<typename _Tp1, typename _Del>
298 shared_ptr&
299 operator=(std::unique_ptr<_Tp1, _Del>&& __r)
301 this->__shared_ptr<_Tp>::operator=(std::move(__r));
302 return *this;
305 private:
306 // This constructor is non-standard, it is used by allocate_shared.
307 template<typename _Alloc, typename... _Args>
308 shared_ptr(_Sp_make_shared_tag __tag, _Alloc __a, _Args&&... __args)
309 : __shared_ptr<_Tp>(__tag, __a, std::forward<_Args>(__args)...)
312 template<typename _Tp1, typename _Alloc, typename... _Args>
313 friend shared_ptr<_Tp1>
314 allocate_shared(_Alloc __a, _Args&&... __args);
317 // 20.8.13.2.7 shared_ptr comparisons
318 template<typename _Tp1, typename _Tp2>
319 inline bool
320 operator==(const shared_ptr<_Tp1>& __a, const shared_ptr<_Tp2>& __b)
321 { return __a.get() == __b.get(); }
323 template<typename _Tp>
324 inline bool
325 operator==(const shared_ptr<_Tp>& __a, nullptr_t)
326 { return __a.get() == nullptr; }
328 template<typename _Tp>
329 inline bool
330 operator==(nullptr_t, const shared_ptr<_Tp>& __b)
331 { return nullptr == __b.get(); }
333 template<typename _Tp1, typename _Tp2>
334 inline bool
335 operator!=(const shared_ptr<_Tp1>& __a, const shared_ptr<_Tp2>& __b)
336 { return __a.get() != __b.get(); }
338 template<typename _Tp>
339 inline bool
340 operator!=(const shared_ptr<_Tp>& __a, nullptr_t)
341 { return __a.get() != nullptr; }
343 template<typename _Tp>
344 inline bool
345 operator!=(nullptr_t, const shared_ptr<_Tp>& __b)
346 { return nullptr != __b.get(); }
348 template<typename _Tp1, typename _Tp2>
349 inline bool
350 operator<(const shared_ptr<_Tp1>& __a, const shared_ptr<_Tp2>& __b)
351 { return __a.get() < __b.get(); }
353 template<typename _Tp>
354 struct less<shared_ptr<_Tp>> : public _Sp_less<shared_ptr<_Tp>>
355 { };
357 // 20.8.13.2.9 shared_ptr specialized algorithms.
358 template<typename _Tp>
359 inline void
360 swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>& __b)
361 { __a.swap(__b); }
363 // 20.8.13.2.10 shared_ptr casts.
364 template<typename _Tp, typename _Tp1>
365 inline shared_ptr<_Tp>
366 static_pointer_cast(const shared_ptr<_Tp1>& __r)
367 { return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get())); }
369 template<typename _Tp, typename _Tp1>
370 inline shared_ptr<_Tp>
371 const_pointer_cast(const shared_ptr<_Tp1>& __r)
372 { return shared_ptr<_Tp>(__r, const_cast<_Tp*>(__r.get())); }
374 template<typename _Tp, typename _Tp1>
375 inline shared_ptr<_Tp>
376 dynamic_pointer_cast(const shared_ptr<_Tp1>& __r)
378 if (_Tp* __p = dynamic_cast<_Tp*>(__r.get()))
379 return shared_ptr<_Tp>(__r, __p);
380 return shared_ptr<_Tp>();
385 * @brief A smart pointer with weak semantics.
387 * With forwarding constructors and assignment operators.
389 template<typename _Tp>
390 class weak_ptr : public __weak_ptr<_Tp>
392 public:
393 constexpr weak_ptr()
394 : __weak_ptr<_Tp>() { }
396 template<typename _Tp1, typename = typename
397 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
398 weak_ptr(const weak_ptr<_Tp1>& __r)
399 : __weak_ptr<_Tp>(__r) { }
401 template<typename _Tp1, typename = typename
402 std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
403 weak_ptr(const shared_ptr<_Tp1>& __r)
404 : __weak_ptr<_Tp>(__r) { }
406 template<typename _Tp1>
407 weak_ptr&
408 operator=(const weak_ptr<_Tp1>& __r) // never throws
410 this->__weak_ptr<_Tp>::operator=(__r);
411 return *this;
414 template<typename _Tp1>
415 weak_ptr&
416 operator=(const shared_ptr<_Tp1>& __r) // never throws
418 this->__weak_ptr<_Tp>::operator=(__r);
419 return *this;
422 shared_ptr<_Tp>
423 lock() const // never throws
425 #ifdef __GTHREADS
426 if (this->expired())
427 return shared_ptr<_Tp>();
429 __try
431 return shared_ptr<_Tp>(*this);
433 __catch(const bad_weak_ptr&)
435 return shared_ptr<_Tp>();
437 #else
438 return this->expired() ? shared_ptr<_Tp>() : shared_ptr<_Tp>(*this);
439 #endif
443 // 20.8.13.3.7 weak_ptr specialized algorithms.
444 template<typename _Tp>
445 inline void
446 swap(weak_ptr<_Tp>& __a, weak_ptr<_Tp>& __b)
447 { __a.swap(__b); }
450 /// Primary template owner_less
451 template<typename _Tp>
452 struct owner_less;
454 /// Partial specialization of owner_less for shared_ptr.
455 template<typename _Tp>
456 struct owner_less<shared_ptr<_Tp>>
457 : public _Sp_owner_less<shared_ptr<_Tp>, weak_ptr<_Tp>>
458 { };
460 /// Partial specialization of owner_less for weak_ptr.
461 template<typename _Tp>
462 struct owner_less<weak_ptr<_Tp>>
463 : public _Sp_owner_less<weak_ptr<_Tp>, shared_ptr<_Tp>>
464 { };
467 * @brief Base class allowing use of member function shared_from_this.
469 template<typename _Tp>
470 class enable_shared_from_this
472 protected:
473 constexpr enable_shared_from_this() { }
475 enable_shared_from_this(const enable_shared_from_this&) { }
477 enable_shared_from_this&
478 operator=(const enable_shared_from_this&)
479 { return *this; }
481 ~enable_shared_from_this() { }
483 public:
484 shared_ptr<_Tp>
485 shared_from_this()
486 { return shared_ptr<_Tp>(this->_M_weak_this); }
488 shared_ptr<const _Tp>
489 shared_from_this() const
490 { return shared_ptr<const _Tp>(this->_M_weak_this); }
492 private:
493 template<typename _Tp1>
494 void
495 _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const
496 { _M_weak_this._M_assign(__p, __n); }
498 template<typename _Tp1>
499 friend void
500 __enable_shared_from_this_helper(const __shared_count<>& __pn,
501 const enable_shared_from_this* __pe,
502 const _Tp1* __px)
504 if (__pe != 0)
505 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
508 mutable weak_ptr<_Tp> _M_weak_this;
512 * @brief Create an object that is owned by a shared_ptr.
513 * @param __a An allocator.
514 * @param __args Arguments for the @a _Tp object's constructor.
515 * @return A shared_ptr that owns the newly created object.
516 * @throw An exception thrown from @a _Alloc::allocate or from the
517 * constructor of @a _Tp.
519 * A copy of @a __a will be used to allocate memory for the shared_ptr
520 * and the new object.
522 template<typename _Tp, typename _Alloc, typename... _Args>
523 inline shared_ptr<_Tp>
524 allocate_shared(_Alloc __a, _Args&&... __args)
526 return shared_ptr<_Tp>(_Sp_make_shared_tag(), std::forward<_Alloc>(__a),
527 std::forward<_Args>(__args)...);
531 * @brief Create an object that is owned by a shared_ptr.
532 * @param __args Arguments for the @a _Tp object's constructor.
533 * @return A shared_ptr that owns the newly created object.
534 * @throw std::bad_alloc, or an exception thrown from the
535 * constructor of @a _Tp.
537 template<typename _Tp, typename... _Args>
538 inline shared_ptr<_Tp>
539 make_shared(_Args&&... __args)
541 typedef typename std::remove_const<_Tp>::type _Tp_nc;
542 return allocate_shared<_Tp>(std::allocator<_Tp_nc>(),
543 std::forward<_Args>(__args)...);
546 /// std::hash specialization for shared_ptr.
547 template<typename _Tp>
548 struct hash<shared_ptr<_Tp>>
549 : public std::unary_function<shared_ptr<_Tp>, size_t>
551 size_t
552 operator()(const shared_ptr<_Tp>& __s) const
553 { return std::hash<_Tp*>()(__s.get()); }
556 // @} group pointer_abstractions
558 _GLIBCXX_END_NAMESPACE
560 #endif // _SHARED_PTR_H