shared_ptr.hpp 16 KB

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