1 // <bits/boost_sp_shared_count.h> -*- C++ -*-
3 // Copyright (C) 2007 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 2, 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 // 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,
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.
31 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
34 // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
35 // Copyright (C) 2001, 2002, 2003 Peter Dimov
38 // Copyright (C) 2001, 2002, 2003 Peter Dimov
40 // enable_shared_from_this.hpp
41 // Copyright (C) 2002 Peter Dimov
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)
47 // GCC Note: based on version 1.32.0 of the Boost library.
49 /** @file bits/boost_sp_shared_count.h
50 * This is an internal header file, included by other library headers.
51 * You should not attempt to use it directly.
54 #ifndef __GXX_EXPERIMENTAL_CXX0X__
55 # include <c++0x_warning.h>
58 #if defined(_GLIBCXX_INCLUDE_AS_TR1)
59 # error C++0x header cannot be included from TR1 header
64 // counted ptr with no deleter or allocator support
65 template<typename _Ptr
, _Lock_policy _Lp
>
67 : public _Sp_counted_base
<_Lp
>
70 _Sp_counted_ptr(_Ptr __p
)
74 _M_dispose() // nothrow
78 _M_destroy() // nothrow
82 _M_get_deleter(const std::type_info
& __ti
)
86 _Sp_counted_ptr(const _Sp_counted_ptr
&);
87 _Sp_counted_ptr
& operator=(const _Sp_counted_ptr
&);
90 _Ptr _M_ptr
; // copy constructor must not throw
93 // support for custom deleter and/or allocator
94 template<typename _Ptr
, typename _Deleter
, typename _Alloc
, _Lock_policy _Lp
>
95 class _Sp_counted_deleter
96 : public _Sp_counted_ptr
<_Ptr
, _Lp
>
98 typedef typename
_Alloc::template
99 rebind
<_Sp_counted_deleter
>::other _My_alloc_type
;
101 // Helper class that stores the Deleter and also acts as an allocator.
102 // Used to dispose of the owned pointer and the internal refcount
103 // Requires that copies of _Alloc can free each other's memory.
105 : public _My_alloc_type
// copy constructor must not throw
107 _Deleter _M_del
; // copy constructor must not throw
108 _My_Deleter(_Deleter __d
, const _Alloc
& __a
)
109 : _My_alloc_type(__a
), _M_del(__d
) { }
113 typedef _Sp_counted_ptr
<_Ptr
, _Lp
> _Base_type
;
118 * @pre __d(__p) must not throw.
120 _Sp_counted_deleter(_Ptr __p
, _Deleter __d
)
121 : _Base_type(__p
), _M_del(__d
, _Alloc()) { }
125 * @pre __d(__p) must not throw.
127 _Sp_counted_deleter(_Ptr __p
, _Deleter __d
, const _Alloc
& __a
)
128 : _Base_type(__p
), _M_del(__d
, __a
) { }
131 _M_dispose() // nothrow
132 { _M_del
._M_del(_Base_type::_M_ptr
); }
135 _M_destroy() // nothrow
137 _My_alloc_type
__a(_M_del
);
138 this->~_Sp_counted_deleter();
139 __a
.deallocate(this, 1);
143 _M_get_deleter(const std::type_info
& __ti
)
144 { return __ti
== typeid(_Deleter
) ? &_M_del
._M_del
: 0; }
147 _Sp_counted_deleter(const _Sp_counted_deleter
&);
148 _Sp_counted_deleter
& operator=(const _Sp_counted_deleter
&);
151 _My_Deleter _M_del
; // copy constructor must not throw
154 // helpers for make_shared / allocate_shared
156 template<typename _Tp
>
157 struct _Sp_destroy_inplace
159 void operator()(_Tp
* __p
) const { if (__p
) __p
->~_Tp(); }
162 struct _Sp_make_shared_tag
{ };
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
>
168 typedef _Sp_counted_deleter
<_Tp
*, _Sp_destroy_inplace
<_Tp
>, _Alloc
, _Lp
>
172 _Sp_counted_ptr_inplace(_Alloc __a
)
173 : _Base_type(static_cast<_Tp
*>(0), _Sp_destroy_inplace
<_Tp
>(), __a
)
176 void* __p
= &_M_storage
;
177 ::new (__p
) _Tp(); // might throw
178 _Base_type::_Base_type::_M_ptr
= static_cast<_Tp
*>(__p
);
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
)
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
);
191 // override because the allocator needs to know the dynamic type
193 _M_destroy() // nothrow
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);
202 // sneaky trick so __shared_ptr can get the managed pointer
204 _M_get_deleter(const std::type_info
& __ti
)
206 return __ti
== typeid(_Sp_make_shared_tag
)
207 ? static_cast<void*>(&_M_storage
)
208 : _Base_type::_M_get_deleter(__ti
);
212 typename aligned_storage
<sizeof(_Tp
), alignment_of
<_Tp
>::value
>::type
216 template<_Lock_policy _Lp
= __default_lock_policy
>
219 template<_Lock_policy _Lp
= __default_lock_policy
>
224 : _M_pi(0) // nothrow
227 template<typename _Ptr
>
228 __shared_count(_Ptr __p
) : _M_pi(0)
232 _M_pi
= new _Sp_counted_ptr
<_Ptr
, _Lp
>(__p
);
237 __throw_exception_again
;
241 template<typename _Ptr
, typename _Deleter
>
242 __shared_count(_Ptr __p
, _Deleter __d
) : _M_pi(0)
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
;
251 _M_pi
= __a2
.allocate(1);
252 new(static_cast<void*>(_M_pi
)) _Sp_cd_type(__p
, __d
);
256 __d(__p
); // Call _Deleter on __p.
258 __a2
.deallocate(static_cast<_Sp_cd_type
*>(_M_pi
), 1);
259 __throw_exception_again
;
263 template<typename _Ptr
, typename _Deleter
, typename _Alloc
>
264 __shared_count(_Ptr __p
, _Deleter __d
, _Alloc __a
) : _M_pi(0)
266 typedef _Sp_counted_deleter
<_Ptr
, _Deleter
, _Alloc
, _Lp
> _Sp_cd_type
;
267 typedef typename
_Alloc::template rebind
<_Sp_cd_type
>::other _Alloc2
;
271 _M_pi
= __a2
.allocate(1);
272 new(static_cast<void*>(_M_pi
)) _Sp_cd_type(__p
, __d
, __a
);
276 __d(__p
); // Call _Deleter on __p.
278 __a2
.deallocate(static_cast<_Sp_cd_type
*>(_M_pi
), 1);
279 __throw_exception_again
;
283 template<typename _Tp
, typename _Alloc
, typename
... _Args
>
284 __shared_count(_Sp_make_shared_tag
, _Tp
*, _Alloc __a
, _Args
&&... __args
)
287 typedef _Sp_counted_ptr_inplace
<_Tp
, _Alloc
, _Lp
> _Sp_cp_type
;
288 typedef typename
_Alloc::template rebind
<_Sp_cp_type
>::other _Alloc2
;
292 _M_pi
= __a2
.allocate(1);
293 new(static_cast<void*>(_M_pi
)) _Sp_cp_type(__a
,
294 std::forward
<_Args
>(__args
)...);
299 __a2
.deallocate(static_cast<_Sp_cp_type
*>(_M_pi
), 1);
300 __throw_exception_again
;
304 #if _GLIBCXX_DEPRECATED
305 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
306 template<typename _Tp
>
308 __shared_count(std::auto_ptr
<_Tp
>& __r
)
309 : _M_pi(new _Sp_counted_ptr
<_Tp
*, _Lp
>(__r
.get()))
313 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
315 __shared_count(const __weak_count
<_Lp
>& __r
);
317 ~__shared_count() // nothrow
323 __shared_count(const __shared_count
& __r
)
324 : _M_pi(__r
._M_pi
) // nothrow
327 _M_pi
->_M_add_ref_copy();
331 operator=(const __shared_count
& __r
) // nothrow
333 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
337 __tmp
->_M_add_ref_copy();
346 _M_swap(__shared_count
& __r
) // nothrow
348 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
354 _M_get_use_count() const // nothrow
355 { return _M_pi
!= 0 ? _M_pi
->_M_get_use_count() : 0; }
358 _M_unique() const // nothrow
359 { return this->_M_get_use_count() == 1; }
362 operator==(const __shared_count
& __a
, const __shared_count
& __b
)
363 { return __a
._M_pi
== __b
._M_pi
; }
366 operator<(const __shared_count
& __a
, const __shared_count
& __b
)
367 { return std::less
<_Sp_counted_base
<_Lp
>*>()(__a
._M_pi
, __b
._M_pi
); }
370 _M_get_deleter(const std::type_info
& __ti
) const
371 { return _M_pi
? _M_pi
->_M_get_deleter(__ti
) : 0; }
374 friend class __weak_count
<_Lp
>;
376 _Sp_counted_base
<_Lp
>* _M_pi
;