From: Patrick Palka Date: Mon, 7 Nov 2022 18:29:42 +0000 (-0500) Subject: libstdc++: Implement ranges::as_rvalue_view from P2446R2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=2ee0165f72be96083deaa8fd315bcfed011acd52;p=platform%2Fupstream%2Fgcc.git libstdc++: Implement ranges::as_rvalue_view from P2446R2 libstdc++-v3/ChangeLog: * include/std/ranges (as_rvalue_view): Define. (enable_borrowed_range): Define. (views::__detail::__can_as_rvalue_view): Define. (views::_AsRvalue, views::as_rvalue): Define. * testsuite/std/ranges/adaptors/as_rvalue/1.cc: New test. --- diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges index 959886a..ba544e1 100644 --- a/libstdc++-v3/include/std/ranges +++ b/libstdc++-v3/include/std/ranges @@ -8486,6 +8486,96 @@ namespace views::__adaptor inline constexpr _CartesianProduct cartesian_product; } + + template + requires view<_Vp> + class as_rvalue_view : public view_interface> + { + _Vp _M_base = _Vp(); + + public: + as_rvalue_view() requires default_initializable<_Vp> = default; + + constexpr explicit + as_rvalue_view(_Vp __base) + : _M_base(std::move(__base)) + { } + + constexpr _Vp + base() const& requires copy_constructible<_Vp> + { return _M_base; } + + constexpr _Vp + base() && + { return std::move(_M_base); } + + constexpr auto + begin() requires (!__detail::__simple_view<_Vp>) + { return move_iterator(ranges::begin(_M_base)); } + + constexpr auto + begin() const requires range + { return move_iterator(ranges::begin(_M_base)); } + + constexpr auto + end() requires (!__detail::__simple_view<_Vp>) + { + if constexpr (common_range<_Vp>) + return move_iterator(ranges::end(_M_base)); + else + return move_sentinel(ranges::end(_M_base)); + } + + constexpr auto + end() const requires range + { + if constexpr (common_range) + return move_iterator(ranges::end(_M_base)); + else + return move_sentinel(ranges::end(_M_base)); + } + + constexpr auto + size() requires sized_range<_Vp> + { return ranges::size(_M_base); } + + constexpr auto + size() const requires sized_range + { return ranges::size(_M_base); } + }; + + template + as_rvalue_view(_Range&&) -> as_rvalue_view>; + + template + inline constexpr bool enable_borrowed_range> + = enable_borrowed_range<_Tp>; + + namespace views + { + namespace __detail + { + template + concept __can_as_rvalue_view = requires { as_rvalue_view(std::declval<_Tp>()); }; + } + + struct _AsRvalue : __adaptor::_RangeAdaptorClosure + { + template + requires __detail::__can_as_rvalue_view<_Range> + constexpr auto + operator() [[nodiscard]] (_Range&& __r) const + { + if constexpr (same_as, + range_reference_t<_Range>>) + return views::all(std::forward<_Range>(__r)); + else + return as_rvalue_view(std::forward<_Range>(__r)); + } + }; + + inline constexpr _AsRvalue as_rvalue; + } #endif // C++23 } // namespace ranges diff --git a/libstdc++-v3/testsuite/std/ranges/adaptors/as_rvalue/1.cc b/libstdc++-v3/testsuite/std/ranges/adaptors/as_rvalue/1.cc new file mode 100644 index 0000000..8ca4f50 --- /dev/null +++ b/libstdc++-v3/testsuite/std/ranges/adaptors/as_rvalue/1.cc @@ -0,0 +1,47 @@ +// { dg-options "-std=gnu++23" } +// { dg-do run { target c++23 } } + +#include +#include +#include +#include +#include + +namespace ranges = std::ranges; +namespace views = std::views; + +constexpr bool +test01() +{ + + std::unique_ptr a[3] = { std::make_unique(1), + std::make_unique(2), + std::make_unique(3) }; + std::unique_ptr b[3]; + auto v = a | views::as_rvalue; + ranges::copy(v, b); + VERIFY( ranges::all_of(a, [](auto& p) { return p.get() == nullptr; }) ); + VERIFY( ranges::equal(b | views::transform([](auto& p) { return *p; }), (int[]){1, 2, 3}) ); + + return true; +} + +void +test02() +{ + std::unique_ptr x = std::make_unique(42); + std::unique_ptr y; + __gnu_test::test_input_range rx(&x, &x+1); + auto v = rx | views::as_rvalue; + static_assert(!ranges::common_range); + ranges::copy(v, &y); + VERIFY( x.get() == nullptr ); + VERIFY( *y == 42 ); +} + +int +main() +{ + static_assert(test01()); + test02(); +}