shared_array.hpp 7.1 KB

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