1 //////////////////////////////////////////////////////////////////////////////
3 // This file is the adaptation for Interprocess of boost/intrusive_ptr.hpp
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)
10 // See http://www.boost.org/libs/interprocess for documentation.
12 //////////////////////////////////////////////////////////////////////////////
14 #ifndef BOOST_INTERPROCESS_INTRUSIVE_PTR_HPP_INCLUDED
15 #define BOOST_INTERPROCESS_INTRUSIVE_PTR_HPP_INCLUDED
17 #ifndef BOOST_CONFIG_HPP
18 # include <boost/config.hpp>
21 #if defined(BOOST_HAS_PRAGMA_ONCE)
26 //!Describes an intrusive ownership pointer.
28 #include <boost/interprocess/detail/config_begin.hpp>
29 #include <boost/interprocess/detail/workaround.hpp>
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 #include <boost/move/core.hpp>
37 #include <iosfwd> // for std::basic_ostream
39 #include <boost/intrusive/detail/minimal_less_equal_header.hpp> //std::less
42 namespace interprocess {
44 //!The intrusive_ptr class template stores a pointer to an object
45 //!with an embedded reference count. intrusive_ptr is parameterized on
46 //!T (the type of the object pointed to) and VoidPointer(a void pointer type
47 //!that defines the type of pointer that intrusive_ptr will store).
48 //!intrusive_ptr<T, void *> defines a class with a T* member whereas
49 //!intrusive_ptr<T, offset_ptr<void> > defines a class with a offset_ptr<T> member.
50 //!Relies on unqualified calls to:
52 //! void intrusive_ptr_add_ref(T * p) BOOST_NOEXCEPT;
53 //! void intrusive_ptr_release(T * p) BOOST_NOEXCEPT;
57 //!The object is responsible for destroying itself.
58 template<class T, class VoidPointer>
62 //!Provides the type of the internal stored pointer.
63 typedef typename boost::intrusive::
64 pointer_traits<VoidPointer>::template
65 rebind_pointer<T>::type pointer;
66 //!Provides the type of the stored pointer.
67 typedef T element_type;
69 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
71 typedef VoidPointer VP;
72 typedef intrusive_ptr this_type;
73 typedef pointer this_type::*unspecified_bool_type;
74 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
76 BOOST_COPYABLE_AND_MOVABLE(intrusive_ptr)
79 //!Constructor. Initializes internal pointer to 0.
81 intrusive_ptr() BOOST_NOEXCEPT
85 //!Constructor. Copies pointer and if "p" is not zero and
86 //!"add_ref" is true calls intrusive_ptr_add_ref(to_raw_pointer(p)).
88 intrusive_ptr(const pointer &p, bool add_ref = true) BOOST_NOEXCEPT
91 if(m_ptr != 0 && add_ref) intrusive_ptr_add_ref(ipcdetail::to_raw_pointer(m_ptr));
94 //!Copy constructor. Copies the internal pointer and if "p" is not
95 //!zero calls intrusive_ptr_add_ref(to_raw_pointer(p)). Does not throw
96 intrusive_ptr(intrusive_ptr const & rhs) BOOST_NOEXCEPT
99 if(m_ptr != 0) intrusive_ptr_add_ref(ipcdetail::to_raw_pointer(m_ptr));
102 //!Move constructor. Moves the internal pointer. Does not throw
103 intrusive_ptr(BOOST_RV_REF(intrusive_ptr) rhs) BOOST_NOEXCEPT
109 //!Constructor from related. Copies the internal pointer and if "p" is not
110 //!zero calls intrusive_ptr_add_ref(to_raw_pointer(p)). Does not throw
111 template<class U> intrusive_ptr(intrusive_ptr<U, VP> const & rhs) BOOST_NOEXCEPT
114 if(m_ptr != 0) intrusive_ptr_add_ref(ipcdetail::to_raw_pointer(m_ptr));
117 //!Destructor. Calls reset(). Does not throw
123 //!Assignment operator. Equivalent to intrusive_ptr(r).swap(*this).
125 intrusive_ptr & operator=(BOOST_COPY_ASSIGN_REF(intrusive_ptr) rhs) BOOST_NOEXCEPT
127 this_type(rhs).swap(*this);
131 //!Move Assignment operator
133 intrusive_ptr & operator=(BOOST_RV_REF(intrusive_ptr) rhs) BOOST_NOEXCEPT
140 //!Assignment from related. Equivalent to intrusive_ptr(r).swap(*this).
142 template<class U> intrusive_ptr & operator=(intrusive_ptr<U, VP> const & rhs) BOOST_NOEXCEPT
144 this_type(rhs).swap(*this);
148 //!Assignment from pointer. Equivalent to intrusive_ptr(r).swap(*this).
150 intrusive_ptr & operator=(pointer rhs) BOOST_NOEXCEPT
152 this_type(rhs).swap(*this);
156 //!Release internal pointer and set it to 0. If internal pointer is not 0, calls
157 //!intrusive_ptr_release(to_raw_pointer(m_ptr)). Does not throw
158 void reset() BOOST_NOEXCEPT {
162 intrusive_ptr_release(ipcdetail::to_raw_pointer(ptr));
166 //!Returns a reference to the internal pointer.
168 pointer &get() BOOST_NOEXCEPT
171 //!Returns a reference to the internal pointer.
173 const pointer &get() const BOOST_NOEXCEPT
178 T & operator*() const BOOST_NOEXCEPT
183 const pointer &operator->() const BOOST_NOEXCEPT
188 pointer &operator->() BOOST_NOEXCEPT
191 //!Conversion to boolean.
193 operator unspecified_bool_type () const BOOST_NOEXCEPT
194 { return m_ptr == 0? 0: &this_type::m_ptr; }
198 bool operator! () const BOOST_NOEXCEPT
199 { return m_ptr == 0; }
201 //!Exchanges the contents of the two smart pointers.
203 void swap(intrusive_ptr & rhs) BOOST_NOEXCEPT
204 { ::boost::adl_move_swap(m_ptr, rhs.m_ptr); }
206 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
209 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
212 //!Returns a.get() == b.get().
214 template<class T, class U, class VP> inline
215 bool operator==(intrusive_ptr<T, VP> const & a,
216 intrusive_ptr<U, VP> const & b) BOOST_NOEXCEPT
217 { return a.get() == b.get(); }
219 //!Returns a.get() != b.get().
221 template<class T, class U, class VP> inline
222 bool operator!=(intrusive_ptr<T, VP> const & a,
223 intrusive_ptr<U, VP> const & b) BOOST_NOEXCEPT
224 { return a.get() != b.get(); }
226 //!Returns a.get() == b.
228 template<class T, class VP> inline
229 bool operator==(intrusive_ptr<T, VP> const & a,
230 const typename intrusive_ptr<T, VP>::pointer &b) BOOST_NOEXCEPT
231 { return a.get() == b; }
233 //!Returns a.get() != b.
235 template<class T, class VP> inline
236 bool operator!=(intrusive_ptr<T, VP> const & a,
237 const typename intrusive_ptr<T, VP>::pointer &b) BOOST_NOEXCEPT
238 { return a.get() != b; }
240 //!Returns a == b.get().
242 template<class T, class VP> inline
243 bool operator==(const typename intrusive_ptr<T, VP>::pointer &a,
244 intrusive_ptr<T, VP> const & b) BOOST_NOEXCEPT
245 { return a == b.get(); }
247 //!Returns a != b.get().
249 template<class T, class VP> inline
250 bool operator!=(const typename intrusive_ptr<T, VP>::pointer &a,
251 intrusive_ptr<T, VP> const & b) BOOST_NOEXCEPT
252 { return a != b.get(); }
254 //!Returns a.get() < b.get().
256 template<class T, class VP> inline
257 bool operator<(intrusive_ptr<T, VP> const & a,
258 intrusive_ptr<T, VP> const & b) BOOST_NOEXCEPT
260 return std::less<typename intrusive_ptr<T, VP>::pointer>()
264 //!Exchanges the contents of the two intrusive_ptrs.
266 template<class T, class VP> inline
267 void swap(intrusive_ptr<T, VP> & lhs,
268 intrusive_ptr<T, VP> & rhs) BOOST_NOEXCEPT
272 template<class E, class T, class Y, class VP>
273 inline std::basic_ostream<E, T> & operator<<
274 (std::basic_ostream<E, T> & os, intrusive_ptr<Y, VP> const & p) BOOST_NOEXCEPT
275 { os << p.get(); return os; }
279 template<class T, class VP>
280 inline typename boost::interprocess::intrusive_ptr<T, VP>::pointer
281 to_raw_pointer(intrusive_ptr<T, VP> p) BOOST_NOEXCEPT
284 /*Emulates static cast operator. Does not throw*/
286 template<class T, class U, class VP>
287 inline boost::interprocess::intrusive_ptr<T, VP> static_pointer_cast
288 (boost::interprocess::intrusive_ptr<U, VP> const & p) BOOST_NOEXCEPT
289 { return do_static_cast<U>(p.get()); }
291 /*Emulates const cast operator. Does not throw*/
293 template<class T, class U, class VP>
294 inline boost::interprocess::intrusive_ptr<T, VP> const_pointer_cast
295 (boost::interprocess::intrusive_ptr<U, VP> const & p) BOOST_NOEXCEPT
296 { return do_const_cast<U>(p.get()); }
299 /*Emulates dynamic cast operator. Does not throw*/
301 template<class T, class U, class VP>
302 inline boost::interprocess::intrusive_ptr<T, VP> dynamic_pointer_cast
303 (boost::interprocess::intrusive_ptr<U, VP> const & p) BOOST_NOEXCEPT
304 { return do_dynamic_cast<U>(p.get()); }
307 /*Emulates reinterpret cast operator. Does not throw*/
309 template<class T, class U, class VP>
310 inline boost::interprocess::intrusive_ptr<T, VP>reinterpret_pointer_cast
311 (boost::interprocess::intrusive_ptr<U, VP> const & p) BOOST_NOEXCEPT
312 { return do_reinterpret_cast<U>(p.get()); }
315 } // namespace interprocess
317 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
319 #if defined(_MSC_VER) && (_MSC_VER < 1400)
322 template<class T, class VP>
323 inline T *to_raw_pointer(boost::interprocess::intrusive_ptr<T, VP> p) BOOST_NOEXCEPT
327 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
331 #include <boost/interprocess/detail/config_end.hpp>
333 #endif // #ifndef BOOST_INTERPROCESS_INTRUSIVE_PTR_HPP_INCLUDED