shared_array.hpp 7.0 KB

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