advanced_insert_int.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2008-2012. 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_ADVANCED_INSERT_INT_HPP
  11. #define BOOST_CONTAINER_ADVANCED_INSERT_INT_HPP
  12. #if defined(_MSC_VER)
  13. # pragma once
  14. #endif
  15. #include "config_begin.hpp"
  16. #include <boost/container/detail/workaround.hpp>
  17. #include <boost/container/allocator_traits.hpp>
  18. #include <boost/container/detail/destroyers.hpp>
  19. #include <boost/aligned_storage.hpp>
  20. #include <boost/move/utility.hpp>
  21. #include <iterator> //std::iterator_traits
  22. #include <boost/assert.hpp>
  23. #include <boost/detail/no_exceptions_support.hpp>
  24. namespace boost { namespace container { namespace container_detail {
  25. template<class A, class FwdIt, class Iterator>
  26. struct move_insert_range_proxy
  27. {
  28. typedef typename allocator_traits<A>::size_type size_type;
  29. typedef typename allocator_traits<A>::value_type value_type;
  30. move_insert_range_proxy(A& a, FwdIt first)
  31. : a_(a), first_(first)
  32. {}
  33. void uninitialized_copy_n_and_update(Iterator p, size_type n)
  34. {
  35. this->first_ = ::boost::container::uninitialized_move_alloc_n_source
  36. (this->a_, this->first_, n, p);
  37. }
  38. void copy_n_and_update(Iterator p, size_type n)
  39. {
  40. this->first_ = ::boost::container::move_n_source(this->first_, n, p);
  41. }
  42. A &a_;
  43. FwdIt first_;
  44. };
  45. template<class A, class FwdIt, class Iterator>
  46. struct insert_range_proxy
  47. {
  48. typedef typename allocator_traits<A>::size_type size_type;
  49. typedef typename allocator_traits<A>::value_type value_type;
  50. insert_range_proxy(A& a, FwdIt first)
  51. : a_(a), first_(first)
  52. {}
  53. void uninitialized_copy_n_and_update(Iterator p, size_type n)
  54. {
  55. this->first_ = ::boost::container::uninitialized_copy_alloc_n_source(this->a_, this->first_, n, p);
  56. }
  57. void copy_n_and_update(Iterator p, size_type n)
  58. {
  59. this->first_ = ::boost::container::copy_n_source(this->first_, n, p);
  60. }
  61. A &a_;
  62. FwdIt first_;
  63. };
  64. template<class A, class Iterator>
  65. struct insert_n_copies_proxy
  66. {
  67. typedef typename allocator_traits<A>::size_type size_type;
  68. typedef typename allocator_traits<A>::value_type value_type;
  69. insert_n_copies_proxy(A& a, const value_type &v)
  70. : a_(a), v_(v)
  71. {}
  72. void uninitialized_copy_n_and_update(Iterator p, size_type n) const
  73. { boost::container::uninitialized_fill_alloc_n(this->a_, v_, n, p); }
  74. void copy_n_and_update(Iterator p, size_type n) const
  75. { std::fill_n(p, n, v_); }
  76. A &a_;
  77. const value_type &v_;
  78. };
  79. template<class A, class Iterator>
  80. struct insert_value_initialized_n_proxy
  81. {
  82. typedef ::boost::container::allocator_traits<A> alloc_traits;
  83. typedef typename allocator_traits<A>::size_type size_type;
  84. typedef typename allocator_traits<A>::value_type value_type;
  85. explicit insert_value_initialized_n_proxy(A &a)
  86. : a_(a)
  87. {}
  88. void uninitialized_copy_n_and_update(Iterator p, size_type n) const
  89. { boost::container::uninitialized_value_init_alloc_n(this->a_, n, p); }
  90. void copy_n_and_update(Iterator, size_type) const
  91. {
  92. BOOST_ASSERT(false);
  93. }
  94. private:
  95. A &a_;
  96. };
  97. template<class A, class Iterator>
  98. struct insert_default_initialized_n_proxy
  99. {
  100. typedef ::boost::container::allocator_traits<A> alloc_traits;
  101. typedef typename allocator_traits<A>::size_type size_type;
  102. typedef typename allocator_traits<A>::value_type value_type;
  103. explicit insert_default_initialized_n_proxy(A &a)
  104. : a_(a)
  105. {}
  106. void uninitialized_copy_n_and_update(Iterator p, size_type n) const
  107. { boost::container::uninitialized_default_init_alloc_n(this->a_, n, p); }
  108. void copy_n_and_update(Iterator, size_type) const
  109. {
  110. BOOST_ASSERT(false);
  111. }
  112. private:
  113. A &a_;
  114. };
  115. template<class A, class Iterator>
  116. struct insert_copy_proxy
  117. {
  118. typedef boost::container::allocator_traits<A> alloc_traits;
  119. typedef typename alloc_traits::size_type size_type;
  120. typedef typename alloc_traits::value_type value_type;
  121. insert_copy_proxy(A& a, const value_type &v)
  122. : a_(a), v_(v)
  123. {}
  124. void uninitialized_copy_n_and_update(Iterator p, size_type n) const
  125. {
  126. BOOST_ASSERT(n == 1); (void)n;
  127. alloc_traits::construct( this->a_
  128. , container_detail::to_raw_pointer(&*p)
  129. , v_
  130. );
  131. }
  132. void copy_n_and_update(Iterator p, size_type n) const
  133. {
  134. BOOST_ASSERT(n == 1); (void)n;
  135. *p =v_;
  136. }
  137. A &a_;
  138. const value_type &v_;
  139. };
  140. template<class A, class Iterator>
  141. struct insert_move_proxy
  142. {
  143. typedef boost::container::allocator_traits<A> alloc_traits;
  144. typedef typename alloc_traits::size_type size_type;
  145. typedef typename alloc_traits::value_type value_type;
  146. insert_move_proxy(A& a, value_type &v)
  147. : a_(a), v_(v)
  148. {}
  149. void uninitialized_copy_n_and_update(Iterator p, size_type n) const
  150. {
  151. BOOST_ASSERT(n == 1); (void)n;
  152. alloc_traits::construct( this->a_
  153. , container_detail::to_raw_pointer(&*p)
  154. , ::boost::move(v_)
  155. );
  156. }
  157. void copy_n_and_update(Iterator p, size_type n) const
  158. {
  159. BOOST_ASSERT(n == 1); (void)n;
  160. *p = ::boost::move(v_);
  161. }
  162. A &a_;
  163. value_type &v_;
  164. };
  165. template<class It, class A>
  166. insert_move_proxy<A, It> get_insert_value_proxy(A& a, BOOST_RV_REF(typename std::iterator_traits<It>::value_type) v)
  167. {
  168. return insert_move_proxy<A, It>(a, v);
  169. }
  170. template<class It, class A>
  171. insert_copy_proxy<A, It> get_insert_value_proxy(A& a, const typename std::iterator_traits<It>::value_type &v)
  172. {
  173. return insert_copy_proxy<A, It>(a, v);
  174. }
  175. }}} //namespace boost { namespace container { namespace container_detail {
  176. #ifdef BOOST_CONTAINER_PERFECT_FORWARDING
  177. #include <boost/container/detail/variadic_templates_tools.hpp>
  178. #include <boost/move/utility.hpp>
  179. #include <typeinfo>
  180. //#include <iostream> //For debugging purposes
  181. namespace boost {
  182. namespace container {
  183. namespace container_detail {
  184. template<class A, class Iterator, class ...Args>
  185. struct insert_non_movable_emplace_proxy
  186. {
  187. typedef boost::container::allocator_traits<A> alloc_traits;
  188. typedef typename alloc_traits::size_type size_type;
  189. typedef typename alloc_traits::value_type value_type;
  190. typedef typename build_number_seq<sizeof...(Args)>::type index_tuple_t;
  191. explicit insert_non_movable_emplace_proxy(A &a, Args&&... args)
  192. : a_(a), args_(args...)
  193. {}
  194. void uninitialized_copy_n_and_update(Iterator p, size_type n)
  195. { this->priv_uninitialized_copy_some_and_update(index_tuple_t(), p, n); }
  196. private:
  197. template<int ...IdxPack>
  198. void priv_uninitialized_copy_some_and_update(const index_tuple<IdxPack...>&, Iterator p, size_type n)
  199. {
  200. BOOST_ASSERT(n == 1); (void)n;
  201. alloc_traits::construct( this->a_
  202. , container_detail::to_raw_pointer(&*p)
  203. , ::boost::forward<Args>(get<IdxPack>(this->args_))...
  204. );
  205. }
  206. protected:
  207. A &a_;
  208. tuple<Args&...> args_;
  209. };
  210. template<class A, class Iterator, class ...Args>
  211. struct insert_emplace_proxy
  212. : public insert_non_movable_emplace_proxy<A, Iterator, Args...>
  213. {
  214. typedef insert_non_movable_emplace_proxy<A, Iterator, Args...> base_t;
  215. typedef boost::container::allocator_traits<A> alloc_traits;
  216. typedef typename base_t::value_type value_type;
  217. typedef typename base_t::size_type size_type;
  218. typedef typename base_t::index_tuple_t index_tuple_t;
  219. explicit insert_emplace_proxy(A &a, Args&&... args)
  220. : base_t(a, ::boost::forward<Args>(args)...)
  221. {}
  222. void copy_n_and_update(Iterator p, size_type n)
  223. { this->priv_copy_some_and_update(index_tuple_t(), p, n); }
  224. private:
  225. template<int ...IdxPack>
  226. void priv_copy_some_and_update(const index_tuple<IdxPack...>&, Iterator p, size_type n)
  227. {
  228. BOOST_ASSERT(n ==1); (void)n;
  229. aligned_storage<sizeof(value_type), alignment_of<value_type>::value> v;
  230. value_type *vp = static_cast<value_type *>(static_cast<void *>(&v));
  231. alloc_traits::construct(this->a_, vp,
  232. ::boost::forward<Args>(get<IdxPack>(this->args_))...);
  233. BOOST_TRY{
  234. *p = ::boost::move(*vp);
  235. }
  236. BOOST_CATCH(...){
  237. alloc_traits::destroy(this->a_, vp);
  238. BOOST_RETHROW
  239. }
  240. BOOST_CATCH_END
  241. alloc_traits::destroy(this->a_, vp);
  242. }
  243. };
  244. }}} //namespace boost { namespace container { namespace container_detail {
  245. #else //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING
  246. #include <boost/container/detail/preprocessor.hpp>
  247. #include <boost/container/detail/value_init.hpp>
  248. namespace boost {
  249. namespace container {
  250. namespace container_detail {
  251. #define BOOST_PP_LOCAL_MACRO(N) \
  252. template<class A, class Iterator BOOST_PP_ENUM_TRAILING_PARAMS(N, class P) > \
  253. struct BOOST_PP_CAT(insert_non_movable_emplace_proxy_arg, N) \
  254. { \
  255. typedef boost::container::allocator_traits<A> alloc_traits; \
  256. typedef typename alloc_traits::size_type size_type; \
  257. typedef typename alloc_traits::value_type value_type; \
  258. \
  259. BOOST_PP_CAT(insert_non_movable_emplace_proxy_arg, N) \
  260. ( A &a BOOST_PP_ENUM_TRAILING(N, BOOST_CONTAINER_PP_PARAM_LIST, _) ) \
  261. : a_(a) \
  262. BOOST_PP_ENUM_TRAILING(N, BOOST_CONTAINER_PP_PARAM_INIT, _) \
  263. {} \
  264. \
  265. void uninitialized_copy_n_and_update(Iterator p, size_type n) \
  266. { \
  267. BOOST_ASSERT(n == 1); (void)n; \
  268. alloc_traits::construct \
  269. ( this->a_ \
  270. , container_detail::to_raw_pointer(&*p) \
  271. BOOST_PP_ENUM_TRAILING(N, BOOST_CONTAINER_PP_MEMBER_FORWARD, _) \
  272. ); \
  273. } \
  274. \
  275. void copy_n_and_update(Iterator, size_type) \
  276. { BOOST_ASSERT(false); } \
  277. \
  278. protected: \
  279. A &a_; \
  280. BOOST_PP_REPEAT(N, BOOST_CONTAINER_PP_PARAM_DEFINE, _) \
  281. }; \
  282. \
  283. template<class A, class Iterator BOOST_PP_ENUM_TRAILING_PARAMS(N, class P) > \
  284. struct BOOST_PP_CAT(insert_emplace_proxy_arg, N) \
  285. : BOOST_PP_CAT(insert_non_movable_emplace_proxy_arg, N) \
  286. < A, Iterator BOOST_PP_ENUM_TRAILING_PARAMS(N, P) > \
  287. { \
  288. typedef BOOST_PP_CAT(insert_non_movable_emplace_proxy_arg, N) \
  289. <A, Iterator BOOST_PP_ENUM_TRAILING_PARAMS(N, P) > base_t; \
  290. typedef typename base_t::value_type value_type; \
  291. typedef typename base_t::size_type size_type; \
  292. typedef boost::container::allocator_traits<A> alloc_traits; \
  293. \
  294. BOOST_PP_CAT(insert_emplace_proxy_arg, N) \
  295. ( A &a BOOST_PP_ENUM_TRAILING(N, BOOST_CONTAINER_PP_PARAM_LIST, _) ) \
  296. : base_t(a BOOST_PP_ENUM_TRAILING(N, BOOST_CONTAINER_PP_PARAM_FORWARD, _) ) \
  297. {} \
  298. \
  299. void copy_n_and_update(Iterator p, size_type n) \
  300. { \
  301. BOOST_ASSERT(n == 1); (void)n; \
  302. aligned_storage<sizeof(value_type), alignment_of<value_type>::value> v; \
  303. value_type *vp = static_cast<value_type *>(static_cast<void *>(&v)); \
  304. alloc_traits::construct(this->a_, vp \
  305. BOOST_PP_ENUM_TRAILING(N, BOOST_CONTAINER_PP_MEMBER_FORWARD, _)); \
  306. BOOST_TRY{ \
  307. *p = ::boost::move(*vp); \
  308. } \
  309. BOOST_CATCH(...){ \
  310. alloc_traits::destroy(this->a_, vp); \
  311. BOOST_RETHROW \
  312. } \
  313. BOOST_CATCH_END \
  314. alloc_traits::destroy(this->a_, vp); \
  315. } \
  316. }; \
  317. //!
  318. #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS)
  319. #include BOOST_PP_LOCAL_ITERATE()
  320. }}} //namespace boost { namespace container { namespace container_detail {
  321. #endif //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING
  322. #include <boost/container/detail/config_end.hpp>
  323. #endif //#ifndef BOOST_CONTAINER_ADVANCED_INSERT_INT_HPP