[libcxx][ranges] Add `ranges::empty_view`.

Differential Revision: https://reviews.llvm.org/D103208
This commit is contained in:
zoecarver 2021-05-26 14:25:02 -07:00
parent 4a08163c73
commit d31a2e7554
5 changed files with 121 additions and 1 deletions

View File

@ -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],,,

1 Section Description Dependencies Assignee Patch Complete
53 [range.counted] view::counted [range.subrange]
54 [range.common] common_view [range.all] Zoe Carver
55 [range.reverse] reverse_view [range.all]
56
57
58
59

View File

@ -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

View File

@ -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

View File

@ -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>

View File

@ -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;
}