]> git.lyx.org Git - lyx.git/blob - boost/boost/shared_ptr.hpp
Small change.
[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 #include <boost/throw_exception.hpp>
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     typedef T value_type;
94
95     shared_ptr(): px(0), pn()
96     {
97     }
98
99     template<typename Y>
100     explicit shared_ptr(Y * p): px(p), pn(p, checked_deleter<Y>(), p) // Y must be complete
101     {
102     }
103
104     //
105     // Requirements: D's copy constructor must not throw
106     //
107     // shared_ptr will release p by calling d(p)
108     //
109
110     template<typename Y, typename D> shared_ptr(Y * p, D d): px(p), pn(p, d)
111     {
112     }
113
114 //  generated copy constructor, assignment, destructor are fine
115
116     template<typename Y>
117     explicit shared_ptr(weak_ptr<Y> const & r): px(r.px), pn(r.pn) // may throw
118     {
119     }
120
121     template<typename Y>
122     shared_ptr(shared_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
123     {
124     }
125
126     template<typename Y>
127     shared_ptr(intrusive_ptr<Y> const & r): px(r.get()), pn(r.get()) // never throws
128     {
129     }
130
131     template<typename Y>
132     shared_ptr(shared_ptr<Y> const & r, detail::static_cast_tag): px(static_cast<element_type *>(r.px)), pn(r.pn)
133     {
134     }
135
136     template<typename Y>
137     shared_ptr(shared_ptr<Y> const & r, detail::dynamic_cast_tag): px(dynamic_cast<element_type *>(r.px)), pn(r.pn)
138     {
139         if (px == 0) // need to allocate new counter -- the cast failed
140         {
141             pn = detail::shared_count();
142         }
143     }
144
145     template<typename Y>
146     shared_ptr(shared_ptr<Y> const & r, detail::polymorphic_cast_tag): px(dynamic_cast<element_type *>(r.px)), pn(r.pn)
147     {
148         if (px == 0)
149         {
150             boost::throw_exception(std::bad_cast());
151         }
152     }
153
154 #ifndef BOOST_NO_AUTO_PTR
155
156     template<typename Y>
157     explicit shared_ptr(std::auto_ptr<Y> & r): px(r.get()), pn(r)
158     {
159     }
160
161 #endif
162
163 #if !defined(BOOST_MSVC) || (BOOST_MSVC > 1200)
164
165     template<typename Y>
166     shared_ptr & operator=(shared_ptr<Y> const & r) // never throws
167     {
168         px = r.px;
169         pn = r.pn; // shared_count::op= doesn't throw
170         return *this;
171     }
172
173 #endif
174
175 #ifndef BOOST_NO_AUTO_PTR
176
177     template<typename Y>
178     shared_ptr & operator=(std::auto_ptr<Y> & r)
179     {
180         this_type(r).swap(*this);
181         return *this;
182     }
183
184 #endif
185
186     void reset()
187     {
188         this_type().swap(*this);
189     }
190
191     template<typename Y> void reset(Y * p) // Y must be complete
192     {
193         BOOST_ASSERT(p == 0 || p != px); // catch self-reset errors
194         this_type(p).swap(*this);
195     }
196
197     template<typename Y, typename D> void reset(Y * p, D d)
198     {
199         this_type(p, d).swap(*this);
200     }
201
202     typename detail::shared_ptr_traits<T>::reference operator* () const // never throws
203     {
204         BOOST_ASSERT(px != 0);
205         return *px;
206     }
207
208     T * operator-> () const // never throws
209     {
210         BOOST_ASSERT(px != 0);
211         return px;
212     }
213     
214     T * get() const // never throws
215     {
216         return px;
217     }
218
219     // implicit conversion to "bool"
220
221     typedef T * (this_type::*unspecified_bool_type)() const;
222
223     operator unspecified_bool_type() const // never throws
224     {
225         return px == 0? 0: &this_type::get;
226     }
227
228     bool operator! () const // never throws
229     {
230         return px == 0;
231     }
232
233     bool unique() const // never throws
234     {
235         return pn.unique();
236     }
237
238     long use_count() const // never throws
239     {
240         return pn.use_count();
241     }
242
243     void swap(shared_ptr<T> & other) // never throws
244     {
245         std::swap(px, other.px);
246         pn.swap(other.pn);
247     }
248
249 // Tasteless as this may seem, making all members public allows member templates
250 // to work in the absence of member template friends. (Matthew Langston)
251
252 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
253
254 private:
255
256     template<typename Y> friend class shared_ptr;
257     template<typename Y> friend class weak_ptr;
258
259
260 #endif
261
262     T * px;                     // contained pointer
263     detail::shared_count pn;    // reference counter
264
265 };  // shared_ptr
266
267 template<typename T, typename U> inline bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b)
268 {
269     return a.get() == b.get();
270 }
271
272 template<typename T, typename U> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<U> const & b)
273 {
274     return a.get() != b.get();
275 }
276
277 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
278
279 // Resolve the ambiguity between our op!= and the one in rel_ops
280
281 template<typename T> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<T> const & b)
282 {
283     return a.get() != b.get();
284 }
285
286 #endif
287
288 template<typename T> inline bool operator<(shared_ptr<T> const & a, shared_ptr<T> const & b)
289 {
290     return std::less<T*>()(a.get(), b.get());
291 }
292
293 template<typename T> inline void swap(shared_ptr<T> & a, shared_ptr<T> & b)
294 {
295     a.swap(b);
296 }
297
298 template<typename T, typename U> shared_ptr<T> shared_static_cast(shared_ptr<U> const & r)
299 {
300     return shared_ptr<T>(r, detail::static_cast_tag());
301 }
302
303 template<typename T, typename U> shared_ptr<T> shared_dynamic_cast(shared_ptr<U> const & r)
304 {
305     return shared_ptr<T>(r, detail::dynamic_cast_tag());
306 }
307
308 template<typename T, typename U> shared_ptr<T> shared_polymorphic_cast(shared_ptr<U> const & r)
309 {
310     return shared_ptr<T>(r, detail::polymorphic_cast_tag());
311 }
312
313 template<typename T, typename U> shared_ptr<T> shared_polymorphic_downcast(shared_ptr<U> const & r)
314 {
315     BOOST_ASSERT(dynamic_cast<T *>(r.get()) == r.get());
316     return shared_static_cast<T>(r);
317 }
318
319 // get_pointer() enables boost::mem_fn to recognize shared_ptr
320
321 template<typename T> inline T * get_pointer(shared_ptr<T> const & p)
322 {
323     return p.get();
324 }
325
326 // shared_from_this() creates a shared_ptr from a raw pointer (usually 'this')
327
328 namespace detail
329 {
330
331 inline void sp_assert_counted_base(boost::counted_base const *)
332 {
333 }
334
335 template<class T> inline T * sp_remove_const(T const * p)
336 {
337     return const_cast<T *>(p);
338 }
339
340 } // namespace detail
341
342 template<class T> shared_ptr<T> shared_from_this(T * p)
343 {
344     detail::sp_assert_counted_base(p);
345     return shared_ptr<T>(detail::sp_remove_const(p));
346 }
347
348 } // namespace boost
349
350 #ifdef BOOST_MSVC
351 # pragma warning(pop)
352 #endif    
353
354 #endif  // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
355
356 #endif  // #ifndef BOOST_SHARED_PTR_HPP_INCLUDED