shared_ptr.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // This file is the adaptation for Interprocess of boost/shared_ptr.hpp
  4. //
  5. // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
  6. // (C) Copyright Peter Dimov 2001, 2002, 2003
  7. // (C) Copyright Ion Gaztanaga 2006-2012.
  8. // Distributed under the Boost Software License, Version 1.0.
  9. // (See accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. //
  12. // See http://www.boost.org/libs/interprocess for documentation.
  13. //
  14. //////////////////////////////////////////////////////////////////////////////
  15. #ifndef BOOST_INTERPROCESS_SHARED_PTR_HPP_INCLUDED
  16. #define BOOST_INTERPROCESS_SHARED_PTR_HPP_INCLUDED
  17. #include <boost/interprocess/detail/config_begin.hpp>
  18. #include <boost/interprocess/detail/workaround.hpp>
  19. #include <boost/interprocess/detail/utilities.hpp>
  20. #include <boost/interprocess/detail/cast_tags.hpp>
  21. #include <boost/assert.hpp>
  22. #include <boost/interprocess/smart_ptr/detail/shared_count.hpp>
  23. #include <boost/interprocess/detail/mpl.hpp>
  24. #include <boost/move/move.hpp>
  25. #include <boost/interprocess/detail/type_traits.hpp>
  26. #include <boost/interprocess/allocators/allocator.hpp>
  27. #include <boost/interprocess/smart_ptr/deleter.hpp>
  28. #include <boost/static_assert.hpp>
  29. #include <boost/intrusive/pointer_traits.hpp>
  30. #include <algorithm> // for std::swap
  31. #include <functional> // for std::less
  32. #include <typeinfo> // for std::bad_cast
  33. #include <iosfwd> // for std::basic_ostream
  34. //!\file
  35. //!Describes the smart pointer shared_ptr
  36. namespace boost{
  37. namespace interprocess{
  38. template<class T, class VoidAllocator, class Deleter> class weak_ptr;
  39. template<class T, class VoidAllocator, class Deleter> class enable_shared_from_this;
  40. namespace ipcdetail{
  41. template<class T, class VoidAllocator, class Deleter>
  42. inline void sp_enable_shared_from_this
  43. (shared_count<T, VoidAllocator, Deleter> const & pn
  44. ,enable_shared_from_this<T, VoidAllocator, Deleter> *pe
  45. ,T *ptr)
  46. {
  47. (void)ptr;
  48. if(pe != 0){
  49. pe->_internal_weak_this._internal_assign(pn);
  50. }
  51. }
  52. template<class T, class VoidAllocator, class Deleter>
  53. inline void sp_enable_shared_from_this(shared_count<T, VoidAllocator, Deleter> const &, ...)
  54. {}
  55. } // namespace ipcdetail
  56. //!shared_ptr stores a pointer to a dynamically allocated object.
  57. //!The object pointed to is guaranteed to be deleted when the last shared_ptr pointing to
  58. //!it is destroyed or reset.
  59. //!
  60. //!shared_ptr is parameterized on
  61. //!T (the type of the object pointed to), VoidAllocator (the void allocator to be used
  62. //!to allocate the auxiliary data) and Deleter (the deleter whose
  63. //!operator() will be used to delete the object.
  64. //!
  65. //!The internal pointer will be of the same pointer type as typename
  66. //!VoidAllocator::pointer type (that is, if typename VoidAllocator::pointer is
  67. //!offset_ptr<void>, the internal pointer will be offset_ptr<T>).
  68. //!
  69. //!Because the implementation uses reference counting, cycles of shared_ptr
  70. //!instances will not be reclaimed. For example, if main() holds a
  71. //!shared_ptr to A, which directly or indirectly holds a shared_ptr back
  72. //!to A, A's use count will be 2. Destruction of the original shared_ptr
  73. //!will leave A dangling with a use count of 1.
  74. //!Use weak_ptr to "break cycles."
  75. template<class T, class VoidAllocator, class Deleter>
  76. class shared_ptr
  77. {
  78. /// @cond
  79. private:
  80. typedef shared_ptr<T, VoidAllocator, Deleter> this_type;
  81. /// @endcond
  82. public:
  83. typedef T element_type;
  84. typedef T value_type;
  85. typedef typename boost::intrusive::
  86. pointer_traits<typename VoidAllocator::pointer>::template
  87. rebind_pointer<T>::type pointer;
  88. typedef typename ipcdetail::add_reference
  89. <value_type>::type reference;
  90. typedef typename ipcdetail::add_reference
  91. <const value_type>::type const_reference;
  92. typedef typename boost::intrusive::
  93. pointer_traits<typename VoidAllocator::pointer>::template
  94. rebind_pointer<const Deleter>::type const_deleter_pointer;
  95. typedef typename boost::intrusive::
  96. pointer_traits<typename VoidAllocator::pointer>::template
  97. rebind_pointer<const VoidAllocator>::type const_allocator_pointer;
  98. BOOST_COPYABLE_AND_MOVABLE(shared_ptr)
  99. public:
  100. //!Constructs an empty shared_ptr.
  101. //!Use_count() == 0 && get()== 0.
  102. shared_ptr()
  103. : m_pn() // never throws
  104. {}
  105. //!Constructs a shared_ptr that owns the pointer p. Auxiliary data will be allocated
  106. //!with a copy of a and the object will be deleted with a copy of d.
  107. //!Requirements: Deleter and A's copy constructor must not throw.
  108. explicit shared_ptr(const pointer&p, const VoidAllocator &a = VoidAllocator(), const Deleter &d = Deleter())
  109. : m_pn(p, a, d)
  110. {
  111. //Check that the pointer passed is of the same type that
  112. //the pointer the allocator defines or it's a raw pointer
  113. typedef typename boost::intrusive::
  114. pointer_traits<pointer>::template
  115. rebind_pointer<T>::type ParameterPointer;
  116. BOOST_STATIC_ASSERT((ipcdetail::is_same<pointer, ParameterPointer>::value) ||
  117. (ipcdetail::is_pointer<pointer>::value));
  118. ipcdetail::sp_enable_shared_from_this<T, VoidAllocator, Deleter>( m_pn, ipcdetail::to_raw_pointer(p), ipcdetail::to_raw_pointer(p) );
  119. }
  120. //!Copy constructs a shared_ptr. If r is empty, constructs an empty shared_ptr. Otherwise, constructs
  121. //!a shared_ptr that shares ownership with r. Never throws.
  122. shared_ptr(const shared_ptr &r)
  123. : m_pn(r.m_pn) // never throws
  124. {}
  125. //!Constructs a shared_ptr that shares ownership with other and stores p.
  126. //!Postconditions: get() == p && use_count() == r.use_count().
  127. //!Throws: nothing.
  128. shared_ptr(const shared_ptr &other, const pointer &p)
  129. : m_pn(other.m_pn, p)
  130. {}
  131. //!If r is empty, constructs an empty shared_ptr. Otherwise, constructs
  132. //!a shared_ptr that shares ownership with r. Never throws.
  133. template<class Y>
  134. shared_ptr(shared_ptr<Y, VoidAllocator, Deleter> const & r)
  135. : m_pn(r.m_pn) // never throws
  136. {}
  137. //!Constructs a shared_ptr that shares ownership with r and stores
  138. //!a copy of the pointer stored in r.
  139. template<class Y>
  140. explicit shared_ptr(weak_ptr<Y, VoidAllocator, Deleter> const & r)
  141. : m_pn(r.m_pn) // may throw
  142. {}
  143. //!Move-Constructs a shared_ptr that takes ownership of other resource and
  144. //!other is put in default-constructed state.
  145. //!Throws: nothing.
  146. explicit shared_ptr(BOOST_RV_REF(shared_ptr) other)
  147. : m_pn()
  148. { this->swap(other); }
  149. /// @cond
  150. template<class Y>
  151. shared_ptr(shared_ptr<Y, VoidAllocator, Deleter> const & r, ipcdetail::static_cast_tag)
  152. : m_pn( pointer(static_cast<T*>(ipcdetail::to_raw_pointer(r.m_pn.to_raw_pointer())))
  153. , r.m_pn)
  154. {}
  155. template<class Y>
  156. shared_ptr(shared_ptr<Y, VoidAllocator, Deleter> const & r, ipcdetail::const_cast_tag)
  157. : m_pn( pointer(const_cast<T*>(ipcdetail::to_raw_pointer(r.m_pn.to_raw_pointer())))
  158. , r.m_pn)
  159. {}
  160. template<class Y>
  161. shared_ptr(shared_ptr<Y, VoidAllocator, Deleter> const & r, ipcdetail::dynamic_cast_tag)
  162. : m_pn( pointer(dynamic_cast<T*>(ipcdetail::to_raw_pointer(r.m_pn.to_raw_pointer())))
  163. , r.m_pn)
  164. {
  165. if(!m_pn.to_raw_pointer()){ // need to allocate new counter -- the cast failed
  166. m_pn = ipcdetail::shared_count<T, VoidAllocator, Deleter>();
  167. }
  168. }
  169. /// @endcond
  170. //!Equivalent to shared_ptr(r).swap(*this).
  171. //!Never throws
  172. template<class Y>
  173. shared_ptr & operator=(shared_ptr<Y, VoidAllocator, Deleter> const & r)
  174. {
  175. m_pn = r.m_pn; // shared_count::op= doesn't throw
  176. return *this;
  177. }
  178. //!Equivalent to shared_ptr(r).swap(*this).
  179. //!Never throws
  180. shared_ptr & operator=(BOOST_COPY_ASSIGN_REF(shared_ptr) r)
  181. {
  182. m_pn = r.m_pn; // shared_count::op= doesn't throw
  183. return *this;
  184. }
  185. //!Move-assignment. Equivalent to shared_ptr(other).swap(*this).
  186. //!Never throws
  187. shared_ptr & operator=(BOOST_RV_REF(shared_ptr) other) // never throws
  188. {
  189. this_type(other).swap(*this);
  190. return *this;
  191. }
  192. //!This is equivalent to:
  193. //!this_type().swap(*this);
  194. void reset()
  195. {
  196. this_type().swap(*this);
  197. }
  198. //!This is equivalent to:
  199. //!this_type(p, a, d).swap(*this);
  200. template<class Pointer>
  201. void reset(const Pointer &p, const VoidAllocator &a = VoidAllocator(), const Deleter &d = Deleter())
  202. {
  203. //Check that the pointer passed is of the same type that
  204. //the pointer the allocator defines or it's a raw pointer
  205. typedef typename boost::intrusive::
  206. pointer_traits<Pointer>::template
  207. rebind_pointer<T>::type ParameterPointer;
  208. BOOST_STATIC_ASSERT((ipcdetail::is_same<pointer, ParameterPointer>::value) ||
  209. (ipcdetail::is_pointer<Pointer>::value));
  210. this_type(p, a, d).swap(*this);
  211. }
  212. template<class Y>
  213. void reset(shared_ptr<Y, VoidAllocator, Deleter> const & r, const pointer &p)
  214. {
  215. this_type(r, p).swap(*this);
  216. }
  217. //!Returns a reference to the
  218. //!pointed type
  219. reference operator* () const // never throws
  220. { BOOST_ASSERT(m_pn.to_raw_pointer() != 0); return *m_pn.to_raw_pointer(); }
  221. //!Returns the pointer pointing
  222. //!to the owned object
  223. pointer operator-> () const // never throws
  224. { BOOST_ASSERT(m_pn.to_raw_pointer() != 0); return m_pn.to_raw_pointer(); }
  225. //!Returns the pointer pointing
  226. //!to the owned object
  227. pointer get() const // never throws
  228. { return m_pn.to_raw_pointer(); }
  229. /// @cond
  230. // implicit conversion to "bool"
  231. void unspecified_bool_type_func() const {}
  232. typedef void (this_type::*unspecified_bool_type)() const;
  233. operator unspecified_bool_type() const // never throws
  234. { return !m_pn.to_raw_pointer() ? 0 : &this_type::unspecified_bool_type_func; }
  235. /// @endcond
  236. //!Not operator.
  237. //!Returns true if this->get() != 0, false otherwise
  238. bool operator! () const // never throws
  239. { return !m_pn.to_raw_pointer(); }
  240. //!Returns use_count() == 1.
  241. //!unique() might be faster than use_count()
  242. bool unique() const // never throws
  243. { return m_pn.unique(); }
  244. //!Returns the number of shared_ptr objects, *this included,
  245. //!that share ownership with *this, or an unspecified nonnegative
  246. //!value when *this is empty.
  247. //!use_count() is not necessarily efficient. Use only for
  248. //!debugging and testing purposes, not for production code.
  249. long use_count() const // never throws
  250. { return m_pn.use_count(); }
  251. //!Exchanges the contents of the two
  252. //!smart pointers.
  253. void swap(shared_ptr<T, VoidAllocator, Deleter> & other) // never throws
  254. { m_pn.swap(other.m_pn); }
  255. /// @cond
  256. template<class T2, class A2, class Deleter2>
  257. bool _internal_less(shared_ptr<T2, A2, Deleter2> const & rhs) const
  258. { return m_pn < rhs.m_pn; }
  259. const_deleter_pointer get_deleter() const
  260. { return m_pn.get_deleter(); }
  261. // const_allocator_pointer get_allocator() const
  262. // { return m_pn.get_allocator(); }
  263. private:
  264. template<class T2, class A2, class Deleter2> friend class shared_ptr;
  265. template<class T2, class A2, class Deleter2> friend class weak_ptr;
  266. ipcdetail::shared_count<T, VoidAllocator, Deleter> m_pn; // reference counter
  267. /// @endcond
  268. }; // shared_ptr
  269. template<class T, class VoidAllocator, class Deleter, class U, class VoidAllocator2, class Deleter2> inline
  270. bool operator==(shared_ptr<T, VoidAllocator, Deleter> const & a, shared_ptr<U, VoidAllocator2, Deleter2> const & b)
  271. { return a.get() == b.get(); }
  272. template<class T, class VoidAllocator, class Deleter, class U, class VoidAllocator2, class Deleter2> inline
  273. bool operator!=(shared_ptr<T, VoidAllocator, Deleter> const & a, shared_ptr<U, VoidAllocator2, Deleter2> const & b)
  274. { return a.get() != b.get(); }
  275. template<class T, class VoidAllocator, class Deleter, class U, class VoidAllocator2, class Deleter2> inline
  276. bool operator<(shared_ptr<T, VoidAllocator, Deleter> const & a, shared_ptr<U, VoidAllocator2, Deleter2> const & b)
  277. { return a._internal_less(b); }
  278. template<class T, class VoidAllocator, class Deleter> inline
  279. void swap(shared_ptr<T, VoidAllocator, Deleter> & a, shared_ptr<T, VoidAllocator, Deleter> & b)
  280. { a.swap(b); }
  281. template<class T, class VoidAllocator, class Deleter, class U> inline
  282. shared_ptr<T, VoidAllocator, Deleter> static_pointer_cast(shared_ptr<U, VoidAllocator, Deleter> const & r)
  283. { return shared_ptr<T, VoidAllocator, Deleter>(r, ipcdetail::static_cast_tag()); }
  284. template<class T, class VoidAllocator, class Deleter, class U> inline
  285. shared_ptr<T, VoidAllocator, Deleter> const_pointer_cast(shared_ptr<U, VoidAllocator, Deleter> const & r)
  286. { return shared_ptr<T, VoidAllocator, Deleter>(r, ipcdetail::const_cast_tag()); }
  287. template<class T, class VoidAllocator, class Deleter, class U> inline
  288. shared_ptr<T, VoidAllocator, Deleter> dynamic_pointer_cast(shared_ptr<U, VoidAllocator, Deleter> const & r)
  289. { return shared_ptr<T, VoidAllocator, Deleter>(r, ipcdetail::dynamic_cast_tag()); }
  290. // to_raw_pointer() enables boost::mem_fn to recognize shared_ptr
  291. template<class T, class VoidAllocator, class Deleter> inline
  292. T * to_raw_pointer(shared_ptr<T, VoidAllocator, Deleter> const & p)
  293. { return p.get(); }
  294. // operator<<
  295. template<class E, class T, class Y, class VoidAllocator, class Deleter> inline
  296. std::basic_ostream<E, T> & operator<<
  297. (std::basic_ostream<E, T> & os, shared_ptr<Y, VoidAllocator, Deleter> const & p)
  298. { os << p.get(); return os; }
  299. //!Returns the type of a shared pointer
  300. //!of type T with the allocator boost::interprocess::allocator allocator
  301. //!and boost::interprocess::deleter deleter
  302. //!that can be constructed in the given managed segment type.
  303. template<class T, class ManagedMemory>
  304. struct managed_shared_ptr
  305. {
  306. typedef typename ManagedMemory::template allocator<void>::type void_allocator;
  307. typedef typename ManagedMemory::template deleter<T>::type deleter;
  308. typedef shared_ptr< T, void_allocator, deleter> type;
  309. };
  310. //!Returns an instance of a shared pointer constructed
  311. //!with the default allocator and deleter from a pointer
  312. //!of type T that has been allocated in the passed managed segment
  313. template<class T, class ManagedMemory>
  314. inline typename managed_shared_ptr<T, ManagedMemory>::type
  315. make_managed_shared_ptr(T *constructed_object, ManagedMemory &managed_memory)
  316. {
  317. return typename managed_shared_ptr<T, ManagedMemory>::type
  318. ( constructed_object
  319. , managed_memory.template get_allocator<void>()
  320. , managed_memory.template get_deleter<T>()
  321. );
  322. }
  323. //!Returns an instance of a shared pointer constructed
  324. //!with the default allocator and deleter from a pointer
  325. //!of type T that has been allocated in the passed managed segment.
  326. //!Does not throw, return null shared pointer in error.
  327. template<class T, class ManagedMemory>
  328. inline typename managed_shared_ptr<T, ManagedMemory>::type
  329. make_managed_shared_ptr(T *constructed_object, ManagedMemory &managed_memory, std::nothrow_t)
  330. {
  331. try{
  332. return typename managed_shared_ptr<T, ManagedMemory>::type
  333. ( constructed_object
  334. , managed_memory.template get_allocator<void>()
  335. , managed_memory.template get_deleter<T>()
  336. );
  337. }
  338. catch(...){
  339. return typename managed_shared_ptr<T, ManagedMemory>::type();
  340. }
  341. }
  342. } // namespace interprocess
  343. /// @cond
  344. #if defined(_MSC_VER) && (_MSC_VER < 1400)
  345. // to_raw_pointer() enables boost::mem_fn to recognize shared_ptr
  346. template<class T, class VoidAllocator, class Deleter> inline
  347. T * to_raw_pointer(boost::interprocess::shared_ptr<T, VoidAllocator, Deleter> const & p)
  348. { return p.get(); }
  349. #endif
  350. /// @endcond
  351. } // namespace boost
  352. #include <boost/interprocess/detail/config_end.hpp>
  353. #endif // #ifndef BOOST_INTERPROCESS_SHARED_PTR_HPP_INCLUDED