flatten_view.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*==============================================================================
  2. Copyright (c) 2013 Jamboree
  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. #ifndef BOOST_FUSION_FLATTEN_VIEW_HPP_INCLUDED
  7. #define BOOST_FUSION_FLATTEN_VIEW_HPP_INCLUDED
  8. #include <boost/fusion/support/config.hpp>
  9. #include <boost/mpl/bool.hpp>
  10. #include <boost/mpl/single_view.hpp>
  11. #include <boost/fusion/support/detail/access.hpp>
  12. #include <boost/fusion/support/is_view.hpp>
  13. #include <boost/fusion/support/category_of.hpp>
  14. #include <boost/fusion/support/sequence_base.hpp>
  15. #include <boost/fusion/sequence/intrinsic/begin.hpp>
  16. #include <boost/fusion/sequence/intrinsic/end.hpp>
  17. #include <boost/fusion/view/flatten_view/flatten_view_iterator.hpp>
  18. #ifdef _MSC_VER
  19. # pragma warning(push)
  20. # pragma warning(disable: 4512) // assignment operator could not be generated.
  21. #endif
  22. namespace boost { namespace fusion
  23. {
  24. struct forward_traversal_tag;
  25. struct flatten_view_tag;
  26. template <typename Sequence>
  27. struct flatten_view
  28. : sequence_base<flatten_view<Sequence> >
  29. {
  30. typedef flatten_view_tag fusion_tag;
  31. typedef fusion_sequence_tag tag; // this gets picked up by MPL
  32. typedef mpl::true_ is_view;
  33. typedef forward_traversal_tag category;
  34. typedef Sequence sequence_type;
  35. typedef typename result_of::begin<Sequence>::type first_type;
  36. typedef typename result_of::end<Sequence>::type last_type;
  37. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  38. explicit flatten_view(Sequence& seq)
  39. : seq(seq)
  40. {}
  41. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  42. first_type first() const { return fusion::begin(seq); }
  43. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  44. last_type last() const { return fusion::end(seq); }
  45. typename mpl::if_<traits::is_view<Sequence>, Sequence, Sequence&>::type seq;
  46. };
  47. }}
  48. #ifdef _MSC_VER
  49. # pragma warning(pop)
  50. #endif
  51. namespace boost { namespace fusion { namespace extension
  52. {
  53. template<>
  54. struct begin_impl<flatten_view_tag>
  55. {
  56. template<typename Sequence>
  57. struct apply
  58. {
  59. typedef typename Sequence::first_type first_type;
  60. typedef typename
  61. result_of::begin<
  62. mpl::single_view<
  63. typename Sequence::sequence_type> >::type
  64. root_iterator;
  65. typedef
  66. detail::seek_descent<root_iterator, first_type>
  67. seek_descent;
  68. typedef typename seek_descent::type type;
  69. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  70. static inline
  71. type call(Sequence& seq)
  72. {
  73. return seek_descent::apply(root_iterator(), seq.first());
  74. }
  75. };
  76. };
  77. template<>
  78. struct end_impl<flatten_view_tag>
  79. {
  80. template<typename Sequence>
  81. struct apply
  82. {
  83. typedef typename Sequence::last_type last_type;
  84. typedef typename
  85. result_of::end<
  86. mpl::single_view<
  87. typename Sequence::sequence_type> >::type
  88. type;
  89. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  90. static inline
  91. type call(Sequence&)
  92. {
  93. return type();
  94. }
  95. };
  96. };
  97. template<>
  98. struct size_impl<flatten_view_tag>
  99. {
  100. template <typename Sequence>
  101. struct apply
  102. : result_of::distance
  103. <
  104. typename result_of::begin<Sequence>::type
  105. , typename result_of::end<Sequence>::type
  106. >
  107. {};
  108. };
  109. template<>
  110. struct empty_impl<flatten_view_tag>
  111. {
  112. template <typename Sequence>
  113. struct apply
  114. : result_of::empty<typename Sequence::sequence_type>
  115. {};
  116. };
  117. }}}
  118. #endif