istream_view.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___RANGES_ISTREAM_VIEW_H
  10. #define _LIBCPP___RANGES_ISTREAM_VIEW_H
  11. #include <__concepts/constructible.h>
  12. #include <__concepts/derived_from.h>
  13. #include <__concepts/movable.h>
  14. #include <__config>
  15. #include <__iterator/default_sentinel.h>
  16. #include <__iterator/iterator_traits.h>
  17. #include <__memory/addressof.h>
  18. #include <__ranges/view_interface.h>
  19. #include <__type_traits/remove_cvref.h>
  20. #include <__utility/forward.h>
  21. #include <cstddef>
  22. #include <iosfwd>
  23. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  24. # pragma GCC system_header
  25. #endif
  26. #if _LIBCPP_STD_VER >= 20
  27. _LIBCPP_BEGIN_NAMESPACE_STD
  28. namespace ranges {
  29. template <class _Val, class _CharT, class _Traits>
  30. concept __stream_extractable = requires(basic_istream<_CharT, _Traits>& __is, _Val& __t) { __is >> __t; };
  31. template <movable _Val, class _CharT, class _Traits = char_traits<_CharT>>
  32. requires default_initializable<_Val> && __stream_extractable<_Val, _CharT, _Traits>
  33. class basic_istream_view : public view_interface<basic_istream_view<_Val, _CharT, _Traits>> {
  34. class __iterator;
  35. public:
  36. _LIBCPP_HIDE_FROM_ABI constexpr explicit basic_istream_view(basic_istream<_CharT, _Traits>& __stream)
  37. : __stream_(std::addressof(__stream)) {}
  38. _LIBCPP_HIDE_FROM_ABI constexpr auto begin() {
  39. *__stream_ >> __value_;
  40. return __iterator{*this};
  41. }
  42. _LIBCPP_HIDE_FROM_ABI constexpr default_sentinel_t end() const noexcept { return default_sentinel; }
  43. private:
  44. basic_istream<_CharT, _Traits>* __stream_;
  45. _LIBCPP_NO_UNIQUE_ADDRESS _Val __value_ = _Val();
  46. };
  47. template <movable _Val, class _CharT, class _Traits>
  48. requires default_initializable<_Val> && __stream_extractable<_Val, _CharT, _Traits>
  49. class basic_istream_view<_Val, _CharT, _Traits>::__iterator {
  50. public:
  51. using iterator_concept = input_iterator_tag;
  52. using difference_type = ptrdiff_t;
  53. using value_type = _Val;
  54. _LIBCPP_HIDE_FROM_ABI constexpr explicit __iterator(
  55. basic_istream_view<_Val, _CharT, _Traits>& __parent) noexcept
  56. : __parent_(std::addressof(__parent)) {}
  57. __iterator(const __iterator&) = delete;
  58. _LIBCPP_HIDE_FROM_ABI __iterator(__iterator&&) = default;
  59. __iterator& operator=(const __iterator&) = delete;
  60. _LIBCPP_HIDE_FROM_ABI __iterator& operator=(__iterator&&) = default;
  61. _LIBCPP_HIDE_FROM_ABI __iterator& operator++() {
  62. *__parent_->__stream_ >> __parent_->__value_;
  63. return *this;
  64. }
  65. _LIBCPP_HIDE_FROM_ABI void operator++(int) { ++*this; }
  66. _LIBCPP_HIDE_FROM_ABI _Val& operator*() const { return __parent_->__value_; }
  67. _LIBCPP_HIDE_FROM_ABI friend bool operator==(const __iterator& __x, default_sentinel_t) {
  68. return !*__x.__get_parent_stream();
  69. }
  70. private:
  71. basic_istream_view<_Val, _CharT, _Traits>* __parent_;
  72. _LIBCPP_HIDE_FROM_ABI constexpr basic_istream<_CharT, _Traits>* __get_parent_stream() const {
  73. return __parent_->__stream_;
  74. }
  75. };
  76. template <class _Val>
  77. using istream_view = basic_istream_view<_Val, char>;
  78. # ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  79. template <class _Val>
  80. using wistream_view = basic_istream_view<_Val, wchar_t>;
  81. # endif
  82. namespace views {
  83. namespace __istream {
  84. // clang-format off
  85. template <class _Tp>
  86. struct __fn {
  87. template <class _Up, class _UnCVRef = remove_cvref_t<_Up>>
  88. requires derived_from<_UnCVRef, basic_istream<typename _UnCVRef::char_type,
  89. typename _UnCVRef::traits_type>>
  90. _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Up&& __u) const
  91. noexcept(noexcept(basic_istream_view<_Tp, typename _UnCVRef::char_type,
  92. typename _UnCVRef::traits_type>(std::forward<_Up>(__u))))
  93. -> decltype( basic_istream_view<_Tp, typename _UnCVRef::char_type,
  94. typename _UnCVRef::traits_type>(std::forward<_Up>(__u)))
  95. { return basic_istream_view<_Tp, typename _UnCVRef::char_type,
  96. typename _UnCVRef::traits_type>(std::forward<_Up>(__u));
  97. }
  98. };
  99. // clang-format on
  100. } // namespace __istream
  101. inline namespace __cpo {
  102. template <class _Tp>
  103. inline constexpr auto istream = __istream::__fn<_Tp>{};
  104. } // namespace __cpo
  105. } // namespace views
  106. } // namespace ranges
  107. _LIBCPP_END_NAMESPACE_STD
  108. #endif // _LIBCPP_STD_VER >= 20
  109. #endif // _LIBCPP___RANGES_ISTREAM_VIEW_H