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