From 6291454658cab950ab0839d75c9e79f99e2603db Mon Sep 17 00:00:00 2001 From: Alex Brachet Date: Mon, 14 Feb 2022 18:13:00 +0000 Subject: [PATCH] [libc] Create cpp::IntegerSequence analogous to std::integer_sequence Reviewed By: sivachandra, lntue Differential Revision: https://reviews.llvm.org/D119511 --- libc/src/__support/CPP/CMakeLists.txt | 1 + libc/src/__support/CPP/Utility.h | 39 +++++++++++++++++++ libc/test/src/__support/CPP/CMakeLists.txt | 10 +++++ .../__support/CPP/integer_sequence_test.cpp | 37 ++++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 libc/src/__support/CPP/Utility.h create mode 100644 libc/test/src/__support/CPP/integer_sequence_test.cpp diff --git a/libc/src/__support/CPP/CMakeLists.txt b/libc/src/__support/CPP/CMakeLists.txt index b591734a6b6f..67499d52fab9 100644 --- a/libc/src/__support/CPP/CMakeLists.txt +++ b/libc/src/__support/CPP/CMakeLists.txt @@ -10,6 +10,7 @@ add_header_library( Limits.h StringView.h TypeTraits.h + Utility.h ) add_header_library( diff --git a/libc/src/__support/CPP/Utility.h b/libc/src/__support/CPP/Utility.h new file mode 100644 index 000000000000..8a997611c8fc --- /dev/null +++ b/libc/src/__support/CPP/Utility.h @@ -0,0 +1,39 @@ +//===-- Analogous to ----------------------------------*- 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 LLVM_LIBC_SRC_SUPPORT_CPP_UTILITY_H +#define LLVM_LIBC_SRC_SUPPORT_CPP_UTILITY_H + +#include "src/__support/CPP/TypeTraits.h" + +namespace __llvm_libc::cpp { + +template struct IntegerSequence { + static_assert(IsIntegral::Value); + template using append = IntegerSequence; +}; + +namespace internal { + +template struct MakeIntegerSequence { + using type = typename MakeIntegerSequence::type::template append; +}; + +template struct MakeIntegerSequence { + using type = IntegerSequence; +}; + +} // namespace internal + +template +using MakeIntegerSequence = + typename internal::MakeIntegerSequence::type; + +} // namespace __llvm_libc::cpp + +#endif // LLVM_LIBC_SRC_SUPPORT_CPP_UTILITY_H diff --git a/libc/test/src/__support/CPP/CMakeLists.txt b/libc/test/src/__support/CPP/CMakeLists.txt index 7d4f15b7af5c..d8a84d99c08b 100644 --- a/libc/test/src/__support/CPP/CMakeLists.txt +++ b/libc/test/src/__support/CPP/CMakeLists.txt @@ -49,3 +49,13 @@ add_libc_unittest( DEPENDS libc.src.__support.CPP.vector ) + +add_libc_unittest( + int_seq_test + SUITE + libc_cpp_utils_unittests + SRCS + integer_sequence_test.cpp + DEPENDS + libc.src.__support.CPP.standalone_cpp +) diff --git a/libc/test/src/__support/CPP/integer_sequence_test.cpp b/libc/test/src/__support/CPP/integer_sequence_test.cpp new file mode 100644 index 000000000000..c2c0a9c00ef2 --- /dev/null +++ b/libc/test/src/__support/CPP/integer_sequence_test.cpp @@ -0,0 +1,37 @@ +//===-- Unittests for IntegerSequence -------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "src/__support/CPP/Utility.h" +#include "utils/UnitTest/Test.h" + +using namespace __llvm_libc::cpp; + +TEST(LlvmLibcIntegerSequencetTest, Basic) { + EXPECT_TRUE((IsSameV, MakeIntegerSequence>)); + using ISeq = IntegerSequence; + EXPECT_TRUE((IsSameV>)); + using LSeq = IntegerSequence; + EXPECT_TRUE((IsSameV>)); + using ULLSeq = IntegerSequence; + EXPECT_TRUE((IsSameV>)); +} + +template +bool checkArray(IntegerSequence seq) { + T arr[sizeof...(Ts)]{Ts...}; + + for (T i = 0; i < static_cast(sizeof...(Ts)); i++) + if (arr[i] != i) + return false; + + return true; +} + +TEST(LlvmLibcIntegerSequencetTest, Many) { + EXPECT_TRUE(checkArray(MakeIntegerSequence{})); +}