istream.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Eric Niebler 2013-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_ISTREAM_HPP
  14. #define RANGES_V3_VIEW_ISTREAM_HPP
  15. #include <istream>
  16. #include <range/v3/range_fwd.hpp>
  17. #include <range/v3/iterator/default_sentinel.hpp>
  18. #include <range/v3/utility/semiregular_box.hpp>
  19. #include <range/v3/utility/static_const.hpp>
  20. #include <range/v3/view/facade.hpp>
  21. #include <range/v3/detail/prologue.hpp>
  22. namespace ranges
  23. {
  24. /// \addtogroup group-views
  25. /// @{
  26. template<typename Val>
  27. struct istream_view : view_facade<istream_view<Val>, unknown>
  28. {
  29. private:
  30. friend range_access;
  31. std::istream * sin_;
  32. semiregular_box_t<Val> obj_;
  33. struct cursor
  34. {
  35. private:
  36. friend range_access;
  37. using single_pass = std::true_type;
  38. istream_view * rng_ = nullptr;
  39. public:
  40. cursor() = default;
  41. explicit cursor(istream_view * rng)
  42. : rng_(rng)
  43. {}
  44. void next()
  45. {
  46. rng_->next();
  47. }
  48. Val & read() const noexcept
  49. {
  50. return rng_->cached();
  51. }
  52. bool equal(default_sentinel_t) const
  53. {
  54. return !rng_->sin_;
  55. }
  56. bool equal(cursor that) const
  57. {
  58. return !rng_->sin_ == !that.rng_->sin_;
  59. }
  60. };
  61. void next()
  62. {
  63. if(!(*sin_ >> cached()))
  64. sin_ = nullptr;
  65. }
  66. cursor begin_cursor()
  67. {
  68. return cursor{this};
  69. }
  70. public:
  71. istream_view() = default;
  72. explicit istream_view(std::istream & sin)
  73. : sin_(&sin)
  74. , obj_{}
  75. {
  76. next(); // prime the pump
  77. }
  78. Val & cached() noexcept
  79. {
  80. return obj_;
  81. }
  82. };
  83. /// \cond
  84. template<typename Val>
  85. using istream_range RANGES_DEPRECATED(
  86. "istream_range<T> has been renamed to istream_view<T>") = istream_view<Val>;
  87. /// \endcond
  88. /// \cond
  89. namespace _istream_
  90. {
  91. /// \endcond
  92. template(typename Val)(
  93. requires copy_constructible<Val> AND default_constructible<Val>)
  94. inline istream_view<Val> istream(std::istream & sin)
  95. {
  96. return istream_view<Val>{sin};
  97. }
  98. /// \cond
  99. } // namespace _istream_
  100. using namespace _istream_;
  101. /// \endcond
  102. namespace cpp20
  103. {
  104. template<typename Val>
  105. using basic_istream_view = ::ranges::istream_view<Val>;
  106. } // namespace cpp20
  107. /// @}
  108. } // namespace ranges
  109. #include <range/v3/detail/epilogue.hpp>
  110. #endif