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