diff --git a/include/etl/type_list.h b/include/etl/type_list.h index 21ee103c..4dbc66ec 100644 --- a/include/etl/type_list.h +++ b/include/etl/type_list.h @@ -32,11 +32,12 @@ SOFTWARE. #include "platform.h" #include "algorithm.h" -#include "nth_type.h" +#include "index_of_type.h" #include "integral_limits.h" #include "static_assert.h" #include "type_traits.h" #include "utility.h" +#include "largest.h" #if ETL_USING_CPP11 namespace etl @@ -85,7 +86,7 @@ namespace etl template struct type_list : type_list { - using type = THead; + using head = THead; using tail = typename private_type_list::recursion_helper::type; static constexpr size_t size = sizeof...(TTail) + 1U; @@ -105,7 +106,7 @@ namespace etl template struct type_list : type_list<> { - using type = THead; + using head = THead; using tail = typename private_type_list::recursion_helper<>::type; static constexpr size_t size = 1U; @@ -119,50 +120,10 @@ namespace etl type_list& operator =(const type_list&) ETL_DELETE; }; - //*************************************************************************** - /// Specialisation of etl::nth_type for etl::type_list - //*************************************************************************** - template - struct nth_type> - { - ETL_STATIC_ASSERT(N <= sizeof...(TTail), "etl::nth_type out of range for etl::type_list"); - - using type = typename nth_type>::type; - }; - - //*************************************************************************** - /// Specialisation of etl::nth_type for etl::type_list with index of 0 - //*************************************************************************** - template - struct nth_type<0, type_list> - { - using type = THead; - }; - - //*************************************************************************** - /// Specialisation of etl::nth_type for empty etl::type_list - //*************************************************************************** - template - struct nth_type> - { - }; - - //*************************************************************************** - /// Declares a new type_list by selecting types from a given type_list, according to an index sequence. - //*************************************************************************** - template - struct type_list_select - { - using type = type_list...>; - }; - - template - using type_list_select_t = typename type_list_select::type; - //*************************************************************************** /// Type list size. //*************************************************************************** - template + template struct type_list_size; template @@ -176,37 +137,60 @@ namespace etl #endif //*************************************************************************** - /// Concatenates two or more type_lists. + /// Defines type as the type found at Index in the type_list. + /// Static asserts if Index is out of range. //*************************************************************************** - template - struct type_list_cat; - - //*************************************************************************** - /// Concatenates two or more type_lists. - /// Specialisation for a single type_list (base case) - //*************************************************************************** - template - struct type_list_cat + template + struct type_list_type_at_index { - using type = TypeList; + ETL_STATIC_ASSERT(Index < type_list_size::value, "etl::type_list_type_at_index out of range"); + ETL_STATIC_ASSERT((etl::is_base_of, TTypeList>::value), "TTypeList must be an etl::type_list"); + + using type = typename type_list_type_at_index::type; }; - //*************************************************************************** - /// Concatenates two or more type_lists. - /// Specialisation for two or more type_lists - //*************************************************************************** - template - struct type_list_cat, etl::type_list, TTail...> + template + struct type_list_type_at_index { - using type = typename type_list_cat, TTail...>::type; + using type = typename TTypeList::head; }; + template + using type_list_type_at_index_t = typename type_list_type_at_index::type; + + //*************************************************************************** + /// 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_type + : public etl::integral_constant::value ? 0 : + (type_list_index_of_type::value == etl::type_list_npos ? etl::type_list_npos : + type_list_index_of_type::value + 1)> + { + ETL_STATIC_ASSERT((etl::is_base_of, TTypeList>::value), "TTypeList must be an etl::type_list"); + }; + + template + struct type_list_index_of_type, T> + : public etl::integral_constant + { + }; + +#if ETL_USING_CPP17 + template + inline constexpr size_t type_list_index_of_v = etl::type_list_index_of_type::value; +#endif + //*************************************************************************** /// 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> + template + struct type_list_contains; + + template + struct type_list_contains, T> + : public etl::integral_constant::value> { }; @@ -217,77 +201,44 @@ namespace etl }; #if ETL_USING_CPP17 - template - inline constexpr bool type_list_contains_v = etl::type_list_contains::value; + template + 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_type - : public etl::integral_constant::value ? 0 : - (type_list_index_of_type::value == etl::type_list_npos ? etl::type_list_npos : - type_list_index_of_type::value + 1)> - { - }; - - template - struct type_list_index_of_type, T> - : public etl::integral_constant - { - }; - -#if ETL_USING_CPP17 - template - inline constexpr size_t type_list_index_of_v = etl::type_list_index_of_type::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)> + template + struct type_list_max_size; + + template + struct type_list_max_size> + : public etl::integral_constant::size> { }; template <> - struct type_list_max_sizeof_type> + struct type_list_max_size> : public etl::integral_constant { }; #if ETL_USING_CPP17 - template - inline constexpr size_t type_list_max_sizeof_type_v = etl::type_list_max_sizeof_type::value; + template + inline constexpr size_t type_list_max_size_v = etl::type_list_max_size::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)> + template + struct type_list_max_alignment; + + template + struct type_list_max_alignment> + : public etl::integral_constant::alignment> { }; @@ -298,9 +249,44 @@ namespace etl }; #if ETL_USING_CPP17 - template - inline constexpr size_t type_list_max_alignment_v = etl::type_list_max_alignment::value; + template + inline constexpr size_t type_list_max_alignment_v = etl::type_list_max_alignment::value; #endif + + //*************************************************************************** + /// Declares a new type_list by selecting types from a given type_list, according to an index sequence. + //*************************************************************************** + template + struct type_list_select + { + ETL_STATIC_ASSERT((etl::is_base_of, TTypeList>::value), "TTypeList must be an etl::type_list"); + + using type = type_list...>; + }; + + template + using type_list_select_t = typename type_list_select::type; + + //*************************************************************************** + /// Concatenates two or more type_lists. + //*************************************************************************** + template + struct type_list_cat; + + template + struct type_list_cat, etl::type_list, TTail...> + { + using type = typename type_list_cat, TTail...>::type; + }; + + template + struct type_list_cat + { + using type = T; + }; + + template + using type_list_cat_t = typename type_list_cat::type; } #endif diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d2641ab5..498fa7db 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -149,6 +149,7 @@ add_executable(etl_tests test_hfsm.cpp test_hfsm_recurse_to_inner_state_on_start.cpp test_histogram.cpp + test_index_of_type.cpp test_indirect_vector.cpp test_indirect_vector_external_buffer.cpp test_instance_count.cpp diff --git a/test/test_type_list.cpp b/test/test_type_list.cpp index 75c8f322..de9be9fc 100644 --- a/test/test_type_list.cpp +++ b/test/test_type_list.cpp @@ -37,16 +37,6 @@ namespace #if ETL_USING_CPP11 SUITE(test_type_list) { - //************************************************************************* - TEST(test_nth_type) - { - typedef etl::type_list t1; - - CHECK_TRUE((std::is_same::type, char>::value)); - CHECK_TRUE((std::is_same::type, int>::value)); - CHECK_TRUE((std::is_same::type, uint32_t>::value)); - } - //************************************************************************* TEST(test_type_list_select) { @@ -54,6 +44,7 @@ namespace typedef etl::type_list t2; CHECK_TRUE((std::is_same::type, t2>::value)); + CHECK_TRUE((std::is_same, t2>::value)); } //************************************************************************* @@ -61,46 +52,54 @@ namespace { typedef etl::type_list t1; typedef etl::type_list t2; + typedef etl::type_list<> t3; CHECK_EQUAL(etl::type_list_size::value, 3); CHECK_EQUAL(etl::type_list_size::value, 2); + CHECK_EQUAL(etl::type_list_size::value, 0); } //************************************************************************* TEST(test_type_list_cat) { typedef etl::type_list t1; - typedef etl::type_list t2; + typedef etl::type_list t2; + typedef etl::type_list<> t3; typedef etl::type_list t_cat1; - typedef etl::type_list t_cat2; + typedef etl::type_list t_cat2; - CHECK_TRUE((std::is_same::type, t_cat1>::value)); - CHECK_FALSE((std::is_same::type, t_cat2>::value)); + CHECK_TRUE((std::is_same::type, t_cat1>::value)); + CHECK_TRUE((std::is_same::type, t_cat1>::value)); + CHECK_FALSE((std::is_same::type, t_cat2>::value)); + + CHECK_TRUE((std::is_same, t_cat1>::value)); + CHECK_TRUE((std::is_same, t_cat1>::value)); + CHECK_FALSE((std::is_same, t_cat2>::value)); } //************************************************************************* TEST(test_type_list_contains) { typedef etl::type_list t1; - typedef etl::type_list t2; + typedef etl::type_list t2; typedef etl::type_list t3; typedef etl::type_list<> t4; - CHECK_TRUE((etl::type_list_contains::value)); + CHECK_TRUE((etl::type_list_contains::value)); CHECK_FALSE((etl::type_list_contains::value)); CHECK_FALSE((etl::type_list_contains::value)); - CHECK_TRUE((etl::type_list_contains::value)); - CHECK_TRUE((etl::type_list_contains::value)); + CHECK_TRUE((etl::type_list_contains::value)); + CHECK_TRUE((etl::type_list_contains::value)); CHECK_FALSE((etl::type_list_contains::value)); CHECK_FALSE((etl::type_list_contains::value)); #if ETL_USING_CPP17 - CHECK_TRUE((etl::type_list_contains_v)); + CHECK_TRUE((etl::type_list_contains_v)); CHECK_FALSE((etl::type_list_contains_v)); CHECK_FALSE((etl::type_list_contains_v)); - CHECK_TRUE((etl::type_list_contains_v)); - CHECK_TRUE((etl::type_list_contains_v)); + CHECK_TRUE((etl::type_list_contains_v)); + CHECK_TRUE((etl::type_list_contains_v)); CHECK_FALSE((etl::type_list_contains_v)); CHECK_FALSE((etl::type_list_contains_v)); #endif @@ -147,16 +146,16 @@ namespace typedef etl::type_list t3; typedef etl::type_list<> t4; - CHECK_EQUAL(etl::type_list_max_sizeof_type::value, 4); - CHECK_EQUAL(etl::type_list_max_sizeof_type::value, 2); - CHECK_EQUAL(etl::type_list_max_sizeof_type::value, 4); - CHECK_EQUAL(etl::type_list_max_sizeof_type::value, 0); + CHECK_EQUAL(etl::type_list_max_size::value, 4); + CHECK_EQUAL(etl::type_list_max_size::value, 2); + CHECK_EQUAL(etl::type_list_max_size::value, 4); + CHECK_EQUAL(etl::type_list_max_size::value, 0); #if ETL_USING_CPP17 - CHECK_EQUAL((etl::type_list_max_sizeof_type_v), 4); - CHECK_EQUAL((etl::type_list_max_sizeof_type_v), 2); - CHECK_EQUAL((etl::type_list_max_sizeof_type_v), 4); - CHECK_EQUAL((etl::type_list_max_sizeof_type_v), 0); + CHECK_EQUAL((etl::type_list_max_size_v), 4); + CHECK_EQUAL((etl::type_list_max_size_v), 2); + CHECK_EQUAL((etl::type_list_max_size_v), 4); + CHECK_EQUAL((etl::type_list_max_size_v), 0); #endif } diff --git a/test/vs2022/etl.vcxproj b/test/vs2022/etl.vcxproj index b796f072..808b085a 100644 --- a/test/vs2022/etl.vcxproj +++ b/test/vs2022/etl.vcxproj @@ -3169,6 +3169,7 @@ + @@ -8477,6 +8478,7 @@ + diff --git a/test/vs2022/etl.vcxproj.filters b/test/vs2022/etl.vcxproj.filters index d5ff0eef..f778794a 100644 --- a/test/vs2022/etl.vcxproj.filters +++ b/test/vs2022/etl.vcxproj.filters @@ -1431,6 +1431,9 @@ ETL\Utilities + + ETL\Utilities + @@ -3434,6 +3437,9 @@ Tests\Types + + Tests\Types +