You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.6KB

  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Eric Niebler 2014-present
  5. //
  6. // Use, modification and distribution is subject to the
  7. // Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // Project home: https://github.com/ericniebler/range-v3
  12. //
  13. #ifndef RANGES_V3_VIEW_ALL_HPP
  14. #define RANGES_V3_VIEW_ALL_HPP
  15. #include <type_traits>
  16. #include <meta/meta.hpp>
  17. #include <range/v3/range_fwd.hpp>
  18. #include <range/v3/range/access.hpp>
  19. #include <range/v3/range/concepts.hpp>
  20. #include <range/v3/range/primitives.hpp>
  21. #include <range/v3/utility/static_const.hpp>
  22. #include <range/v3/view/ref.hpp>
  23. #include <range/v3/view/subrange.hpp>
  24. namespace ranges
  25. {
  26. /// \addtogroup group-views
  27. /// @{
  28. namespace views
  29. {
  30. struct all_fn : pipeable_base
  31. {
  32. private:
  33. /// If it's a view already, pass it though.
  34. template<typename T>
  35. static constexpr auto from_range_(T && t, std::true_type, detail::ignore_t,
  36. detail::ignore_t)
  37. {
  38. return static_cast<T &&>(t);
  39. }
  40. /// If it is container-like, turn it into a view, being careful
  41. /// to preserve the Sized-ness of the range.
  42. template<typename T>
  43. static constexpr auto from_range_(T && t, std::false_type, std::true_type,
  44. detail::ignore_t)
  45. {
  46. return ranges::views::ref(t);
  47. }
  48. /// Not a view and not an lvalue? If it's a forwarding_range_, then
  49. /// return a subrange holding the range's begin/end.
  50. template<typename T>
  51. static constexpr auto from_range_(T && t, std::false_type, std::false_type,
  52. std::true_type)
  53. {
  54. return make_subrange(static_cast<T &&>(t));
  55. }
  56. public:
  57. template<typename T>
  58. constexpr auto CPP_fun(operator())(T && t)(const requires viewable_range<T>)
  59. {
  60. return all_fn::from_range_(static_cast<T &&>(t),
  61. meta::bool_<view_<uncvref_t<T>>>{},
  62. std::is_lvalue_reference<T>{},
  63. meta::bool_<forwarding_range_<T>>{});
  64. }
  65. template<typename T>
  66. RANGES_DEPRECATED("Passing a reference_wrapper to views::all is deprecated.")
  67. constexpr auto operator()(std::reference_wrapper<T> r) const
  68. -> CPP_ret(ref_view<T>)( //
  69. requires range<T &>)
  70. {
  71. return ranges::views::ref(r.get());
  72. }
  73. };
  74. /// \relates all_fn
  75. /// \ingroup group-views
  76. RANGES_INLINE_VARIABLE(all_fn, all)
  77. template<typename Rng>
  78. using all_t = decltype(all(std::declval<Rng>()));
  79. } // namespace views
  80. template<typename Rng>
  81. struct identity_adaptor : Rng
  82. {
  83. CPP_assert(view_<Rng>);
  84. identity_adaptor() = default;
  85. constexpr explicit identity_adaptor(Rng const & rng)
  86. : Rng(rng)
  87. {}
  88. constexpr explicit identity_adaptor(Rng && rng)
  89. : Rng(detail::move(rng))
  90. {}
  91. };
  92. namespace cpp20
  93. {
  94. namespace views
  95. {
  96. using ranges::views::all;
  97. }
  98. CPP_template(typename Rng)( //
  99. requires viewable_range<Rng>) //
  100. using all_view = ranges::views::all_t<Rng>;
  101. } // namespace cpp20
  102. /// @}
  103. } // namespace ranges
  104. #endif