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>
36 #include <iosfwd> // for std::basic_ostream
38 #include <boost/intrusive/detail/minimal_less_equal_header.hpp> //std::less
41 namespace interprocess {
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:
51 //! void intrusive_ptr_add_ref(T * p);
52 //! void intrusive_ptr_release(T * p);
56 //!The object is responsible for destroying itself.
57 template<class T, class VoidPointer>
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;
68 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
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
76 //!Constructor. Initializes internal pointer to 0.
78 intrusive_ptr(): m_ptr(0)
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)).
84 intrusive_ptr(const pointer &p, bool add_ref = true): m_ptr(p)
86 if(m_ptr != 0 && add_ref) intrusive_ptr_add_ref(ipcdetail::to_raw_pointer(m_ptr));
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)
94 if(m_ptr != 0) intrusive_ptr_add_ref(ipcdetail::to_raw_pointer(m_ptr));
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)
103 if(m_ptr != 0) intrusive_ptr_add_ref(ipcdetail::to_raw_pointer(m_ptr));
106 //!Destructor. If internal pointer is not 0, calls
107 //!intrusive_ptr_release(to_raw_pointer(m_ptr)). Does not throw
110 if(m_ptr != 0) intrusive_ptr_release(ipcdetail::to_raw_pointer(m_ptr));
113 //!Assignment operator. Equivalent to intrusive_ptr(r).swap(*this).
115 intrusive_ptr & operator=(intrusive_ptr const & rhs)
117 this_type(rhs).swap(*this);
121 //!Assignment from related. Equivalent to intrusive_ptr(r).swap(*this).
123 template<class U> intrusive_ptr & operator=
124 (intrusive_ptr<U, VP> const & rhs)
126 this_type(rhs).swap(*this);
130 //!Assignment from pointer. Equivalent to intrusive_ptr(r).swap(*this).
132 intrusive_ptr & operator=(pointer rhs)
134 this_type(rhs).swap(*this);
138 //!Returns a reference to the internal pointer.
143 //!Returns a reference to the internal pointer.
145 const pointer &get() const
150 T & operator*() const
155 const pointer &operator->() const
160 pointer &operator->()
163 //!Conversion to boolean.
165 operator unspecified_bool_type () const
166 { return m_ptr == 0? 0: &this_type::m_ptr; }
170 bool operator! () const
171 { return m_ptr == 0; }
173 //!Exchanges the contents of the two smart pointers.
175 void swap(intrusive_ptr & rhs)
176 { ::boost::adl_move_swap(m_ptr, rhs.m_ptr); }
178 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
181 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
184 //!Returns a.get() == b.get().
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(); }
191 //!Returns a.get() != b.get().
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(); }
198 //!Returns a.get() == b.
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; }
205 //!Returns a.get() != b.
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; }
212 //!Returns a == b.get().
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(); }
219 //!Returns a != b.get().
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(); }
226 //!Returns a.get() < b.get().
228 template<class T, class VP> inline
229 bool operator<(intrusive_ptr<T, VP> const & a,
230 intrusive_ptr<T, VP> const & b)
232 return std::less<typename intrusive_ptr<T, VP>::pointer>()
236 //!Exchanges the contents of the two intrusive_ptrs.
238 template<class T, class VP> inline
239 void swap(intrusive_ptr<T, VP> & lhs,
240 intrusive_ptr<T, VP> & rhs)
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; }
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)
256 /*Emulates static cast operator. Does not throw*/
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()); }
263 /*Emulates const cast operator. Does not throw*/
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()); }
271 /*Emulates dynamic cast operator. Does not throw*/
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()); }
279 /*Emulates reinterpret cast operator. Does not throw*/
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()); }
287 } // namespace interprocess
289 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
291 #if defined(_MSC_VER) && (_MSC_VER < 1400)
294 template<class T, class VP>
295 inline T *to_raw_pointer(boost::interprocess::intrusive_ptr<T, VP> p)
299 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
303 #include <boost/interprocess/detail/config_end.hpp>
305 #endif // #ifndef BOOST_INTERPROCESS_INTRUSIVE_PTR_HPP_INCLUDED