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