]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libstdc++-v3/contrib/libstdc++-v3-4.3.3/include/tr1/boost_sp_shared_count.h
update
[l4.git] / l4 / pkg / libstdc++-v3 / contrib / libstdc++-v3-4.3.3 / include / tr1 / boost_sp_shared_count.h
1 // <tr1/boost_sp_shared_count.h> -*- C++ -*-
2
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.
10
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.
15
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.
20
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.
29
30 //  shared_count.hpp
31 //  Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
32
33 //  shared_ptr.hpp
34 //  Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
35 //  Copyright (C) 2001, 2002, 2003 Peter Dimov
36
37 //  weak_ptr.hpp
38 //  Copyright (C) 2001, 2002, 2003 Peter Dimov
39
40 //  enable_shared_from_this.hpp
41 //  Copyright (C) 2002 Peter Dimov
42
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)
46
47 // GCC Note:  based on version 1.32.0 of the Boost library.
48
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.
52  */
53
54 #if defined(_GLIBCXX_INCLUDE_AS_CXX0X)
55 #  error TR1 header cannot be included from C++0x header
56 #endif
57
58 namespace std
59 {
60 namespace tr1
61 {
62
63   template<typename _Ptr, typename _Deleter, _Lock_policy _Lp>
64     class _Sp_counted_base_impl
65     : public _Sp_counted_base<_Lp>
66     {
67     public:
68       /**
69        *  @brief   
70        *  @pre     __d(__p) must not throw.
71        */
72       _Sp_counted_base_impl(_Ptr __p, _Deleter __d)
73       : _M_ptr(__p), _M_del(__d) { }
74     
75       virtual void
76       _M_dispose() // nothrow
77       { _M_del(_M_ptr); }
78       
79       virtual void*
80       _M_get_deleter(const std::type_info& __ti)
81       { return __ti == typeid(_Deleter) ? &_M_del : 0; }
82       
83     private:
84       _Sp_counted_base_impl(const _Sp_counted_base_impl&);
85       _Sp_counted_base_impl& operator=(const _Sp_counted_base_impl&);
86       
87       _Ptr      _M_ptr;  // copy constructor must not throw
88       _Deleter  _M_del;  // copy constructor must not throw
89     };
90
91   template<_Lock_policy _Lp = __default_lock_policy>
92     class __weak_count;
93
94   template<typename _Tp>
95     struct _Sp_deleter
96     {
97       typedef void result_type;
98       typedef _Tp* argument_type;
99       void operator()(_Tp* __p) const { delete __p; }
100     };
101
102   template<_Lock_policy _Lp = __default_lock_policy>
103     class __shared_count
104     {
105     public: 
106       __shared_count()
107       : _M_pi(0) // nothrow
108       { }
109   
110       template<typename _Ptr>
111         __shared_count(_Ptr __p) : _M_pi(0)
112         {
113           try
114             {
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>());
118             }
119           catch(...)
120             {
121               delete __p;
122               __throw_exception_again;
123             }
124         }
125
126       template<typename _Ptr, typename _Deleter>
127         __shared_count(_Ptr __p, _Deleter __d) : _M_pi(0)
128         {
129           try
130             {
131               _M_pi = new _Sp_counted_base_impl<_Ptr, _Deleter, _Lp>(__p, __d);
132             }
133           catch(...)
134             {
135               __d(__p); // Call _Deleter on __p.
136               __throw_exception_again;
137             }
138         }
139
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(); }
147
148       // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
149       explicit
150       __shared_count(const __weak_count<_Lp>& __r);
151   
152       ~__shared_count() // nothrow
153       {
154         if (_M_pi != 0)
155           _M_pi->_M_release();
156       }
157       
158       __shared_count(const __shared_count& __r)
159       : _M_pi(__r._M_pi) // nothrow
160       {
161         if (_M_pi != 0)
162           _M_pi->_M_add_ref_copy();
163       }
164   
165       __shared_count&
166       operator=(const __shared_count& __r) // nothrow
167       {
168         _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
169         if (__tmp != _M_pi)
170           {
171             if (__tmp != 0)
172               __tmp->_M_add_ref_copy();
173             if (_M_pi != 0)
174               _M_pi->_M_release();
175             _M_pi = __tmp;
176           }
177         return *this;
178       }
179   
180       void
181       _M_swap(__shared_count& __r) // nothrow
182       {
183         _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
184         __r._M_pi = _M_pi;
185         _M_pi = __tmp;
186       }
187   
188       long
189       _M_get_use_count() const // nothrow
190       { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
191
192       bool
193       _M_unique() const // nothrow
194       { return this->_M_get_use_count() == 1; }
195       
196       friend inline bool
197       operator==(const __shared_count& __a, const __shared_count& __b)
198       { return __a._M_pi == __b._M_pi; }
199   
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); }
203   
204       void*
205       _M_get_deleter(const std::type_info& __ti) const
206       { return _M_pi ? _M_pi->_M_get_deleter(__ti) : 0; }
207
208     private:
209       friend class __weak_count<_Lp>;
210
211       _Sp_counted_base<_Lp>*  _M_pi;
212     };
213 }
214 }