shared_ptr.hpp 16 KB

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