]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/boost/boost/smart_ptr/intrusive_ptr.hpp
Add subset of boost library headers needed for compilation on PowerPC
[eurobot/public.git] / src / boost / boost / smart_ptr / intrusive_ptr.hpp
1 #ifndef BOOST_SMART_PTR_INTRUSIVE_PTR_HPP_INCLUDED
2 #define BOOST_SMART_PTR_INTRUSIVE_PTR_HPP_INCLUDED
3
4 //
5 //  intrusive_ptr.hpp
6 //
7 //  Copyright (c) 2001, 2002 Peter Dimov
8 //
9 // Distributed under the Boost Software License, Version 1.0. (See
10 // accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt)
12 //
13 //  See http://www.boost.org/libs/smart_ptr/intrusive_ptr.html for documentation.
14 //
15
16 #include <boost/config.hpp>
17
18 #include <boost/assert.hpp>
19 #include <boost/detail/workaround.hpp>
20 #include <boost/smart_ptr/detail/sp_convertible.hpp>
21
22 #include <boost/config/no_tr1/functional.hpp>           // for std::less
23
24 #if !defined(BOOST_NO_IOSTREAM)
25 #if !defined(BOOST_NO_IOSFWD)
26 #include <iosfwd>               // for std::basic_ostream
27 #else
28 #include <ostream>
29 #endif
30 #endif
31
32
33 namespace boost
34 {
35
36 //
37 //  intrusive_ptr
38 //
39 //  A smart pointer that uses intrusive reference counting.
40 //
41 //  Relies on unqualified calls to
42 //  
43 //      void intrusive_ptr_add_ref(T * p);
44 //      void intrusive_ptr_release(T * p);
45 //
46 //          (p != 0)
47 //
48 //  The object is responsible for destroying itself.
49 //
50
51 template<class T> class intrusive_ptr
52 {
53 private:
54
55     typedef intrusive_ptr this_type;
56
57 public:
58
59     typedef T element_type;
60
61     intrusive_ptr(): px( 0 )
62     {
63     }
64
65     intrusive_ptr( T * p, bool add_ref = true ): px( p )
66     {
67         if( px != 0 && add_ref ) intrusive_ptr_add_ref( px );
68     }
69
70 #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES)
71
72     template<class U>
73 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
74
75     intrusive_ptr( intrusive_ptr<U> const & rhs, typename boost::detail::sp_enable_if_convertible<U,T>::type = boost::detail::sp_empty() )
76
77 #else
78
79     intrusive_ptr( intrusive_ptr<U> const & rhs )
80
81 #endif
82     : px( rhs.get() )
83     {
84         if( px != 0 ) intrusive_ptr_add_ref( px );
85     }
86
87 #endif
88
89     intrusive_ptr(intrusive_ptr const & rhs): px( rhs.px )
90     {
91         if( px != 0 ) intrusive_ptr_add_ref( px );
92     }
93
94     ~intrusive_ptr()
95     {
96         if( px != 0 ) intrusive_ptr_release( px );
97     }
98
99 #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES)
100
101     template<class U> intrusive_ptr & operator=(intrusive_ptr<U> const & rhs)
102     {
103         this_type(rhs).swap(*this);
104         return *this;
105     }
106
107 #endif
108
109 // Move support
110
111 #if defined( BOOST_HAS_RVALUE_REFS )
112
113     intrusive_ptr(intrusive_ptr && rhs): px( rhs.px )
114     {
115         rhs.px = 0;
116     }
117
118     intrusive_ptr & operator=(intrusive_ptr && rhs)
119     {
120         this_type( static_cast< intrusive_ptr && >( rhs ) ).swap(*this);
121         return *this;
122     }
123
124 #endif
125
126     intrusive_ptr & operator=(intrusive_ptr const & rhs)
127     {
128         this_type(rhs).swap(*this);
129         return *this;
130     }
131
132     intrusive_ptr & operator=(T * rhs)
133     {
134         this_type(rhs).swap(*this);
135         return *this;
136     }
137
138     void reset()
139     {
140         this_type().swap( *this );
141     }
142
143     void reset( T * rhs )
144     {
145         this_type( rhs ).swap( *this );
146     }
147
148     T * get() const
149     {
150         return px;
151     }
152
153     T & operator*() const
154     {
155         BOOST_ASSERT( px != 0 );
156         return *px;
157     }
158
159     T * operator->() const
160     {
161         BOOST_ASSERT( px != 0 );
162         return px;
163     }
164
165 // implicit conversion to "bool"
166 #include <boost/smart_ptr/detail/operator_bool.hpp>
167
168     void swap(intrusive_ptr & rhs)
169     {
170         T * tmp = px;
171         px = rhs.px;
172         rhs.px = tmp;
173     }
174
175 private:
176
177     T * px;
178 };
179
180 template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
181 {
182     return a.get() == b.get();
183 }
184
185 template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
186 {
187     return a.get() != b.get();
188 }
189
190 template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, U * b)
191 {
192     return a.get() == b;
193 }
194
195 template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, U * b)
196 {
197     return a.get() != b;
198 }
199
200 template<class T, class U> inline bool operator==(T * a, intrusive_ptr<U> const & b)
201 {
202     return a == b.get();
203 }
204
205 template<class T, class U> inline bool operator!=(T * a, intrusive_ptr<U> const & b)
206 {
207     return a != b.get();
208 }
209
210 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
211
212 // Resolve the ambiguity between our op!= and the one in rel_ops
213
214 template<class T> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
215 {
216     return a.get() != b.get();
217 }
218
219 #endif
220
221 template<class T> inline bool operator<(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
222 {
223     return std::less<T *>()(a.get(), b.get());
224 }
225
226 template<class T> void swap(intrusive_ptr<T> & lhs, intrusive_ptr<T> & rhs)
227 {
228     lhs.swap(rhs);
229 }
230
231 // mem_fn support
232
233 template<class T> T * get_pointer(intrusive_ptr<T> const & p)
234 {
235     return p.get();
236 }
237
238 template<class T, class U> intrusive_ptr<T> static_pointer_cast(intrusive_ptr<U> const & p)
239 {
240     return static_cast<T *>(p.get());
241 }
242
243 template<class T, class U> intrusive_ptr<T> const_pointer_cast(intrusive_ptr<U> const & p)
244 {
245     return const_cast<T *>(p.get());
246 }
247
248 template<class T, class U> intrusive_ptr<T> dynamic_pointer_cast(intrusive_ptr<U> const & p)
249 {
250     return dynamic_cast<T *>(p.get());
251 }
252
253 // operator<<
254
255 #if !defined(BOOST_NO_IOSTREAM)
256
257 #if defined(BOOST_NO_TEMPLATED_IOSTREAMS) || ( defined(__GNUC__) &&  (__GNUC__ < 3) )
258
259 template<class Y> std::ostream & operator<< (std::ostream & os, intrusive_ptr<Y> const & p)
260 {
261     os << p.get();
262     return os;
263 }
264
265 #else
266
267 // in STLport's no-iostreams mode no iostream symbols can be used
268 #ifndef _STLP_NO_IOSTREAMS
269
270 # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300 && __SGI_STL_PORT)
271 // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
272 using std::basic_ostream;
273 template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
274 # else
275 template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
276 # endif 
277 {
278     os << p.get();
279     return os;
280 }
281
282 #endif // _STLP_NO_IOSTREAMS
283
284 #endif // __GNUC__ < 3
285
286 #endif // !defined(BOOST_NO_IOSTREAM)
287
288 // hash_value
289
290 template< class T > struct hash;
291
292 template< class T > std::size_t hash_value( boost::intrusive_ptr<T> const & p )
293 {
294     return boost::hash< T* >()( p.get() );
295 }
296
297 } // namespace boost
298
299 #endif  // #ifndef BOOST_SMART_PTR_INTRUSIVE_PTR_HPP_INCLUDED