[libcxx-commits] [libcxx] d31a2e7 - [libcxx][ranges] Add `ranges::empty_view`.
via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Jun 4 09:38:56 PDT 2021
Author: zoecarver
Date: 2021-06-04T09:38:49-07:00
New Revision: d31a2e7554ea1ddc37a4aa53318549036ae1d780
URL: https://github.com/llvm/llvm-project/commit/d31a2e7554ea1ddc37a4aa53318549036ae1d780
DIFF: https://github.com/llvm/llvm-project/commit/d31a2e7554ea1ddc37a4aa53318549036ae1d780.diff
LOG: [libcxx][ranges] Add `ranges::empty_view`.
Differential Revision: https://reviews.llvm.org/D103208
Added:
libcxx/include/__ranges/empty_view.h
libcxx/test/std/ranges/range.adaptors/range.empty/empty_view.pass.cpp
Modified:
libcxx/docs/OneRangesProposalStatus.csv
libcxx/include/CMakeLists.txt
libcxx/include/ranges
Removed:
################################################################################
diff --git a/libcxx/docs/OneRangesProposalStatus.csv b/libcxx/docs/OneRangesProposalStatus.csv
index 95e3030f6e295..24c7a3761bfe8 100644
--- a/libcxx/docs/OneRangesProposalStatus.csv
+++ b/libcxx/docs/OneRangesProposalStatus.csv
@@ -53,7 +53,7 @@ bidirectional_range: `D100278 <https://llvm.org/D100278>`_",
[range.iota],iota_view,[range.all],Louis Dionne,,
[range.take],take_view,[range.all],Zoe Carver,,
[range.join],join_view,[range.all],Christopher Di Bella,,
-[range.empty],empty_view,[view.interface],Zoe Carver,,
+[range.empty],empty_view,[view.interface],Zoe Carver,,✅
[range.single],single_view,[view.interface],,,
[range.split],split_view,[range.all],,,
[range.counted],view::counted,[range.subrange],,,
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 75569e2bf53ae..bceb7c3c2efd3 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -122,6 +122,7 @@ set(files
__ranges/concepts.h
__ranges/data.h
__ranges/empty.h
+ __ranges/empty_view.h
__ranges/enable_borrowed_range.h
__ranges/view_interface.h
__ranges/view.h
diff --git a/libcxx/include/__ranges/empty_view.h b/libcxx/include/__ranges/empty_view.h
new file mode 100644
index 0000000000000..96dc78e7dd141
--- /dev/null
+++ b/libcxx/include/__ranges/empty_view.h
@@ -0,0 +1,45 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+#ifndef _LIBCPP___RANGES_EMPTY_VIEW_H
+#define _LIBCPP___RANGES_EMPTY_VIEW_H
+
+#include <__config>
+#include <__ranges/view_interface.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#pragma GCC system_header
+#endif
+
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if !defined(_LIBCPP_HAS_NO_RANGES)
+
+namespace ranges {
+ template<class _Tp>
+ requires is_object_v<_Tp>
+ class empty_view : public view_interface<empty_view<_Tp>> {
+ public:
+ static constexpr _Tp* begin() noexcept { return nullptr; }
+ static constexpr _Tp* end() noexcept { return nullptr; }
+ static constexpr _Tp* data() noexcept { return nullptr; }
+ static constexpr size_t size() noexcept { return 0; }
+ static constexpr bool empty() noexcept { return true; }
+ };
+} // namespace ranges
+
+#endif // !defined(_LIBCPP_HAS_NO_RANGES)
+
+_LIBCPP_END_NAMESPACE_STD
+
+_LIBCPP_POP_MACROS
+
+#endif // _LIBCPP___RANGES_EMPTY_VIEW_H
diff --git a/libcxx/include/ranges b/libcxx/include/ranges
index 5bbe9f73af27b..dbfaadf885e79 100644
--- a/libcxx/include/ranges
+++ b/libcxx/include/ranges
@@ -83,6 +83,11 @@ namespace std::ranges {
template<class D>
requires is_class_v<D> && same_as<D, remove_cv_t<D>>
class view_interface;
+
+ // [range.empty], empty view
+ template<class T>
+ requires is_object_v<T>
+ class empty_view;
}
*/
@@ -92,6 +97,7 @@ namespace std::ranges {
#include <__ranges/concepts.h>
#include <__ranges/data.h>
#include <__ranges/empty.h>
+#include <__ranges/empty_view.h>
#include <__ranges/enable_borrowed_range.h>
#include <__ranges/size.h>
#include <__ranges/view_interface.h>
diff --git a/libcxx/test/std/ranges/range.adaptors/range.empty/empty_view.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.empty/empty_view.pass.cpp
new file mode 100644
index 0000000000000..6ee337fbf6f1e
--- /dev/null
+++ b/libcxx/test/std/ranges/range.adaptors/range.empty/empty_view.pass.cpp
@@ -0,0 +1,68 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// template<class T>
+// class empty_view;
+
+#include <ranges>
+#include <cassert>
+
+#include "test_macros.h"
+
+template<class T>
+constexpr void testType() {
+ static_assert(std::ranges::range<std::ranges::empty_view<T>>);
+ static_assert(std::ranges::range<const std::ranges::empty_view<T>>);
+ static_assert(std::ranges::view<std::ranges::empty_view<T>>);
+
+ std::ranges::empty_view<T> empty;
+
+ assert(empty.begin() == nullptr);
+ assert(empty.end() == nullptr);
+ assert(empty.data() == nullptr);
+ assert(empty.size() == 0);
+ assert(empty.empty() == true);
+
+ assert(std::ranges::begin(empty) == nullptr);
+ assert(std::ranges::end(empty) == nullptr);
+ assert(std::ranges::data(empty) == nullptr);
+ assert(std::ranges::size(empty) == 0);
+ assert(std::ranges::empty(empty) == true);
+}
+
+struct Empty {};
+struct BigType { char buff[8]; };
+
+template<class T>
+concept ValidEmptyView = requires { typename std::ranges::empty_view<T>; };
+
+constexpr bool test() {
+ // Not objects:
+ static_assert(!ValidEmptyView<int&>);
+ static_assert(!ValidEmptyView<void>);
+
+ testType<int>();
+ testType<const int>();
+ testType<int*>();
+ testType<Empty>();
+ testType<const Empty>();
+ testType<BigType>();
+
+ return true;
+}
+
+int main(int, char**) {
+ test();
+ static_assert(test());
+
+ return 0;
+}
More information about the libcxx-commits
mailing list