small_vector.hpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2015-2015. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/container for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_CONTAINER_CONTAINER_SMALL_VECTOR_HPP
  11. #define BOOST_CONTAINER_CONTAINER_SMALL_VECTOR_HPP
  12. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #if defined(BOOST_HAS_PRAGMA_ONCE)
  16. # pragma once
  17. #endif
  18. #include <boost/container/detail/config_begin.hpp>
  19. #include <boost/container/detail/workaround.hpp>
  20. // container
  21. #include <boost/container/container_fwd.hpp>
  22. #include <boost/container/vector.hpp>
  23. #include <boost/container/allocator_traits.hpp>
  24. #include <boost/container/new_allocator.hpp> //new_allocator
  25. // container/detail
  26. #include <boost/container/detail/type_traits.hpp>
  27. #include <boost/container/detail/version_type.hpp>
  28. //move
  29. #include <boost/move/adl_move_swap.hpp>
  30. #include <boost/move/iterator.hpp>
  31. //move/detail
  32. #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  33. #include <boost/move/detail/fwd_macros.hpp>
  34. #endif
  35. #include <boost/move/detail/force_ptr.hpp>
  36. //std
  37. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  38. #include <initializer_list> //for std::initializer_list
  39. #endif
  40. namespace boost {
  41. namespace container {
  42. namespace dtl{
  43. template<class Options>
  44. struct get_small_vector_opt
  45. {
  46. typedef Options type;
  47. };
  48. template<>
  49. struct get_small_vector_opt<void>
  50. {
  51. typedef small_vector_null_opt type;
  52. };
  53. template<class Options>
  54. struct get_vopt_from_svopt
  55. : get_small_vector_opt<Options>::type
  56. {
  57. typedef typename get_small_vector_opt<Options>::type options_t;
  58. typedef vector_opt< typename options_t::growth_factor_type, void> type;
  59. };
  60. template<>
  61. struct get_vopt_from_svopt<void>
  62. {
  63. typedef void type;
  64. };
  65. template <class T, class SecAlloc, class Options>
  66. struct vector_for_small_vector
  67. {
  68. typedef vector
  69. < T
  70. , small_vector_allocator
  71. < T
  72. , typename allocator_traits<typename real_allocator<T, SecAlloc>::type>::template portable_rebind_alloc<void>::type
  73. , Options>
  74. , typename dtl::get_vopt_from_svopt<Options>::type
  75. > type;
  76. };
  77. } //namespace dtl
  78. //! A non-standard allocator used to implement `small_vector`.
  79. //! Users should never use it directly. It is described here
  80. //! for documentation purposes.
  81. //!
  82. //! This allocator inherits from a standard-conforming allocator
  83. //! and forwards member functions to the standard allocator except
  84. //! when internal storage is being used as memory source.
  85. //!
  86. //! This allocator is a "partially_propagable" allocator and
  87. //! defines `is_partially_propagable` as true_type.
  88. //!
  89. //! A partially propagable allocator means that not all storage
  90. //! allocatod by an instance of `small_vector_allocator` can be
  91. //! deallocated by another instance of this type, even if both
  92. //! instances compare equal or an instance is propagated to another
  93. //! one using the copy/move constructor or assignment. The storage that
  94. //! can never be propagated is identified by `storage_is_unpropagable(p)`.
  95. //!
  96. //! `boost::container::vector` supports partially propagable allocators
  97. //! fallbacking to deep copy/swap/move operations when internal storage
  98. //! is being used to store vector elements.
  99. //!
  100. //! `small_vector_allocator` assumes that will be instantiated as
  101. //! `boost::container::vector< T, small_vector_allocator<T, Allocator> >`
  102. //! and internal storage can be obtained downcasting that vector
  103. //! to `small_vector_base<T>`.
  104. template<class T, class VoidAlloc BOOST_CONTAINER_DOCONLY(= void), class Options BOOST_CONTAINER_DOCONLY(= void)>
  105. class small_vector_allocator
  106. : public allocator_traits<VoidAlloc>::template portable_rebind_alloc<T>::type
  107. {
  108. typedef unsigned int allocation_type;
  109. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  110. private:
  111. typedef typename allocator_traits<VoidAlloc>::template portable_rebind_alloc<T>::type allocator_type;
  112. BOOST_COPYABLE_AND_MOVABLE(small_vector_allocator)
  113. BOOST_CONTAINER_FORCEINLINE const allocator_type &as_base() const BOOST_NOEXCEPT
  114. { return static_cast<const allocator_type&>(*this); }
  115. BOOST_CONTAINER_FORCEINLINE allocator_type &as_base() BOOST_NOEXCEPT
  116. { return static_cast<allocator_type&>(*this); }
  117. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  118. public:
  119. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  120. typedef allocator_traits<allocator_type> allocator_traits_type;
  121. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  122. typedef typename allocator_traits<allocator_type>::value_type value_type;
  123. typedef typename allocator_traits<allocator_type>::pointer pointer;
  124. typedef typename allocator_traits<allocator_type>::const_pointer const_pointer;
  125. typedef typename allocator_traits<allocator_type>::reference reference;
  126. typedef typename allocator_traits<allocator_type>::const_reference const_reference;
  127. typedef typename allocator_traits<allocator_type>::size_type size_type;
  128. typedef typename allocator_traits<allocator_type>::difference_type difference_type;
  129. typedef typename allocator_traits<allocator_type>::void_pointer void_pointer;
  130. typedef typename allocator_traits<allocator_type>::const_void_pointer const_void_pointer;
  131. typedef typename allocator_traits<allocator_type>::propagate_on_container_copy_assignment propagate_on_container_copy_assignment;
  132. typedef typename allocator_traits<allocator_type>::propagate_on_container_move_assignment propagate_on_container_move_assignment;
  133. typedef typename allocator_traits<allocator_type>::propagate_on_container_swap propagate_on_container_swap;
  134. //! An integral constant with member `value == false`
  135. typedef BOOST_CONTAINER_IMPDEF(dtl::bool_<false>) is_always_equal;
  136. //! An integral constant with member `value == true`
  137. typedef BOOST_CONTAINER_IMPDEF(dtl::bool_<true>) is_partially_propagable;
  138. BOOST_CONTAINER_DOCIGN(typedef dtl::version_type<small_vector_allocator BOOST_CONTAINER_I 1> version;)
  139. //!Obtains an small_vector_allocator that allocates
  140. //!objects of type T2
  141. template<class T2>
  142. struct rebind
  143. {
  144. typedef typename allocator_traits<allocator_type>::template portable_rebind_alloc<T2>::type other;
  145. };
  146. BOOST_CONTAINER_FORCEINLINE small_vector_allocator() BOOST_NOEXCEPT_IF(dtl::is_nothrow_default_constructible<allocator_type>::value)
  147. {}
  148. //!Constructor from other small_vector_allocator.
  149. //!Never throws
  150. BOOST_CONTAINER_FORCEINLINE small_vector_allocator
  151. (const small_vector_allocator &other) BOOST_NOEXCEPT_OR_NOTHROW
  152. : allocator_type(other.as_base())
  153. {}
  154. //!Move constructor from small_vector_allocator.
  155. //!Never throws
  156. BOOST_CONTAINER_FORCEINLINE small_vector_allocator
  157. (BOOST_RV_REF(small_vector_allocator) other) BOOST_NOEXCEPT_OR_NOTHROW
  158. : allocator_type(::boost::move(other.as_base()))
  159. {}
  160. //!Constructor from related small_vector_allocator.
  161. //!Never throws
  162. template<class U, class OtherVoidAllocator, class OtherOptions>
  163. BOOST_CONTAINER_FORCEINLINE small_vector_allocator
  164. (const small_vector_allocator<U, OtherVoidAllocator, OtherOptions> &other) BOOST_NOEXCEPT_OR_NOTHROW
  165. : allocator_type(other.as_base())
  166. {}
  167. //!Move constructor from related small_vector_allocator.
  168. //!Never throws
  169. template<class U, class OtherVoidAllocator, class OtherOptions>
  170. BOOST_CONTAINER_FORCEINLINE small_vector_allocator
  171. (BOOST_RV_REF(small_vector_allocator<U BOOST_MOVE_I OtherVoidAllocator BOOST_MOVE_I OtherOptions>) other) BOOST_NOEXCEPT_OR_NOTHROW
  172. : allocator_type(::boost::move(other.as_base()))
  173. {}
  174. //!Constructor from allocator_type.
  175. //!Never throws
  176. BOOST_CONTAINER_FORCEINLINE explicit small_vector_allocator
  177. (const allocator_type &other) BOOST_NOEXCEPT_OR_NOTHROW
  178. : allocator_type(other)
  179. {}
  180. //!Assignment from other small_vector_allocator.
  181. //!Never throws
  182. BOOST_CONTAINER_FORCEINLINE small_vector_allocator &
  183. operator=(BOOST_COPY_ASSIGN_REF(small_vector_allocator) other) BOOST_NOEXCEPT_OR_NOTHROW
  184. { return static_cast<small_vector_allocator&>(this->allocator_type::operator=(other.as_base())); }
  185. //!Move assignment from other small_vector_allocator.
  186. //!Never throws
  187. BOOST_CONTAINER_FORCEINLINE small_vector_allocator &
  188. operator=(BOOST_RV_REF(small_vector_allocator) other) BOOST_NOEXCEPT_OR_NOTHROW
  189. { return static_cast<small_vector_allocator&>(this->allocator_type::operator=(::boost::move(other.as_base()))); }
  190. //!Assignment from related small_vector_allocator.
  191. //!Never throws
  192. template<class U, class OtherVoidAllocator>
  193. BOOST_CONTAINER_FORCEINLINE small_vector_allocator &
  194. operator=(BOOST_COPY_ASSIGN_REF(small_vector_allocator<U BOOST_MOVE_I OtherVoidAllocator BOOST_MOVE_I Options>) other) BOOST_NOEXCEPT_OR_NOTHROW
  195. { return static_cast<small_vector_allocator&>(this->allocator_type::operator=(other.as_base())); }
  196. //!Move assignment from related small_vector_allocator.
  197. //!Never throws
  198. template<class U, class OtherVoidAllocator>
  199. BOOST_CONTAINER_FORCEINLINE small_vector_allocator &
  200. operator=(BOOST_RV_REF(small_vector_allocator<U BOOST_MOVE_I OtherVoidAllocator BOOST_MOVE_I Options>) other) BOOST_NOEXCEPT_OR_NOTHROW
  201. { return static_cast<small_vector_allocator&>(this->allocator_type::operator=(::boost::move(other.as_base()))); }
  202. //!Move assignment from allocator_type.
  203. //!Never throws
  204. BOOST_CONTAINER_FORCEINLINE small_vector_allocator &
  205. operator=(const allocator_type &other) BOOST_NOEXCEPT_OR_NOTHROW
  206. { return static_cast<small_vector_allocator&>(this->allocator_type::operator=(other)); }
  207. //!Allocates storage from the standard-conforming allocator
  208. BOOST_CONTAINER_FORCEINLINE pointer allocate(size_type count, const_void_pointer hint = const_void_pointer())
  209. { return allocator_traits_type::allocate(this->as_base(), count, hint); }
  210. //!Deallocates previously allocated memory.
  211. //!Never throws
  212. void deallocate(pointer ptr, size_type n) BOOST_NOEXCEPT_OR_NOTHROW
  213. {
  214. if(!this->is_internal_storage(ptr))
  215. allocator_traits_type::deallocate(this->as_base(), ptr, n);
  216. }
  217. //!Returns the maximum number of elements that could be allocated.
  218. //!Never throws
  219. BOOST_CONTAINER_FORCEINLINE size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
  220. { return allocator_traits_type::max_size(this->as_base()); }
  221. small_vector_allocator select_on_container_copy_construction() const
  222. { return small_vector_allocator(allocator_traits_type::select_on_container_copy_construction(this->as_base())); }
  223. bool storage_is_unpropagable(pointer p) const
  224. { return this->is_internal_storage(p) || allocator_traits_type::storage_is_unpropagable(this->as_base(), p); }
  225. //!Swaps two allocators, does nothing
  226. //!because this small_vector_allocator is stateless
  227. BOOST_CONTAINER_FORCEINLINE friend void swap(small_vector_allocator &l, small_vector_allocator &r) BOOST_NOEXCEPT_OR_NOTHROW
  228. { boost::adl_move_swap(l.as_base(), r.as_base()); }
  229. //!An small_vector_allocator always compares to true, as memory allocated with one
  230. //!instance can be deallocated by another instance (except for unpropagable storage)
  231. BOOST_CONTAINER_FORCEINLINE friend bool operator==(const small_vector_allocator &l, const small_vector_allocator &r) BOOST_NOEXCEPT_OR_NOTHROW
  232. { return allocator_traits_type::equal(l.as_base(), r.as_base()); }
  233. //!An small_vector_allocator always compares to false, as memory allocated with one
  234. //!instance can be deallocated by another instance
  235. BOOST_CONTAINER_FORCEINLINE friend bool operator!=(const small_vector_allocator &l, const small_vector_allocator &r) BOOST_NOEXCEPT_OR_NOTHROW
  236. { return !(l == r); }
  237. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  238. private:
  239. typedef small_vector_base<value_type, allocator_type, Options> derived_type;
  240. typedef typename dtl::vector_for_small_vector
  241. <value_type, allocator_type, Options>::type vector_type;
  242. BOOST_CONTAINER_FORCEINLINE bool is_internal_storage(const_pointer p) const
  243. { return this->internal_storage() == p; }
  244. public:
  245. BOOST_CONTAINER_FORCEINLINE const_pointer internal_storage() const BOOST_NOEXCEPT_OR_NOTHROW;
  246. BOOST_CONTAINER_FORCEINLINE pointer internal_storage() BOOST_NOEXCEPT_OR_NOTHROW;
  247. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  248. };
  249. template<class T, std::size_t N, std::size_t Alignment>
  250. struct small_vector_storage
  251. {
  252. typedef typename dtl::aligned_storage
  253. <sizeof(T)*N, Alignment>::type storage_type;
  254. storage_type m_storage;
  255. static const std::size_t sms_size = sizeof(storage_type)/sizeof(T);
  256. };
  257. template<class T, std::size_t Alignment>
  258. struct small_vector_storage<T, 0u, Alignment>
  259. {
  260. static const std::size_t sms_size = 0u;
  261. };
  262. //! This class consists of common code from all small_vector<T, N> types that don't depend on the
  263. //! "N" template parameter. This class is non-copyable and non-destructible, so this class typically
  264. //! used as reference argument to functions that read or write small vectors. Since `small_vector<T, N>`
  265. //! derives from `small_vector_base<T>`, the conversion to `small_vector_base` is implicit
  266. //! <pre>
  267. //!
  268. //! //Clients can pass any small_vector<Foo, N>.
  269. //! void read_any_small_vector_of_foo(const small_vector_base<Foo> &in_parameter);
  270. //!
  271. //! void modify_any_small_vector_of_foo(small_vector_base<Foo> &in_out_parameter);
  272. //!
  273. //! void some_function()
  274. //! {
  275. //!
  276. //! small_vector<Foo, 8> myvector;
  277. //!
  278. //! read_any_small_vector_of_foo(myvector); // Reads myvector
  279. //!
  280. //! modify_any_small_vector_of_foo(myvector); // Modifies myvector
  281. //!
  282. //! }
  283. //! </pre>
  284. //!
  285. //! All `boost::container:vector` member functions are inherited. See `vector` documentation for details.
  286. //!
  287. template <class T, class SecAlloc, class Options>
  288. class small_vector_base
  289. : public dtl::vector_for_small_vector<T, SecAlloc, Options>::type
  290. {
  291. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKEDVECTOR
  292. public:
  293. //Make it public as it will be inherited by small_vector and container
  294. //must have this public member
  295. typedef typename real_allocator<T, SecAlloc>::type secondary_allocator_t;
  296. typedef typename allocator_traits<secondary_allocator_t>::
  297. template portable_rebind_alloc<void>::type void_allocator_t;
  298. typedef typename dtl::get_small_vector_opt<Options>::type options_t;
  299. typedef typename dtl::vector_for_small_vector
  300. <T, SecAlloc, Options>::type base_type;
  301. typedef typename allocator_traits<secondary_allocator_t>::pointer pointer;
  302. typedef typename allocator_traits<secondary_allocator_t>::const_pointer const_pointer;
  303. typedef typename allocator_traits<secondary_allocator_t>::void_pointer void_pointer;
  304. typedef typename allocator_traits<secondary_allocator_t>::const_void_pointer const_void_pointer;
  305. typedef small_vector_allocator<T, void_allocator_t, Options> allocator_type;
  306. private:
  307. BOOST_COPYABLE_AND_MOVABLE(small_vector_base)
  308. friend class small_vector_allocator<T, void_allocator_t, Options>;
  309. BOOST_CONTAINER_FORCEINLINE
  310. const_pointer internal_storage() const BOOST_NOEXCEPT_OR_NOTHROW
  311. { return this->base_type::get_stored_allocator().internal_storage(); }
  312. BOOST_CONTAINER_FORCEINLINE
  313. pointer internal_storage() BOOST_NOEXCEPT_OR_NOTHROW
  314. { return this->base_type::get_stored_allocator().internal_storage(); }
  315. private:
  316. base_type &as_base() { return static_cast<base_type&>(*this); }
  317. const base_type &as_base() const { return static_cast<const base_type&>(*this); }
  318. public:
  319. protected:
  320. BOOST_CONTAINER_FORCEINLINE explicit small_vector_base(initial_capacity_t, std::size_t initial_capacity)
  321. : base_type(initial_capacity_t(), this->internal_storage(), initial_capacity)
  322. {}
  323. template<class AllocFwd>
  324. BOOST_CONTAINER_FORCEINLINE explicit small_vector_base(initial_capacity_t, std::size_t capacity, BOOST_FWD_REF(AllocFwd) a)
  325. : base_type(initial_capacity_t(), this->internal_storage(), capacity, ::boost::forward<AllocFwd>(a))
  326. {}
  327. BOOST_CONTAINER_FORCEINLINE explicit small_vector_base(maybe_initial_capacity_t, std::size_t initial_capacity, std::size_t initial_size)
  328. : base_type( maybe_initial_capacity_t()
  329. , (initial_capacity >= initial_size) ? this->internal_storage() : pointer()
  330. , (initial_capacity >= initial_size) ? initial_capacity : initial_size
  331. )
  332. {}
  333. template<class AllocFwd>
  334. BOOST_CONTAINER_FORCEINLINE explicit small_vector_base(maybe_initial_capacity_t, std::size_t initial_capacity, std::size_t initial_size, BOOST_FWD_REF(AllocFwd) a)
  335. : base_type(maybe_initial_capacity_t()
  336. , (initial_capacity >= initial_size) ? this->internal_storage() : pointer()
  337. , (initial_capacity >= initial_size) ? initial_capacity : initial_size
  338. , ::boost::forward<AllocFwd>(a)
  339. )
  340. {}
  341. using base_type::protected_set_size;
  342. //~small_vector_base(){}
  343. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  344. public:
  345. BOOST_CONTAINER_FORCEINLINE small_vector_base& operator=(BOOST_COPY_ASSIGN_REF(small_vector_base) other)
  346. { return static_cast<small_vector_base&>(this->base_type::operator=(static_cast<base_type const&>(other))); }
  347. BOOST_CONTAINER_FORCEINLINE small_vector_base& operator=(BOOST_RV_REF(small_vector_base) other)
  348. { return static_cast<small_vector_base&>(this->base_type::operator=(BOOST_MOVE_BASE(base_type, other))); }
  349. BOOST_CONTAINER_FORCEINLINE void swap(small_vector_base &other)
  350. { return this->base_type::swap(other); }
  351. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  352. protected:
  353. void move_construct_impl(base_type &x, const allocator_type &a)
  354. {
  355. if(base_type::is_propagable_from(x.get_stored_allocator(), x.data(), a, true)){
  356. this->steal_resources(x);
  357. }
  358. else{
  359. const typename base_type::size_type sz = x.size();
  360. ::boost::container::uninitialized_move_alloc_n_source
  361. (this->base_type::get_stored_allocator(), x.begin(), sz, this->begin());
  362. this->protected_set_size(sz);
  363. x.clear();
  364. }
  365. }
  366. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  367. };
  368. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  369. /////////////////////////////////////////////////////
  370. //
  371. // small_vector_storage_definer
  372. //
  373. /////////////////////////////////////////////////////
  374. template<class T, std::size_t N, class Options>
  375. struct small_vector_storage_definer
  376. {
  377. typedef typename dtl::get_small_vector_opt<Options>::type options_t;
  378. static const std::size_t final_alignment =
  379. options_t::inplace_alignment ? options_t::inplace_alignment : dtl::alignment_of<T>::value;
  380. typedef small_vector_storage<T, N, final_alignment> type;
  381. };
  382. template <class T, class SecAlloc, class Options>
  383. struct small_vector_storage_strawman
  384. : public small_vector_base<T, SecAlloc, Options>
  385. , public small_vector_storage_definer<T, 1, Options>::type
  386. {
  387. typedef typename small_vector_storage_definer<T, 1, Options>::type sm_storage_t;
  388. };
  389. //Internal storage hack
  390. template<class T, class VoidAlloc, class Options>
  391. BOOST_CONTAINER_FORCEINLINE typename small_vector_allocator<T, VoidAlloc, Options>::const_pointer
  392. small_vector_allocator<T, VoidAlloc, Options>::internal_storage() const BOOST_NOEXCEPT_OR_NOTHROW
  393. {
  394. typedef small_vector_storage_strawman<T, allocator_type, Options> strawman_t;
  395. typedef typename strawman_t::sm_storage_t sm_storage_t;
  396. //These warnings are false positives, as we know the alignment is correct
  397. //and aligned storage is allowed to hold any type
  398. #if defined(BOOST_GCC) && (BOOST_GCC >= 40600)
  399. #pragma GCC diagnostic push
  400. #pragma GCC diagnostic ignored "-Wcast-align"
  401. #pragma GCC diagnostic ignored "-Wstrict-aliasing"
  402. #endif
  403. const vector_type& v = reinterpret_cast<const vector_type&>(*this);
  404. BOOST_ASSERT((std::size_t(this) % dtl::alignment_of<strawman_t>::value) == 0);
  405. const strawman_t &straw = static_cast<const strawman_t&>(v);
  406. const sm_storage_t& stor = static_cast<const sm_storage_t&>(straw);
  407. return boost::intrusive::pointer_traits<const_pointer>::pointer_to(*((const T*)stor.m_storage.data));
  408. #if defined(BOOST_GCC) && (BOOST_GCC >= 40600)
  409. #pragma GCC diagnostic pop
  410. #endif
  411. }
  412. template <class T, class VoidAlloc, class Options>
  413. BOOST_CONTAINER_FORCEINLINE typename small_vector_allocator<T, VoidAlloc, Options>::pointer
  414. small_vector_allocator<T, VoidAlloc, Options>::internal_storage() BOOST_NOEXCEPT_OR_NOTHROW
  415. {
  416. typedef small_vector_storage_strawman<T, allocator_type, Options> strawman_t;
  417. typedef typename strawman_t::sm_storage_t sm_storage_t;
  418. #if defined(BOOST_GCC) && (BOOST_GCC >= 40600)
  419. #pragma GCC diagnostic push
  420. #pragma GCC diagnostic ignored "-Wcast-align"
  421. #pragma GCC diagnostic ignored "-Wstrict-aliasing"
  422. #endif
  423. vector_type& v = reinterpret_cast<vector_type&>(*this);
  424. BOOST_ASSERT((std::size_t(this) % dtl::alignment_of<strawman_t>::value) == 0);
  425. strawman_t &straw = static_cast<strawman_t&>(v);
  426. sm_storage_t& stor = static_cast<sm_storage_t&>(straw);
  427. return boost::intrusive::pointer_traits<pointer>::pointer_to(*((T*)stor.m_storage.data));
  428. #if defined(BOOST_GCC) && (BOOST_GCC >= 40600)
  429. #pragma GCC diagnostic pop
  430. #endif
  431. }
  432. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  433. //! small_vector is a vector-like container optimized for the case when it contains few elements.
  434. //! It contains some preallocated elements in-place, which can avoid the use of dynamic storage allocation
  435. //! when the actual number of elements is below that preallocated threshold.
  436. //!
  437. //! `small_vector<T, N, Allocator, Options>` is convertible to `small_vector_base<T, Allocator, Options>` that is independent
  438. //! from the preallocated element capacity, so client code does not need to be templated on that N argument.
  439. //!
  440. //! All `boost::container::vector` member functions are inherited. See `vector` documentation for details.
  441. //!
  442. //! Any change to the capacity of the vector, including decreasing its size such as with the shrink_to_fit method, will
  443. //! cause the vector to permanently switch to dynamically allocated storage.
  444. //!
  445. //! \tparam T The type of object that is stored in the small_vector
  446. //! \tparam N The number of preallocated elements stored inside small_vector. It shall be less than Allocator::max_size();
  447. //! \tparam Allocator The allocator used for memory management when the number of elements exceeds N. Use void
  448. //! for the default allocator
  449. //! \tparam Options A type produced from \c boost::container::small_vector_options.
  450. template <class T, std::size_t N, class Allocator BOOST_CONTAINER_DOCONLY(= void), class Options BOOST_CONTAINER_DOCONLY(= void) >
  451. class small_vector
  452. : public small_vector_base<T, Allocator, Options>
  453. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  454. , private small_vector_storage_definer<T, N, Options>::type
  455. #endif
  456. {
  457. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  458. BOOST_COPYABLE_AND_MOVABLE(small_vector)
  459. public:
  460. typedef small_vector_base<T, Allocator, Options> base_type;
  461. typedef typename base_type::allocator_type allocator_type;
  462. typedef typename base_type::size_type size_type;
  463. typedef typename base_type::value_type value_type;
  464. BOOST_CONTAINER_FORCEINLINE static std::size_t internal_capacity()
  465. { return static_capacity; }
  466. typedef allocator_traits<typename base_type::allocator_type> allocator_traits_type;
  467. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  468. //! @brief The capacity/max size of the container
  469. static const size_type static_capacity = small_vector_storage_definer<T, N, Options>::type::sms_size;
  470. public:
  471. BOOST_CONTAINER_FORCEINLINE small_vector()
  472. BOOST_NOEXCEPT_IF(dtl::is_nothrow_default_constructible<allocator_type>::value)
  473. : base_type(initial_capacity_t(), internal_capacity())
  474. {}
  475. BOOST_CONTAINER_FORCEINLINE explicit small_vector(const allocator_type &a)
  476. : base_type(initial_capacity_t(), internal_capacity(), a)
  477. {}
  478. BOOST_CONTAINER_FORCEINLINE explicit small_vector(size_type n)
  479. : base_type(maybe_initial_capacity_t(), internal_capacity(), n)
  480. { this->protected_init_n(n, value_init); }
  481. BOOST_CONTAINER_FORCEINLINE small_vector(size_type n, const allocator_type &a)
  482. : base_type(maybe_initial_capacity_t(), internal_capacity(), n, a)
  483. { this->protected_init_n(n, value_init); }
  484. BOOST_CONTAINER_FORCEINLINE small_vector(size_type n, default_init_t)
  485. : base_type(maybe_initial_capacity_t(), internal_capacity(), n)
  486. { this->protected_init_n(n, default_init_t()); }
  487. BOOST_CONTAINER_FORCEINLINE small_vector(size_type n, default_init_t, const allocator_type &a)
  488. : base_type(maybe_initial_capacity_t(), internal_capacity(), n, a)
  489. { this->protected_init_n(n, default_init_t()); }
  490. BOOST_CONTAINER_FORCEINLINE small_vector(size_type n, const value_type &v)
  491. : base_type(maybe_initial_capacity_t(), internal_capacity(), n)
  492. { this->protected_init_n(n, v); }
  493. BOOST_CONTAINER_FORCEINLINE small_vector(size_type n, const value_type &v, const allocator_type &a)
  494. : base_type(maybe_initial_capacity_t(), internal_capacity(), n, a)
  495. { this->protected_init_n(n, v); }
  496. template <class InIt>
  497. BOOST_CONTAINER_FORCEINLINE small_vector(InIt first, InIt last
  498. BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename dtl::disable_if_c
  499. < dtl::is_convertible<InIt BOOST_MOVE_I size_type>::value
  500. BOOST_MOVE_I dtl::nat >::type * = 0)
  501. )
  502. : base_type(initial_capacity_t(), internal_capacity())
  503. { this->assign(first, last); }
  504. template <class InIt>
  505. BOOST_CONTAINER_FORCEINLINE small_vector(InIt first, InIt last, const allocator_type& a
  506. BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename dtl::disable_if_c
  507. < dtl::is_convertible<InIt BOOST_MOVE_I size_type>::value
  508. BOOST_MOVE_I dtl::nat >::type * = 0)
  509. )
  510. : base_type(initial_capacity_t(), internal_capacity(), a)
  511. { this->assign(first, last); }
  512. BOOST_CONTAINER_FORCEINLINE small_vector(const small_vector &other)
  513. : base_type( initial_capacity_t(), internal_capacity()
  514. , allocator_traits_type::select_on_container_copy_construction(other.get_stored_allocator()))
  515. { this->assign(other.cbegin(), other.cend()); }
  516. BOOST_CONTAINER_FORCEINLINE small_vector(const small_vector &other, const allocator_type &a)
  517. : base_type(initial_capacity_t(), internal_capacity(), a)
  518. { this->assign(other.cbegin(), other.cend()); }
  519. BOOST_CONTAINER_FORCEINLINE explicit small_vector(const base_type &other)
  520. : base_type( initial_capacity_t(), internal_capacity()
  521. , allocator_traits_type::select_on_container_copy_construction(other.get_stored_allocator()))
  522. { this->assign(other.cbegin(), other.cend()); }
  523. BOOST_CONTAINER_FORCEINLINE explicit small_vector(BOOST_RV_REF(base_type) other)
  524. : base_type(initial_capacity_t(), internal_capacity(), ::boost::move(other.get_stored_allocator()))
  525. { this->move_construct_impl(other, other.get_stored_allocator()); }
  526. BOOST_CONTAINER_FORCEINLINE small_vector(BOOST_RV_REF(small_vector) other)
  527. BOOST_NOEXCEPT_IF(boost::container::dtl::is_nothrow_move_constructible<value_type>::value)
  528. : base_type(initial_capacity_t(), internal_capacity(), ::boost::move(other.get_stored_allocator()))
  529. { this->move_construct_impl(other, other.get_stored_allocator()); }
  530. BOOST_CONTAINER_FORCEINLINE small_vector(BOOST_RV_REF(small_vector) other, const allocator_type &a)
  531. : base_type(initial_capacity_t(), internal_capacity(), a)
  532. { this->move_construct_impl(other, a); }
  533. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  534. BOOST_CONTAINER_FORCEINLINE small_vector(std::initializer_list<value_type> il, const allocator_type& a = allocator_type())
  535. : base_type(initial_capacity_t(), internal_capacity(), a)
  536. {
  537. this->assign(il.begin(), il.end());
  538. }
  539. #endif
  540. BOOST_CONTAINER_FORCEINLINE small_vector& operator=(BOOST_COPY_ASSIGN_REF(small_vector) other)
  541. { return static_cast<small_vector&>(this->base_type::operator=(static_cast<base_type const&>(other))); }
  542. BOOST_CONTAINER_FORCEINLINE small_vector& operator=(BOOST_RV_REF(small_vector) other)
  543. BOOST_NOEXCEPT_IF(boost::container::dtl::is_nothrow_move_assignable<value_type>::value
  544. && (allocator_traits_type::propagate_on_container_move_assignment::value
  545. || allocator_traits_type::is_always_equal::value))
  546. { return static_cast<small_vector&>(this->base_type::operator=(BOOST_MOVE_BASE(base_type, other))); }
  547. BOOST_CONTAINER_FORCEINLINE small_vector& operator=(const base_type &other)
  548. { return static_cast<small_vector&>(this->base_type::operator=(other)); }
  549. BOOST_CONTAINER_FORCEINLINE small_vector& operator=(BOOST_RV_REF(base_type) other)
  550. { return static_cast<small_vector&>(this->base_type::operator=(boost::move(other))); }
  551. BOOST_CONTAINER_FORCEINLINE void swap(small_vector &other)
  552. { return this->base_type::swap(other); }
  553. };
  554. }}
  555. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  556. /*
  557. namespace boost {
  558. //!has_trivial_destructor_after_move<> == true_type
  559. //!specialization for optimizations
  560. template <class T, class Allocator>
  561. struct has_trivial_destructor_after_move<boost::container::vector<T, Allocator> >
  562. {
  563. typedef typename ::boost::container::allocator_traits<Allocator>::pointer pointer;
  564. static const bool value = ::boost::has_trivial_destructor_after_move<Allocator>::value &&
  565. ::boost::has_trivial_destructor_after_move<pointer>::value;
  566. };
  567. }
  568. */
  569. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  570. #include <boost/container/detail/config_end.hpp>
  571. #endif // #ifndef BOOST_CONTAINER_CONTAINER_SMALL_VECTOR_HPP