Install gcc-4.4.0-tdm-1-core-2.tar.gz
[msysgit.git] / mingw / lib / gcc / mingw32 / 4.3.3 / include / c++ / bits / boost_sp_shared_count.h
blob75ee16d8eb21383bb62ded1e13304462a6bb8f13
1 // <bits/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 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>
56 #endif
58 #if defined(_GLIBCXX_INCLUDE_AS_TR1)
59 # error C++0x header cannot be included from TR1 header
60 #endif
62 namespace std
64 // counted ptr with no deleter or allocator support
65 template<typename _Ptr, _Lock_policy _Lp>
66 class _Sp_counted_ptr
67 : public _Sp_counted_base<_Lp>
69 public:
70 _Sp_counted_ptr(_Ptr __p)
71 : _M_ptr(__p) { }
73 virtual void
74 _M_dispose() // nothrow
75 { delete _M_ptr; }
77 virtual void
78 _M_destroy() // nothrow
79 { delete this; }
81 virtual void*
82 _M_get_deleter(const std::type_info& __ti)
83 { return 0; }
85 private:
86 _Sp_counted_ptr(const _Sp_counted_ptr&);
87 _Sp_counted_ptr& operator=(const _Sp_counted_ptr&);
89 protected:
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.
104 struct _My_Deleter
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) { }
112 protected:
113 typedef _Sp_counted_ptr<_Ptr, _Lp> _Base_type;
115 public:
117 * @brief
118 * @pre __d(__p) must not throw.
120 _Sp_counted_deleter(_Ptr __p, _Deleter __d)
121 : _Base_type(__p), _M_del(__d, _Alloc()) { }
124 * @brief
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) { }
130 virtual void
131 _M_dispose() // nothrow
132 { _M_del._M_del(_Base_type::_M_ptr); }
134 virtual void
135 _M_destroy() // nothrow
137 _My_alloc_type __a(_M_del);
138 this->~_Sp_counted_deleter();
139 __a.deallocate(this, 1);
142 virtual void*
143 _M_get_deleter(const std::type_info& __ti)
144 { return __ti == typeid(_Deleter) ? &_M_del._M_del : 0; }
146 private:
147 _Sp_counted_deleter(const _Sp_counted_deleter&);
148 _Sp_counted_deleter& operator=(const _Sp_counted_deleter&);
150 protected:
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>
169 _Base_type;
171 public:
172 _Sp_counted_ptr_inplace(_Alloc __a)
173 : _Base_type(static_cast<_Tp*>(0), _Sp_destroy_inplace<_Tp>(), __a)
174 , _M_storage()
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)
184 , _M_storage()
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
192 virtual void
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
203 virtual void*
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);
211 private:
212 typename aligned_storage<sizeof(_Tp), alignment_of<_Tp>::value>::type
213 _M_storage;
216 template<_Lock_policy _Lp = __default_lock_policy>
217 class __weak_count;
219 template<_Lock_policy _Lp = __default_lock_policy>
220 class __shared_count
222 public:
223 __shared_count()
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);
234 catch(...)
236 delete __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;
248 _Alloc2 __a2;
251 _M_pi = __a2.allocate(1);
252 new(static_cast<void*>(_M_pi)) _Sp_cd_type(__p, __d);
254 catch(...)
256 __d(__p); // Call _Deleter on __p.
257 if (_M_pi)
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;
268 _Alloc2 __a2(__a);
271 _M_pi = __a2.allocate(1);
272 new(static_cast<void*>(_M_pi)) _Sp_cd_type(__p, __d, __a);
274 catch(...)
276 __d(__p); // Call _Deleter on __p.
277 if (_M_pi)
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)
285 : _M_pi(0)
287 typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
288 typedef typename _Alloc::template rebind<_Sp_cp_type>::other _Alloc2;
289 _Alloc2 __a2(__a);
292 _M_pi = __a2.allocate(1);
293 new(static_cast<void*>(_M_pi)) _Sp_cp_type(__a,
294 std::forward<_Args>(__args)...);
296 catch(...)
298 if (_M_pi)
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>
307 explicit
308 __shared_count(std::auto_ptr<_Tp>& __r)
309 : _M_pi(new _Sp_counted_ptr<_Tp*, _Lp>(__r.get()))
310 { __r.release(); }
311 #endif
313 // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
314 explicit
315 __shared_count(const __weak_count<_Lp>& __r);
317 ~__shared_count() // nothrow
319 if (_M_pi != 0)
320 _M_pi->_M_release();
323 __shared_count(const __shared_count& __r)
324 : _M_pi(__r._M_pi) // nothrow
326 if (_M_pi != 0)
327 _M_pi->_M_add_ref_copy();
330 __shared_count&
331 operator=(const __shared_count& __r) // nothrow
333 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
334 if (__tmp != _M_pi)
336 if (__tmp != 0)
337 __tmp->_M_add_ref_copy();
338 if (_M_pi != 0)
339 _M_pi->_M_release();
340 _M_pi = __tmp;
342 return *this;
345 void
346 _M_swap(__shared_count& __r) // nothrow
348 _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
349 __r._M_pi = _M_pi;
350 _M_pi = __tmp;
353 long
354 _M_get_use_count() const // nothrow
355 { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
357 bool
358 _M_unique() const // nothrow
359 { return this->_M_get_use_count() == 1; }
361 friend inline bool
362 operator==(const __shared_count& __a, const __shared_count& __b)
363 { return __a._M_pi == __b._M_pi; }
365 friend inline bool
366 operator<(const __shared_count& __a, const __shared_count& __b)
367 { return std::less<_Sp_counted_base<_Lp>*>()(__a._M_pi, __b._M_pi); }
369 void*
370 _M_get_deleter(const std::type_info& __ti) const
371 { return _M_pi ? _M_pi->_M_get_deleter(__ti) : 0; }
373 private:
374 friend class __weak_count<_Lp>;
376 _Sp_counted_base<_Lp>* _M_pi;