insert.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #if !defined(FUSION_INSERT_07222005_0730)
  7. #define FUSION_INSERT_07222005_0730
  8. #include <boost/fusion/support/config.hpp>
  9. #include <boost/fusion/support/detail/as_fusion_element.hpp>
  10. #include <boost/fusion/iterator/mpl/convert_iterator.hpp>
  11. #include <boost/fusion/view/joint_view/joint_view.hpp>
  12. #include <boost/fusion/view/single_view/single_view.hpp>
  13. #include <boost/fusion/view/iterator_range/iterator_range.hpp>
  14. #include <boost/fusion/sequence/intrinsic/begin.hpp>
  15. #include <boost/fusion/sequence/intrinsic/end.hpp>
  16. #include <boost/fusion/adapted/mpl/mpl_iterator.hpp>
  17. #include <boost/fusion/support/is_sequence.hpp>
  18. #include <boost/utility/enable_if.hpp>
  19. namespace boost { namespace fusion
  20. {
  21. namespace result_of
  22. {
  23. template <typename Sequence, typename Position, typename T>
  24. struct insert
  25. {
  26. typedef typename detail::as_fusion_element<T>::type element_type;
  27. typedef typename convert_iterator<Position>::type pos_type;
  28. typedef typename result_of::begin<Sequence>::type first_type;
  29. typedef typename result_of::end<Sequence>::type last_type;
  30. typedef iterator_range<first_type, pos_type> left_type;
  31. typedef iterator_range<pos_type, last_type> right_type;
  32. typedef fusion::single_view<element_type> single_view;
  33. typedef joint_view<left_type, single_view const> left_insert_type;
  34. typedef joint_view<left_insert_type, right_type> type;
  35. };
  36. }
  37. template <typename Sequence, typename Position, typename T>
  38. BOOST_CXX14_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  39. inline typename
  40. lazy_enable_if<
  41. traits::is_sequence<Sequence>
  42. , result_of::insert<Sequence const, Position, T>
  43. >::type
  44. insert(Sequence const& seq, Position const& pos, T const& x)
  45. {
  46. typedef result_of::insert<
  47. Sequence const, Position, T>
  48. result_of;
  49. typedef typename result_of::left_type left_type;
  50. typedef typename result_of::right_type right_type;
  51. typedef typename result_of::single_view single_view;
  52. typedef typename result_of::left_insert_type left_insert_type;
  53. typedef typename result_of::type result;
  54. left_type left(fusion::begin(seq), convert_iterator<Position>::call(pos));
  55. right_type right(convert_iterator<Position>::call(pos), fusion::end(seq));
  56. single_view insert(x);
  57. left_insert_type left_insert(left, insert);
  58. return result(left_insert, right);
  59. }
  60. }}
  61. #endif