advanced_insert_int.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2008-2013. 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. #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/allocator_traits.hpp>
  22. // container/detail
  23. #include <boost/container/detail/copy_move_algo.hpp>
  24. #include <boost/container/detail/destroyers.hpp>
  25. #include <boost/container/detail/mpl.hpp>
  26. #include <boost/container/detail/type_traits.hpp>
  27. #include <boost/container/detail/iterator.hpp>
  28. #include <boost/container/detail/iterators.hpp>
  29. #include <boost/move/detail/iterator_to_raw_pointer.hpp>
  30. #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  31. #include <boost/move/detail/fwd_macros.hpp>
  32. #endif
  33. // move
  34. #include <boost/move/utility_core.hpp>
  35. // other
  36. #include <boost/assert.hpp>
  37. #include <boost/core/no_exceptions_support.hpp>
  38. namespace boost { namespace container { namespace dtl {
  39. template<class Allocator, class FwdIt, class Iterator>
  40. struct move_insert_range_proxy
  41. {
  42. typedef typename allocator_traits<Allocator>::size_type size_type;
  43. typedef typename allocator_traits<Allocator>::value_type value_type;
  44. explicit move_insert_range_proxy(FwdIt first)
  45. : first_(first)
  46. {}
  47. void uninitialized_copy_n_and_update(Allocator &a, Iterator p, size_type n)
  48. {
  49. this->first_ = ::boost::container::uninitialized_move_alloc_n_source
  50. (a, this->first_, n, p);
  51. }
  52. void copy_n_and_update(Allocator &, Iterator p, size_type n)
  53. {
  54. this->first_ = ::boost::container::move_n_source(this->first_, n, p);
  55. }
  56. FwdIt first_;
  57. };
  58. template<class Allocator, class FwdIt, class Iterator>
  59. struct insert_range_proxy
  60. {
  61. typedef typename allocator_traits<Allocator>::size_type size_type;
  62. typedef typename allocator_traits<Allocator>::value_type value_type;
  63. explicit insert_range_proxy(FwdIt first)
  64. : first_(first)
  65. {}
  66. void uninitialized_copy_n_and_update(Allocator &a, Iterator p, size_type n)
  67. {
  68. this->first_ = ::boost::container::uninitialized_copy_alloc_n_source(a, this->first_, n, p);
  69. }
  70. void copy_n_and_update(Allocator &, Iterator p, size_type n)
  71. {
  72. this->first_ = ::boost::container::copy_n_source(this->first_, n, p);
  73. }
  74. FwdIt first_;
  75. };
  76. template<class Allocator, class Iterator>
  77. struct insert_n_copies_proxy
  78. {
  79. typedef typename allocator_traits<Allocator>::size_type size_type;
  80. typedef typename allocator_traits<Allocator>::value_type value_type;
  81. explicit insert_n_copies_proxy(const value_type &v)
  82. : v_(v)
  83. {}
  84. void uninitialized_copy_n_and_update(Allocator &a, Iterator p, size_type n) const
  85. { boost::container::uninitialized_fill_alloc_n(a, v_, n, p); }
  86. void copy_n_and_update(Allocator &, Iterator p, size_type n) const
  87. {
  88. for (; 0 < n; --n, ++p){
  89. *p = v_;
  90. }
  91. }
  92. const value_type &v_;
  93. };
  94. template<class Allocator, class Iterator>
  95. struct insert_value_initialized_n_proxy
  96. {
  97. typedef ::boost::container::allocator_traits<Allocator> alloc_traits;
  98. typedef typename allocator_traits<Allocator>::size_type size_type;
  99. typedef typename allocator_traits<Allocator>::value_type value_type;
  100. void uninitialized_copy_n_and_update(Allocator &a, Iterator p, size_type n) const
  101. { boost::container::uninitialized_value_init_alloc_n(a, n, p); }
  102. void copy_n_and_update(Allocator &a, Iterator p, size_type n) const
  103. {
  104. for (; 0 < n; --n, ++p){
  105. typename dtl::aligned_storage<sizeof(value_type), dtl::alignment_of<value_type>::value>::type v;
  106. value_type *vp = reinterpret_cast<value_type *>(v.data);
  107. alloc_traits::construct(a, vp);
  108. value_destructor<Allocator> on_exit(a, *vp); (void)on_exit;
  109. *p = ::boost::move(*vp);
  110. }
  111. }
  112. };
  113. template<class Allocator, class Iterator>
  114. struct insert_default_initialized_n_proxy
  115. {
  116. typedef ::boost::container::allocator_traits<Allocator> alloc_traits;
  117. typedef typename allocator_traits<Allocator>::size_type size_type;
  118. typedef typename allocator_traits<Allocator>::value_type value_type;
  119. void uninitialized_copy_n_and_update(Allocator &a, Iterator p, size_type n) const
  120. { boost::container::uninitialized_default_init_alloc_n(a, n, p); }
  121. void copy_n_and_update(Allocator &a, Iterator p, size_type n) const
  122. {
  123. if(!is_pod<value_type>::value){
  124. for (; 0 < n; --n, ++p){
  125. typename dtl::aligned_storage<sizeof(value_type), dtl::alignment_of<value_type>::value>::type v;
  126. value_type *vp = reinterpret_cast<value_type *>(v.data);
  127. alloc_traits::construct(a, vp, default_init);
  128. value_destructor<Allocator> on_exit(a, *vp); (void)on_exit;
  129. *p = ::boost::move(*vp);
  130. }
  131. }
  132. }
  133. };
  134. template<class Allocator, class Iterator>
  135. struct insert_copy_proxy
  136. {
  137. typedef boost::container::allocator_traits<Allocator> alloc_traits;
  138. typedef typename alloc_traits::size_type size_type;
  139. typedef typename alloc_traits::value_type value_type;
  140. explicit insert_copy_proxy(const value_type &v)
  141. : v_(v)
  142. {}
  143. void uninitialized_copy_n_and_update(Allocator &a, Iterator p, size_type n) const
  144. {
  145. BOOST_ASSERT(n == 1); (void)n;
  146. alloc_traits::construct( a, boost::movelib::iterator_to_raw_pointer(p), v_);
  147. }
  148. void copy_n_and_update(Allocator &, Iterator p, size_type n) const
  149. {
  150. BOOST_ASSERT(n == 1); (void)n;
  151. *p = v_;
  152. }
  153. const value_type &v_;
  154. };
  155. template<class Allocator, class Iterator>
  156. struct insert_move_proxy
  157. {
  158. typedef boost::container::allocator_traits<Allocator> alloc_traits;
  159. typedef typename alloc_traits::size_type size_type;
  160. typedef typename alloc_traits::value_type value_type;
  161. BOOST_CONTAINER_FORCEINLINE explicit insert_move_proxy(value_type &v)
  162. : v_(v)
  163. {}
  164. BOOST_CONTAINER_FORCEINLINE void uninitialized_copy_n_and_update(Allocator &a, Iterator p, size_type n) const
  165. {
  166. BOOST_ASSERT(n == 1); (void)n;
  167. alloc_traits::construct( a, boost::movelib::iterator_to_raw_pointer(p), ::boost::move(v_) );
  168. }
  169. BOOST_CONTAINER_FORCEINLINE void copy_n_and_update(Allocator &, Iterator p, size_type n) const
  170. {
  171. BOOST_ASSERT(n == 1); (void)n;
  172. *p = ::boost::move(v_);
  173. }
  174. value_type &v_;
  175. };
  176. template<class It, class Allocator>
  177. insert_move_proxy<Allocator, It> get_insert_value_proxy(BOOST_RV_REF(typename boost::container::iterator_traits<It>::value_type) v)
  178. {
  179. return insert_move_proxy<Allocator, It>(v);
  180. }
  181. template<class It, class Allocator>
  182. insert_copy_proxy<Allocator, It> get_insert_value_proxy(const typename boost::container::iterator_traits<It>::value_type &v)
  183. {
  184. return insert_copy_proxy<Allocator, It>(v);
  185. }
  186. }}} //namespace boost { namespace container { namespace dtl {
  187. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  188. #include <boost/container/detail/variadic_templates_tools.hpp>
  189. #include <boost/move/utility_core.hpp>
  190. namespace boost {
  191. namespace container {
  192. namespace dtl {
  193. template<class Allocator, class Iterator, class ...Args>
  194. struct insert_nonmovable_emplace_proxy
  195. {
  196. typedef boost::container::allocator_traits<Allocator> alloc_traits;
  197. typedef typename alloc_traits::size_type size_type;
  198. typedef typename alloc_traits::value_type value_type;
  199. typedef typename build_number_seq<sizeof...(Args)>::type index_tuple_t;
  200. explicit insert_nonmovable_emplace_proxy(BOOST_FWD_REF(Args)... args)
  201. : args_(args...)
  202. {}
  203. void uninitialized_copy_n_and_update(Allocator &a, Iterator p, size_type n)
  204. { this->priv_uninitialized_copy_some_and_update(a, index_tuple_t(), p, n); }
  205. private:
  206. template<std::size_t ...IdxPack>
  207. void priv_uninitialized_copy_some_and_update(Allocator &a, const index_tuple<IdxPack...>&, Iterator p, size_type n)
  208. {
  209. BOOST_ASSERT(n == 1); (void)n;
  210. alloc_traits::construct( a, boost::movelib::iterator_to_raw_pointer(p), ::boost::forward<Args>(get<IdxPack>(this->args_))... );
  211. }
  212. protected:
  213. tuple<Args&...> args_;
  214. };
  215. template<class Allocator, class Iterator, class ...Args>
  216. struct insert_emplace_proxy
  217. : public insert_nonmovable_emplace_proxy<Allocator, Iterator, Args...>
  218. {
  219. typedef insert_nonmovable_emplace_proxy<Allocator, Iterator, Args...> base_t;
  220. typedef boost::container::allocator_traits<Allocator> alloc_traits;
  221. typedef typename base_t::value_type value_type;
  222. typedef typename base_t::size_type size_type;
  223. typedef typename base_t::index_tuple_t index_tuple_t;
  224. explicit insert_emplace_proxy(BOOST_FWD_REF(Args)... args)
  225. : base_t(::boost::forward<Args>(args)...)
  226. {}
  227. void copy_n_and_update(Allocator &a, Iterator p, size_type n)
  228. { this->priv_copy_some_and_update(a, index_tuple_t(), p, n); }
  229. private:
  230. template<std::size_t ...IdxPack>
  231. void priv_copy_some_and_update(Allocator &a, const index_tuple<IdxPack...>&, Iterator p, size_type n)
  232. {
  233. BOOST_ASSERT(n ==1); (void)n;
  234. typename dtl::aligned_storage<sizeof(value_type), dtl::alignment_of<value_type>::value>::type v;
  235. value_type *vp = reinterpret_cast<value_type *>(v.data);
  236. alloc_traits::construct(a, vp,
  237. ::boost::forward<Args>(get<IdxPack>(this->args_))...);
  238. BOOST_TRY{
  239. *p = ::boost::move(*vp);
  240. }
  241. BOOST_CATCH(...){
  242. alloc_traits::destroy(a, vp);
  243. BOOST_RETHROW
  244. }
  245. BOOST_CATCH_END
  246. alloc_traits::destroy(a, vp);
  247. }
  248. };
  249. //Specializations to avoid an unneeded temporary when emplacing from a single argument o type value_type
  250. template<class Allocator, class Iterator>
  251. struct insert_emplace_proxy<Allocator, Iterator, typename boost::container::allocator_traits<Allocator>::value_type>
  252. : public insert_move_proxy<Allocator, Iterator>
  253. {
  254. explicit insert_emplace_proxy(typename boost::container::allocator_traits<Allocator>::value_type &&v)
  255. : insert_move_proxy<Allocator, Iterator>(v)
  256. {}
  257. };
  258. //We use "add_const" here as adding "const" only confuses MSVC12(and maybe later) provoking
  259. //compiler error C2752 ("more than one partial specialization matches").
  260. //Any problem is solvable with an extra layer of indirection? ;-)
  261. template<class Allocator, class Iterator>
  262. struct insert_emplace_proxy<Allocator, Iterator
  263. , typename boost::container::dtl::add_const<typename boost::container::allocator_traits<Allocator>::value_type>::type
  264. >
  265. : public insert_copy_proxy<Allocator, Iterator>
  266. {
  267. explicit insert_emplace_proxy(const typename boost::container::allocator_traits<Allocator>::value_type &v)
  268. : insert_copy_proxy<Allocator, Iterator>(v)
  269. {}
  270. };
  271. template<class Allocator, class Iterator>
  272. struct insert_emplace_proxy<Allocator, Iterator, typename boost::container::allocator_traits<Allocator>::value_type &>
  273. : public insert_copy_proxy<Allocator, Iterator>
  274. {
  275. explicit insert_emplace_proxy(const typename boost::container::allocator_traits<Allocator>::value_type &v)
  276. : insert_copy_proxy<Allocator, Iterator>(v)
  277. {}
  278. };
  279. template<class Allocator, class Iterator>
  280. struct insert_emplace_proxy<Allocator, Iterator
  281. , typename boost::container::dtl::add_const<typename boost::container::allocator_traits<Allocator>::value_type>::type &
  282. >
  283. : public insert_copy_proxy<Allocator, Iterator>
  284. {
  285. explicit insert_emplace_proxy(const typename boost::container::allocator_traits<Allocator>::value_type &v)
  286. : insert_copy_proxy<Allocator, Iterator>(v)
  287. {}
  288. };
  289. }}} //namespace boost { namespace container { namespace dtl {
  290. #else // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  291. #include <boost/container/detail/value_init.hpp>
  292. namespace boost {
  293. namespace container {
  294. namespace dtl {
  295. #define BOOST_CONTAINER_ADVANCED_INSERT_INT_CODE(N) \
  296. template< class Allocator, class Iterator BOOST_MOVE_I##N BOOST_MOVE_CLASS##N >\
  297. struct insert_nonmovable_emplace_proxy##N\
  298. {\
  299. typedef boost::container::allocator_traits<Allocator> alloc_traits;\
  300. typedef typename alloc_traits::size_type size_type;\
  301. typedef typename alloc_traits::value_type value_type;\
  302. \
  303. explicit insert_nonmovable_emplace_proxy##N(BOOST_MOVE_UREF##N)\
  304. BOOST_MOVE_COLON##N BOOST_MOVE_FWD_INIT##N {}\
  305. \
  306. void uninitialized_copy_n_and_update(Allocator &a, Iterator p, size_type n)\
  307. {\
  308. BOOST_ASSERT(n == 1); (void)n;\
  309. alloc_traits::construct(a, boost::movelib::iterator_to_raw_pointer(p) BOOST_MOVE_I##N BOOST_MOVE_MFWD##N);\
  310. }\
  311. \
  312. void copy_n_and_update(Allocator &, Iterator, size_type)\
  313. { BOOST_ASSERT(false); }\
  314. \
  315. protected:\
  316. BOOST_MOVE_MREF##N\
  317. };\
  318. \
  319. template< class Allocator, class Iterator BOOST_MOVE_I##N BOOST_MOVE_CLASS##N >\
  320. struct insert_emplace_proxy_arg##N\
  321. : insert_nonmovable_emplace_proxy##N< Allocator, Iterator BOOST_MOVE_I##N BOOST_MOVE_TARG##N >\
  322. {\
  323. typedef insert_nonmovable_emplace_proxy##N\
  324. < Allocator, Iterator BOOST_MOVE_I##N BOOST_MOVE_TARG##N > base_t;\
  325. typedef typename base_t::value_type value_type;\
  326. typedef typename base_t::size_type size_type;\
  327. typedef boost::container::allocator_traits<Allocator> alloc_traits;\
  328. \
  329. explicit insert_emplace_proxy_arg##N(BOOST_MOVE_UREF##N)\
  330. : base_t(BOOST_MOVE_FWD##N){}\
  331. \
  332. void copy_n_and_update(Allocator &a, Iterator p, size_type n)\
  333. {\
  334. BOOST_ASSERT(n == 1); (void)n;\
  335. typename dtl::aligned_storage<sizeof(value_type), dtl::alignment_of<value_type>::value>::type v;\
  336. BOOST_ASSERT((((size_type)(&v)) % alignment_of<value_type>::value) == 0);\
  337. value_type *vp = reinterpret_cast<value_type *>(v.data);\
  338. alloc_traits::construct(a, vp BOOST_MOVE_I##N BOOST_MOVE_MFWD##N);\
  339. BOOST_TRY{\
  340. *p = ::boost::move(*vp);\
  341. }\
  342. BOOST_CATCH(...){\
  343. alloc_traits::destroy(a, vp);\
  344. BOOST_RETHROW\
  345. }\
  346. BOOST_CATCH_END\
  347. alloc_traits::destroy(a, vp);\
  348. }\
  349. };\
  350. //
  351. BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_ADVANCED_INSERT_INT_CODE)
  352. #undef BOOST_CONTAINER_ADVANCED_INSERT_INT_CODE
  353. #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  354. //Specializations to avoid an unneeded temporary when emplacing from a single argument o type value_type
  355. template<class Allocator, class Iterator>
  356. struct insert_emplace_proxy_arg1<Allocator, Iterator, ::boost::rv<typename boost::container::allocator_traits<Allocator>::value_type> >
  357. : public insert_move_proxy<Allocator, Iterator>
  358. {
  359. explicit insert_emplace_proxy_arg1(typename boost::container::allocator_traits<Allocator>::value_type &v)
  360. : insert_move_proxy<Allocator, Iterator>(v)
  361. {}
  362. };
  363. template<class Allocator, class Iterator>
  364. struct insert_emplace_proxy_arg1<Allocator, Iterator, typename boost::container::allocator_traits<Allocator>::value_type>
  365. : public insert_copy_proxy<Allocator, Iterator>
  366. {
  367. explicit insert_emplace_proxy_arg1(const typename boost::container::allocator_traits<Allocator>::value_type &v)
  368. : insert_copy_proxy<Allocator, Iterator>(v)
  369. {}
  370. };
  371. #else //e.g. MSVC10 & MSVC11
  372. //Specializations to avoid an unneeded temporary when emplacing from a single argument o type value_type
  373. template<class Allocator, class Iterator>
  374. struct insert_emplace_proxy_arg1<Allocator, Iterator, typename boost::container::allocator_traits<Allocator>::value_type>
  375. : public insert_move_proxy<Allocator, Iterator>
  376. {
  377. explicit insert_emplace_proxy_arg1(typename boost::container::allocator_traits<Allocator>::value_type &&v)
  378. : insert_move_proxy<Allocator, Iterator>(v)
  379. {}
  380. };
  381. //We use "add_const" here as adding "const" only confuses MSVC10&11 provoking
  382. //compiler error C2752 ("more than one partial specialization matches").
  383. //Any problem is solvable with an extra layer of indirection? ;-)
  384. template<class Allocator, class Iterator>
  385. struct insert_emplace_proxy_arg1<Allocator, Iterator
  386. , typename boost::container::dtl::add_const<typename boost::container::allocator_traits<Allocator>::value_type>::type
  387. >
  388. : public insert_copy_proxy<Allocator, Iterator>
  389. {
  390. explicit insert_emplace_proxy_arg1(const typename boost::container::allocator_traits<Allocator>::value_type &v)
  391. : insert_copy_proxy<Allocator, Iterator>(v)
  392. {}
  393. };
  394. template<class Allocator, class Iterator>
  395. struct insert_emplace_proxy_arg1<Allocator, Iterator, typename boost::container::allocator_traits<Allocator>::value_type &>
  396. : public insert_copy_proxy<Allocator, Iterator>
  397. {
  398. explicit insert_emplace_proxy_arg1(const typename boost::container::allocator_traits<Allocator>::value_type &v)
  399. : insert_copy_proxy<Allocator, Iterator>(v)
  400. {}
  401. };
  402. template<class Allocator, class Iterator>
  403. struct insert_emplace_proxy_arg1<Allocator, Iterator
  404. , typename boost::container::dtl::add_const<typename boost::container::allocator_traits<Allocator>::value_type>::type &
  405. >
  406. : public insert_copy_proxy<Allocator, Iterator>
  407. {
  408. explicit insert_emplace_proxy_arg1(const typename boost::container::allocator_traits<Allocator>::value_type &v)
  409. : insert_copy_proxy<Allocator, Iterator>(v)
  410. {}
  411. };
  412. #endif
  413. }}} //namespace boost { namespace container { namespace dtl {
  414. #endif // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  415. #include <boost/container/detail/config_end.hpp>
  416. #endif //#ifndef BOOST_CONTAINER_ADVANCED_INSERT_INT_HPP