1 // <tr1/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 tr1/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 #if defined(_GLIBCXX_INCLUDE_AS_CXX0X)
55 # error TR1 header cannot be included from C++0x header
63 template<typename _Ptr
, typename _Deleter
, _Lock_policy _Lp
>
64 class _Sp_counted_base_impl
65 : public _Sp_counted_base
<_Lp
>
70 * @pre __d(__p) must not throw.
72 _Sp_counted_base_impl(_Ptr __p
, _Deleter __d
)
73 : _M_ptr(__p
), _M_del(__d
) { }
76 _M_dispose() // nothrow
80 _M_get_deleter(const std::type_info
& __ti
)
81 { return __ti
== typeid(_Deleter
) ? &_M_del
: 0; }
84 _Sp_counted_base_impl(const _Sp_counted_base_impl
&);
85 _Sp_counted_base_impl
& operator=(const _Sp_counted_base_impl
&);
87 _Ptr _M_ptr
; // copy constructor must not throw
88 _Deleter _M_del
; // copy constructor must not throw
91 template<_Lock_policy _Lp
= __default_lock_policy
>
94 template<typename _Tp
>
97 typedef void result_type
;
98 typedef _Tp
* argument_type
;
99 void operator()(_Tp
* __p
) const { delete __p
; }
102 template<_Lock_policy _Lp
= __default_lock_policy
>
107 : _M_pi(0) // nothrow
110 template<typename _Ptr
>
111 __shared_count(_Ptr __p
) : _M_pi(0)
115 typedef typename
std::tr1::remove_pointer
<_Ptr
>::type _Tp
;
116 _M_pi
= new _Sp_counted_base_impl
<_Ptr
, _Sp_deleter
<_Tp
>, _Lp
>(
117 __p
, _Sp_deleter
<_Tp
>());
122 __throw_exception_again
;
126 template<typename _Ptr
, typename _Deleter
>
127 __shared_count(_Ptr __p
, _Deleter __d
) : _M_pi(0)
131 _M_pi
= new _Sp_counted_base_impl
<_Ptr
, _Deleter
, _Lp
>(__p
, __d
);
135 __d(__p
); // Call _Deleter on __p.
136 __throw_exception_again
;
140 // Special case for auto_ptr<_Tp> to provide the strong guarantee.
141 template<typename _Tp
>
143 __shared_count(std::auto_ptr
<_Tp
>& __r
)
144 : _M_pi(new _Sp_counted_base_impl
<_Tp
*,
145 _Sp_deleter
<_Tp
>, _Lp
>(__r
.get(), _Sp_deleter
<_Tp
>()))
148 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
150 __shared_count(const __weak_count
<_Lp
>& __r
);
152 ~__shared_count() // nothrow
158 __shared_count(const __shared_count
& __r
)
159 : _M_pi(__r
._M_pi
) // nothrow
162 _M_pi
->_M_add_ref_copy();
166 operator=(const __shared_count
& __r
) // nothrow
168 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
172 __tmp
->_M_add_ref_copy();
181 _M_swap(__shared_count
& __r
) // nothrow
183 _Sp_counted_base
<_Lp
>* __tmp
= __r
._M_pi
;
189 _M_get_use_count() const // nothrow
190 { return _M_pi
!= 0 ? _M_pi
->_M_get_use_count() : 0; }
193 _M_unique() const // nothrow
194 { return this->_M_get_use_count() == 1; }
197 operator==(const __shared_count
& __a
, const __shared_count
& __b
)
198 { return __a
._M_pi
== __b
._M_pi
; }
201 operator<(const __shared_count
& __a
, const __shared_count
& __b
)
202 { return std::less
<_Sp_counted_base
<_Lp
>*>()(__a
._M_pi
, __b
._M_pi
); }
205 _M_get_deleter(const std::type_info
& __ti
) const
206 { return _M_pi
? _M_pi
->_M_get_deleter(__ti
) : 0; }
209 friend class __weak_count
<_Lp
>;
211 _Sp_counted_base
<_Lp
>* _M_pi
;