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