]> git.lyx.org Git - lyx.git/blob - boost/boost/intrusive_ptr.hpp
boost::filesystem added
[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 #else
132
133     typedef T * (intrusive_ptr::*unspecified_bool_type) () const;
134
135     operator unspecified_bool_type () const
136     {
137         return p_ == 0? 0: &intrusive_ptr::get;
138     }
139
140 #endif
141
142     // operator! is a Borland-specific workaround
143     bool operator! () const
144     {
145         return p_ == 0;
146     }
147
148     void swap(intrusive_ptr & rhs)
149     {
150         T * tmp = p_;
151         p_ = rhs.p_;
152         rhs.p_ = tmp;
153     }
154
155 private:
156
157     T * p_;
158 };
159
160 template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
161 {
162     return a.get() == b.get();
163 }
164
165 template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
166 {
167     return a.get() != b.get();
168 }
169
170 template<class T> inline bool operator==(intrusive_ptr<T> const & a, T * b)
171 {
172     return a.get() == b;
173 }
174
175 template<class T> inline bool operator!=(intrusive_ptr<T> const & a, T * b)
176 {
177     return a.get() != b;
178 }
179
180 template<class T> inline bool operator==(T * a, intrusive_ptr<T> const & b)
181 {
182     return a == b.get();
183 }
184
185 template<class T> inline bool operator!=(T * a, intrusive_ptr<T> const & b)
186 {
187     return a != b.get();
188 }
189
190 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
191
192 // Resolve the ambiguity between our op!= and the one in rel_ops
193
194 template<class T> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
195 {
196     return a.get() != b.get();
197 }
198
199 #endif
200
201 template<class T> inline bool operator<(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
202 {
203     return std::less<T *>()(a.get(), b.get());
204 }
205
206 template<class T> void swap(intrusive_ptr<T> & lhs, intrusive_ptr<T> & rhs)
207 {
208     lhs.swap(rhs);
209 }
210
211 // mem_fn support
212
213 template<class T> T * get_pointer(intrusive_ptr<T> const & p)
214 {
215     return p.get();
216 }
217
218 template<class T, class U> intrusive_ptr<T> static_pointer_cast(intrusive_ptr<U> const & p)
219 {
220     return static_cast<T *>(p.get());
221 }
222
223 template<class T, class U> intrusive_ptr<T> dynamic_pointer_cast(intrusive_ptr<U> const & p)
224 {
225     return dynamic_cast<T *>(p.get());
226 }
227
228 // operator<<
229
230 #if defined(__GNUC__) &&  (__GNUC__ < 3)
231
232 template<class Y> std::ostream & operator<< (std::ostream & os, intrusive_ptr<Y> const & p)
233 {
234     os << p.get();
235     return os;
236 }
237
238 #else
239
240 # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, <= 1200 && __SGI_STL_PORT)
241 // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
242 using std::basic_ostream;
243 template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
244 # else
245 template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
246 # endif 
247 {
248     os << p.get();
249     return os;
250 }
251
252 #endif
253
254 } // namespace boost
255
256 #ifdef BOOST_MSVC
257 # pragma warning(pop)
258 #endif    
259
260 #endif  // #ifndef BOOST_INTRUSIVE_PTR_HPP_INCLUDED