shared_array.hpp 7.1 KB

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