shared_array.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #ifndef BOOST_SMART_PTR_SHARED_ARRAY_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_SHARED_ARRAY_HPP_INCLUDED
  3. //
  4. // shared_array.hpp
  5. //
  6. // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
  7. // Copyright (c) 2001, 2002, 2012 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/ for documentation.
  14. //
  15. #include <boost/smart_ptr/detail/requires_cxx11.hpp>
  16. #include <boost/config.hpp> // for broken compiler workarounds
  17. #include <memory> // TR1 cyclic inclusion fix
  18. #include <boost/assert.hpp>
  19. #include <boost/core/checked_delete.hpp>
  20. #include <boost/smart_ptr/shared_ptr.hpp>
  21. #include <boost/smart_ptr/detail/shared_count.hpp>
  22. #include <boost/smart_ptr/detail/sp_nullptr_t.hpp>
  23. #include <boost/smart_ptr/detail/sp_noexcept.hpp>
  24. #include <boost/config/workaround.hpp>
  25. #include <cstddef> // for std::ptrdiff_t
  26. #include <algorithm> // for std::swap
  27. #include <functional> // for std::less
  28. namespace boost
  29. {
  30. //
  31. // shared_array
  32. //
  33. // shared_array extends shared_ptr to arrays.
  34. // The array pointed to is deleted when the last shared_array pointing to it
  35. // is destroyed or reset.
  36. //
  37. template<class T> class shared_array
  38. {
  39. private:
  40. // Borland 5.5.1 specific workarounds
  41. typedef checked_array_deleter<T> deleter;
  42. typedef shared_array<T> this_type;
  43. public:
  44. typedef T element_type;
  45. shared_array() BOOST_SP_NOEXCEPT : px( 0 ), pn()
  46. {
  47. }
  48. #if !defined( BOOST_NO_CXX11_NULLPTR )
  49. shared_array( boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT : px( 0 ), pn()
  50. {
  51. }
  52. #endif
  53. template<class Y>
  54. explicit shared_array( Y * p ): px( p ), pn( p, checked_array_deleter<Y>() )
  55. {
  56. boost::detail::sp_assert_convertible< Y[], T[] >();
  57. }
  58. //
  59. // Requirements: D's copy constructor must not throw
  60. //
  61. // shared_array will release p by calling d(p)
  62. //
  63. template<class Y, class D> shared_array( Y * p, D d ): px( p ), pn( p, d )
  64. {
  65. boost::detail::sp_assert_convertible< Y[], T[] >();
  66. }
  67. // As above, but with allocator. A's copy constructor shall not throw.
  68. template<class Y, class D, class A> shared_array( Y * p, D d, A a ): px( p ), pn( p, d, a )
  69. {
  70. boost::detail::sp_assert_convertible< Y[], T[] >();
  71. }
  72. // generated copy constructor, destructor are fine...
  73. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  74. // ... except in C++0x, move disables the implicit copy
  75. shared_array( shared_array const & r ) BOOST_SP_NOEXCEPT : px( r.px ), pn( r.pn )
  76. {
  77. }
  78. shared_array( shared_array && r ) BOOST_SP_NOEXCEPT : px( r.px ), pn()
  79. {
  80. pn.swap( r.pn );
  81. r.px = 0;
  82. }
  83. #endif
  84. // conversion
  85. template<class Y>
  86. #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
  87. shared_array( shared_array<Y> const & r, typename boost::detail::sp_enable_if_convertible< Y[], T[] >::type = boost::detail::sp_empty() )
  88. #else
  89. shared_array( shared_array<Y> const & r )
  90. #endif
  91. BOOST_SP_NOEXCEPT : px( r.px ), pn( r.pn )
  92. {
  93. boost::detail::sp_assert_convertible< Y[], T[] >();
  94. }
  95. // aliasing
  96. template< class Y >
  97. shared_array( shared_array<Y> const & r, element_type * p ) BOOST_SP_NOEXCEPT : px( p ), pn( r.pn )
  98. {
  99. }
  100. // assignment
  101. shared_array & operator=( shared_array const & r ) BOOST_SP_NOEXCEPT
  102. {
  103. this_type( r ).swap( *this );
  104. return *this;
  105. }
  106. #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1400)
  107. template<class Y>
  108. shared_array & operator=( shared_array<Y> const & r ) BOOST_SP_NOEXCEPT
  109. {
  110. this_type( r ).swap( *this );
  111. return *this;
  112. }
  113. #endif
  114. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  115. shared_array & operator=( shared_array && r ) BOOST_SP_NOEXCEPT
  116. {
  117. this_type( static_cast< shared_array && >( r ) ).swap( *this );
  118. return *this;
  119. }
  120. template<class Y>
  121. shared_array & operator=( shared_array<Y> && r ) BOOST_SP_NOEXCEPT
  122. {
  123. this_type( static_cast< shared_array<Y> && >( r ) ).swap( *this );
  124. return *this;
  125. }
  126. #endif
  127. void reset() BOOST_SP_NOEXCEPT
  128. {
  129. this_type().swap( *this );
  130. }
  131. template<class Y> void reset( Y * p ) // Y must be complete
  132. {
  133. BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors
  134. this_type( p ).swap( *this );
  135. }
  136. template<class Y, class D> void reset( Y * p, D d )
  137. {
  138. this_type( p, d ).swap( *this );
  139. }
  140. template<class Y, class D, class A> void reset( Y * p, D d, A a )
  141. {
  142. this_type( p, d, a ).swap( *this );
  143. }
  144. template<class Y> void reset( shared_array<Y> const & r, element_type * p ) BOOST_SP_NOEXCEPT
  145. {
  146. this_type( r, p ).swap( *this );
  147. }
  148. T & operator[] (std::ptrdiff_t i) const BOOST_SP_NOEXCEPT_WITH_ASSERT
  149. {
  150. BOOST_ASSERT(px != 0);
  151. BOOST_ASSERT(i >= 0);
  152. return px[i];
  153. }
  154. T * get() const BOOST_SP_NOEXCEPT
  155. {
  156. return px;
  157. }
  158. // implicit conversion to "bool"
  159. #include <boost/smart_ptr/detail/operator_bool.hpp>
  160. bool unique() const BOOST_SP_NOEXCEPT
  161. {
  162. return pn.unique();
  163. }
  164. long use_count() const BOOST_SP_NOEXCEPT
  165. {
  166. return pn.use_count();
  167. }
  168. void swap(shared_array<T> & other) BOOST_SP_NOEXCEPT
  169. {
  170. std::swap(px, other.px);
  171. pn.swap(other.pn);
  172. }
  173. void * _internal_get_deleter( boost::detail::sp_typeinfo_ const & ti ) const BOOST_SP_NOEXCEPT
  174. {
  175. return pn.get_deleter( ti );
  176. }
  177. private:
  178. template<class Y> friend class shared_array;
  179. T * px; // contained pointer
  180. detail::shared_count pn; // reference counter
  181. }; // shared_array
  182. template<class T> inline bool operator==(shared_array<T> const & a, shared_array<T> const & b) BOOST_SP_NOEXCEPT
  183. {
  184. return a.get() == b.get();
  185. }
  186. template<class T> inline bool operator!=(shared_array<T> const & a, shared_array<T> const & b) BOOST_SP_NOEXCEPT
  187. {
  188. return a.get() != b.get();
  189. }
  190. #if !defined( BOOST_NO_CXX11_NULLPTR )
  191. template<class T> inline bool operator==( shared_array<T> const & p, boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT
  192. {
  193. return p.get() == 0;
  194. }
  195. template<class T> inline bool operator==( boost::detail::sp_nullptr_t, shared_array<T> const & p ) BOOST_SP_NOEXCEPT
  196. {
  197. return p.get() == 0;
  198. }
  199. template<class T> inline bool operator!=( shared_array<T> const & p, boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT
  200. {
  201. return p.get() != 0;
  202. }
  203. template<class T> inline bool operator!=( boost::detail::sp_nullptr_t, shared_array<T> const & p ) BOOST_SP_NOEXCEPT
  204. {
  205. return p.get() != 0;
  206. }
  207. #endif
  208. template<class T> inline bool operator<(shared_array<T> const & a, shared_array<T> const & b) BOOST_SP_NOEXCEPT
  209. {
  210. return std::less<T*>()(a.get(), b.get());
  211. }
  212. template<class T> void swap(shared_array<T> & a, shared_array<T> & b) BOOST_SP_NOEXCEPT
  213. {
  214. a.swap(b);
  215. }
  216. template< class D, class T > D * get_deleter( shared_array<T> const & p ) BOOST_SP_NOEXCEPT
  217. {
  218. return static_cast< D * >( p._internal_get_deleter( BOOST_SP_TYPEID_(D) ) );
  219. }
  220. } // namespace boost
  221. #endif // #ifndef BOOST_SMART_PTR_SHARED_ARRAY_HPP_INCLUDED