From: Patrick Palka <ppalka@redhat.com>
To: gcc-patches@gcc.gnu.org
Cc: libstdc++@gcc.gnu.org, Patrick Palka <ppalka@redhat.com>
Subject: [PATCH] libstdc++: Implement ranges::as_rvalue_view from P2446R2
Date: Mon, 31 Oct 2022 16:33:10 -0400 [thread overview]
Message-ID: <20221031203310.852924-1-ppalka@redhat.com> (raw)
Tested on x86_64-pc-linux-gnu, does this look OK for trunk?
libstdc++-v3/ChangeLog:
* include/std/ranges (as_rvalue_view): Define.
(enable_borrowed_range<as_rvalue_view>): Define.
(views::__detail::__can_as_rvalue_view): Define.
(views::_AsRvalue, views::as_rvalue): Define.
* testsuite/std/ranges/adaptors/as_rvalue/1.cc: New test.
---
libstdc++-v3/include/std/ranges | 88 +++++++++++++++++++
.../std/ranges/adaptors/as_rvalue/1.cc | 47 ++++++++++
2 files changed, 135 insertions(+)
create mode 100644 libstdc++-v3/testsuite/std/ranges/adaptors/as_rvalue/1.cc
diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
index 959886a1a55..239b3b61d30 100644
--- a/libstdc++-v3/include/std/ranges
+++ b/libstdc++-v3/include/std/ranges
@@ -8486,6 +8486,94 @@ namespace views::__adaptor
inline constexpr _CartesianProduct cartesian_product;
}
+
+ template<input_range _Vp>
+ requires view<_Vp>
+ class as_rvalue_view : public view_interface<as_rvalue_view<_Vp>>
+ {
+ _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<const _Vp>
+ { 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<const _Vp>
+ {
+ if constexpr (common_range<const _Vp>)
+ 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<const _Vp>
+ { return ranges::size(_M_base); }
+ };
+
+ template<typename _Range>
+ as_rvalue_view(_Range&&) -> as_rvalue_view<views::all_t<_Range>>;
+
+ template<typename _Tp>
+ inline constexpr bool enable_borrowed_range<as_rvalue_view<_Tp>>
+ = enable_borrowed_range<_Tp>;
+
+ namespace views
+ {
+ namespace __detail
+ {
+ template<typename _Tp>
+ concept __can_as_rvalue_view = requires { as_rvalue_view(std::declval<_Tp>()); };
+ }
+
+ struct _AsRvalue : __adaptor::_RangeAdaptorClosure
+ {
+ template<viewable_range _Range>
+ requires __detail::__can_as_rvalue_view<_Range>
+ constexpr auto
+ operator() [[nodiscard]] (_Range&& __r) const
+ {
+ if constexpr (same_as<range_rvalue_reference_t<_Range>,
+ 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 00000000000..8ca4f50e9d2
--- /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 <ranges>
+#include <algorithm>
+#include <memory>
+#include <testsuite_hooks.h>
+#include <testsuite_iterators.h>
+
+namespace ranges = std::ranges;
+namespace views = std::views;
+
+constexpr bool
+test01()
+{
+
+ std::unique_ptr<int> a[3] = { std::make_unique<int>(1),
+ std::make_unique<int>(2),
+ std::make_unique<int>(3) };
+ std::unique_ptr<int> 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<int> x = std::make_unique<int>(42);
+ std::unique_ptr<int> y;
+ __gnu_test::test_input_range rx(&x, &x+1);
+ auto v = rx | views::as_rvalue;
+ static_assert(!ranges::common_range<decltype(v)>);
+ ranges::copy(v, &y);
+ VERIFY( x.get() == nullptr );
+ VERIFY( *y == 42 );
+}
+
+int
+main()
+{
+ static_assert(test01());
+ test02();
+}
--
2.38.1.381.gc03801e19c
next reply other threads:[~2022-10-31 20:33 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-31 20:33 Patrick Palka [this message]
2022-11-01 12:34 ` Jonathan Wakely
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221031203310.852924-1-ppalka@redhat.com \
--to=ppalka@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=libstdc++@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).