]> AND Private Git Repository - canny.git/blob - stc/exp/ml_stc_linux_make_v1.0/include/boost/smart_ptr/detail/shared_count.hpp
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
ce1beb0c98a677a564e3de1196b84218a6a6421d
[canny.git] / stc / exp / ml_stc_linux_make_v1.0 / include / boost / smart_ptr / detail / shared_count.hpp
1 #ifndef BOOST_SMART_PTR_DETAIL_SHARED_COUNT_HPP_INCLUDED\r
2 #define BOOST_SMART_PTR_DETAIL_SHARED_COUNT_HPP_INCLUDED\r
3 \r
4 // MS compatible compilers support #pragma once\r
5 \r
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)\r
7 # pragma once\r
8 #endif\r
9 \r
10 //\r
11 //  detail/shared_count.hpp\r
12 //\r
13 //  Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.\r
14 //  Copyright 2004-2005 Peter Dimov\r
15 //\r
16 // Distributed under the Boost Software License, Version 1.0. (See\r
17 // accompanying file LICENSE_1_0.txt or copy at\r
18 // http://www.boost.org/LICENSE_1_0.txt)\r
19 //\r
20 \r
21 #ifdef __BORLANDC__\r
22 # pragma warn -8027     // Functions containing try are not expanded inline\r
23 #endif\r
24 \r
25 #include <boost/config.hpp>\r
26 #include <boost/checked_delete.hpp>\r
27 #include <boost/throw_exception.hpp>\r
28 #include <boost/smart_ptr/bad_weak_ptr.hpp>\r
29 #include <boost/smart_ptr/detail/sp_counted_base.hpp>\r
30 #include <boost/smart_ptr/detail/sp_counted_impl.hpp>\r
31 #include <boost/detail/workaround.hpp>\r
32 // In order to avoid circular dependencies with Boost.TR1\r
33 // we make sure that our include of <memory> doesn't try to\r
34 // pull in the TR1 headers: that's why we use this header \r
35 // rather than including <memory> directly:\r
36 #include <boost/config/no_tr1/memory.hpp>  // std::auto_ptr\r
37 #include <functional>       // std::less\r
38 #include <new>              // std::bad_alloc\r
39 \r
40 namespace boost\r
41 {\r
42 \r
43 namespace detail\r
44 {\r
45 \r
46 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)\r
47 \r
48 int const shared_count_id = 0x2C35F101;\r
49 int const   weak_count_id = 0x298C38A4;\r
50 \r
51 #endif\r
52 \r
53 struct sp_nothrow_tag {};\r
54 \r
55 class weak_count;\r
56 \r
57 class shared_count\r
58 {\r
59 private:\r
60 \r
61     sp_counted_base * pi_;\r
62 \r
63 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)\r
64     int id_;\r
65 #endif\r
66 \r
67     friend class weak_count;\r
68 \r
69 public:\r
70 \r
71     shared_count(): pi_(0) // nothrow\r
72 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)\r
73         , id_(shared_count_id)\r
74 #endif\r
75     {\r
76     }\r
77 \r
78     template<class Y> explicit shared_count( Y * p ): pi_( 0 )\r
79 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)\r
80         , id_(shared_count_id)\r
81 #endif\r
82     {\r
83 #ifndef BOOST_NO_EXCEPTIONS\r
84 \r
85         try\r
86         {\r
87             pi_ = new sp_counted_impl_p<Y>( p );\r
88         }\r
89         catch(...)\r
90         {\r
91             boost::checked_delete( p );\r
92             throw;\r
93         }\r
94 \r
95 #else\r
96 \r
97         pi_ = new sp_counted_impl_p<Y>( p );\r
98 \r
99         if( pi_ == 0 )\r
100         {\r
101             boost::checked_delete( p );\r
102             boost::throw_exception( std::bad_alloc() );\r
103         }\r
104 \r
105 #endif\r
106     }\r
107 \r
108 #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, <= 1200 )\r
109     template<class Y, class D> shared_count( Y * p, D d ): pi_(0)\r
110 #else\r
111     template<class P, class D> shared_count( P p, D d ): pi_(0)\r
112 #endif\r
113 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)\r
114         , id_(shared_count_id)\r
115 #endif\r
116     {\r
117 #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, <= 1200 )\r
118         typedef Y* P;\r
119 #endif\r
120 #ifndef BOOST_NO_EXCEPTIONS\r
121 \r
122         try\r
123         {\r
124             pi_ = new sp_counted_impl_pd<P, D>(p, d);\r
125         }\r
126         catch(...)\r
127         {\r
128             d(p); // delete p\r
129             throw;\r
130         }\r
131 \r
132 #else\r
133 \r
134         pi_ = new sp_counted_impl_pd<P, D>(p, d);\r
135 \r
136         if(pi_ == 0)\r
137         {\r
138             d(p); // delete p\r
139             boost::throw_exception(std::bad_alloc());\r
140         }\r
141 \r
142 #endif\r
143     }\r
144 \r
145     template<class P, class D, class A> shared_count( P p, D d, A a ): pi_( 0 )\r
146 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)\r
147         , id_(shared_count_id)\r
148 #endif\r
149     {\r
150         typedef sp_counted_impl_pda<P, D, A> impl_type;\r
151         typedef typename A::template rebind< impl_type >::other A2;\r
152 \r
153         A2 a2( a );\r
154 \r
155 #ifndef BOOST_NO_EXCEPTIONS\r
156 \r
157         try\r
158         {\r
159             pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) );\r
160             new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );\r
161         }\r
162         catch(...)\r
163         {\r
164             d( p );\r
165 \r
166             if( pi_ != 0 )\r
167             {\r
168                 a2.deallocate( static_cast< impl_type* >( pi_ ), 1 );\r
169             }\r
170 \r
171             throw;\r
172         }\r
173 \r
174 #else\r
175 \r
176         pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) );\r
177 \r
178         if( pi_ != 0 )\r
179         {\r
180             new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );\r
181         }\r
182         else\r
183         {\r
184             d( p );\r
185             boost::throw_exception( std::bad_alloc() );\r
186         }\r
187 \r
188 #endif\r
189     }\r
190 \r
191 #ifndef BOOST_NO_AUTO_PTR\r
192 \r
193     // auto_ptr<Y> is special cased to provide the strong guarantee\r
194 \r
195     template<class Y>\r
196     explicit shared_count( std::auto_ptr<Y> & r ): pi_( new sp_counted_impl_p<Y>( r.get() ) )\r
197 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)\r
198         , id_(shared_count_id)\r
199 #endif\r
200     {\r
201 #ifdef BOOST_NO_EXCEPTIONS\r
202 \r
203         if( pi_ == 0 )\r
204         {\r
205             boost::throw_exception(std::bad_alloc());\r
206         }\r
207 \r
208 #endif\r
209 \r
210         r.release();\r
211     }\r
212 \r
213 #endif \r
214 \r
215     ~shared_count() // nothrow\r
216     {\r
217         if( pi_ != 0 ) pi_->release();\r
218 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)\r
219         id_ = 0;\r
220 #endif\r
221     }\r
222 \r
223     shared_count(shared_count const & r): pi_(r.pi_) // nothrow\r
224 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)\r
225         , id_(shared_count_id)\r
226 #endif\r
227     {\r
228         if( pi_ != 0 ) pi_->add_ref_copy();\r
229     }\r
230 \r
231 #if defined( BOOST_HAS_RVALUE_REFS )\r
232 \r
233     shared_count(shared_count && r): pi_(r.pi_) // nothrow\r
234 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)\r
235         , id_(shared_count_id)\r
236 #endif\r
237     {\r
238         r.pi_ = 0;\r
239     }\r
240 \r
241 #endif\r
242 \r
243     explicit shared_count(weak_count const & r); // throws bad_weak_ptr when r.use_count() == 0\r
244     shared_count( weak_count const & r, sp_nothrow_tag ); // constructs an empty *this when r.use_count() == 0\r
245 \r
246     shared_count & operator= (shared_count const & r) // nothrow\r
247     {\r
248         sp_counted_base * tmp = r.pi_;\r
249 \r
250         if( tmp != pi_ )\r
251         {\r
252             if( tmp != 0 ) tmp->add_ref_copy();\r
253             if( pi_ != 0 ) pi_->release();\r
254             pi_ = tmp;\r
255         }\r
256 \r
257         return *this;\r
258     }\r
259 \r
260     void swap(shared_count & r) // nothrow\r
261     {\r
262         sp_counted_base * tmp = r.pi_;\r
263         r.pi_ = pi_;\r
264         pi_ = tmp;\r
265     }\r
266 \r
267     long use_count() const // nothrow\r
268     {\r
269         return pi_ != 0? pi_->use_count(): 0;\r
270     }\r
271 \r
272     bool unique() const // nothrow\r
273     {\r
274         return use_count() == 1;\r
275     }\r
276 \r
277     bool empty() const // nothrow\r
278     {\r
279         return pi_ == 0;\r
280     }\r
281 \r
282     friend inline bool operator==(shared_count const & a, shared_count const & b)\r
283     {\r
284         return a.pi_ == b.pi_;\r
285     }\r
286 \r
287     friend inline bool operator<(shared_count const & a, shared_count const & b)\r
288     {\r
289         return std::less<sp_counted_base *>()( a.pi_, b.pi_ );\r
290     }\r
291 \r
292     void * get_deleter( sp_typeinfo const & ti ) const\r
293     {\r
294         return pi_? pi_->get_deleter( ti ): 0;\r
295     }\r
296 };\r
297 \r
298 \r
299 class weak_count\r
300 {\r
301 private:\r
302 \r
303     sp_counted_base * pi_;\r
304 \r
305 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)\r
306     int id_;\r
307 #endif\r
308 \r
309     friend class shared_count;\r
310 \r
311 public:\r
312 \r
313     weak_count(): pi_(0) // nothrow\r
314 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)\r
315         , id_(weak_count_id)\r
316 #endif\r
317     {\r
318     }\r
319 \r
320     weak_count(shared_count const & r): pi_(r.pi_) // nothrow\r
321 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)\r
322         , id_(weak_count_id)\r
323 #endif\r
324     {\r
325         if(pi_ != 0) pi_->weak_add_ref();\r
326     }\r
327 \r
328     weak_count(weak_count const & r): pi_(r.pi_) // nothrow\r
329 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)\r
330         , id_(weak_count_id)\r
331 #endif\r
332     {\r
333         if(pi_ != 0) pi_->weak_add_ref();\r
334     }\r
335 \r
336 // Move support\r
337 \r
338 #if defined( BOOST_HAS_RVALUE_REFS )\r
339 \r
340     weak_count(weak_count && r): pi_(r.pi_) // nothrow\r
341 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)\r
342         , id_(weak_count_id)\r
343 #endif\r
344     {\r
345         r.pi_ = 0;\r
346     }\r
347 \r
348 #endif\r
349 \r
350     ~weak_count() // nothrow\r
351     {\r
352         if(pi_ != 0) pi_->weak_release();\r
353 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)\r
354         id_ = 0;\r
355 #endif\r
356     }\r
357 \r
358     weak_count & operator= (shared_count const & r) // nothrow\r
359     {\r
360         sp_counted_base * tmp = r.pi_;\r
361 \r
362         if( tmp != pi_ )\r
363         {\r
364             if(tmp != 0) tmp->weak_add_ref();\r
365             if(pi_ != 0) pi_->weak_release();\r
366             pi_ = tmp;\r
367         }\r
368 \r
369         return *this;\r
370     }\r
371 \r
372     weak_count & operator= (weak_count const & r) // nothrow\r
373     {\r
374         sp_counted_base * tmp = r.pi_;\r
375 \r
376         if( tmp != pi_ )\r
377         {\r
378             if(tmp != 0) tmp->weak_add_ref();\r
379             if(pi_ != 0) pi_->weak_release();\r
380             pi_ = tmp;\r
381         }\r
382 \r
383         return *this;\r
384     }\r
385 \r
386     void swap(weak_count & r) // nothrow\r
387     {\r
388         sp_counted_base * tmp = r.pi_;\r
389         r.pi_ = pi_;\r
390         pi_ = tmp;\r
391     }\r
392 \r
393     long use_count() const // nothrow\r
394     {\r
395         return pi_ != 0? pi_->use_count(): 0;\r
396     }\r
397 \r
398     bool empty() const // nothrow\r
399     {\r
400         return pi_ == 0;\r
401     }\r
402 \r
403     friend inline bool operator==(weak_count const & a, weak_count const & b)\r
404     {\r
405         return a.pi_ == b.pi_;\r
406     }\r
407 \r
408     friend inline bool operator<(weak_count const & a, weak_count const & b)\r
409     {\r
410         return std::less<sp_counted_base *>()(a.pi_, b.pi_);\r
411     }\r
412 };\r
413 \r
414 inline shared_count::shared_count( weak_count const & r ): pi_( r.pi_ )\r
415 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)\r
416         , id_(shared_count_id)\r
417 #endif\r
418 {\r
419     if( pi_ == 0 || !pi_->add_ref_lock() )\r
420     {\r
421         boost::throw_exception( boost::bad_weak_ptr() );\r
422     }\r
423 }\r
424 \r
425 inline shared_count::shared_count( weak_count const & r, sp_nothrow_tag ): pi_( r.pi_ )\r
426 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)\r
427         , id_(shared_count_id)\r
428 #endif\r
429 {\r
430     if( pi_ != 0 && !pi_->add_ref_lock() )\r
431     {\r
432         pi_ = 0;\r
433     }\r
434 }\r
435 \r
436 } // namespace detail\r
437 \r
438 } // namespace boost\r
439 \r
440 #ifdef __BORLANDC__\r
441 # pragma warn .8027     // Functions containing try are not expanded inline\r
442 #endif\r
443 \r
444 #endif  // #ifndef BOOST_SMART_PTR_DETAIL_SHARED_COUNT_HPP_INCLUDED\r