]> git.lyx.org Git - lyx.git/blob - boost/boost/shared_ptr.hpp
update boost
[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 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
27 #include <boost/detail/shared_count.hpp>
28
29 #include <memory>             // for std::auto_ptr
30 #include <algorithm>          // for std::swap
31 #include <functional>         // for std::less
32 #include <typeinfo>           // for std::bad_cast
33
34 #ifdef BOOST_MSVC  // moved here to work around VC++ compiler crash
35 # pragma warning(push)
36 # pragma warning(disable:4284) // odd return type for operator->
37 #endif
38
39 namespace boost
40 {
41
42 namespace detail
43 {
44
45 struct static_cast_tag {};
46 struct dynamic_cast_tag {};
47 struct polymorphic_cast_tag {};
48
49 template<typename T> struct shared_ptr_traits
50 {
51     typedef T & reference;
52 };
53
54 template<> struct shared_ptr_traits<void>
55 {
56     typedef void reference;
57 };
58
59 #if !defined(BOOST_NO_CV_VOID_SPECIALIZATIONS)
60
61 template<> struct shared_ptr_traits<void const>
62 {
63     typedef void reference;
64 };
65
66 #endif
67
68 } // namespace detail
69
70
71 //
72 //  shared_ptr
73 //
74 //  An enhanced relative of scoped_ptr with reference counted copy semantics.
75 //  The object pointed to is deleted when the last shared_ptr pointing to it
76 //  is destroyed or reset.
77 //
78
79 template<typename T> class weak_ptr;
80 template<typename T> class intrusive_ptr;
81
82 template<typename T> class shared_ptr
83 {
84 private:
85
86     // Borland 5.5.1 specific workarounds
87 //  typedef checked_deleter<T> deleter;
88     typedef shared_ptr<T> this_type;
89
90 public:
91
92     typedef T element_type;
93
94     shared_ptr(): px(0), pn()
95     {
96     }
97
98     template<typename Y>
99     explicit shared_ptr(Y * p): px(p), pn(p, checked_deleter<Y>(), p) // Y must be complete
100     {
101     }
102
103     //
104     // Requirements: D's copy constructor must not throw
105     //
106     // shared_ptr will release p by calling d(p)
107     //
108
109     template<typename Y, typename D> shared_ptr(Y * p, D d): px(p), pn(p, d)
110     {
111     }
112
113 //  generated copy constructor, assignment, destructor are fine
114
115     template<typename Y>
116     explicit shared_ptr(weak_ptr<Y> const & r): px(r.px), pn(r.pn) // may throw
117     {
118     }
119
120     template<typename Y>
121     shared_ptr(shared_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
122     {
123     }
124
125     template<typename Y>
126     shared_ptr(intrusive_ptr<Y> const & r): px(r.get()), pn(r.get()) // never throws
127     {
128     }
129
130     template<typename Y>
131     shared_ptr(shared_ptr<Y> const & r, detail::static_cast_tag): px(static_cast<element_type *>(r.px)), pn(r.pn)
132     {
133     }
134
135     template<typename Y>
136     shared_ptr(shared_ptr<Y> const & r, detail::dynamic_cast_tag): px(dynamic_cast<element_type *>(r.px)), pn(r.pn)
137     {
138         if (px == 0) // need to allocate new counter -- the cast failed
139         {
140             pn = detail::shared_count();
141         }
142     }
143
144     template<typename Y>
145     shared_ptr(shared_ptr<Y> const & r, detail::polymorphic_cast_tag): px(dynamic_cast<element_type *>(r.px)), pn(r.pn)
146     {
147         if (px == 0)
148         {
149             throw std::bad_cast();
150         }
151     }
152
153 #ifndef BOOST_NO_AUTO_PTR
154
155     template<typename Y>
156     explicit shared_ptr(std::auto_ptr<Y> & r): px(r.get()), pn(r)
157     {
158     }
159
160 #endif
161
162 #if !defined(BOOST_MSVC) || (BOOST_MSVC > 1200)
163
164     template<typename Y>
165     shared_ptr & operator=(shared_ptr<Y> const & r) // never throws
166     {
167         px = r.px;
168         pn = r.pn; // shared_count::op= doesn't throw
169         return *this;
170     }
171
172 #endif
173
174 #ifndef BOOST_NO_AUTO_PTR
175
176     template<typename Y>
177     shared_ptr & operator=(std::auto_ptr<Y> & r)
178     {
179         this_type(r).swap(*this);
180         return *this;
181     }
182
183 #endif
184
185     void reset()
186     {
187         this_type().swap(*this);
188     }
189
190     template<typename Y> void reset(Y * p) // Y must be complete
191     {
192         BOOST_ASSERT(p == 0 || p != px); // catch self-reset errors
193         this_type(p).swap(*this);
194     }
195
196     template<typename Y, typename D> void reset(Y * p, D d)
197     {
198         this_type(p, d).swap(*this);
199     }
200
201     typename detail::shared_ptr_traits<T>::reference operator* () const // never throws
202     {
203         BOOST_ASSERT(px != 0);
204         return *px;
205     }
206
207     T * operator-> () const // never throws
208     {
209         BOOST_ASSERT(px != 0);
210         return px;
211     }
212
213     T * get() const // never throws
214     {
215         return px;
216     }
217
218     // implicit conversion to "bool"
219
220     typedef T * (this_type::*unspecified_bool_type)() const;
221
222     operator unspecified_bool_type() const // never throws
223     {
224         return px == 0? 0: &this_type::get;
225     }
226
227     bool operator! () const // never throws
228     {
229         return px == 0;
230     }
231
232     bool unique() const // never throws
233     {
234         return pn.unique();
235     }
236
237     long use_count() const // never throws
238     {
239         return pn.use_count();
240     }
241
242     void swap(shared_ptr<T> & other) // never throws
243     {
244         std::swap(px, other.px);
245         pn.swap(other.pn);
246     }
247
248 // Tasteless as this may seem, making all members public allows member templates
249 // to work in the absence of member template friends. (Matthew Langston)
250
251 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
252
253 private:
254
255     template<typename Y> friend class shared_ptr;
256     template<typename Y> friend class weak_ptr;
257
258
259 #endif
260
261     T * px;                     // contained pointer
262     detail::shared_count pn;    // reference counter
263
264 };  // shared_ptr
265
266 template<typename T, typename U> inline bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b)
267 {
268     return a.get() == b.get();
269 }
270
271 template<typename T, typename U> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<U> const & b)
272 {
273     return a.get() != b.get();
274 }
275
276 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
277
278 // Resolve the ambiguity between our op!= and the one in rel_ops
279
280 template<typename T> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<T> const & b)
281 {
282     return a.get() != b.get();
283 }
284
285 #endif
286
287 template<typename T> inline bool operator<(shared_ptr<T> const & a, shared_ptr<T> const & b)
288 {
289     return std::less<T*>()(a.get(), b.get());
290 }
291
292 template<typename T> inline void swap(shared_ptr<T> & a, shared_ptr<T> & b)
293 {
294     a.swap(b);
295 }
296
297 template<typename T, typename U> shared_ptr<T> shared_static_cast(shared_ptr<U> const & r)
298 {
299     return shared_ptr<T>(r, detail::static_cast_tag());
300 }
301
302 template<typename T, typename U> shared_ptr<T> shared_dynamic_cast(shared_ptr<U> const & r)
303 {
304     return shared_ptr<T>(r, detail::dynamic_cast_tag());
305 }
306
307 template<typename T, typename U> shared_ptr<T> shared_polymorphic_cast(shared_ptr<U> const & r)
308 {
309     return shared_ptr<T>(r, detail::polymorphic_cast_tag());
310 }
311
312 template<typename T, typename U> shared_ptr<T> shared_polymorphic_downcast(shared_ptr<U> const & r)
313 {
314     BOOST_ASSERT(dynamic_cast<T *>(r.get()) == r.get());
315     return shared_static_cast<T>(r);
316 }
317
318 // get_pointer() enables boost::mem_fn to recognize shared_ptr
319
320 template<typename T> inline T * get_pointer(shared_ptr<T> const & p)
321 {
322     return p.get();
323 }
324
325 // shared_from_this() creates a shared_ptr from a raw pointer (usually 'this')
326
327 namespace detail
328 {
329
330 inline void sp_assert_counted_base(boost::counted_base const *)
331 {
332 }
333
334 template<class T> inline T * sp_remove_const(T const * p)
335 {
336     return const_cast<T *>(p);
337 }
338
339 } // namespace detail
340
341 template<class T> shared_ptr<T> shared_from_this(T * p)
342 {
343     detail::sp_assert_counted_base(p);
344     return shared_ptr<T>(detail::sp_remove_const(p));
345 }
346
347 } // namespace boost
348
349 #ifdef BOOST_MSVC
350 # pragma warning(pop)
351 #endif
352
353 #endif  // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
354
355 #endif  // #ifndef BOOST_SHARED_PTR_HPP_INCLUDED