Added type_list_has_duplicates_of and type_list_count_of

This commit is contained in:
John Wellbelove 2025-10-13 23:02:45 +01:00
parent c7d2682659
commit 4b8ab3eeda
2 changed files with 100 additions and 0 deletions

View File

@ -205,6 +205,52 @@ namespace etl
inline constexpr bool type_list_contains_v = etl::type_list_contains<TTypeList, T>::value;
#endif
//***************************************************************************
/// Defines a bool constant that is true if the type_list has duplicates of the specified type, otherwise false.
//***************************************************************************
template <typename T, typename TTypes>
struct type_list_has_duplicates_of;
template <typename T, typename... TTypes>
struct type_list_has_duplicates_of<etl::type_list<TTypes...>, T>
: public etl::has_duplicates_of<T, TTypes...>
{
};
template <typename T>
struct type_list_has_duplicates_of<type_list<>, T>
: public etl::integral_constant<bool, false>
{
};
#if ETL_USING_CPP17
template <typename TTypeList, typename T>
inline constexpr bool type_list_has_duplicates_of_v = etl::type_list_has_duplicates_of<TTypeList, T>::value;
#endif
//***************************************************************************
/// Defines an integral constant that is the count of the number of times a type is in the type list.
//***************************************************************************
template <typename T, typename TTypes>
struct type_list_count_of;
template <typename T, typename... TTypes>
struct type_list_count_of<etl::type_list<TTypes...>, T>
: public etl::count_of<T, TTypes...>
{
};
template <typename T>
struct type_list_count_of<type_list<>, T>
: public etl::integral_constant<size_t, 0>
{
};
#if ETL_USING_CPP17
template <typename TTypeList, typename T>
inline constexpr size_t type_list_count_of_v = etl::type_list_count_of<TTypeList, T>::value;
#endif
//***************************************************************************
/// Defines an integral constant that is maximum sizeof all types in the type_list.
/// If the type_list is empty, then defined as 0.

View File

@ -105,6 +105,60 @@ namespace
#endif
}
//*************************************************************************
TEST(test_type_list_has_duplicates_of)
{
typedef etl::type_list<char, int, uint32_t> t1;
typedef etl::type_list<uint8_t, uint16_t, uint8_t> t2;
typedef etl::type_list<uint16_t> t3;
typedef etl::type_list<> t4;
CHECK_FALSE((etl::type_list_has_duplicates_of<t1, char>::value));
CHECK_FALSE((etl::type_list_has_duplicates_of<t1, uint8_t>::value));
CHECK_FALSE((etl::type_list_has_duplicates_of<t2, int>::value));
CHECK_TRUE((etl::type_list_has_duplicates_of<t2, uint8_t>::value));
CHECK_FALSE((etl::type_list_has_duplicates_of<t3, uint16_t>::value));
CHECK_FALSE((etl::type_list_has_duplicates_of<t3, uint32_t>::value));
CHECK_FALSE((etl::type_list_has_duplicates_of<t4, uint32_t>::value));
#if ETL_USING_CPP17
CHECK_FALSE((etl::type_list_has_duplicates_of_v<t1, char>));
CHECK_FALSE((etl::type_list_has_duplicates_of_v<t1, uint8_t>));
CHECK_FALSE((etl::type_list_has_duplicates_of_v<t2, int>));
CHECK_TRUE((etl::type_list_has_duplicates_of_v<t2, uint8_t>));
CHECK_FALSE((etl::type_list_has_duplicates_of_v<t3, uint16_t>));
CHECK_FALSE((etl::type_list_has_duplicates_of_v<t3, uint32_t>));
CHECK_FALSE((etl::type_list_has_duplicates_of_v<t4, uint32_t>));
#endif
}
//*************************************************************************
TEST(test_type_list_count_of)
{
typedef etl::type_list<char, int, uint32_t> t1;
typedef etl::type_list<uint8_t, uint16_t, uint8_t> t2;
typedef etl::type_list<uint16_t> t3;
typedef etl::type_list<> t4;
CHECK_EQUAL(1, (etl::type_list_count_of<t1, char>::value));
CHECK_EQUAL(0, (etl::type_list_count_of<t1, uint8_t>::value));
CHECK_EQUAL(0, (etl::type_list_count_of<t2, int>::value));
CHECK_EQUAL(2, (etl::type_list_count_of<t2, uint8_t>::value));
CHECK_EQUAL(1, (etl::type_list_count_of<t3, uint16_t>::value));
CHECK_EQUAL(0, (etl::type_list_count_of<t3, uint32_t>::value));
CHECK_EQUAL(0, (etl::type_list_count_of<t4, uint32_t>::value));
#if ETL_USING_CPP17
CHECK_EQUAL(1, (etl::type_list_count_of_v<t1, char>));
CHECK_EQUAL(0, (etl::type_list_count_of_v<t1, uint8_t>));
CHECK_EQUAL(0, (etl::type_list_count_of_v<t2, int>));
CHECK_EQUAL(2, (etl::type_list_count_of_v<t2, uint8_t>));
CHECK_EQUAL(1, (etl::type_list_count_of_v<t3, uint16_t>));
CHECK_EQUAL(0, (etl::type_list_count_of_v<t3, uint32_t>));
CHECK_EQUAL(0, (etl::type_list_count_of_v<t4, uint32_t>));
#endif
}
//*************************************************************************
TEST(test_type_list_index_of_type)
{