]> git.lyx.org Git - lyx.git/blob - boost/boost/intrusive_ptr.hpp
LFUN_ESCAPE gets ReadOnly (fix bug 1142)
[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 #ifdef BOOST_MSVC  // moved here to work around VC++ compiler crash
18 # pragma warning(push)
19 # pragma warning(disable:4284) // odd return type for operator->
20 #endif
21
22 #include <boost/assert.hpp>
23 #include <boost/detail/workaround.hpp>
24
25 #include <functional>           // for std::less
26 #include <iosfwd>               // for std::basic_ostream
27
28
29 namespace boost
30 {
31
32 //
33 //  intrusive_ptr
34 //
35 //  A smart pointer that uses intrusive reference counting.
36 //
37 //  Relies on unqualified calls to
38 //  
39 //      void intrusive_ptr_add_ref(T * p);
40 //      void intrusive_ptr_release(T * p);
41 //
42 //          (p != 0)
43 //
44 //  The object is responsible for destroying itself.
45 //
46
47 template<class T> class intrusive_ptr
48 {
49 private:
50
51     typedef intrusive_ptr this_type;
52
53 public:
54
55     typedef T element_type;
56
57     intrusive_ptr(): p_(0)
58     {
59     }
60
61     intrusive_ptr(T * p, bool add_ref = true): p_(p)
62     {
63         if(p_ != 0 && add_ref) intrusive_ptr_add_ref(p_);
64     }
65
66 #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES)
67
68     template<class U> intrusive_ptr(intrusive_ptr<U> const & rhs): p_(rhs.get())
69     {
70         if(p_ != 0) intrusive_ptr_add_ref(p_);
71     }
72
73 #endif
74
75     intrusive_ptr(intrusive_ptr const & rhs): p_(rhs.p_)
76     {
77         if(p_ != 0) intrusive_ptr_add_ref(p_);
78     }
79
80     ~intrusive_ptr()
81     {
82         if(p_ != 0) intrusive_ptr_release(p_);
83     }
84
85 #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES)
86
87     template<class U> intrusive_ptr & operator=(intrusive_ptr<U> const & rhs)
88     {
89         this_type(rhs).swap(*this);
90         return *this;
91     }
92
93 #endif
94
95     intrusive_ptr & operator=(intrusive_ptr const & rhs)
96     {
97         this_type(rhs).swap(*this);
98         return *this;
99     }
100
101     intrusive_ptr & operator=(T * rhs)
102     {
103         this_type(rhs).swap(*this);
104         return *this;
105     }
106
107     T * get() const
108     {
109         return p_;
110     }
111
112     T & operator*() const
113     {
114         return *p_;
115     }
116
117     T * operator->() const
118     {
119         return p_;
120     }
121
122     typedef T * (intrusive_ptr::*unspecified_bool_type) () const;
123
124     operator unspecified_bool_type () const
125     {
126         return p_ == 0? 0: &intrusive_ptr::get;
127     }
128
129     // operator! is a Borland-specific workaround
130     bool operator! () const
131     {
132         return p_ == 0;
133     }
134
135     void swap(intrusive_ptr & rhs)
136     {
137         T * tmp = p_;
138         p_ = rhs.p_;
139         rhs.p_ = tmp;
140     }
141
142 private:
143
144     T * p_;
145 };
146
147 template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
148 {
149     return a.get() == b.get();
150 }
151
152 template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
153 {
154     return a.get() != b.get();
155 }
156
157 template<class T> inline bool operator==(intrusive_ptr<T> const & a, T * b)
158 {
159     return a.get() == b;
160 }
161
162 template<class T> inline bool operator!=(intrusive_ptr<T> const & a, T * b)
163 {
164     return a.get() != b;
165 }
166
167 template<class T> inline bool operator==(T * a, intrusive_ptr<T> const & b)
168 {
169     return a == b.get();
170 }
171
172 template<class T> inline bool operator!=(T * a, intrusive_ptr<T> const & b)
173 {
174     return a != b.get();
175 }
176
177 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
178
179 // Resolve the ambiguity between our op!= and the one in rel_ops
180
181 template<class T> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
182 {
183     return a.get() != b.get();
184 }
185
186 #endif
187
188 template<class T> inline bool operator<(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
189 {
190     return std::less<T *>()(a.get(), b.get());
191 }
192
193 template<class T> void swap(intrusive_ptr<T> & lhs, intrusive_ptr<T> & rhs)
194 {
195     lhs.swap(rhs);
196 }
197
198 // mem_fn support
199
200 template<class T> T * get_pointer(intrusive_ptr<T> const & p)
201 {
202     return p.get();
203 }
204
205 template<class T, class U> intrusive_ptr<T> static_pointer_cast(intrusive_ptr<U> const & p)
206 {
207     return static_cast<T *>(p.get());
208 }
209
210 template<class T, class U> intrusive_ptr<T> dynamic_pointer_cast(intrusive_ptr<U> const & p)
211 {
212     return dynamic_cast<T *>(p.get());
213 }
214
215 // operator<<
216
217 #if defined(__GNUC__) &&  (__GNUC__ < 3)
218
219 template<class Y> std::ostream & operator<< (std::ostream & os, intrusive_ptr<Y> const & p)
220 {
221     os << p.get();
222     return os;
223 }
224
225 #else
226
227 # if BOOST_WORKAROUND(BOOST_MSVC, <= 1200 && __SGI_STL_PORT)
228 // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
229 using std::basic_ostream;
230 template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
231 # else
232 template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
233 # endif 
234 {
235     os << p.get();
236     return os;
237 }
238
239 #endif
240
241 } // namespace boost
242
243 #ifdef BOOST_MSVC
244 # pragma warning(pop)
245 #endif    
246
247 #endif  // #ifndef BOOST_INTRUSIVE_PTR_HPP_INCLUDED