]> git.lyx.org Git - lyx.git/blob - boost/boost/intrusive_ptr.hpp
7db6dcf2490555c943b33a8c2f17149072b6ced4
[lyx.git] / boost / boost / intrusive_ptr.hpp
1 #ifndef BOOST_INTRUSIVE_PTR_HPP_INCLUDED
2 #define BOOST_INTRUSIVE_PTR_HPP_INCLUDED
3
4 //
5 //  intrusive_ptr.hpp
6 //
7 //  Copyright (c) 2001, 2002 Peter Dimov
8 //
9 //  Permission to copy, use, modify, sell and distribute this software
10 //  is granted provided this copyright notice appears in all copies.
11 //  This software is provided "as is" without express or implied
12 //  warranty, and with no claim as to its suitability for any purpose.
13 //
14 //  See http://www.boost.org/libs/smart_ptr/intrusive_ptr.html for documentation.
15 //
16
17 #include <boost/config.hpp>
18
19 #ifdef BOOST_MSVC  // moved here to work around VC++ compiler crash
20 # pragma warning(push)
21 # pragma warning(disable:4284) // odd return type for operator->
22 #endif
23
24 #include <boost/assert.hpp>
25 #include <boost/detail/workaround.hpp>
26
27 #include <functional>           // for std::less
28 #include <iosfwd>               // for std::basic_ostream
29
30
31 namespace boost
32 {
33
34 //
35 //  intrusive_ptr
36 //
37 //  A smart pointer that uses intrusive reference counting.
38 //
39 //  Relies on unqualified calls to
40 //  
41 //      void intrusive_ptr_add_ref(T * p);
42 //      void intrusive_ptr_release(T * p);
43 //
44 //          (p != 0)
45 //
46 //  The object is responsible for destroying itself.
47 //
48
49 template<class T> class intrusive_ptr
50 {
51 private:
52
53     typedef intrusive_ptr this_type;
54
55 public:
56
57     typedef T element_type;
58
59     intrusive_ptr(): p_(0)
60     {
61     }
62
63     intrusive_ptr(T * p, bool add_ref = true): p_(p)
64     {
65         if(p_ != 0 && add_ref) intrusive_ptr_add_ref(p_);
66     }
67
68 #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES)
69
70     template<class U> intrusive_ptr(intrusive_ptr<U> const & rhs): p_(rhs.get())
71     {
72         if(p_ != 0) intrusive_ptr_add_ref(p_);
73     }
74
75 #endif
76
77     intrusive_ptr(intrusive_ptr const & rhs): p_(rhs.p_)
78     {
79         if(p_ != 0) intrusive_ptr_add_ref(p_);
80     }
81
82     ~intrusive_ptr()
83     {
84         if(p_ != 0) intrusive_ptr_release(p_);
85     }
86
87 #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES)
88
89     template<class U> intrusive_ptr & operator=(intrusive_ptr<U> const & rhs)
90     {
91         this_type(rhs).swap(*this);
92         return *this;
93     }
94
95 #endif
96
97     intrusive_ptr & operator=(intrusive_ptr const & rhs)
98     {
99         this_type(rhs).swap(*this);
100         return *this;
101     }
102
103     intrusive_ptr & operator=(T * rhs)
104     {
105         this_type(rhs).swap(*this);
106         return *this;
107     }
108
109     T * get() const
110     {
111         return p_;
112     }
113
114     T & operator*() const
115     {
116         return *p_;
117     }
118
119     T * operator->() const
120     {
121         return p_;
122     }
123
124 #if defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x530)
125
126     operator bool () const
127     {
128         return p_ != 0;
129     }
130
131 #elif defined(__MWERKS__) && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
132     typedef T * (this_type::*unspecified_bool_type)() const;
133     
134     operator unspecified_bool_type() const // never throws
135     {
136         return p_ == 0? 0: &this_type::get;
137     }
138
139 #else 
140
141     typedef T * this_type::*unspecified_bool_type;
142
143     operator unspecified_bool_type () const
144     {
145         return p_ == 0? 0: &this_type::p_;
146     }
147
148 #endif
149
150     // operator! is a Borland-specific workaround
151     bool operator! () const
152     {
153         return p_ == 0;
154     }
155
156     void swap(intrusive_ptr & rhs)
157     {
158         T * tmp = p_;
159         p_ = rhs.p_;
160         rhs.p_ = tmp;
161     }
162
163 private:
164
165     T * p_;
166 };
167
168 template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
169 {
170     return a.get() == b.get();
171 }
172
173 template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
174 {
175     return a.get() != b.get();
176 }
177
178 template<class T> inline bool operator==(intrusive_ptr<T> const & a, T * b)
179 {
180     return a.get() == b;
181 }
182
183 template<class T> inline bool operator!=(intrusive_ptr<T> const & a, T * b)
184 {
185     return a.get() != b;
186 }
187
188 template<class T> inline bool operator==(T * a, intrusive_ptr<T> const & b)
189 {
190     return a == b.get();
191 }
192
193 template<class T> inline bool operator!=(T * a, intrusive_ptr<T> const & b)
194 {
195     return a != b.get();
196 }
197
198 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
199
200 // Resolve the ambiguity between our op!= and the one in rel_ops
201
202 template<class T> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
203 {
204     return a.get() != b.get();
205 }
206
207 #endif
208
209 template<class T> inline bool operator<(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
210 {
211     return std::less<T *>()(a.get(), b.get());
212 }
213
214 template<class T> void swap(intrusive_ptr<T> & lhs, intrusive_ptr<T> & rhs)
215 {
216     lhs.swap(rhs);
217 }
218
219 // mem_fn support
220
221 template<class T> T * get_pointer(intrusive_ptr<T> const & p)
222 {
223     return p.get();
224 }
225
226 template<class T, class U> intrusive_ptr<T> static_pointer_cast(intrusive_ptr<U> const & p)
227 {
228     return static_cast<T *>(p.get());
229 }
230
231 template<class T, class U> intrusive_ptr<T> const_pointer_cast(intrusive_ptr<U> const & p)
232 {
233     return const_cast<T *>(p.get());
234 }
235
236 template<class T, class U> intrusive_ptr<T> dynamic_pointer_cast(intrusive_ptr<U> const & p)
237 {
238     return dynamic_cast<T *>(p.get());
239 }
240
241 // operator<<
242
243 #if defined(__GNUC__) &&  (__GNUC__ < 3)
244
245 template<class Y> std::ostream & operator<< (std::ostream & os, intrusive_ptr<Y> const & p)
246 {
247     os << p.get();
248     return os;
249 }
250
251 #else
252
253 # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, <= 1200 && __SGI_STL_PORT)
254 // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
255 using std::basic_ostream;
256 template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
257 # else
258 template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
259 # endif 
260 {
261     os << p.get();
262     return os;
263 }
264
265 #endif
266
267 } // namespace boost
268
269 #ifdef BOOST_MSVC
270 # pragma warning(pop)
271 #endif    
272
273 #endif  // #ifndef BOOST_INTRUSIVE_PTR_HPP_INCLUDED