From d2a304ae87bd89bb0d3c99d0d04dff976b7d7f98 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Fri, 23 Dec 2022 09:18:15 -0500 Subject: [PATCH] AK: Specialize TypeList for Variant types This allows callers to use the following semantics: using MyVariant = Variant; template size_t size() { return TypeList::size; } auto s = size(); This will be needed for an upcoming IPC change, which will result in us knowing the Variant type, but not the underlying variadic types that the Variant holds. --- AK/Variant.h | 5 ++++- Tests/AK/TestVariant.cpp | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/AK/Variant.h b/AK/Variant.h index ee423927a0..ed477b7bd4 100644 --- a/AK/Variant.h +++ b/AK/Variant.h @@ -226,7 +226,7 @@ template struct Variant : public Detail::MergeAndDeduplicatePacks>...> { public: - using IndexType = Conditional; // Note: size+1 reserved for internal value checks + using IndexType = Conditional<(sizeof...(Ts) < 255), u8, size_t>; // Note: size+1 reserved for internal value checks private: static constexpr IndexType invalid_index = sizeof...(Ts); @@ -518,6 +518,9 @@ private: IndexType m_index; }; +template +struct TypeList> : TypeList { }; + } #if USING_AK_GLOBALLY diff --git a/Tests/AK/TestVariant.cpp b/Tests/AK/TestVariant.cpp index 37bbf646cd..88a3e5fb7d 100644 --- a/Tests/AK/TestVariant.cpp +++ b/Tests/AK/TestVariant.cpp @@ -260,3 +260,16 @@ TEST_CASE(default_empty) EXPECT(my_variant.has()); EXPECT(!my_variant.has()); } + +TEST_CASE(type_list_specialization) +{ + EXPECT_EQ((TypeList>::size), 1u); + EXPECT_EQ((TypeList>::size), 2u); + EXPECT_EQ((TypeList>::size), 3u); + + using MyVariant = Variant; + using MyList = TypeList; + EXPECT((IsSame, Empty>)); + EXPECT((IsSame, int>)); + EXPECT((IsSame, String>)); +}