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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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_GENERATE_HPP
  14. #define RANGES_V3_VIEW_GENERATE_HPP
  15. #include <type_traits>
  16. #include <utility>
  17. #include <meta/meta.hpp>
  18. #include <range/v3/range_fwd.hpp>
  19. #include <range/v3/functional/invoke.hpp>
  20. #include <range/v3/iterator/unreachable_sentinel.hpp>
  21. #include <range/v3/range/access.hpp>
  22. #include <range/v3/range/primitives.hpp>
  23. #include <range/v3/range/traits.hpp>
  24. #include <range/v3/utility/optional.hpp>
  25. #include <range/v3/utility/semiregular_box.hpp>
  26. #include <range/v3/utility/static_const.hpp>
  27. #include <range/v3/view/facade.hpp>
  28. namespace ranges
  29. {
  30. /// \addtogroup group-views
  31. /// @{
  32. template<typename G>
  33. struct generate_view : view_facade<generate_view<G>, infinite>
  34. {
  35. private:
  36. friend range_access;
  37. using result_t = invoke_result_t<G &>;
  38. semiregular_box_t<G> gen_;
  39. detail::non_propagating_cache<result_t> val_;
  40. struct cursor
  41. {
  42. private:
  43. generate_view * view_;
  44. public:
  45. cursor() = default;
  46. explicit cursor(generate_view * view)
  47. : view_(view)
  48. {}
  49. result_t && read() const
  50. {
  51. if(!view_->val_)
  52. view_->val_.emplace(view_->gen_());
  53. return static_cast<result_t &&>(static_cast<result_t &>(*view_->val_));
  54. }
  55. void next()
  56. {
  57. if(view_->val_)
  58. view_->val_.reset();
  59. else
  60. view_->gen_();
  61. }
  62. };
  63. cursor begin_cursor()
  64. {
  65. return cursor{this};
  66. }
  67. unreachable_sentinel_t end_cursor() const
  68. {
  69. return {};
  70. }
  71. public:
  72. generate_view() = default;
  73. explicit generate_view(G g)
  74. : gen_(std::move(g))
  75. {}
  76. result_t & cached()
  77. {
  78. return *val_;
  79. }
  80. };
  81. namespace views
  82. {
  83. struct generate_fn
  84. {
  85. template<typename G>
  86. auto operator()(G g) const -> CPP_ret(generate_view<G>)( //
  87. requires invocable<G &> && copy_constructible<G> &&
  88. std::is_object<detail::decay_t<invoke_result_t<G &>>>::value &&
  89. constructible_from<detail::decay_t<invoke_result_t<G &>>,
  90. invoke_result_t<G &>> &&
  91. assignable_from<detail::decay_t<invoke_result_t<G &>> &,
  92. invoke_result_t<G &>>)
  93. {
  94. return generate_view<G>{std::move(g)};
  95. }
  96. };
  97. /// \relates generate_fn
  98. /// \ingroup group-views
  99. RANGES_INLINE_VARIABLE(generate_fn, generate)
  100. } // namespace views
  101. /// \@}
  102. } // namespace ranges
  103. #include <range/v3/detail/satisfy_boost_range.hpp>
  104. RANGES_SATISFY_BOOST_RANGE(::ranges::generate_view)
  105. #endif