]> gcc.gnu.org Git - gcc.git/commitdiff
libstdc++: Implement ranges::as_rvalue_view from P2446R2
authorPatrick Palka <ppalka@redhat.com>
Mon, 7 Nov 2022 18:29:42 +0000 (13:29 -0500)
committerPatrick Palka <ppalka@redhat.com>
Mon, 7 Nov 2022 18:29:42 +0000 (13:29 -0500)
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
libstdc++-v3/testsuite/std/ranges/adaptors/as_rvalue/1.cc [new file with mode: 0644]

index 959886a1a550dc01c1ce9aa5d34c11b34287075e..ba544e116e14bec8d70e2e2e00e1907badf39891 100644 (file)
@@ -8486,6 +8486,96 @@ 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 (file)
index 0000000..8ca4f50
--- /dev/null
@@ -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();
+}
This page took 0.06991 seconds and 5 git commands to generate.