diff --git a/include/etl/type_list.h b/include/etl/type_list.h index 556787abe..f4607dc95 100644 --- a/include/etl/type_list.h +++ b/include/etl/type_list.h @@ -41,8 +41,10 @@ SOFTWARE. #if ETL_USING_CPP11 namespace etl { - - static ETL_CONSTEXPR size_t type_list_npos = etl::integral_limits::max; + //*************************************************************************** + /// Defines a no-position constant. + //*************************************************************************** + static ETL_CONSTANT size_t type_list_npos = etl::integral_limits::max; //*************************************************************************** /// Type list forward declaration. @@ -69,14 +71,12 @@ namespace etl namespace private_type_list { - // helper to solve the issue that recursed-rest can't be put directly in type_list::tail definition template struct recursion_helper { using type = type_list; }; - } //*************************************************************************** @@ -201,6 +201,9 @@ namespace etl using type = typename type_list_cat, TTail...>::type; }; + //*************************************************************************** + /// Defines a bool constant that is true if the type_list contains the specified type, otherwise false. + //*************************************************************************** template struct type_list_contains : public etl::integral_constant::value ? true : type_list_contains::value> @@ -218,17 +221,21 @@ namespace etl inline constexpr bool type_list_contains_v = etl::type_list_contains::value; #endif + //*************************************************************************** + /// Defines an integral constant that is the index of the specified type in the type_list. + /// If the type is not in the type_list, then defined as etl::type_list_npos. + //*************************************************************************** template struct type_list_index_of : public etl::integral_constant::value ? 0 : - (type_list_index_of::value == type_list_npos ? - type_list_npos : type_list_index_of::value + 1)> + (type_list_index_of::value == etl::type_list_npos ? etl::type_list_npos : + type_list_index_of::value + 1)> { }; template struct type_list_index_of, T> - : public etl::integral_constant + : public etl::integral_constant { }; @@ -237,6 +244,25 @@ namespace etl inline constexpr size_t type_list_index_of_v = etl::type_list_index_of::value; #endif + //*************************************************************************** + /// Defines type as the type found at Index in the type_list. + /// Static asserts if Index is out of range. + //*************************************************************************** + template + struct type_list_type_at_index + { + ETL_STATIC_ASSERT(Index <= type_list_size::value, "etl::type_list_type_at_index out of range"); + + using type = nth_type_t; + }; + + template + using type_list_type_at_index_t = typename type_list_type_at_index::type; + + //*************************************************************************** + /// Defines an integral constant that is maximum sizeof all types in the type_list. + /// If the type_list is empty, then defined as 0. + //*************************************************************************** template struct type_list_max_sizeof_type : public etl::integral_constant::value)> @@ -254,10 +280,14 @@ namespace etl inline constexpr size_t type_list_max_sizeof_type_v = etl::type_list_max_sizeof_type::value; #endif + //*************************************************************************** + /// Defines an integral constant that is maximum alignment all types in the type_list. + /// If the type_list is empty, then defined as 1. + //*************************************************************************** template struct type_list_max_alignment : public etl::integral_constant::value, - type_list_max_alignment::value)> + type_list_max_alignment::value)> { }; diff --git a/test/test_type_list.cpp b/test/test_type_list.cpp index 44f197f8f..49641e6f7 100644 --- a/test/test_type_list.cpp +++ b/test/test_type_list.cpp @@ -125,6 +125,20 @@ namespace #endif } + //************************************************************************* + TEST(test_type_list_type_at_index) + { + typedef etl::type_list t1; + + CHECK_TRUE((std::is_same::type>::value)); + CHECK_TRUE((std::is_same::type>::value)); + CHECK_TRUE((std::is_same::type>::value)); + + CHECK_TRUE((std::is_same>::value)); + CHECK_TRUE((std::is_same>::value)); + CHECK_TRUE((std::is_same>::value)); + } + //************************************************************************* TEST(test_type_list_max_sizeof_type) { diff --git a/test/vs2022/etl.vcxproj b/test/vs2022/etl.vcxproj index dfa03400a..b796f072d 100644 --- a/test/vs2022/etl.vcxproj +++ b/test/vs2022/etl.vcxproj @@ -9453,6 +9453,7 @@ + diff --git a/test/vs2022/etl.vcxproj.filters b/test/vs2022/etl.vcxproj.filters index 5251ef8b5..d5ff0eef6 100644 --- a/test/vs2022/etl.vcxproj.filters +++ b/test/vs2022/etl.vcxproj.filters @@ -1429,7 +1429,7 @@ UnitTest++\Header Files - UnitTest++\Header Files + ETL\Utilities @@ -3431,6 +3431,9 @@ Tests\Syntax Checks\Source + + Tests\Types +