varray_detail.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. // Boost.Geometry
  2. //
  3. // varray details
  4. //
  5. // Copyright (c) 2012-2013 Adam Wulkiewicz, Lodz, Poland.
  6. // Copyright (c) 2011-2013 Andrew Hundt.
  7. //
  8. // Use, modification and distribution is subject to the Boost Software License,
  9. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. #ifndef BOOST_GEOMETRY_INDEX_DETAIL_VARRAY_DETAIL_HPP
  12. #define BOOST_GEOMETRY_INDEX_DETAIL_VARRAY_DETAIL_HPP
  13. #include <cstddef>
  14. #include <cstring>
  15. #include <memory>
  16. #include <limits>
  17. #include <boost/mpl/if.hpp>
  18. #include <boost/mpl/and.hpp>
  19. #include <boost/mpl/or.hpp>
  20. #include <boost/mpl/int.hpp>
  21. #include <boost/type_traits/is_same.hpp>
  22. #include <boost/type_traits/remove_const.hpp>
  23. #include <boost/type_traits/remove_reference.hpp>
  24. #include <boost/type_traits/has_trivial_assign.hpp>
  25. #include <boost/type_traits/has_trivial_copy.hpp>
  26. #include <boost/type_traits/has_trivial_constructor.hpp>
  27. #include <boost/type_traits/has_trivial_destructor.hpp>
  28. #include <boost/type_traits/has_trivial_move_constructor.hpp>
  29. #include <boost/type_traits/has_trivial_move_assign.hpp>
  30. //#include <boost/type_traits/has_nothrow_constructor.hpp>
  31. //#include <boost/type_traits/has_nothrow_copy.hpp>
  32. //#include <boost/type_traits/has_nothrow_assign.hpp>
  33. //#include <boost/type_traits/has_nothrow_destructor.hpp>
  34. #include <boost/detail/no_exceptions_support.hpp>
  35. #include <boost/config.hpp>
  36. #include <boost/move/move.hpp>
  37. #include <boost/utility/addressof.hpp>
  38. #include <boost/iterator/iterator_traits.hpp>
  39. // TODO - move vectors iterators optimization to the other, optional file instead of checking defines?
  40. #if defined(BOOST_GEOMETRY_INDEX_DETAIL_VARRAY_ENABLE_VECTOR_OPTIMIZATION) && !defined(BOOST_NO_EXCEPTIONS)
  41. #include <vector>
  42. #include <boost/container/vector.hpp>
  43. #endif // BOOST_GEOMETRY_INDEX_DETAIL_VARRAY_ENABLE_VECTOR_OPTIMIZATION && !BOOST_NO_EXCEPTIONS
  44. namespace boost { namespace geometry { namespace index { namespace detail { namespace varray_detail {
  45. template <typename I>
  46. struct are_elements_contiguous : boost::is_pointer<I>
  47. {};
  48. // EXPERIMENTAL - not finished
  49. // Conditional setup - mark vector iterators defined in known implementations
  50. // as iterators pointing to contiguous ranges
  51. #if defined(BOOST_GEOMETRY_INDEX_DETAIL_VARRAY_ENABLE_VECTOR_OPTIMIZATION) && !defined(BOOST_NO_EXCEPTIONS)
  52. template <typename Pointer>
  53. struct are_elements_contiguous<
  54. boost::container::container_detail::vector_const_iterator<Pointer>
  55. > : boost::true_type
  56. {};
  57. template <typename Pointer>
  58. struct are_elements_contiguous<
  59. boost::container::container_detail::vector_iterator<Pointer>
  60. > : boost::true_type
  61. {};
  62. #if defined(BOOST_DINKUMWARE_STDLIB)
  63. template <typename T>
  64. struct are_elements_contiguous<
  65. std::_Vector_const_iterator<T>
  66. > : boost::true_type
  67. {};
  68. template <typename T>
  69. struct are_elements_contiguous<
  70. std::_Vector_iterator<T>
  71. > : boost::true_type
  72. {};
  73. #elif defined(BOOST_GNU_STDLIB)
  74. template <typename P, typename T, typename A>
  75. struct are_elements_contiguous<
  76. __gnu_cxx::__normal_iterator<P, std::vector<T, A> >
  77. > : boost::true_type
  78. {};
  79. #elif defined(_LIBCPP_VERSION)
  80. // TODO - test it first
  81. //template <typename P>
  82. //struct are_elements_contiguous<
  83. // __wrap_iter<P>
  84. //> : boost::true_type
  85. //{};
  86. #else // OTHER_STDLIB
  87. // TODO - add other iterators implementations
  88. #endif // STDLIB
  89. #endif // BOOST_GEOMETRY_INDEX_DETAIL_VARRAY_ENABLE_VECTOR_OPTIMIZATION && !BOOST_NO_EXCEPTIONS
  90. // True if iterator values are the same and both iterators points to the ranges of contiguous elements
  91. template <typename I, typename O>
  92. struct are_corresponding :
  93. ::boost::mpl::and_<
  94. ::boost::is_same<
  95. ::boost::remove_const<
  96. typename ::boost::iterator_value<I>::type
  97. >,
  98. ::boost::remove_const<
  99. typename ::boost::iterator_value<O>::type
  100. >
  101. >,
  102. are_elements_contiguous<I>,
  103. are_elements_contiguous<O>
  104. >
  105. {};
  106. template <typename I, typename V>
  107. struct is_corresponding_value :
  108. ::boost::is_same<
  109. ::boost::remove_const<
  110. typename ::boost::iterator_value<I>::type
  111. >,
  112. ::boost::remove_const<V>
  113. >
  114. {};
  115. // destroy(I, I)
  116. template <typename I>
  117. void destroy_dispatch(I /*first*/, I /*last*/,
  118. boost::true_type const& /*has_trivial_destructor*/)
  119. {}
  120. template <typename I>
  121. void destroy_dispatch(I first, I last,
  122. boost::false_type const& /*has_trivial_destructor*/)
  123. {
  124. typedef typename boost::iterator_value<I>::type value_type;
  125. for ( ; first != last ; ++first )
  126. first->~value_type();
  127. }
  128. template <typename I>
  129. void destroy(I first, I last)
  130. {
  131. typedef typename boost::iterator_value<I>::type value_type;
  132. destroy_dispatch(first, last, has_trivial_destructor<value_type>());
  133. }
  134. // destroy(I)
  135. template <typename I>
  136. void destroy_dispatch(I /*pos*/,
  137. boost::true_type const& /*has_trivial_destructor*/)
  138. {}
  139. template <typename I>
  140. void destroy_dispatch(I pos,
  141. boost::false_type const& /*has_trivial_destructor*/)
  142. {
  143. typedef typename boost::iterator_value<I>::type value_type;
  144. pos->~value_type();
  145. }
  146. template <typename I>
  147. void destroy(I pos)
  148. {
  149. typedef typename boost::iterator_value<I>::type value_type;
  150. destroy_dispatch(pos, has_trivial_destructor<value_type>());
  151. }
  152. // copy(I, I, O)
  153. template <typename I, typename O>
  154. inline O copy_dispatch(I first, I last, O dst,
  155. boost::mpl::bool_<true> const& /*use_memmove*/)
  156. {
  157. typedef typename boost::iterator_value<I>::type value_type;
  158. typename boost::iterator_difference<I>::type d = std::distance(first, last);
  159. ::memmove(boost::addressof(*dst), boost::addressof(*first), sizeof(value_type) * d);
  160. return dst + d;
  161. }
  162. template <typename I, typename O>
  163. inline O copy_dispatch(I first, I last, O dst,
  164. boost::mpl::bool_<false> const& /*use_memmove*/)
  165. {
  166. return std::copy(first, last, dst); // may throw
  167. }
  168. template <typename I, typename O>
  169. inline O copy(I first, I last, O dst)
  170. {
  171. typedef typename
  172. ::boost::mpl::and_<
  173. are_corresponding<I, O>,
  174. ::boost::has_trivial_assign<
  175. typename ::boost::iterator_value<O>::type
  176. >
  177. >::type
  178. use_memmove;
  179. return copy_dispatch(first, last, dst, use_memmove()); // may throw
  180. }
  181. // uninitialized_copy(I, I, O)
  182. template <typename I, typename O>
  183. inline
  184. O uninitialized_copy_dispatch(I first, I last, O dst,
  185. boost::mpl::bool_<true> const& /*use_memcpy*/)
  186. {
  187. typedef typename boost::iterator_value<I>::type value_type;
  188. typename boost::iterator_difference<I>::type d = std::distance(first, last);
  189. ::memcpy(boost::addressof(*dst), boost::addressof(*first), sizeof(value_type) * d);
  190. return dst + d;
  191. }
  192. template <typename I, typename F>
  193. inline
  194. F uninitialized_copy_dispatch(I first, I last, F dst,
  195. boost::mpl::bool_<false> const& /*use_memcpy*/)
  196. {
  197. return std::uninitialized_copy(first, last, dst); // may throw
  198. }
  199. template <typename I, typename F>
  200. inline
  201. F uninitialized_copy(I first, I last, F dst)
  202. {
  203. typedef typename
  204. ::boost::mpl::and_<
  205. are_corresponding<I, F>,
  206. ::boost::has_trivial_copy<
  207. typename ::boost::iterator_value<F>::type
  208. >
  209. >::type
  210. use_memcpy;
  211. return uninitialized_copy_dispatch(first, last, dst, use_memcpy()); // may throw
  212. }
  213. // uninitialized_move(I, I, O)
  214. template <typename I, typename O>
  215. inline
  216. O uninitialized_move_dispatch(I first, I last, O dst,
  217. boost::mpl::bool_<true> const& /*use_memcpy*/)
  218. {
  219. typedef typename boost::iterator_value<I>::type value_type;
  220. typename boost::iterator_difference<I>::type d = std::distance(first, last);
  221. ::memcpy(boost::addressof(*dst), boost::addressof(*first), sizeof(value_type) * d);
  222. return dst + d;
  223. }
  224. template <typename I, typename O>
  225. inline
  226. O uninitialized_move_dispatch(I first, I last, O dst,
  227. boost::mpl::bool_<false> const& /*use_memcpy*/)
  228. {
  229. //return boost::uninitialized_move(first, last, dst); // may throw
  230. O o = dst;
  231. BOOST_TRY
  232. {
  233. typedef typename std::iterator_traits<O>::value_type value_type;
  234. for (; first != last; ++first, ++o )
  235. new (boost::addressof(*o)) value_type(boost::move(*first));
  236. }
  237. BOOST_CATCH(...)
  238. {
  239. destroy(dst, o);
  240. BOOST_RETHROW;
  241. }
  242. BOOST_CATCH_END
  243. return dst;
  244. }
  245. template <typename I, typename O>
  246. inline
  247. O uninitialized_move(I first, I last, O dst)
  248. {
  249. typedef typename
  250. ::boost::mpl::and_<
  251. are_corresponding<I, O>,
  252. ::boost::has_trivial_copy<
  253. typename ::boost::iterator_value<O>::type
  254. >
  255. >::type
  256. use_memcpy;
  257. return uninitialized_move_dispatch(first, last, dst, use_memcpy()); // may throw
  258. }
  259. // TODO - move uses memmove - implement 2nd version using memcpy?
  260. // move(I, I, O)
  261. template <typename I, typename O>
  262. inline
  263. O move_dispatch(I first, I last, O dst,
  264. boost::mpl::bool_<true> const& /*use_memmove*/)
  265. {
  266. typedef typename boost::iterator_value<I>::type value_type;
  267. typename boost::iterator_difference<I>::type d = std::distance(first, last);
  268. ::memmove(boost::addressof(*dst), boost::addressof(*first), sizeof(value_type) * d);
  269. return dst + d;
  270. }
  271. template <typename I, typename O>
  272. inline
  273. O move_dispatch(I first, I last, O dst,
  274. boost::mpl::bool_<false> const& /*use_memmove*/)
  275. {
  276. return boost::move(first, last, dst); // may throw
  277. }
  278. template <typename I, typename O>
  279. inline
  280. O move(I first, I last, O dst)
  281. {
  282. typedef typename
  283. ::boost::mpl::and_<
  284. are_corresponding<I, O>,
  285. ::boost::has_trivial_assign<
  286. typename ::boost::iterator_value<O>::type
  287. >
  288. >::type
  289. use_memmove;
  290. return move_dispatch(first, last, dst, use_memmove()); // may throw
  291. }
  292. // move_backward(BDI, BDI, BDO)
  293. template <typename BDI, typename BDO>
  294. inline
  295. BDO move_backward_dispatch(BDI first, BDI last, BDO dst,
  296. boost::mpl::bool_<true> const& /*use_memmove*/)
  297. {
  298. typedef typename boost::iterator_value<BDI>::type value_type;
  299. typename boost::iterator_difference<BDI>::type d = std::distance(first, last);
  300. BDO foo(dst - d);
  301. ::memmove(boost::addressof(*foo), boost::addressof(*first), sizeof(value_type) * d);
  302. return foo;
  303. }
  304. template <typename BDI, typename BDO>
  305. inline
  306. BDO move_backward_dispatch(BDI first, BDI last, BDO dst,
  307. boost::mpl::bool_<false> const& /*use_memmove*/)
  308. {
  309. return boost::move_backward(first, last, dst); // may throw
  310. }
  311. template <typename BDI, typename BDO>
  312. inline
  313. BDO move_backward(BDI first, BDI last, BDO dst)
  314. {
  315. typedef typename
  316. ::boost::mpl::and_<
  317. are_corresponding<BDI, BDO>,
  318. ::boost::has_trivial_assign<
  319. typename ::boost::iterator_value<BDO>::type
  320. >
  321. >::type
  322. use_memmove;
  323. return move_backward_dispatch(first, last, dst, use_memmove()); // may throw
  324. }
  325. template <typename T>
  326. struct has_nothrow_move : public
  327. ::boost::mpl::or_<
  328. boost::mpl::bool_<
  329. ::boost::has_nothrow_move<
  330. typename ::boost::remove_const<T>::type
  331. >::value
  332. >,
  333. boost::mpl::bool_<
  334. ::boost::has_nothrow_move<T>::value
  335. >
  336. >
  337. {};
  338. // uninitialized_move_if_noexcept(I, I, O)
  339. template <typename I, typename O>
  340. inline
  341. O uninitialized_move_if_noexcept_dispatch(I first, I last, O dst, boost::mpl::bool_<true> const& /*use_move*/)
  342. { return varray_detail::uninitialized_move(first, last, dst); }
  343. template <typename I, typename O>
  344. inline
  345. O uninitialized_move_if_noexcept_dispatch(I first, I last, O dst, boost::mpl::bool_<false> const& /*use_move*/)
  346. { return varray_detail::uninitialized_copy(first, last, dst); }
  347. template <typename I, typename O>
  348. inline
  349. O uninitialized_move_if_noexcept(I first, I last, O dst)
  350. {
  351. typedef typename has_nothrow_move<
  352. typename ::boost::iterator_value<O>::type
  353. >::type use_move;
  354. return uninitialized_move_if_noexcept_dispatch(first, last, dst, use_move()); // may throw
  355. }
  356. // move_if_noexcept(I, I, O)
  357. template <typename I, typename O>
  358. inline
  359. O move_if_noexcept_dispatch(I first, I last, O dst, boost::mpl::bool_<true> const& /*use_move*/)
  360. { return move(first, last, dst); }
  361. template <typename I, typename O>
  362. inline
  363. O move_if_noexcept_dispatch(I first, I last, O dst, boost::mpl::bool_<false> const& /*use_move*/)
  364. { return copy(first, last, dst); }
  365. template <typename I, typename O>
  366. inline
  367. O move_if_noexcept(I first, I last, O dst)
  368. {
  369. typedef typename has_nothrow_move<
  370. typename ::boost::iterator_value<O>::type
  371. >::type use_move;
  372. return move_if_noexcept_dispatch(first, last, dst, use_move()); // may throw
  373. }
  374. // uninitialized_fill(I, I)
  375. template <typename I>
  376. inline
  377. void uninitialized_fill_dispatch(I /*first*/, I /*last*/,
  378. boost::true_type const& /*has_trivial_constructor*/,
  379. boost::true_type const& /*disable_trivial_init*/)
  380. {}
  381. template <typename I>
  382. inline
  383. void uninitialized_fill_dispatch(I first, I last,
  384. boost::true_type const& /*has_trivial_constructor*/,
  385. boost::false_type const& /*disable_trivial_init*/)
  386. {
  387. typedef typename boost::iterator_value<I>::type value_type;
  388. for ( ; first != last ; ++first )
  389. new (boost::addressof(*first)) value_type();
  390. }
  391. template <typename I, typename DisableTrivialInit>
  392. inline
  393. void uninitialized_fill_dispatch(I first, I last,
  394. boost::false_type const& /*has_trivial_constructor*/,
  395. DisableTrivialInit const& /*not_used*/)
  396. {
  397. typedef typename boost::iterator_value<I>::type value_type;
  398. I it = first;
  399. BOOST_TRY
  400. {
  401. for ( ; it != last ; ++it )
  402. new (boost::addressof(*it)) value_type(); // may throw
  403. }
  404. BOOST_CATCH(...)
  405. {
  406. destroy(first, it);
  407. BOOST_RETHROW;
  408. }
  409. BOOST_CATCH_END
  410. }
  411. template <typename I, typename DisableTrivialInit>
  412. inline
  413. void uninitialized_fill(I first, I last, DisableTrivialInit const& disable_trivial_init)
  414. {
  415. typedef typename boost::iterator_value<I>::type value_type;
  416. uninitialized_fill_dispatch(first, last, boost::has_trivial_constructor<value_type>(), disable_trivial_init); // may throw
  417. }
  418. // construct(I)
  419. template <typename I>
  420. inline
  421. void construct_dispatch(boost::mpl::bool_<true> const& /*dont_init*/, I /*pos*/)
  422. {}
  423. template <typename I>
  424. inline
  425. void construct_dispatch(boost::mpl::bool_<false> const& /*dont_init*/, I pos)
  426. {
  427. typedef typename ::boost::iterator_value<I>::type value_type;
  428. new (static_cast<void*>(::boost::addressof(*pos))) value_type(); // may throw
  429. }
  430. template <typename DisableTrivialInit, typename I>
  431. inline
  432. void construct(DisableTrivialInit const&, I pos)
  433. {
  434. typedef typename ::boost::iterator_value<I>::type value_type;
  435. typedef typename ::boost::mpl::and_<
  436. boost::has_trivial_constructor<value_type>,
  437. DisableTrivialInit
  438. >::type dont_init;
  439. construct_dispatch(dont_init(), pos); // may throw
  440. }
  441. // construct(I, V)
  442. template <typename I, typename V>
  443. inline
  444. void construct_copy_dispatch(I pos, V const& v,
  445. boost::mpl::bool_<true> const& /*use_memcpy*/)
  446. {
  447. ::memcpy(boost::addressof(*pos), boost::addressof(v), sizeof(V));
  448. }
  449. template <typename I, typename P>
  450. inline
  451. void construct_copy_dispatch(I pos, P const& p,
  452. boost::mpl::bool_<false> const& /*use_memcpy*/)
  453. {
  454. typedef typename boost::iterator_value<I>::type V;
  455. new (static_cast<void*>(boost::addressof(*pos))) V(p); // may throw
  456. }
  457. template <typename DisableTrivialInit, typename I, typename P>
  458. inline
  459. void construct(DisableTrivialInit const&,
  460. I pos, P const& p)
  461. {
  462. typedef typename
  463. ::boost::mpl::and_<
  464. is_corresponding_value<I, P>,
  465. ::boost::has_trivial_copy<P>
  466. >::type
  467. use_memcpy;
  468. construct_copy_dispatch(pos, p, use_memcpy()); // may throw
  469. }
  470. // Needed by push_back(V &&)
  471. template <typename I, typename V>
  472. inline
  473. void construct_move_dispatch(I pos, V const& v,
  474. boost::mpl::bool_<true> const& /*use_memcpy*/)
  475. {
  476. ::memcpy(boost::addressof(*pos), boost::addressof(v), sizeof(V));
  477. }
  478. template <typename I, typename P>
  479. inline
  480. void construct_move_dispatch(I pos, BOOST_RV_REF(P) p,
  481. boost::mpl::bool_<false> const& /*use_memcpy*/)
  482. {
  483. typedef typename boost::iterator_value<I>::type V;
  484. new (static_cast<void*>(boost::addressof(*pos))) V(::boost::move(p)); // may throw
  485. }
  486. template <typename DisableTrivialInit, typename I, typename P>
  487. inline
  488. void construct(DisableTrivialInit const&, I pos, BOOST_RV_REF(P) p)
  489. {
  490. typedef typename
  491. ::boost::mpl::and_<
  492. is_corresponding_value<I, P>,
  493. ::boost::has_trivial_move_constructor<P>
  494. >::type
  495. use_memcpy;
  496. construct_move_dispatch(pos, ::boost::move(p), use_memcpy()); // may throw
  497. }
  498. // Needed by emplace_back() and emplace()
  499. #if !defined(BOOST_CONTAINER_VARRAY_DISABLE_EMPLACE)
  500. #if !defined(BOOST_NO_VARIADIC_TEMPLATES)
  501. template <typename DisableTrivialInit, typename I, class ...Args>
  502. inline
  503. void construct(DisableTrivialInit const&,
  504. I pos,
  505. BOOST_FWD_REF(Args) ...args)
  506. {
  507. typedef typename boost::iterator_value<I>::type V;
  508. new (static_cast<void*>(boost::addressof(*pos))) V(::boost::forward<Args>(args)...); // may throw
  509. }
  510. #else // !BOOST_NO_VARIADIC_TEMPLATES
  511. // BOOST_NO_RVALUE_REFERENCES -> P0 const& p0
  512. // !BOOST_NO_RVALUE_REFERENCES -> P0 && p0
  513. // which means that version with one parameter may take V const& v
  514. #define BOOST_PP_LOCAL_MACRO(n) \
  515. template <typename DisableTrivialInit, typename I, typename P BOOST_PP_ENUM_TRAILING_PARAMS(n, typename P) > \
  516. inline \
  517. void construct(DisableTrivialInit const&, \
  518. I pos, \
  519. BOOST_CONTAINER_PP_PARAM(P, p) \
  520. BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \
  521. { \
  522. typedef typename boost::iterator_value<I>::type V; \
  523. new \
  524. (static_cast<void*>(boost::addressof(*pos))) \
  525. V(p, BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)); /*may throw*/ \
  526. } \
  527. //
  528. #define BOOST_PP_LOCAL_LIMITS (1, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS)
  529. #include BOOST_PP_LOCAL_ITERATE()
  530. #endif // !BOOST_NO_VARIADIC_TEMPLATES
  531. #endif // !BOOST_CONTAINER_VARRAY_DISABLE_EMPLACE
  532. // assign(I, V)
  533. template <typename I, typename V>
  534. inline
  535. void assign_copy_dispatch(I pos, V const& v,
  536. boost::mpl::bool_<true> const& /*use_memcpy*/)
  537. {
  538. // TODO - use memmove here?
  539. ::memcpy(boost::addressof(*pos), boost::addressof(v), sizeof(V));
  540. }
  541. template <typename I, typename V>
  542. inline
  543. void assign_copy_dispatch(I pos, V const& v,
  544. boost::mpl::bool_<false> const& /*use_memcpy*/)
  545. {
  546. *pos = v; // may throw
  547. }
  548. template <typename I, typename V>
  549. inline
  550. void assign(I pos, V const& v)
  551. {
  552. typedef typename
  553. ::boost::mpl::and_<
  554. is_corresponding_value<I, V>,
  555. ::boost::has_trivial_assign<V>
  556. >::type
  557. use_memcpy;
  558. assign_copy_dispatch(pos, v, use_memcpy()); // may throw
  559. }
  560. template <typename I, typename V>
  561. inline
  562. void assign_move_dispatch(I pos, V const& v,
  563. boost::mpl::bool_<true> const& /*use_memcpy*/)
  564. {
  565. // TODO - use memmove here?
  566. ::memcpy(boost::addressof(*pos), boost::addressof(v), sizeof(V));
  567. }
  568. template <typename I, typename V>
  569. inline
  570. void assign_move_dispatch(I pos, BOOST_RV_REF(V) v,
  571. boost::mpl::bool_<false> const& /*use_memcpy*/)
  572. {
  573. *pos = boost::move(v); // may throw
  574. }
  575. template <typename I, typename V>
  576. inline
  577. void assign(I pos, BOOST_RV_REF(V) v)
  578. {
  579. typedef typename
  580. ::boost::mpl::and_<
  581. is_corresponding_value<I, V>,
  582. ::boost::has_trivial_move_assign<V>
  583. >::type
  584. use_memcpy;
  585. assign_move_dispatch(pos, ::boost::move(v), use_memcpy());
  586. }
  587. // uninitialized_copy_s
  588. template <typename I, typename F>
  589. inline std::size_t uninitialized_copy_s(I first, I last, F dest, std::size_t max_count)
  590. {
  591. std::size_t count = 0;
  592. F it = dest;
  593. BOOST_TRY
  594. {
  595. for ( ; first != last ; ++it, ++first, ++count )
  596. {
  597. if ( max_count <= count )
  598. return (std::numeric_limits<std::size_t>::max)();
  599. // dummy 0 as DisableTrivialInit
  600. construct(0, it, *first); // may throw
  601. }
  602. }
  603. BOOST_CATCH(...)
  604. {
  605. destroy(dest, it);
  606. BOOST_RETHROW;
  607. }
  608. BOOST_CATCH_END
  609. return count;
  610. }
  611. // scoped_destructor
  612. template<class T>
  613. class scoped_destructor
  614. {
  615. public:
  616. scoped_destructor(T * ptr) : m_ptr(ptr) {}
  617. ~scoped_destructor()
  618. {
  619. if(m_ptr)
  620. destroy(m_ptr);
  621. }
  622. void release() { m_ptr = 0; }
  623. private:
  624. T * m_ptr;
  625. };
  626. }}}}} // namespace boost::geometry::index::detail::varray_detail
  627. #endif // BOOST_GEOMETRY_INDEX_DETAIL_VARRAY_DETAIL_HPP