-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This implements `std::ranges::views::empty`, see https://en.cppreference.com/w/cpp/ranges/empty_view
- Loading branch information
Showing
6 changed files
with
253 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// -*- 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 | ||
// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES | ||
// | ||
//===----------------------------------------------------------------------===// | ||
#ifndef _LIBCUDACXX___RANGES_EMPTY_VIEW_H | ||
#define _LIBCUDACXX___RANGES_EMPTY_VIEW_H | ||
|
||
#include <cuda/std/detail/__config> | ||
|
||
#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) | ||
# pragma GCC system_header | ||
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) | ||
# pragma clang system_header | ||
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) | ||
# pragma system_header | ||
#endif // no system header | ||
|
||
#include <cuda/std/__ranges/enable_borrowed_range.h> | ||
#include <cuda/std/__ranges/view_interface.h> | ||
#include <cuda/std/__type_traits/is_object.h> | ||
|
||
#if _CCCL_STD_VER >= 2017 && !_CCCL_COMPILER(MSVC2017) | ||
|
||
_LIBCUDACXX_BEGIN_NAMESPACE_RANGES | ||
_LIBCUDACXX_BEGIN_NAMESPACE_RANGES_ABI | ||
|
||
_CCCL_TEMPLATE(class _Tp) | ||
_CCCL_REQUIRES(_CCCL_TRAIT(is_object, _Tp)) | ||
class empty_view : public view_interface<empty_view<_Tp>> | ||
{ | ||
public: | ||
_LIBCUDACXX_HIDE_FROM_ABI static constexpr _Tp* begin() noexcept | ||
{ | ||
return nullptr; | ||
} | ||
_LIBCUDACXX_HIDE_FROM_ABI static constexpr _Tp* end() noexcept | ||
{ | ||
return nullptr; | ||
} | ||
_LIBCUDACXX_HIDE_FROM_ABI static constexpr _Tp* data() noexcept | ||
{ | ||
return nullptr; | ||
} | ||
_LIBCUDACXX_HIDE_FROM_ABI static constexpr size_t size() noexcept | ||
{ | ||
return 0; | ||
} | ||
_LIBCUDACXX_HIDE_FROM_ABI static constexpr bool empty() noexcept | ||
{ | ||
return true; | ||
} | ||
}; | ||
|
||
_LIBCUDACXX_END_NAMESPACE_RANGES_ABI | ||
|
||
template <class _Tp> | ||
_CCCL_INLINE_VAR constexpr bool enable_borrowed_range<empty_view<_Tp>> = true; | ||
|
||
_LIBCUDACXX_END_NAMESPACE_RANGES | ||
|
||
_LIBCUDACXX_BEGIN_NAMESPACE_VIEWS | ||
|
||
# if _CCCL_COMPILER(MSVC) | ||
template <class _Tp> | ||
_CCCL_INLINE_VAR constexpr empty_view<_Tp> empty{}; | ||
# else // ^^^ _CCCL_COMPILER_MSVC ^^^ / vvv !_CCCL_COMPILER_MSVC vvv | ||
template <class _Tp> | ||
_CCCL_GLOBAL_CONSTANT empty_view<_Tp> empty{}; | ||
# endif // !_CCCL_COMPILER_MSVC | ||
|
||
_LIBCUDACXX_END_NAMESPACE_VIEWS | ||
|
||
#endif // _CCCL_STD_VER >= 2017 && !_CCCL_COMPILER_MSVC_2017 | ||
|
||
#endif // _LIBCUDACXX___RANGES_EMPTY_VIEW_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
libcudacxx/test/libcudacxx/std/ranges/range.adaptors/range.empty/borrowing.compile.pass.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// UNSUPPORTED: c++03, c++11, c++14 | ||
// UNSUPPORTED: msvc-19.16 | ||
|
||
// template<class T> | ||
// inline constexpr bool enable_borrowed_range<empty_view<T>> = true; | ||
|
||
#include <cuda/std/ranges> | ||
|
||
#include "test_range.h" | ||
|
||
static_assert(cuda::std::ranges::borrowed_range<cuda::std::ranges::empty_view<int>>); | ||
static_assert(cuda::std::ranges::borrowed_range<cuda::std::ranges::empty_view<int*>>); | ||
static_assert(cuda::std::ranges::borrowed_range<cuda::std::ranges::empty_view<BorrowedView>>); | ||
#if _LIBCUDACXX_HAS_RANGES | ||
static_assert(cuda::std::ranges::borrowed_range<cuda::std::ranges::empty_view<NonBorrowedView>>); | ||
#endif | ||
|
||
int main(int, char**) | ||
{ | ||
return 0; | ||
} |
83 changes: 83 additions & 0 deletions
83
libcudacxx/test/libcudacxx/std/ranges/range.adaptors/range.empty/empty_view.pass.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// UNSUPPORTED: c++03, c++11, c++14 | ||
// UNSUPPORTED: msvc-19.16 | ||
|
||
// template<class T> | ||
// class empty_view; | ||
|
||
#include <cuda/std/cassert> | ||
#include <cuda/std/ranges> | ||
|
||
#include "test_macros.h" | ||
|
||
template <class T> | ||
__host__ __device__ constexpr void testType() | ||
{ | ||
static_assert(cuda::std::ranges::range<cuda::std::ranges::empty_view<T>>); | ||
static_assert(cuda::std::ranges::range<const cuda::std::ranges::empty_view<T>>); | ||
static_assert(cuda::std::ranges::view<cuda::std::ranges::empty_view<T>>); | ||
|
||
cuda::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(cuda::std::ranges::begin(empty) == nullptr); | ||
assert(cuda::std::ranges::end(empty) == nullptr); | ||
assert(cuda::std::ranges::data(empty) == nullptr); | ||
assert(cuda::std::ranges::size(empty) == 0); | ||
assert(cuda::std::ranges::empty(empty) == true); | ||
} | ||
|
||
struct Empty | ||
{}; | ||
struct BigType | ||
{ | ||
char buff[8]; | ||
}; | ||
|
||
#if TEST_STD_VER >= 2020 | ||
template <class T> | ||
concept ValidEmptyView = requires { typename cuda::std::ranges::empty_view<T>; }; | ||
#else // ^^^ C++20 ^^^ / vvv C++17 vvv | ||
template <class T, class = void> | ||
constexpr bool ValidEmptyView = false; | ||
|
||
template <class T> | ||
constexpr bool ValidEmptyView<T, cuda::std::void_t<cuda::std::ranges::empty_view<T>>> = true; | ||
#endif // TEST_STD_VER <= 2017 | ||
|
||
__host__ __device__ 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; | ||
} |
56 changes: 56 additions & 0 deletions
56
libcudacxx/test/libcudacxx/std/ranges/range.adaptors/range.empty/views.empty.pass.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// UNSUPPORTED: c++03, c++11, c++14 | ||
// UNSUPPORTED: msvc-19.16 | ||
|
||
// template <class _Tp> | ||
// inline constexpr empty_view<_Tp> empty{}; | ||
|
||
#include <cuda/std/cassert> | ||
#include <cuda/std/ranges> | ||
|
||
#include "test_macros.h" | ||
|
||
template <class T> | ||
__host__ __device__ constexpr void testType() | ||
{ | ||
ASSERT_SAME_TYPE(decltype(cuda::std::views::empty<T>), const cuda::std::ranges::empty_view<T>); | ||
ASSERT_SAME_TYPE(decltype((cuda::std::views::empty<T>) ), const cuda::std::ranges::empty_view<T>&); | ||
|
||
auto v = cuda::std::views::empty<T>; | ||
assert(cuda::std::ranges::empty(v)); | ||
} | ||
|
||
struct Empty | ||
{}; | ||
struct BigType | ||
{ | ||
char buff[8]; | ||
}; | ||
|
||
__host__ __device__ constexpr bool test() | ||
{ | ||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters