ptr_array.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. //
  2. // Boost.Pointer Container
  3. //
  4. // Copyright Thorsten Ottosen 2003-2005. Use, modification and
  5. // distribution is subject to the Boost Software License, Version
  6. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // For more information, see http://www.boost.org/libs/ptr_container/
  10. //
  11. #ifndef BOOST_PTR_CONTAINER_PTR_ARRAY_HPP
  12. #define BOOST_PTR_CONTAINER_PTR_ARRAY_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif
  16. #include <boost/array.hpp>
  17. #include <boost/static_assert.hpp>
  18. #include <boost/ptr_container/ptr_sequence_adapter.hpp>
  19. #include <boost/ptr_container/detail/ptr_container_disable_deprecated.hpp>
  20. #if defined(BOOST_PTR_CONTAINER_DISABLE_DEPRECATED)
  21. #pragma GCC diagnostic push
  22. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  23. #endif
  24. namespace boost
  25. {
  26. namespace ptr_container_detail
  27. {
  28. template
  29. <
  30. class T,
  31. size_t N,
  32. class Allocator = int // dummy
  33. >
  34. class ptr_array_impl : public boost::array<T,N>
  35. {
  36. public:
  37. typedef Allocator allocator_type;
  38. ptr_array_impl( Allocator /*a*/ = Allocator() )
  39. {
  40. this->assign( 0 );
  41. }
  42. ptr_array_impl( size_t, T*, Allocator /*a*/ = Allocator() )
  43. {
  44. this->assign( 0 );
  45. }
  46. };
  47. }
  48. template
  49. <
  50. class T,
  51. size_t N,
  52. class CloneAllocator = heap_clone_allocator
  53. >
  54. class ptr_array : public
  55. ptr_sequence_adapter< T,
  56. ptr_container_detail::ptr_array_impl<
  57. typename ptr_container_detail::void_ptr<T>::type,N>,
  58. CloneAllocator >
  59. {
  60. private:
  61. typedef ptr_sequence_adapter< T,
  62. ptr_container_detail::ptr_array_impl<
  63. typename ptr_container_detail::void_ptr<T>::type,N>,
  64. CloneAllocator >
  65. base_class;
  66. typedef BOOST_DEDUCED_TYPENAME remove_nullable<T>::type U;
  67. typedef ptr_array<T,N,CloneAllocator>
  68. this_type;
  69. public:
  70. typedef std::size_t size_type;
  71. typedef U* value_type;
  72. typedef U* pointer;
  73. typedef U& reference;
  74. typedef const U& const_reference;
  75. typedef BOOST_DEDUCED_TYPENAME base_class::auto_type
  76. auto_type;
  77. public: // constructors
  78. ptr_array() : base_class()
  79. { }
  80. ptr_array( const ptr_array& r )
  81. {
  82. size_t i = 0;
  83. for( ; i != N; ++i )
  84. this->base()[i] = this->null_policy_allocate_clone(
  85. static_cast<const U*>( &r[i] ) );
  86. }
  87. template< class U >
  88. ptr_array( const ptr_array<U,N>& r )
  89. {
  90. size_t i = 0;
  91. for( ; i != N; ++i )
  92. this->base()[i] = this->null_policy_allocate_clone(
  93. static_cast<const T*>( &r[i] ) );
  94. }
  95. #ifndef BOOST_NO_AUTO_PTR
  96. explicit ptr_array( std::auto_ptr<this_type> r )
  97. : base_class( r ) { }
  98. #endif
  99. #ifndef BOOST_NO_CXX11_SMART_PTR
  100. explicit ptr_array( std::unique_ptr<this_type> r )
  101. : base_class( std::move( r ) ) { }
  102. #endif
  103. ptr_array& operator=( ptr_array r )
  104. {
  105. this->swap( r );
  106. return *this;
  107. }
  108. #ifndef BOOST_NO_AUTO_PTR
  109. ptr_array& operator=( std::auto_ptr<this_type> r )
  110. {
  111. base_class::operator=(r);
  112. return *this;
  113. }
  114. #endif
  115. #ifndef BOOST_NO_CXX11_SMART_PTR
  116. ptr_array& operator=( std::unique_ptr<this_type> r )
  117. {
  118. base_class::operator=(std::move(r));
  119. return *this;
  120. }
  121. #endif
  122. #ifndef BOOST_NO_AUTO_PTR
  123. std::auto_ptr<this_type> release()
  124. {
  125. std::auto_ptr<this_type> ptr( new this_type );
  126. this->swap( *ptr );
  127. return ptr;
  128. }
  129. std::auto_ptr<this_type> clone() const
  130. {
  131. std::auto_ptr<this_type> pa( new this_type );
  132. clone_array_elements( *pa );
  133. return pa;
  134. }
  135. #else
  136. std::unique_ptr<this_type> release()
  137. {
  138. std::unique_ptr<this_type> ptr( new this_type );
  139. this->swap( *ptr );
  140. return ptr;
  141. }
  142. std::unique_ptr<this_type> clone() const
  143. {
  144. std::unique_ptr<this_type> pa( new this_type );
  145. clone_array_elements( *pa );
  146. return pa;
  147. }
  148. #endif
  149. private:
  150. void clone_array_elements( this_type &pa ) const
  151. {
  152. for( size_t i = 0; i != N; ++i )
  153. {
  154. if( !this->is_null(i) )
  155. pa.replace( i, pa.null_policy_allocate_clone( &(*this)[i] ) );
  156. }
  157. }
  158. private: // hide some members
  159. using base_class::insert;
  160. using base_class::erase;
  161. using base_class::push_back;
  162. using base_class::push_front;
  163. using base_class::pop_front;
  164. using base_class::pop_back;
  165. using base_class::transfer;
  166. using base_class::get_allocator;
  167. public: // compile-time interface
  168. template< size_t idx >
  169. auto_type replace( U* r ) // strong
  170. {
  171. BOOST_STATIC_ASSERT( idx < N );
  172. this->enforce_null_policy( r, "Null pointer in 'ptr_array::replace()'" );
  173. auto_type res( static_cast<U*>(this->base()[idx]), *this ); // nothrow
  174. this->base()[idx] = r; // nothrow
  175. return boost::ptr_container::move(res); // nothrow
  176. }
  177. #ifndef BOOST_NO_AUTO_PTR
  178. template< size_t idx, class V >
  179. auto_type replace( std::auto_ptr<V> r )
  180. {
  181. return replace<idx>( r.release() );
  182. }
  183. #endif
  184. #ifndef BOOST_NO_CXX11_SMART_PTR
  185. template< size_t idx, class V >
  186. auto_type replace( std::unique_ptr<V> r )
  187. {
  188. return replace<idx>( r.release() );
  189. }
  190. #endif
  191. auto_type replace( size_t idx, U* r ) // strong
  192. {
  193. this->enforce_null_policy( r, "Null pointer in 'ptr_array::replace()'" );
  194. auto_type ptr( r, *this );
  195. BOOST_PTR_CONTAINER_THROW_EXCEPTION( idx >= N, bad_index,
  196. "'replace()' aout of bounds" );
  197. auto_type res( static_cast<U*>(this->base()[idx]), *this ); // nothrow
  198. this->base()[idx] = ptr.release(); // nothrow
  199. return boost::ptr_container::move(res); // nothrow
  200. }
  201. #ifndef BOOST_NO_AUTO_PTR
  202. template< class V >
  203. auto_type replace( size_t idx, std::auto_ptr<V> r )
  204. {
  205. return replace( idx, r.release() );
  206. }
  207. #endif
  208. #ifndef BOOST_NO_CXX11_SMART_PTR
  209. template< class V >
  210. auto_type replace( size_t idx, std::unique_ptr<V> r )
  211. {
  212. return replace( idx, r.release() );
  213. }
  214. #endif
  215. using base_class::at;
  216. template< size_t idx >
  217. T& at()
  218. {
  219. BOOST_STATIC_ASSERT( idx < N );
  220. return (*this)[idx];
  221. }
  222. template< size_t idx >
  223. const T& at() const
  224. {
  225. BOOST_STATIC_ASSERT( idx < N );
  226. return (*this)[idx];
  227. }
  228. bool is_null( size_t idx ) const
  229. {
  230. return base_class::is_null(idx);
  231. }
  232. template< size_t idx >
  233. bool is_null() const
  234. {
  235. BOOST_STATIC_ASSERT( idx < N );
  236. return this->base()[idx] == 0;
  237. }
  238. };
  239. //////////////////////////////////////////////////////////////////////////////
  240. // clonability
  241. template< typename T, size_t size, typename CA >
  242. inline ptr_array<T,size,CA>* new_clone( const ptr_array<T,size,CA>& r )
  243. {
  244. return r.clone().release();
  245. }
  246. /////////////////////////////////////////////////////////////////////////
  247. // swap
  248. template< typename T, size_t size, typename CA >
  249. inline void swap( ptr_array<T,size,CA>& l, ptr_array<T,size,CA>& r )
  250. {
  251. l.swap(r);
  252. }
  253. }
  254. #if defined(BOOST_PTR_CONTAINER_DISABLE_DEPRECATED)
  255. #pragma GCC diagnostic pop
  256. #endif
  257. #endif