]> git.sesse.net Git - casparcg/blob - dependencies/boost/boost/serialization/detail/shared_ptr_132.hpp
Manually merged pull request #222
[casparcg] / dependencies / boost / boost / serialization / detail / shared_ptr_132.hpp
1 #ifndef BOOST_SHARED_PTR_132_HPP_INCLUDED
2 #define BOOST_SHARED_PTR_132_HPP_INCLUDED
3
4 //
5 //  shared_ptr.hpp
6 //
7 //  (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
8 //  Copyright (c) 2001, 2002, 2003 Peter Dimov
9 //
10 //  Distributed under the Boost Software License, Version 1.0. (See
11 //  accompanying file LICENSE_1_0.txt or copy at
12 //  http://www.boost.org/LICENSE_1_0.txt)
13 //
14 //  See http://www.boost.org/libs/smart_ptr/shared_ptr.htm for documentation.
15 //
16
17 #include <boost/config.hpp>   // for broken compiler workarounds
18
19 #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
20 #include <boost/serialization/detail/shared_ptr_nmt_132.hpp>
21 #else
22
23 #include <boost/assert.hpp>
24 #include <boost/checked_delete.hpp>
25 #include <boost/serialization/throw_exception.hpp>
26 #include <boost/detail/workaround.hpp>
27
28 #include <boost/serialization/access.hpp>
29 #include <boost/serialization/detail/shared_count_132.hpp>
30
31 #include <memory>               // for std::auto_ptr
32 #include <algorithm>            // for std::swap
33 #include <functional>           // for std::less
34 #include <typeinfo>             // for std::bad_cast
35 #include <iosfwd>               // for std::basic_ostream
36
37 #ifdef BOOST_MSVC  // moved here to work around VC++ compiler crash
38 # pragma warning(push)
39 # pragma warning(disable:4284) // odd return type for operator->
40 #endif
41
42 namespace boost_132 {
43
44 template<class T> class weak_ptr;
45 template<class T> class enable_shared_from_this;
46
47 namespace detail
48 {
49
50 struct static_cast_tag {};
51 struct const_cast_tag {};
52 struct dynamic_cast_tag {};
53 struct polymorphic_cast_tag {};
54
55 template<class T> struct shared_ptr_traits
56 {
57     typedef T & reference;
58 };
59
60 template<> struct shared_ptr_traits<void>
61 {
62     typedef void reference;
63 };
64
65 #if !defined(BOOST_NO_CV_VOID_SPECIALIZATIONS)
66
67 template<> struct shared_ptr_traits<void const>
68 {
69     typedef void reference;
70 };
71
72 template<> struct shared_ptr_traits<void volatile>
73 {
74     typedef void reference;
75 };
76
77 template<> struct shared_ptr_traits<void const volatile>
78 {
79     typedef void reference;
80 };
81
82 #endif
83
84 // enable_shared_from_this support
85
86 template<class T, class Y> void sp_enable_shared_from_this( shared_count const & pn, enable_shared_from_this< T > const * pe, Y const * px )
87 {
88     if(pe != 0) pe->_internal_weak_this._internal_assign(const_cast<Y*>(px), pn);
89 }
90
91 inline void sp_enable_shared_from_this( shared_count const & /*pn*/, ... )
92 {
93 }
94
95 } // namespace detail
96
97
98 //
99 //  shared_ptr
100 //
101 //  An enhanced relative of scoped_ptr with reference counted copy semantics.
102 //  The object pointed to is deleted when the last shared_ptr pointing to it
103 //  is destroyed or reset.
104 //
105
106 template<class T> class shared_ptr
107 {
108 private:
109     // Borland 5.5.1 specific workaround
110     typedef shared_ptr< T > this_type;
111
112 public:
113
114     typedef T element_type;
115     typedef T value_type;
116     typedef T * pointer;
117     typedef BOOST_DEDUCED_TYPENAME detail::shared_ptr_traits< T >::reference reference;
118
119     shared_ptr(): px(0), pn() // never throws in 1.30+
120     {
121     }
122
123 #if BOOST_WORKAROUND( __BORLANDC__, BOOST_TESTED_AT( 0x564) )
124     template<class Y>
125     explicit shared_ptr(Y * p): px(p), pn(p, boost::checked_deleter<Y>()) // Y must be complete
126 #else
127     template<class Y>
128     explicit shared_ptr(Y * p): px(p), pn(p, boost::checked_deleter<Y>()) // Y must be complete
129 #endif
130     {
131         detail::sp_enable_shared_from_this( pn, p, p );
132     }
133
134     //
135     // Requirements: D's copy constructor must not throw
136     //
137     // shared_ptr will release p by calling d(p)
138     //
139
140     template<class Y, class D> shared_ptr(Y * p, D d): px(p), pn(p, d)
141     {
142         detail::sp_enable_shared_from_this( pn, p, p );
143     }
144
145 //  generated copy constructor, assignment, destructor are fine...
146
147 //  except that Borland C++ has a bug, and g++ with -Wsynth warns
148 #if defined(__BORLANDC__) || defined(__GNUC__)
149
150     shared_ptr & operator=(shared_ptr const & r) // never throws
151     {
152         px = r.px;
153         pn = r.pn; // shared_count::op= doesn't throw
154         return *this;
155     }
156
157 #endif
158
159     template<class Y>
160     explicit shared_ptr(weak_ptr<Y> const & r): pn(r.pn) // may throw
161     {
162         // it is now safe to copy r.px, as pn(r.pn) did not throw
163         px = r.px;
164     }
165
166     template<class Y>
167     shared_ptr(shared_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
168     {
169     }
170
171     template<class Y>
172     shared_ptr(shared_ptr<Y> const & r, detail::static_cast_tag): px(static_cast<element_type *>(r.px)), pn(r.pn)
173     {
174     }
175
176     template<class Y>
177     shared_ptr(shared_ptr<Y> const & r, detail::const_cast_tag): px(const_cast<element_type *>(r.px)), pn(r.pn)
178     {
179     }
180
181     template<class Y>
182     shared_ptr(shared_ptr<Y> const & r, detail::dynamic_cast_tag): px(dynamic_cast<element_type *>(r.px)), pn(r.pn)
183     {
184         if(px == 0) // need to allocate new counter -- the cast failed
185         {
186             pn = detail::shared_count();
187         }
188     }
189
190     template<class Y>
191     shared_ptr(shared_ptr<Y> const & r, detail::polymorphic_cast_tag): px(dynamic_cast<element_type *>(r.px)), pn(r.pn)
192     {
193         if(px == 0)
194         {
195             boost::serialization::throw_exception(std::bad_cast());
196         }
197     }
198
199 #ifndef BOOST_NO_AUTO_PTR
200
201     template<class Y>
202     explicit shared_ptr(std::auto_ptr<Y> & r): px(r.get()), pn()
203     {
204         Y * tmp = r.get();
205         pn = detail::shared_count(r);
206         detail::sp_enable_shared_from_this( pn, tmp, tmp );
207     }
208
209 #endif
210
211 #if !defined(BOOST_MSVC) || (BOOST_MSVC > 1200)
212
213     template<class Y>
214     shared_ptr & operator=(shared_ptr<Y> const & r) // never throws
215     {
216         px = r.px;
217         pn = r.pn; // shared_count::op= doesn't throw
218         return *this;
219     }
220
221 #endif
222
223 #ifndef BOOST_NO_AUTO_PTR
224
225     template<class Y>
226     shared_ptr & operator=(std::auto_ptr<Y> & r)
227     {
228         this_type(r).swap(*this);
229         return *this;
230     }
231
232 #endif
233
234     void reset() // never throws in 1.30+
235     {
236         this_type().swap(*this);
237     }
238
239     template<class Y> void reset(Y * p) // Y must be complete
240     {
241         BOOST_ASSERT(p == 0 || p != px); // catch self-reset errors
242         this_type(p).swap(*this);
243     }
244
245     template<class Y, class D> void reset(Y * p, D d)
246     {
247         this_type(p, d).swap(*this);
248     }
249
250     reference operator* () const // never throws
251     {
252         BOOST_ASSERT(px != 0);
253         return *px;
254     }
255
256     T * operator-> () const // never throws
257     {
258         BOOST_ASSERT(px != 0);
259         return px;
260     }
261     
262     T * get() const // never throws
263     {
264         return px;
265     }
266
267     // implicit conversion to "bool"
268
269 #if defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x530)
270
271     operator bool () const
272     {
273         return px != 0;
274     }
275
276 #elif defined(__MWERKS__) && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
277     typedef T * (this_type::*unspecified_bool_type)() const;
278     
279     operator unspecified_bool_type() const // never throws
280     {
281         return px == 0? 0: &this_type::get;
282     }
283
284 #else 
285
286     typedef T * this_type::*unspecified_bool_type;
287
288     operator unspecified_bool_type() const // never throws
289     {
290         return px == 0? 0: &this_type::px;
291     }
292
293 #endif
294
295     // operator! is redundant, but some compilers need it
296
297     bool operator! () const // never throws
298     {
299         return px == 0;
300     }
301
302     bool unique() const // never throws
303     {
304         return pn.unique();
305     }
306
307     long use_count() const // never throws
308     {
309         return pn.use_count();
310     }
311
312     void swap(shared_ptr< T > & other) // never throws
313     {
314         std::swap(px, other.px);
315         pn.swap(other.pn);
316     }
317
318     template<class Y> bool _internal_less(shared_ptr<Y> const & rhs) const
319     {
320         return pn < rhs.pn;
321     }
322
323     void * _internal_get_deleter(std::type_info const & ti) const
324     {
325         return pn.get_deleter(ti);
326     }
327
328 // Tasteless as this may seem, making all members public allows member templates
329 // to work in the absence of member template friends. (Matthew Langston)
330
331 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
332
333 private:
334
335     template<class Y> friend class shared_ptr;
336     template<class Y> friend class weak_ptr;
337
338
339 #endif
340 public: // for serialization
341     T * px;                     // contained pointer
342     detail::shared_count pn;    // reference counter
343
344 };  // shared_ptr
345
346 template<class T, class U> inline bool operator==(shared_ptr< T > const & a, shared_ptr<U> const & b)
347 {
348     return a.get() == b.get();
349 }
350
351 template<class T, class U> inline bool operator!=(shared_ptr< T > const & a, shared_ptr<U> const & b)
352 {
353     return a.get() != b.get();
354 }
355
356 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
357
358 // Resolve the ambiguity between our op!= and the one in rel_ops
359
360 template<class T> inline bool operator!=(shared_ptr< T > const & a, shared_ptr< T > const & b)
361 {
362     return a.get() != b.get();
363 }
364
365 #endif
366
367 template<class T, class U> inline bool operator<(shared_ptr< T > const & a, shared_ptr<U> const & b)
368 {
369     return a._internal_less(b);
370 }
371
372 template<class T> inline void swap(shared_ptr< T > & a, shared_ptr< T > & b)
373 {
374     a.swap(b);
375 }
376
377 template<class T, class U> shared_ptr< T > static_pointer_cast(shared_ptr<U> const & r)
378 {
379     return shared_ptr< T >(r, detail::static_cast_tag());
380 }
381
382 template<class T, class U> shared_ptr< T > const_pointer_cast(shared_ptr<U> const & r)
383 {
384     return shared_ptr< T >(r, detail::const_cast_tag());
385 }
386
387 template<class T, class U> shared_ptr< T > dynamic_pointer_cast(shared_ptr<U> const & r)
388 {
389     return shared_ptr< T >(r, detail::dynamic_cast_tag());
390 }
391
392 // shared_*_cast names are deprecated. Use *_pointer_cast instead.
393
394 template<class T, class U> shared_ptr< T > shared_static_cast(shared_ptr<U> const & r)
395 {
396     return shared_ptr< T >(r, detail::static_cast_tag());
397 }
398
399 template<class T, class U> shared_ptr< T > shared_dynamic_cast(shared_ptr<U> const & r)
400 {
401     return shared_ptr< T >(r, detail::dynamic_cast_tag());
402 }
403
404 template<class T, class U> shared_ptr< T > shared_polymorphic_cast(shared_ptr<U> const & r)
405 {
406     return shared_ptr< T >(r, detail::polymorphic_cast_tag());
407 }
408
409 template<class T, class U> shared_ptr< T > shared_polymorphic_downcast(shared_ptr<U> const & r)
410 {
411     BOOST_ASSERT(dynamic_cast<T *>(r.get()) == r.get());
412     return shared_static_cast< T >(r);
413 }
414
415 // get_pointer() enables boost::mem_fn to recognize shared_ptr
416
417 template<class T> inline T * get_pointer(shared_ptr< T > const & p)
418 {
419     return p.get();
420 }
421
422 // operator<<
423
424 #if defined(__GNUC__) &&  (__GNUC__ < 3)
425
426 template<class Y> std::ostream & operator<< (std::ostream & os, shared_ptr<Y> const & p)
427 {
428     os << p.get();
429     return os;
430 }
431
432 #else
433
434 # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, <= 1200 && __SGI_STL_PORT)
435 // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
436 using std::basic_ostream;
437 template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, shared_ptr<Y> const & p)
438 # else
439 template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, shared_ptr<Y> const & p)
440 # endif 
441 {
442     os << p.get();
443     return os;
444 }
445
446 #endif
447
448 // get_deleter (experimental)
449
450 #if (defined(__GNUC__) &&  (__GNUC__ < 3)) || (defined(__EDG_VERSION__) && (__EDG_VERSION__ <= 238))
451
452 // g++ 2.9x doesn't allow static_cast<X const *>(void *)
453 // apparently EDG 2.38 also doesn't accept it
454
455 template<class D, class T> D * get_deleter(shared_ptr< T > const & p)
456 {
457     void const * q = p._internal_get_deleter(typeid(D));
458     return const_cast<D *>(static_cast<D const *>(q));
459 }
460
461 #else
462
463 template<class D, class T> D * get_deleter(shared_ptr< T > const & p)
464 {
465     return static_cast<D *>(p._internal_get_deleter(typeid(D)));
466 }
467
468 #endif
469
470 } // namespace boost
471
472 #ifdef BOOST_MSVC
473 # pragma warning(pop)
474 #endif    
475
476 #endif  // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
477
478 #endif  // #ifndef BOOST_SHARED_PTR_132_HPP_INCLUDED