Chris@16
|
1 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
2 //
|
Chris@16
|
3 // This file is the adaptation for Interprocess of boost/intrusive_ptr.hpp
|
Chris@16
|
4 //
|
Chris@16
|
5 // (C) Copyright Peter Dimov 2001, 2002
|
Chris@16
|
6 // (C) Copyright Ion Gaztanaga 2006-2012. Distributed under the Boost
|
Chris@16
|
7 // Software License, Version 1.0. (See accompanying file
|
Chris@16
|
8 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 //
|
Chris@16
|
10 // See http://www.boost.org/libs/interprocess for documentation.
|
Chris@16
|
11 //
|
Chris@16
|
12 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
13
|
Chris@16
|
14 #ifndef BOOST_INTERPROCESS_INTRUSIVE_PTR_HPP_INCLUDED
|
Chris@16
|
15 #define BOOST_INTERPROCESS_INTRUSIVE_PTR_HPP_INCLUDED
|
Chris@16
|
16
|
Chris@101
|
17 #ifndef BOOST_CONFIG_HPP
|
Chris@101
|
18 # include <boost/config.hpp>
|
Chris@101
|
19 #endif
|
Chris@101
|
20 #
|
Chris@101
|
21 #if defined(BOOST_HAS_PRAGMA_ONCE)
|
Chris@101
|
22 # pragma once
|
Chris@101
|
23 #endif
|
Chris@101
|
24
|
Chris@16
|
25 //!\file
|
Chris@16
|
26 //!Describes an intrusive ownership pointer.
|
Chris@16
|
27
|
Chris@16
|
28 #include <boost/interprocess/detail/config_begin.hpp>
|
Chris@16
|
29 #include <boost/interprocess/detail/workaround.hpp>
|
Chris@16
|
30
|
Chris@16
|
31 #include <boost/assert.hpp>
|
Chris@16
|
32 #include <boost/interprocess/detail/utilities.hpp>
|
Chris@16
|
33 #include <boost/intrusive/pointer_traits.hpp>
|
Chris@101
|
34 #include <boost/move/adl_move_swap.hpp>
|
Chris@16
|
35
|
Chris@16
|
36 #include <iosfwd> // for std::basic_ostream
|
Chris@16
|
37
|
Chris@101
|
38 #include <boost/intrusive/detail/minimal_less_equal_header.hpp> //std::less
|
Chris@16
|
39
|
Chris@16
|
40 namespace boost {
|
Chris@16
|
41 namespace interprocess {
|
Chris@16
|
42
|
Chris@16
|
43 //!The intrusive_ptr class template stores a pointer to an object
|
Chris@16
|
44 //!with an embedded reference count. intrusive_ptr is parameterized on
|
Chris@16
|
45 //!T (the type of the object pointed to) and VoidPointer(a void pointer type
|
Chris@16
|
46 //!that defines the type of pointer that intrusive_ptr will store).
|
Chris@16
|
47 //!intrusive_ptr<T, void *> defines a class with a T* member whereas
|
Chris@16
|
48 //!intrusive_ptr<T, offset_ptr<void> > defines a class with a offset_ptr<T> member.
|
Chris@16
|
49 //!Relies on unqualified calls to:
|
Chris@16
|
50 //!
|
Chris@16
|
51 //! void intrusive_ptr_add_ref(T * p);
|
Chris@16
|
52 //! void intrusive_ptr_release(T * p);
|
Chris@16
|
53 //!
|
Chris@16
|
54 //! with (p != 0)
|
Chris@16
|
55 //!
|
Chris@16
|
56 //!The object is responsible for destroying itself.
|
Chris@16
|
57 template<class T, class VoidPointer>
|
Chris@16
|
58 class intrusive_ptr
|
Chris@16
|
59 {
|
Chris@16
|
60 public:
|
Chris@16
|
61 //!Provides the type of the internal stored pointer.
|
Chris@16
|
62 typedef typename boost::intrusive::
|
Chris@16
|
63 pointer_traits<VoidPointer>::template
|
Chris@16
|
64 rebind_pointer<T>::type pointer;
|
Chris@16
|
65 //!Provides the type of the stored pointer.
|
Chris@16
|
66 typedef T element_type;
|
Chris@16
|
67
|
Chris@101
|
68 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
69 private:
|
Chris@16
|
70 typedef VoidPointer VP;
|
Chris@16
|
71 typedef intrusive_ptr this_type;
|
Chris@16
|
72 typedef pointer this_type::*unspecified_bool_type;
|
Chris@101
|
73 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
74
|
Chris@16
|
75 public:
|
Chris@16
|
76 //!Constructor. Initializes internal pointer to 0.
|
Chris@16
|
77 //!Does not throw
|
Chris@16
|
78 intrusive_ptr(): m_ptr(0)
|
Chris@16
|
79 {}
|
Chris@16
|
80
|
Chris@16
|
81 //!Constructor. Copies pointer and if "p" is not zero and
|
Chris@16
|
82 //!"add_ref" is true calls intrusive_ptr_add_ref(to_raw_pointer(p)).
|
Chris@16
|
83 //!Does not throw
|
Chris@16
|
84 intrusive_ptr(const pointer &p, bool add_ref = true): m_ptr(p)
|
Chris@16
|
85 {
|
Chris@16
|
86 if(m_ptr != 0 && add_ref) intrusive_ptr_add_ref(ipcdetail::to_raw_pointer(m_ptr));
|
Chris@16
|
87 }
|
Chris@16
|
88
|
Chris@16
|
89 //!Copy constructor. Copies the internal pointer and if "p" is not
|
Chris@16
|
90 //!zero calls intrusive_ptr_add_ref(to_raw_pointer(p)). Does not throw
|
Chris@16
|
91 intrusive_ptr(intrusive_ptr const & rhs)
|
Chris@16
|
92 : m_ptr(rhs.m_ptr)
|
Chris@16
|
93 {
|
Chris@16
|
94 if(m_ptr != 0) intrusive_ptr_add_ref(ipcdetail::to_raw_pointer(m_ptr));
|
Chris@16
|
95 }
|
Chris@16
|
96
|
Chris@16
|
97 //!Constructor from related. Copies the internal pointer and if "p" is not
|
Chris@16
|
98 //!zero calls intrusive_ptr_add_ref(to_raw_pointer(p)). Does not throw
|
Chris@16
|
99 template<class U> intrusive_ptr
|
Chris@16
|
100 (intrusive_ptr<U, VP> const & rhs)
|
Chris@16
|
101 : m_ptr(rhs.get())
|
Chris@16
|
102 {
|
Chris@16
|
103 if(m_ptr != 0) intrusive_ptr_add_ref(ipcdetail::to_raw_pointer(m_ptr));
|
Chris@16
|
104 }
|
Chris@16
|
105
|
Chris@16
|
106 //!Destructor. If internal pointer is not 0, calls
|
Chris@16
|
107 //!intrusive_ptr_release(to_raw_pointer(m_ptr)). Does not throw
|
Chris@16
|
108 ~intrusive_ptr()
|
Chris@16
|
109 {
|
Chris@16
|
110 if(m_ptr != 0) intrusive_ptr_release(ipcdetail::to_raw_pointer(m_ptr));
|
Chris@16
|
111 }
|
Chris@16
|
112
|
Chris@16
|
113 //!Assignment operator. Equivalent to intrusive_ptr(r).swap(*this).
|
Chris@16
|
114 //!Does not throw
|
Chris@16
|
115 intrusive_ptr & operator=(intrusive_ptr const & rhs)
|
Chris@16
|
116 {
|
Chris@16
|
117 this_type(rhs).swap(*this);
|
Chris@16
|
118 return *this;
|
Chris@16
|
119 }
|
Chris@16
|
120
|
Chris@16
|
121 //!Assignment from related. Equivalent to intrusive_ptr(r).swap(*this).
|
Chris@16
|
122 //!Does not throw
|
Chris@16
|
123 template<class U> intrusive_ptr & operator=
|
Chris@16
|
124 (intrusive_ptr<U, VP> const & rhs)
|
Chris@16
|
125 {
|
Chris@16
|
126 this_type(rhs).swap(*this);
|
Chris@16
|
127 return *this;
|
Chris@16
|
128 }
|
Chris@16
|
129
|
Chris@16
|
130 //!Assignment from pointer. Equivalent to intrusive_ptr(r).swap(*this).
|
Chris@16
|
131 //!Does not throw
|
Chris@16
|
132 intrusive_ptr & operator=(pointer rhs)
|
Chris@16
|
133 {
|
Chris@16
|
134 this_type(rhs).swap(*this);
|
Chris@16
|
135 return *this;
|
Chris@16
|
136 }
|
Chris@16
|
137
|
Chris@16
|
138 //!Returns a reference to the internal pointer.
|
Chris@16
|
139 //!Does not throw
|
Chris@16
|
140 pointer &get()
|
Chris@16
|
141 { return m_ptr; }
|
Chris@16
|
142
|
Chris@16
|
143 //!Returns a reference to the internal pointer.
|
Chris@16
|
144 //!Does not throw
|
Chris@16
|
145 const pointer &get() const
|
Chris@16
|
146 { return m_ptr; }
|
Chris@16
|
147
|
Chris@16
|
148 //!Returns *get().
|
Chris@16
|
149 //!Does not throw
|
Chris@16
|
150 T & operator*() const
|
Chris@16
|
151 { return *m_ptr; }
|
Chris@16
|
152
|
Chris@16
|
153 //!Returns *get().
|
Chris@16
|
154 //!Does not throw
|
Chris@16
|
155 const pointer &operator->() const
|
Chris@16
|
156 { return m_ptr; }
|
Chris@16
|
157
|
Chris@16
|
158 //!Returns get().
|
Chris@16
|
159 //!Does not throw
|
Chris@16
|
160 pointer &operator->()
|
Chris@16
|
161 { return m_ptr; }
|
Chris@16
|
162
|
Chris@16
|
163 //!Conversion to boolean.
|
Chris@16
|
164 //!Does not throw
|
Chris@16
|
165 operator unspecified_bool_type () const
|
Chris@16
|
166 { return m_ptr == 0? 0: &this_type::m_ptr; }
|
Chris@16
|
167
|
Chris@16
|
168 //!Not operator.
|
Chris@16
|
169 //!Does not throw
|
Chris@16
|
170 bool operator! () const
|
Chris@16
|
171 { return m_ptr == 0; }
|
Chris@16
|
172
|
Chris@16
|
173 //!Exchanges the contents of the two smart pointers.
|
Chris@16
|
174 //!Does not throw
|
Chris@16
|
175 void swap(intrusive_ptr & rhs)
|
Chris@101
|
176 { ::boost::adl_move_swap(m_ptr, rhs.m_ptr); }
|
Chris@16
|
177
|
Chris@101
|
178 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
179 private:
|
Chris@16
|
180 pointer m_ptr;
|
Chris@101
|
181 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
182 };
|
Chris@16
|
183
|
Chris@16
|
184 //!Returns a.get() == b.get().
|
Chris@16
|
185 //!Does not throw
|
Chris@16
|
186 template<class T, class U, class VP> inline
|
Chris@16
|
187 bool operator==(intrusive_ptr<T, VP> const & a,
|
Chris@16
|
188 intrusive_ptr<U, VP> const & b)
|
Chris@16
|
189 { return a.get() == b.get(); }
|
Chris@16
|
190
|
Chris@16
|
191 //!Returns a.get() != b.get().
|
Chris@16
|
192 //!Does not throw
|
Chris@16
|
193 template<class T, class U, class VP> inline
|
Chris@16
|
194 bool operator!=(intrusive_ptr<T, VP> const & a,
|
Chris@16
|
195 intrusive_ptr<U, VP> const & b)
|
Chris@16
|
196 { return a.get() != b.get(); }
|
Chris@16
|
197
|
Chris@16
|
198 //!Returns a.get() == b.
|
Chris@16
|
199 //!Does not throw
|
Chris@16
|
200 template<class T, class VP> inline
|
Chris@16
|
201 bool operator==(intrusive_ptr<T, VP> const & a,
|
Chris@16
|
202 const typename intrusive_ptr<T, VP>::pointer &b)
|
Chris@16
|
203 { return a.get() == b; }
|
Chris@16
|
204
|
Chris@16
|
205 //!Returns a.get() != b.
|
Chris@16
|
206 //!Does not throw
|
Chris@16
|
207 template<class T, class VP> inline
|
Chris@16
|
208 bool operator!=(intrusive_ptr<T, VP> const & a,
|
Chris@16
|
209 const typename intrusive_ptr<T, VP>::pointer &b)
|
Chris@16
|
210 { return a.get() != b; }
|
Chris@16
|
211
|
Chris@16
|
212 //!Returns a == b.get().
|
Chris@16
|
213 //!Does not throw
|
Chris@16
|
214 template<class T, class VP> inline
|
Chris@16
|
215 bool operator==(const typename intrusive_ptr<T, VP>::pointer &a,
|
Chris@16
|
216 intrusive_ptr<T, VP> const & b)
|
Chris@16
|
217 { return a == b.get(); }
|
Chris@16
|
218
|
Chris@16
|
219 //!Returns a != b.get().
|
Chris@16
|
220 //!Does not throw
|
Chris@16
|
221 template<class T, class VP> inline
|
Chris@16
|
222 bool operator!=(const typename intrusive_ptr<T, VP>::pointer &a,
|
Chris@16
|
223 intrusive_ptr<T, VP> const & b)
|
Chris@16
|
224 { return a != b.get(); }
|
Chris@16
|
225
|
Chris@16
|
226 //!Returns a.get() < b.get().
|
Chris@16
|
227 //!Does not throw
|
Chris@16
|
228 template<class T, class VP> inline
|
Chris@16
|
229 bool operator<(intrusive_ptr<T, VP> const & a,
|
Chris@16
|
230 intrusive_ptr<T, VP> const & b)
|
Chris@16
|
231 {
|
Chris@16
|
232 return std::less<typename intrusive_ptr<T, VP>::pointer>()
|
Chris@16
|
233 (a.get(), b.get());
|
Chris@16
|
234 }
|
Chris@16
|
235
|
Chris@16
|
236 //!Exchanges the contents of the two intrusive_ptrs.
|
Chris@16
|
237 //!Does not throw
|
Chris@16
|
238 template<class T, class VP> inline
|
Chris@16
|
239 void swap(intrusive_ptr<T, VP> & lhs,
|
Chris@16
|
240 intrusive_ptr<T, VP> & rhs)
|
Chris@16
|
241 { lhs.swap(rhs); }
|
Chris@16
|
242
|
Chris@16
|
243 // operator<<
|
Chris@16
|
244 template<class E, class T, class Y, class VP>
|
Chris@16
|
245 inline std::basic_ostream<E, T> & operator<<
|
Chris@16
|
246 (std::basic_ostream<E, T> & os, intrusive_ptr<Y, VP> const & p)
|
Chris@16
|
247 { os << p.get(); return os; }
|
Chris@16
|
248
|
Chris@16
|
249 //!Returns p.get().
|
Chris@16
|
250 //!Does not throw
|
Chris@16
|
251 template<class T, class VP>
|
Chris@16
|
252 inline typename boost::interprocess::intrusive_ptr<T, VP>::pointer
|
Chris@16
|
253 to_raw_pointer(intrusive_ptr<T, VP> p)
|
Chris@16
|
254 { return p.get(); }
|
Chris@16
|
255
|
Chris@16
|
256 /*Emulates static cast operator. Does not throw*/
|
Chris@16
|
257 /*
|
Chris@16
|
258 template<class T, class U, class VP>
|
Chris@16
|
259 inline boost::interprocess::intrusive_ptr<T, VP> static_pointer_cast
|
Chris@16
|
260 (boost::interprocess::intrusive_ptr<U, VP> const & p)
|
Chris@16
|
261 { return do_static_cast<U>(p.get()); }
|
Chris@16
|
262 */
|
Chris@16
|
263 /*Emulates const cast operator. Does not throw*/
|
Chris@16
|
264 /*
|
Chris@16
|
265 template<class T, class U, class VP>
|
Chris@16
|
266 inline boost::interprocess::intrusive_ptr<T, VP> const_pointer_cast
|
Chris@16
|
267 (boost::interprocess::intrusive_ptr<U, VP> const & p)
|
Chris@16
|
268 { return do_const_cast<U>(p.get()); }
|
Chris@16
|
269 */
|
Chris@16
|
270
|
Chris@16
|
271 /*Emulates dynamic cast operator. Does not throw*/
|
Chris@16
|
272 /*
|
Chris@16
|
273 template<class T, class U, class VP>
|
Chris@16
|
274 inline boost::interprocess::intrusive_ptr<T, VP> dynamic_pointer_cast
|
Chris@16
|
275 (boost::interprocess::intrusive_ptr<U, VP> const & p)
|
Chris@16
|
276 { return do_dynamic_cast<U>(p.get()); }
|
Chris@16
|
277 */
|
Chris@16
|
278
|
Chris@16
|
279 /*Emulates reinterpret cast operator. Does not throw*/
|
Chris@16
|
280 /*
|
Chris@16
|
281 template<class T, class U, class VP>
|
Chris@16
|
282 inline boost::interprocess::intrusive_ptr<T, VP>reinterpret_pointer_cast
|
Chris@16
|
283 (boost::interprocess::intrusive_ptr<U, VP> const & p)
|
Chris@16
|
284 { return do_reinterpret_cast<U>(p.get()); }
|
Chris@16
|
285 */
|
Chris@16
|
286
|
Chris@16
|
287 } // namespace interprocess
|
Chris@16
|
288
|
Chris@101
|
289 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
290
|
Chris@16
|
291 #if defined(_MSC_VER) && (_MSC_VER < 1400)
|
Chris@16
|
292 //!Returns p.get().
|
Chris@16
|
293 //!Does not throw
|
Chris@16
|
294 template<class T, class VP>
|
Chris@16
|
295 inline T *to_raw_pointer(boost::interprocess::intrusive_ptr<T, VP> p)
|
Chris@16
|
296 { return p.get(); }
|
Chris@16
|
297 #endif
|
Chris@16
|
298
|
Chris@101
|
299 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
300
|
Chris@16
|
301 } // namespace boost
|
Chris@16
|
302
|
Chris@16
|
303 #include <boost/interprocess/detail/config_end.hpp>
|
Chris@16
|
304
|
Chris@16
|
305 #endif // #ifndef BOOST_INTERPROCESS_INTRUSIVE_PTR_HPP_INCLUDED
|