Install gcc-4.3.3-tdm-1-g++.tar.gz
[msysgit.git] / mingw / lib / gcc / mingw32 / 4.3.3 / include / c++ / tr1 / boost_sp_shared_count.h
bloba2de21f44399ce1138a9db3c75d0ca78e248a7f3
1 // <tr1/boost_sp_shared_count.h> -*- C++ -*-
3 // Copyright (C) 2007 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 2, 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 // 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,
19 // USA.
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.
30 // shared_count.hpp
31 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
33 // shared_ptr.hpp
34 // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
35 // Copyright (C) 2001, 2002, 2003 Peter Dimov
37 // weak_ptr.hpp
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
56 #endif
58 namespace std
60 namespace tr1
63 template<typename _Ptr, typename _Deleter, _Lock_policy _Lp>
64 class _Sp_counted_base_impl
65 : public _Sp_counted_base<_Lp>
67 public:
68 /**
69 * @brief
70 * @pre __d(__p) must not throw.
72 _Sp_counted_base_impl(_Ptr __p, _Deleter __d)
73 : _M_ptr(__p), _M_del(__d) { }
75 virtual void
76 _M_dispose() // nothrow
77 { _M_del(_M_ptr); }
79 virtual void*
80 _M_get_deleter(const std::type_info& __ti)
81 { return __ti == typeid(_Deleter) ? &_M_del : 0; }
83 private:
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>
92 class __weak_count;
94 template<typename _Tp>
95 struct _Sp_deleter
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>
103 class __shared_count
105 public:
106 __shared_count()
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>());
119 catch(...)
121 delete __p;
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);
133 catch(...)
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>
142 explicit
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>()))
146 { __r.release(); }
148 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
149 explicit
150 __shared_count(const __weak_count<_Lp>& __r);
152 ~__shared_count() // nothrow
154 if (_M_pi != 0)
155 _M_pi->_M_release();
158 __shared_count(const __shared_count& __r)
159 : _M_pi(__r._M_pi) // nothrow
161 if (_M_pi != 0)
162 _M_pi->_M_add_ref_copy();
165 __shared_count&
166 operator=(const __shared_count& __r) // nothrow
168 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
169 if (__tmp != _M_pi)
171 if (__tmp != 0)
172 __tmp->_M_add_ref_copy();
173 if (_M_pi != 0)
174 _M_pi->_M_release();
175 _M_pi = __tmp;
177 return *this;
180 void
181 _M_swap(__shared_count& __r) // nothrow
183 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
184 __r._M_pi = _M_pi;
185 _M_pi = __tmp;
188 long
189 _M_get_use_count() const // nothrow
190 { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
192 bool
193 _M_unique() const // nothrow
194 { return this->_M_get_use_count() == 1; }
196 friend inline bool
197 operator==(const __shared_count& __a, const __shared_count& __b)
198 { return __a._M_pi == __b._M_pi; }
200 friend inline bool
201 operator<(const __shared_count& __a, const __shared_count& __b)
202 { return std::less<_Sp_counted_base<_Lp>*>()(__a._M_pi, __b._M_pi); }
204 void*
205 _M_get_deleter(const std::type_info& __ti) const
206 { return _M_pi ? _M_pi->_M_get_deleter(__ti) : 0; }
208 private:
209 friend class __weak_count<_Lp>;
211 _Sp_counted_base<_Lp>* _M_pi;