shared_array.hpp 6.3 KB

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