1 // shared_ptr and weak_ptr implementation -*- C++ -*-
3 // Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
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)
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.
28 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
31 // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
32 // Copyright (C) 2001, 2002, 2003 Peter Dimov
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.
50 #define _SHARED_PTR_H 1
52 #include <bits/shared_ptr_base.h>
54 _GLIBCXX_BEGIN_NAMESPACE(std
)
57 * @addtogroup pointer_abstractions
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
)
71 /// 2.2.3.10 shared_ptr get_deleter (experimental)
72 template<typename _Del
, typename _Tp
, _Lock_policy _Lp
>
74 get_deleter(const __shared_ptr
<_Tp
, _Lp
>& __p
)
77 return static_cast<_Del
*>(__p
._M_get_deleter(typeid(_Del
)));
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
>
95 * @brief Construct an empty %shared_ptr.
96 * @post use_count()==0 && get()==0
98 shared_ptr() : __shared_ptr
<_Tp
>() { }
101 * @brief Construct a %shared_ptr that owns the pointer @a __p.
102 * @param __p A pointer that is convertible to element_type*.
103 * @post use_count() == 1 && get() == __p
104 * @throw std::bad_alloc, in which case @c delete @a __p is called.
106 template<typename _Tp1
>
107 explicit shared_ptr(_Tp1
* __p
) : __shared_ptr
<_Tp
>(__p
) { }
110 * @brief Construct a %shared_ptr that owns the pointer @a __p
111 * and the deleter @a __d.
112 * @param __p A pointer.
113 * @param __d A deleter.
114 * @post use_count() == 1 && get() == __p
115 * @throw std::bad_alloc, in which case @a __d(__p) is called.
117 * Requirements: _Deleter's copy constructor and destructor must
120 * __shared_ptr will release __p by calling __d(__p)
122 template<typename _Tp1
, typename _Deleter
>
123 shared_ptr(_Tp1
* __p
, _Deleter __d
) : __shared_ptr
<_Tp
>(__p
, __d
) { }
126 * @brief Construct a %shared_ptr that owns a null pointer
127 * and the deleter @a __d.
128 * @param __p A null pointer constant.
129 * @param __d A deleter.
130 * @post use_count() == 1 && get() == __p
131 * @throw std::bad_alloc, in which case @a __d(__p) is called.
133 * Requirements: _Deleter's copy constructor and destructor must
136 * The last owner will call __d(__p)
138 template<typename _Deleter
>
139 shared_ptr(nullptr_t __p
, _Deleter __d
)
140 : __shared_ptr
<_Tp
>(__p
, __d
) { }
143 * @brief Construct a %shared_ptr that owns the pointer @a __p
144 * and the deleter @a __d.
145 * @param __p A pointer.
146 * @param __d A deleter.
147 * @param __a An allocator.
148 * @post use_count() == 1 && get() == __p
149 * @throw std::bad_alloc, in which case @a __d(__p) is called.
151 * Requirements: _Deleter's copy constructor and destructor must
152 * not throw _Alloc's copy constructor and destructor must not
155 * __shared_ptr will release __p by calling __d(__p)
157 template<typename _Tp1
, typename _Deleter
, typename _Alloc
>
158 shared_ptr(_Tp1
* __p
, _Deleter __d
, const _Alloc
& __a
)
159 : __shared_ptr
<_Tp
>(__p
, __d
, __a
) { }
162 * @brief Construct a %shared_ptr that owns a null pointer
163 * and the deleter @a __d.
164 * @param __p A null pointer constant.
165 * @param __d A deleter.
166 * @param __a An allocator.
167 * @post use_count() == 1 && get() == __p
168 * @throw std::bad_alloc, in which case @a __d(__p) is called.
170 * Requirements: _Deleter's copy constructor and destructor must
171 * not throw _Alloc's copy constructor and destructor must not
174 * The last owner will call __d(__p)
176 template<typename _Deleter
, typename _Alloc
>
177 shared_ptr(nullptr_t __p
, _Deleter __d
, const _Alloc
& __a
)
178 : __shared_ptr
<_Tp
>(__p
, __d
, __a
) { }
180 // Aliasing constructor
183 * @brief Constructs a %shared_ptr instance that stores @a __p
184 * and shares ownership with @a __r.
185 * @param __r A %shared_ptr.
186 * @param __p A pointer that will remain valid while @a *__r is valid.
187 * @post get() == __p && use_count() == __r.use_count()
189 * This can be used to construct a @c shared_ptr to a sub-object
190 * of an object managed by an existing @c shared_ptr.
193 * shared_ptr< pair<int,int> > pii(new pair<int,int>());
194 * shared_ptr<int> pi(pii, &pii->first);
195 * assert(pii.use_count() == 2);
198 template<typename _Tp1
>
199 shared_ptr(const shared_ptr
<_Tp1
>& __r
, _Tp
* __p
)
200 : __shared_ptr
<_Tp
>(__r
, __p
) { }
203 * @brief If @a __r is empty, constructs an empty %shared_ptr;
204 * otherwise construct a %shared_ptr that shares ownership
206 * @param __r A %shared_ptr.
207 * @post get() == __r.get() && use_count() == __r.use_count()
209 template<typename _Tp1
>
210 shared_ptr(const shared_ptr
<_Tp1
>& __r
) : __shared_ptr
<_Tp
>(__r
) { }
213 * @brief Move-constructs a %shared_ptr instance from @a __r.
214 * @param __r A %shared_ptr rvalue.
215 * @post *this contains the old value of @a __r, @a __r is empty.
217 shared_ptr(shared_ptr
&& __r
)
218 : __shared_ptr
<_Tp
>(std::move(__r
)) { }
221 * @brief Move-constructs a %shared_ptr instance from @a __r.
222 * @param __r A %shared_ptr rvalue.
223 * @post *this contains the old value of @a __r, @a __r is empty.
225 template<typename _Tp1
>
226 shared_ptr(shared_ptr
<_Tp1
>&& __r
)
227 : __shared_ptr
<_Tp
>(std::move(__r
)) { }
230 * @brief Constructs a %shared_ptr that shares ownership with @a __r
231 * and stores a copy of the pointer stored in @a __r.
232 * @param __r A weak_ptr.
233 * @post use_count() == __r.use_count()
234 * @throw bad_weak_ptr when __r.expired(),
235 * in which case the constructor has no effect.
237 template<typename _Tp1
>
238 explicit shared_ptr(const weak_ptr
<_Tp1
>& __r
)
239 : __shared_ptr
<_Tp
>(__r
) { }
241 #if _GLIBCXX_DEPRECATED
242 template<typename _Tp1
>
243 shared_ptr(std::auto_ptr
<_Tp1
>&& __r
)
244 : __shared_ptr
<_Tp
>(std::move(__r
)) { }
247 template<typename _Tp1
, typename _Del
>
248 shared_ptr(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
249 : __shared_ptr
<_Tp
>(std::move(__r
)) { }
252 * @brief Construct an empty %shared_ptr.
253 * @param __p A null pointer constant.
254 * @post use_count() == 0 && get() == nullptr
256 shared_ptr(nullptr_t __p
) : __shared_ptr
<_Tp
>(__p
) { }
258 template<typename _Tp1
>
260 operator=(const shared_ptr
<_Tp1
>& __r
) // never throws
262 this->__shared_ptr
<_Tp
>::operator=(__r
);
266 #if _GLIBCXX_DEPRECATED
267 template<typename _Tp1
>
269 operator=(std::auto_ptr
<_Tp1
>&& __r
)
271 this->__shared_ptr
<_Tp
>::operator=(std::move(__r
));
277 operator=(shared_ptr
&& __r
)
279 this->__shared_ptr
<_Tp
>::operator=(std::move(__r
));
285 operator=(shared_ptr
<_Tp1
>&& __r
)
287 this->__shared_ptr
<_Tp
>::operator=(std::move(__r
));
291 template<typename _Tp1
, typename _Del
>
293 operator=(std::unique_ptr
<_Tp1
, _Del
>&& __r
)
295 this->__shared_ptr
<_Tp
>::operator=(std::move(__r
));
300 // This constructor is non-standard, it is used by allocate_shared.
301 template<typename _Alloc
, typename
... _Args
>
302 shared_ptr(_Sp_make_shared_tag __tag
, _Alloc __a
, _Args
&&... __args
)
303 : __shared_ptr
<_Tp
>(__tag
, __a
, std::forward
<_Args
>(__args
)...)
306 template<typename _Tp1
, typename _Alloc
, typename
... _Args
>
307 friend shared_ptr
<_Tp1
>
308 allocate_shared(_Alloc __a
, _Args
&&... __args
);
311 // 20.8.13.2.7 shared_ptr comparisons
312 template<typename _Tp1
, typename _Tp2
>
314 operator==(const shared_ptr
<_Tp1
>& __a
, const shared_ptr
<_Tp2
>& __b
)
315 { return __a
.get() == __b
.get(); }
317 template<typename _Tp1
, typename _Tp2
>
319 operator!=(const shared_ptr
<_Tp1
>& __a
, const shared_ptr
<_Tp2
>& __b
)
320 { return __a
.get() != __b
.get(); }
322 template<typename _Tp1
, typename _Tp2
>
324 operator<(const shared_ptr
<_Tp1
>& __a
, const shared_ptr
<_Tp2
>& __b
)
325 { return __a
.get() < __b
.get(); }
327 template<typename _Tp
>
328 struct less
<shared_ptr
<_Tp
>> : public _Sp_less
<shared_ptr
<_Tp
>>
331 // 20.8.13.2.9 shared_ptr specialized algorithms.
332 template<typename _Tp
>
334 swap(shared_ptr
<_Tp
>& __a
, shared_ptr
<_Tp
>& __b
)
337 // 20.8.13.2.10 shared_ptr casts.
338 template<typename _Tp
, typename _Tp1
>
339 inline shared_ptr
<_Tp
>
340 static_pointer_cast(const shared_ptr
<_Tp1
>& __r
)
341 { return shared_ptr
<_Tp
>(__r
, static_cast<_Tp
*>(__r
.get())); }
343 template<typename _Tp
, typename _Tp1
>
344 inline shared_ptr
<_Tp
>
345 const_pointer_cast(const shared_ptr
<_Tp1
>& __r
)
346 { return shared_ptr
<_Tp
>(__r
, const_cast<_Tp
*>(__r
.get())); }
348 template<typename _Tp
, typename _Tp1
>
349 inline shared_ptr
<_Tp
>
350 dynamic_pointer_cast(const shared_ptr
<_Tp1
>& __r
)
352 if (_Tp
* __p
= dynamic_cast<_Tp
*>(__r
.get()))
353 return shared_ptr
<_Tp
>(__r
, __p
);
354 return shared_ptr
<_Tp
>();
359 * @brief A smart pointer with weak semantics.
361 * With forwarding constructors and assignment operators.
363 template<typename _Tp
>
364 class weak_ptr
: public __weak_ptr
<_Tp
>
367 weak_ptr() : __weak_ptr
<_Tp
>() { }
369 template<typename _Tp1
>
370 weak_ptr(const weak_ptr
<_Tp1
>& __r
)
371 : __weak_ptr
<_Tp
>(__r
) { }
373 template<typename _Tp1
>
374 weak_ptr(const shared_ptr
<_Tp1
>& __r
)
375 : __weak_ptr
<_Tp
>(__r
) { }
377 template<typename _Tp1
>
379 operator=(const weak_ptr
<_Tp1
>& __r
) // never throws
381 this->__weak_ptr
<_Tp
>::operator=(__r
);
385 template<typename _Tp1
>
387 operator=(const shared_ptr
<_Tp1
>& __r
) // never throws
389 this->__weak_ptr
<_Tp
>::operator=(__r
);
394 lock() const // never throws
398 return shared_ptr
<_Tp
>();
402 return shared_ptr
<_Tp
>(*this);
404 __catch(const bad_weak_ptr
&)
406 return shared_ptr
<_Tp
>();
409 return this->expired() ? shared_ptr
<_Tp
>() : shared_ptr
<_Tp
>(*this);
414 // 20.8.13.3.7 weak_ptr specialized algorithms.
415 template<typename _Tp
>
417 swap(weak_ptr
<_Tp
>& __a
, weak_ptr
<_Tp
>& __b
)
421 /// Primary template owner_less
422 template<typename _Tp
>
425 /// Partial specialization of owner_less for shared_ptr.
426 template<typename _Tp
>
427 struct owner_less
<shared_ptr
<_Tp
>>
428 : public _Sp_owner_less
<shared_ptr
<_Tp
>, weak_ptr
<_Tp
>>
431 /// Partial specialization of owner_less for weak_ptr.
432 template<typename _Tp
>
433 struct owner_less
<weak_ptr
<_Tp
>>
434 : public _Sp_owner_less
<weak_ptr
<_Tp
>, shared_ptr
<_Tp
>>
438 * @brief Base class allowing use of member function shared_from_this.
440 template<typename _Tp
>
441 class enable_shared_from_this
444 enable_shared_from_this() { }
446 enable_shared_from_this(const enable_shared_from_this
&) { }
448 enable_shared_from_this
&
449 operator=(const enable_shared_from_this
&)
452 ~enable_shared_from_this() { }
457 { return shared_ptr
<_Tp
>(this->_M_weak_this
); }
459 shared_ptr
<const _Tp
>
460 shared_from_this() const
461 { return shared_ptr
<const _Tp
>(this->_M_weak_this
); }
464 template<typename _Tp1
>
466 _M_weak_assign(_Tp1
* __p
, const __shared_count
<>& __n
) const
467 { _M_weak_this
._M_assign(__p
, __n
); }
469 template<typename _Tp1
>
471 __enable_shared_from_this_helper(const __shared_count
<>& __pn
,
472 const enable_shared_from_this
* __pe
,
476 __pe
->_M_weak_assign(const_cast<_Tp1
*>(__px
), __pn
);
479 mutable weak_ptr
<_Tp
> _M_weak_this
;
483 * @brief Create an object that is owned by a shared_ptr.
484 * @param __a An allocator.
485 * @param __args Arguments for the @a _Tp object's constructor.
486 * @return A shared_ptr that owns the newly created object.
487 * @throw An exception thrown from @a _Alloc::allocate or from the
488 * constructor of @a _Tp.
490 * A copy of @a __a will be used to allocate memory for the shared_ptr
491 * and the new object.
493 template<typename _Tp
, typename _Alloc
, typename
... _Args
>
494 inline shared_ptr
<_Tp
>
495 allocate_shared(_Alloc __a
, _Args
&&... __args
)
497 return shared_ptr
<_Tp
>(_Sp_make_shared_tag(), std::forward
<_Alloc
>(__a
),
498 std::forward
<_Args
>(__args
)...);
502 * @brief Create an object that is owned by a shared_ptr.
503 * @param __args Arguments for the @a _Tp object's constructor.
504 * @return A shared_ptr that owns the newly created object.
505 * @throw std::bad_alloc, or an exception thrown from the
506 * constructor of @a _Tp.
508 template<typename _Tp
, typename
... _Args
>
509 inline shared_ptr
<_Tp
>
510 make_shared(_Args
&&... __args
)
512 typedef typename
std::remove_const
<_Tp
>::type _Tp_nc
;
513 return allocate_shared
<_Tp
>(std::allocator
<_Tp_nc
>(),
514 std::forward
<_Args
>(__args
)...);
517 // @} group pointer_abstractions
519 _GLIBCXX_END_NAMESPACE
521 #endif // _SHARED_PTR_H