shared_ptr_132.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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 BOOST_DEDUCED_TYPENAME detail::shared_ptr_traits<T>::reference reference;
  91. shared_ptr(): px(0), pn() // never throws in 1.30+
  92. {
  93. }
  94. #if BOOST_WORKAROUND( __BORLANDC__, BOOST_TESTED_AT( 0x564) )
  95. template<class Y>
  96. explicit shared_ptr(Y * p): px(p), pn(p, boost::checked_deleter<Y>()) // Y must be complete
  97. #else
  98. template<class Y>
  99. explicit shared_ptr(Y * p): px(p), pn(p, boost::checked_deleter<Y>()) // Y must be complete
  100. #endif
  101. {
  102. detail::sp_enable_shared_from_this( pn, p, p );
  103. }
  104. //
  105. // Requirements: D's copy constructor must not throw
  106. //
  107. // shared_ptr will release p by calling d(p)
  108. //
  109. template<class Y, class D> shared_ptr(Y * p, D d): px(p), pn(p, d)
  110. {
  111. detail::sp_enable_shared_from_this( pn, p, p );
  112. }
  113. // generated copy constructor, assignment, destructor are fine...
  114. // except that Borland C++ has a bug, and g++ with -Wsynth warns
  115. #if defined(__BORLANDC__) || defined(__GNUC__)
  116. shared_ptr & operator=(shared_ptr const & r) // never throws
  117. {
  118. px = r.px;
  119. pn = r.pn; // shared_count::op= doesn't throw
  120. return *this;
  121. }
  122. #endif
  123. template<class Y>
  124. explicit shared_ptr(weak_ptr<Y> const & r): pn(r.pn) // may throw
  125. {
  126. // it is now safe to copy r.px, as pn(r.pn) did not throw
  127. px = r.px;
  128. }
  129. template<class Y>
  130. shared_ptr(shared_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
  131. {
  132. }
  133. template<class Y>
  134. shared_ptr(shared_ptr<Y> const & r, detail::static_cast_tag): px(static_cast<element_type *>(r.px)), pn(r.pn)
  135. {
  136. }
  137. template<class Y>
  138. shared_ptr(shared_ptr<Y> const & r, detail::const_cast_tag): px(const_cast<element_type *>(r.px)), pn(r.pn)
  139. {
  140. }
  141. template<class Y>
  142. shared_ptr(shared_ptr<Y> const & r, detail::dynamic_cast_tag): px(dynamic_cast<element_type *>(r.px)), pn(r.pn)
  143. {
  144. if(px == 0) // need to allocate new counter -- the cast failed
  145. {
  146. pn = detail::shared_count();
  147. }
  148. }
  149. template<class Y>
  150. shared_ptr(shared_ptr<Y> const & r, detail::polymorphic_cast_tag): px(dynamic_cast<element_type *>(r.px)), pn(r.pn)
  151. {
  152. if(px == 0)
  153. {
  154. boost::serialization::throw_exception(std::bad_cast());
  155. }
  156. }
  157. #ifndef BOOST_NO_AUTO_PTR
  158. template<class Y>
  159. explicit shared_ptr(std::auto_ptr<Y> & r): px(r.get()), pn()
  160. {
  161. Y * tmp = r.get();
  162. pn = detail::shared_count(r);
  163. detail::sp_enable_shared_from_this( pn, tmp, tmp );
  164. }
  165. #endif
  166. #if !defined(BOOST_MSVC) || (BOOST_MSVC > 1200)
  167. template<class Y>
  168. shared_ptr & operator=(shared_ptr<Y> const & r) // never throws
  169. {
  170. px = r.px;
  171. pn = r.pn; // shared_count::op= doesn't throw
  172. return *this;
  173. }
  174. #endif
  175. #ifndef BOOST_NO_AUTO_PTR
  176. template<class Y>
  177. shared_ptr & operator=(std::auto_ptr<Y> & r)
  178. {
  179. this_type(r).swap(*this);
  180. return *this;
  181. }
  182. #endif
  183. void reset() // never throws in 1.30+
  184. {
  185. this_type().swap(*this);
  186. }
  187. template<class Y> void reset(Y * p) // Y must be complete
  188. {
  189. BOOST_ASSERT(p == 0 || p != px); // catch self-reset errors
  190. this_type(p).swap(*this);
  191. }
  192. template<class Y, class D> void reset(Y * p, D d)
  193. {
  194. this_type(p, d).swap(*this);
  195. }
  196. reference operator* () const // never throws
  197. {
  198. BOOST_ASSERT(px != 0);
  199. return *px;
  200. }
  201. T * operator-> () const // never throws
  202. {
  203. BOOST_ASSERT(px != 0);
  204. return px;
  205. }
  206. T * get() const // never throws
  207. {
  208. return px;
  209. }
  210. // implicit conversion to "bool"
  211. #if defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x530)
  212. operator bool () const
  213. {
  214. return px != 0;
  215. }
  216. #elif defined(__MWERKS__) && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
  217. typedef T * (this_type::*unspecified_bool_type)() const;
  218. operator unspecified_bool_type() const // never throws
  219. {
  220. return px == 0? 0: &this_type::get;
  221. }
  222. #else
  223. typedef T * this_type::*unspecified_bool_type;
  224. operator unspecified_bool_type() const // never throws
  225. {
  226. return px == 0? 0: &this_type::px;
  227. }
  228. #endif
  229. // operator! is redundant, but some compilers need it
  230. bool operator! () const // never throws
  231. {
  232. return px == 0;
  233. }
  234. bool unique() const // never throws
  235. {
  236. return pn.unique();
  237. }
  238. long use_count() const // never throws
  239. {
  240. return pn.use_count();
  241. }
  242. void swap(shared_ptr<T> & other) // never throws
  243. {
  244. std::swap(px, other.px);
  245. pn.swap(other.pn);
  246. }
  247. template<class Y> bool _internal_less(shared_ptr<Y> const & rhs) const
  248. {
  249. return pn < rhs.pn;
  250. }
  251. void * _internal_get_deleter(std::type_info const & ti) const
  252. {
  253. return pn.get_deleter(ti);
  254. }
  255. // Tasteless as this may seem, making all members public allows member templates
  256. // to work in the absence of member template friends. (Matthew Langston)
  257. #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  258. private:
  259. template<class Y> friend class shared_ptr;
  260. template<class Y> friend class weak_ptr;
  261. #endif
  262. public: // for serialization
  263. T * px; // contained pointer
  264. detail::shared_count pn; // reference counter
  265. }; // shared_ptr
  266. template<class T, class U> inline bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b)
  267. {
  268. return a.get() == b.get();
  269. }
  270. template<class T, class U> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<U> const & b)
  271. {
  272. return a.get() != b.get();
  273. }
  274. #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
  275. // Resolve the ambiguity between our op!= and the one in rel_ops
  276. template<class T> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<T> const & b)
  277. {
  278. return a.get() != b.get();
  279. }
  280. #endif
  281. template<class T, class U> inline bool operator<(shared_ptr<T> const & a, shared_ptr<U> const & b)
  282. {
  283. return a._internal_less(b);
  284. }
  285. template<class T> inline void swap(shared_ptr<T> & a, shared_ptr<T> & b)
  286. {
  287. a.swap(b);
  288. }
  289. template<class T, class U> shared_ptr<T> static_pointer_cast(shared_ptr<U> const & r)
  290. {
  291. return shared_ptr<T>(r, detail::static_cast_tag());
  292. }
  293. template<class T, class U> shared_ptr<T> const_pointer_cast(shared_ptr<U> const & r)
  294. {
  295. return shared_ptr<T>(r, detail::const_cast_tag());
  296. }
  297. template<class T, class U> shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const & r)
  298. {
  299. return shared_ptr<T>(r, detail::dynamic_cast_tag());
  300. }
  301. // shared_*_cast names are deprecated. Use *_pointer_cast instead.
  302. template<class T, class U> shared_ptr<T> shared_static_cast(shared_ptr<U> const & r)
  303. {
  304. return shared_ptr<T>(r, detail::static_cast_tag());
  305. }
  306. template<class T, class U> shared_ptr<T> shared_dynamic_cast(shared_ptr<U> const & r)
  307. {
  308. return shared_ptr<T>(r, detail::dynamic_cast_tag());
  309. }
  310. template<class T, class U> shared_ptr<T> shared_polymorphic_cast(shared_ptr<U> const & r)
  311. {
  312. return shared_ptr<T>(r, detail::polymorphic_cast_tag());
  313. }
  314. template<class T, class U> shared_ptr<T> shared_polymorphic_downcast(shared_ptr<U> const & r)
  315. {
  316. BOOST_ASSERT(dynamic_cast<T *>(r.get()) == r.get());
  317. return shared_static_cast<T>(r);
  318. }
  319. // get_pointer() enables boost::mem_fn to recognize shared_ptr
  320. template<class T> inline T * get_pointer(shared_ptr<T> const & p)
  321. {
  322. return p.get();
  323. }
  324. // operator<<
  325. #if defined(__GNUC__) && (__GNUC__ < 3)
  326. template<class Y> std::ostream & operator<< (std::ostream & os, shared_ptr<Y> const & p)
  327. {
  328. os << p.get();
  329. return os;
  330. }
  331. #else
  332. # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, <= 1200 && __SGI_STL_PORT)
  333. // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
  334. using std::basic_ostream;
  335. template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, shared_ptr<Y> const & p)
  336. # else
  337. template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, shared_ptr<Y> const & p)
  338. # endif
  339. {
  340. os << p.get();
  341. return os;
  342. }
  343. #endif
  344. // get_deleter (experimental)
  345. #if (defined(__GNUC__) && (__GNUC__ < 3)) || (defined(__EDG_VERSION__) && (__EDG_VERSION__ <= 238))
  346. // g++ 2.9x doesn't allow static_cast<X const *>(void *)
  347. // apparently EDG 2.38 also doesn't accept it
  348. template<class D, class T> D * get_deleter(shared_ptr<T> const & p)
  349. {
  350. void const * q = p._internal_get_deleter(typeid(D));
  351. return const_cast<D *>(static_cast<D const *>(q));
  352. }
  353. #else
  354. template<class D, class T> D * get_deleter(shared_ptr<T> const & p)
  355. {
  356. return static_cast<D *>(p._internal_get_deleter(typeid(D)));
  357. }
  358. #endif
  359. } // namespace boost
  360. #ifdef BOOST_MSVC
  361. # pragma warning(pop)
  362. #endif
  363. #endif // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
  364. #endif // #ifndef BOOST_SHARED_PTR_132_HPP_INCLUDED