nview.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*=============================================================================
  2. Copyright (c) 2009 Hartmut Kaiser
  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(BOOST_FUSION_NVIEW_SEP_23_2009_0948PM)
  7. #define BOOST_FUSION_NVIEW_SEP_23_2009_0948PM
  8. #include <boost/mpl/size.hpp>
  9. #include <boost/mpl/if.hpp>
  10. #include <boost/mpl/vector_c.hpp>
  11. #include <boost/utility/result_of.hpp>
  12. #include <boost/type_traits/remove_reference.hpp>
  13. #include <boost/type_traits/add_reference.hpp>
  14. #include <boost/type_traits/add_const.hpp>
  15. #include <boost/fusion/support/is_view.hpp>
  16. #include <boost/fusion/support/category_of.hpp>
  17. #include <boost/fusion/support/sequence_base.hpp>
  18. #include <boost/fusion/container/vector.hpp>
  19. #include <boost/fusion/view/transform_view.hpp>
  20. #include <boost/config.hpp>
  21. namespace boost { namespace fusion
  22. {
  23. namespace detail
  24. {
  25. struct addref
  26. {
  27. template<typename Sig>
  28. struct result;
  29. template<typename U>
  30. struct result<addref(U)> : add_reference<U> {};
  31. #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
  32. template <typename T>
  33. typename add_reference<T>::type
  34. operator()(T& x) const
  35. {
  36. return x;
  37. }
  38. #else
  39. template <typename T>
  40. typename result<addref(T)>::type
  41. operator()(T&& x) const
  42. {
  43. return x;
  44. }
  45. #endif
  46. };
  47. struct addconstref
  48. {
  49. template<typename Sig>
  50. struct result;
  51. template<typename U>
  52. struct result<addconstref(U)>
  53. : add_reference<typename add_const<U>::type>
  54. {};
  55. template <typename T>
  56. typename add_reference<typename add_const<T>::type>::type
  57. operator()(T& x) const
  58. {
  59. return x;
  60. }
  61. template <typename T>
  62. typename add_reference<typename add_const<T>::type>::type
  63. operator()(T const& x) const
  64. {
  65. return x;
  66. }
  67. };
  68. }
  69. struct nview_tag;
  70. struct random_access_traversal_tag;
  71. struct fusion_sequence_tag;
  72. template<typename Sequence, typename Indicies>
  73. struct nview
  74. : sequence_base<nview<Sequence, Indicies> >
  75. {
  76. typedef nview_tag fusion_tag;
  77. typedef fusion_sequence_tag tag; // this gets picked up by MPL
  78. typedef random_access_traversal_tag category;
  79. typedef mpl::true_ is_view;
  80. typedef Indicies index_type;
  81. typedef typename mpl::size<Indicies>::type size;
  82. typedef typename mpl::if_<
  83. is_const<Sequence>, detail::addconstref, detail::addref
  84. >::type transform_type;
  85. typedef transform_view<Sequence, transform_type> transform_view_type;
  86. typedef typename result_of::as_vector<transform_view_type>::type
  87. sequence_type;
  88. explicit nview(Sequence& val)
  89. : seq(sequence_type(transform_view_type(val, transform_type())))
  90. {}
  91. sequence_type seq;
  92. };
  93. }}
  94. // define the nview() generator functions
  95. #include <boost/fusion/view/nview/detail/nview_impl.hpp>
  96. #endif