shared_ptr_132.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. #ifndef BOOST_SHARED_PTR_132_HPP_INCLUDED
  2. #define BOOST_SHARED_PTR_132_HPP_INCLUDED
  3. //
  4. // shared_ptr.hpp
  5. //
  6. // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
  7. // Copyright (c) 2001, 2002, 2003 Peter Dimov
  8. //
  9. // Distributed under the Boost Software License, Version 1.0. (See
  10. // accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. //
  13. // See http://www.boost.org/libs/smart_ptr/shared_ptr.htm for documentation.
  14. //
  15. #include <boost/config.hpp> // for broken compiler workarounds
  16. #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
  17. #include <boost/serialization/detail/shared_ptr_nmt_132.hpp>
  18. #else
  19. #include <boost/assert.hpp>
  20. #include <boost/checked_delete.hpp>
  21. #include <boost/serialization/throw_exception.hpp>
  22. #include <boost/detail/workaround.hpp>
  23. #include <boost/serialization/access.hpp>
  24. #include <boost/serialization/detail/shared_count_132.hpp>
  25. #include <memory> // for std::auto_ptr
  26. #include <algorithm> // for std::swap
  27. #include <functional> // for std::less
  28. #include <typeinfo> // for std::bad_cast
  29. #include <iosfwd> // for std::basic_ostream
  30. #ifdef BOOST_MSVC // moved here to work around VC++ compiler crash
  31. # pragma warning(push)
  32. # pragma warning(disable:4284) // odd return type for operator->
  33. #endif
  34. namespace boost_132 {
  35. template<class T> class weak_ptr;
  36. template<class T> class enable_shared_from_this;
  37. namespace detail
  38. {
  39. struct static_cast_tag {};
  40. struct const_cast_tag {};
  41. struct dynamic_cast_tag {};
  42. struct polymorphic_cast_tag {};
  43. template<class T> struct shared_ptr_traits
  44. {
  45. typedef T & reference;
  46. };
  47. template<> struct shared_ptr_traits<void>
  48. {
  49. typedef void reference;
  50. };
  51. #if !defined(BOOST_NO_CV_VOID_SPECIALIZATIONS)
  52. template<> struct shared_ptr_traits<void const>
  53. {
  54. typedef void reference;
  55. };
  56. template<> struct shared_ptr_traits<void volatile>
  57. {
  58. typedef void reference;
  59. };
  60. template<> struct shared_ptr_traits<void const volatile>
  61. {
  62. typedef void reference;
  63. };
  64. #endif
  65. // enable_shared_from_this support
  66. template<class T, class Y> void sp_enable_shared_from_this( shared_count const & pn, enable_shared_from_this< T > const * pe, Y const * px )
  67. {
  68. if(pe != 0) pe->_internal_weak_this._internal_assign(const_cast<Y*>(px), pn);
  69. }
  70. inline void sp_enable_shared_from_this( shared_count const & /*pn*/, ... )
  71. {
  72. }
  73. } // namespace detail
  74. //
  75. // shared_ptr
  76. //
  77. // An enhanced relative of scoped_ptr with reference counted copy semantics.
  78. // The object pointed to is deleted when the last shared_ptr pointing to it
  79. // is destroyed or reset.
  80. //
  81. template<class T> class shared_ptr
  82. {
  83. private:
  84. // Borland 5.5.1 specific workaround
  85. typedef shared_ptr< T > this_type;
  86. public:
  87. typedef T element_type;
  88. typedef T value_type;
  89. typedef T * pointer;
  90. typedef typename detail::shared_ptr_traits< T >::reference reference;
  91. shared_ptr(): px(0), pn() // never throws in 1.30+
  92. {
  93. }
  94. template<class Y>
  95. explicit shared_ptr(Y * p): px(p), pn(p, boost::checked_deleter<Y>()) // Y must be complete
  96. {
  97. detail::sp_enable_shared_from_this( pn, p, p );
  98. }
  99. //
  100. // Requirements: D's copy constructor must not throw
  101. //
  102. // shared_ptr will release p by calling d(p)
  103. //
  104. template<class Y, class D> shared_ptr(Y * p, D d): px(p), pn(p, d)
  105. {
  106. detail::sp_enable_shared_from_this( pn, p, p );
  107. }
  108. // generated copy constructor, assignment, destructor are fine...
  109. // except that Borland C++ has a bug, and g++ with -Wsynth warns
  110. #if defined(__GNUC__)
  111. shared_ptr & operator=(shared_ptr const & r) // never throws
  112. {
  113. px = r.px;
  114. pn = r.pn; // shared_count::op= doesn't throw
  115. return *this;
  116. }
  117. #endif
  118. template<class Y>
  119. explicit shared_ptr(weak_ptr<Y> const & r): pn(r.pn) // may throw
  120. {
  121. // it is now safe to copy r.px, as pn(r.pn) did not throw
  122. px = r.px;
  123. }
  124. template<class Y>
  125. shared_ptr(shared_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
  126. {
  127. }
  128. template<class Y>
  129. shared_ptr(shared_ptr<Y> const & r, detail::static_cast_tag): px(static_cast<element_type *>(r.px)), pn(r.pn)
  130. {
  131. }
  132. template<class Y>
  133. shared_ptr(shared_ptr<Y> const & r, detail::const_cast_tag): px(const_cast<element_type *>(r.px)), pn(r.pn)
  134. {
  135. }
  136. template<class Y>
  137. shared_ptr(shared_ptr<Y> const & r, detail::dynamic_cast_tag): px(dynamic_cast<element_type *>(r.px)), pn(r.pn)
  138. {
  139. if(px == 0) // need to allocate new counter -- the cast failed
  140. {
  141. pn = detail::shared_count();
  142. }
  143. }
  144. template<class Y>
  145. shared_ptr(shared_ptr<Y> const & r, detail::polymorphic_cast_tag): px(dynamic_cast<element_type *>(r.px)), pn(r.pn)
  146. {
  147. if(px == 0)
  148. {
  149. boost::serialization::throw_exception(std::bad_cast());
  150. }
  151. }
  152. #ifndef BOOST_NO_AUTO_PTR
  153. template<class Y>
  154. explicit shared_ptr(std::auto_ptr<Y> & r): px(r.get()), pn()
  155. {
  156. Y * tmp = r.get();
  157. pn = detail::shared_count(r);
  158. detail::sp_enable_shared_from_this( pn, tmp, tmp );
  159. }
  160. #endif
  161. #if !defined(BOOST_MSVC) || (BOOST_MSVC > 1200)
  162. template<class Y>
  163. shared_ptr & operator=(shared_ptr<Y> const & r) // never throws
  164. {
  165. px = r.px;
  166. pn = r.pn; // shared_count::op= doesn't throw
  167. return *this;
  168. }
  169. #endif
  170. #ifndef BOOST_NO_AUTO_PTR
  171. template<class Y>
  172. shared_ptr & operator=(std::auto_ptr<Y> & r)
  173. {
  174. this_type(r).swap(*this);
  175. return *this;
  176. }
  177. #endif
  178. void reset() // never throws in 1.30+
  179. {
  180. this_type().swap(*this);
  181. }
  182. template<class Y> void reset(Y * p) // Y must be complete
  183. {
  184. BOOST_ASSERT(p == 0 || p != px); // catch self-reset errors
  185. this_type(p).swap(*this);
  186. }
  187. template<class Y, class D> void reset(Y * p, D d)
  188. {
  189. this_type(p, d).swap(*this);
  190. }
  191. reference operator* () const // never throws
  192. {
  193. BOOST_ASSERT(px != 0);
  194. return *px;
  195. }
  196. T * operator-> () const // never throws
  197. {
  198. BOOST_ASSERT(px != 0);
  199. return px;
  200. }
  201. T * get() const // never throws
  202. {
  203. return px;
  204. }
  205. // implicit conversion to "bool"
  206. #if defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x530)
  207. operator bool () const
  208. {
  209. return px != 0;
  210. }
  211. #elif defined(__MWERKS__) && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
  212. typedef T * (this_type::*unspecified_bool_type)() const;
  213. operator unspecified_bool_type() const // never throws
  214. {
  215. return px == 0? 0: &this_type::get;
  216. }
  217. #else
  218. typedef T * this_type::*unspecified_bool_type;
  219. operator unspecified_bool_type() const // never throws
  220. {
  221. return px == 0? 0: &this_type::px;
  222. }
  223. #endif
  224. // operator! is redundant, but some compilers need it
  225. bool operator! () const // never throws
  226. {
  227. return px == 0;
  228. }
  229. bool unique() const // never throws
  230. {
  231. return pn.unique();
  232. }
  233. long use_count() const // never throws
  234. {
  235. return pn.use_count();
  236. }
  237. void swap(shared_ptr< T > & other) // never throws
  238. {
  239. std::swap(px, other.px);
  240. pn.swap(other.pn);
  241. }
  242. template<class Y> bool _internal_less(shared_ptr<Y> const & rhs) const
  243. {
  244. return pn < rhs.pn;
  245. }
  246. void * _internal_get_deleter(std::type_info const & ti) const
  247. {
  248. return pn.get_deleter(ti);
  249. }
  250. // Tasteless as this may seem, making all members public allows member templates
  251. // to work in the absence of member template friends. (Matthew Langston)
  252. #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  253. private:
  254. template<class Y> friend class shared_ptr;
  255. template<class Y> friend class weak_ptr;
  256. #endif
  257. public: // for serialization
  258. T * px; // contained pointer
  259. detail::shared_count pn; // reference counter
  260. }; // shared_ptr
  261. template<class T, class U> inline bool operator==(shared_ptr< T > const & a, shared_ptr<U> const & b)
  262. {
  263. return a.get() == b.get();
  264. }
  265. template<class T, class U> inline bool operator!=(shared_ptr< T > const & a, shared_ptr<U> const & b)
  266. {
  267. return a.get() != b.get();
  268. }
  269. template<class T, class U> inline bool operator<(shared_ptr< T > const & a, shared_ptr<U> const & b)
  270. {
  271. return a._internal_less(b);
  272. }
  273. template<class T> inline void swap(shared_ptr< T > & a, shared_ptr< T > & b)
  274. {
  275. a.swap(b);
  276. }
  277. template<class T, class U> shared_ptr< T > static_pointer_cast(shared_ptr<U> const & r)
  278. {
  279. return shared_ptr< T >(r, detail::static_cast_tag());
  280. }
  281. template<class T, class U> shared_ptr< T > const_pointer_cast(shared_ptr<U> const & r)
  282. {
  283. return shared_ptr< T >(r, detail::const_cast_tag());
  284. }
  285. template<class T, class U> shared_ptr< T > dynamic_pointer_cast(shared_ptr<U> const & r)
  286. {
  287. return shared_ptr< T >(r, detail::dynamic_cast_tag());
  288. }
  289. // shared_*_cast names are deprecated. Use *_pointer_cast instead.
  290. template<class T, class U> shared_ptr< T > shared_static_cast(shared_ptr<U> const & r)
  291. {
  292. return shared_ptr< T >(r, detail::static_cast_tag());
  293. }
  294. template<class T, class U> shared_ptr< T > shared_dynamic_cast(shared_ptr<U> const & r)
  295. {
  296. return shared_ptr< T >(r, detail::dynamic_cast_tag());
  297. }
  298. template<class T, class U> shared_ptr< T > shared_polymorphic_cast(shared_ptr<U> const & r)
  299. {
  300. return shared_ptr< T >(r, detail::polymorphic_cast_tag());
  301. }
  302. template<class T, class U> shared_ptr< T > shared_polymorphic_downcast(shared_ptr<U> const & r)
  303. {
  304. BOOST_ASSERT(dynamic_cast<T *>(r.get()) == r.get());
  305. return shared_static_cast< T >(r);
  306. }
  307. // get_pointer() enables boost::mem_fn to recognize shared_ptr
  308. template<class T> inline T * get_pointer(shared_ptr< T > const & p)
  309. {
  310. return p.get();
  311. }
  312. // operator<<
  313. template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, shared_ptr<Y> const & p)
  314. {
  315. os << p.get();
  316. return os;
  317. }
  318. // get_deleter (experimental)
  319. #if defined(__EDG_VERSION__) && (__EDG_VERSION__ <= 238)
  320. // g++ 2.9x doesn't allow static_cast<X const *>(void *)
  321. // apparently EDG 2.38 also doesn't accept it
  322. template<class D, class T> D * get_deleter(shared_ptr< T > const & p)
  323. {
  324. void const * q = p._internal_get_deleter(typeid(D));
  325. return const_cast<D *>(static_cast<D const *>(q));
  326. }
  327. #else
  328. template<class D, class T> D * get_deleter(shared_ptr< T > const & p)
  329. {
  330. return static_cast<D *>(p._internal_get_deleter(typeid(D)));
  331. }
  332. #endif
  333. } // namespace boost
  334. #ifdef BOOST_MSVC
  335. # pragma warning(pop)
  336. #endif
  337. #endif // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
  338. #endif // #ifndef BOOST_SHARED_PTR_132_HPP_INCLUDED