Added etl::type_list_type_at_index

This commit is contained in:
John Wellbelove 2025-03-09 10:47:24 +00:00
parent d4813dff86
commit f1d5b16d38
4 changed files with 57 additions and 9 deletions

View File

@ -41,8 +41,10 @@ SOFTWARE.
#if ETL_USING_CPP11
namespace etl
{
static ETL_CONSTEXPR size_t type_list_npos = etl::integral_limits<size_t>::max;
//***************************************************************************
/// Defines a no-position constant.
//***************************************************************************
static ETL_CONSTANT size_t type_list_npos = etl::integral_limits<size_t>::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 <typename... TTypes>
struct recursion_helper
{
using type = type_list<TTypes...>;
};
}
//***************************************************************************
@ -201,6 +201,9 @@ namespace etl
using type = typename type_list_cat<etl::type_list<TTypes1..., TTypes2...>, TTail...>::type;
};
//***************************************************************************
/// Defines a bool constant that is true if the type_list contains the specified type, otherwise false.
//***************************************************************************
template<typename TypeList, typename T>
struct type_list_contains
: public etl::integral_constant<bool, etl::is_same<typename TypeList::type, T>::value ? true : type_list_contains<typename TypeList::tail, T>::value>
@ -218,17 +221,21 @@ namespace etl
inline constexpr bool type_list_contains_v = etl::type_list_contains<TypeList, T>::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<typename TypeList, typename T>
struct type_list_index_of
: public etl::integral_constant<size_t, etl::is_same<typename TypeList::type, T>::value ? 0 :
(type_list_index_of<typename TypeList::tail, T>::value == type_list_npos ?
type_list_npos : type_list_index_of<typename TypeList::tail, T>::value + 1)>
(type_list_index_of<typename TypeList::tail, T>::value == etl::type_list_npos ? etl::type_list_npos :
type_list_index_of<typename TypeList::tail, T>::value + 1)>
{
};
template<typename T>
struct type_list_index_of<type_list<>, T>
: public etl::integral_constant<size_t, type_list_npos>
: public etl::integral_constant<size_t, etl::type_list_npos>
{
};
@ -237,6 +244,25 @@ namespace etl
inline constexpr size_t type_list_index_of_v = etl::type_list_index_of<TypeList, T>::value;
#endif
//***************************************************************************
/// Defines type as the type found at Index in the type_list.
/// Static asserts if Index is out of range.
//***************************************************************************
template<typename TypeList, size_t Index>
struct type_list_type_at_index
{
ETL_STATIC_ASSERT(Index <= type_list_size<TypeList>::value, "etl::type_list_type_at_index out of range");
using type = nth_type_t<Index, TypeList>;
};
template<typename TypeList, size_t Index>
using type_list_type_at_index_t = typename type_list_type_at_index<TypeList, 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<typename TypeList>
struct type_list_max_sizeof_type
: public etl::integral_constant<size_t, etl::max(sizeof(typename TypeList::type), type_list_max_sizeof_type<typename TypeList::tail>::value)>
@ -254,10 +280,14 @@ namespace etl
inline constexpr size_t type_list_max_sizeof_type_v = etl::type_list_max_sizeof_type<TypeList>::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<typename TypeList>
struct type_list_max_alignment
: public etl::integral_constant<size_t, etl::max(etl::alignment_of<typename TypeList::type>::value,
type_list_max_alignment<typename TypeList::tail>::value)>
type_list_max_alignment<typename TypeList::tail>::value)>
{
};

View File

@ -125,6 +125,20 @@ namespace
#endif
}
//*************************************************************************
TEST(test_type_list_type_at_index)
{
typedef etl::type_list<char, int, uint32_t> t1;
CHECK_TRUE((std::is_same<char, etl::type_list_type_at_index<t1, 0>::type>::value));
CHECK_TRUE((std::is_same<int, etl::type_list_type_at_index<t1, 1>::type>::value));
CHECK_TRUE((std::is_same<uint32_t, etl::type_list_type_at_index<t1, 2>::type>::value));
CHECK_TRUE((std::is_same<char, etl::type_list_type_at_index_t<t1, 0>>::value));
CHECK_TRUE((std::is_same<int, etl::type_list_type_at_index_t<t1, 1>>::value));
CHECK_TRUE((std::is_same<uint32_t, etl::type_list_type_at_index_t<t1, 2>>::value));
}
//*************************************************************************
TEST(test_type_list_max_sizeof_type)
{

View File

@ -9453,6 +9453,7 @@
<ClCompile Include="..\test_to_u8string.cpp" />
<ClCompile Include="..\test_to_wstring.cpp" />
<ClCompile Include="..\test_type_def.cpp" />
<ClCompile Include="..\test_type_list.cpp" />
<ClCompile Include="..\test_type_lookup.cpp" />
<ClCompile Include="..\test_type_select.cpp" />
<ClCompile Include="..\test_type_traits.cpp" />

View File

@ -1429,7 +1429,7 @@
<Filter>UnitTest++\Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\include\etl\type_list.h">
<Filter>UnitTest++\Header Files</Filter>
<Filter>ETL\Utilities</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
@ -3431,6 +3431,9 @@
<ClCompile Include="..\syntax_check\singleton_base.h.t.cpp">
<Filter>Tests\Syntax Checks\Source</Filter>
</ClCompile>
<ClCompile Include="..\test_type_list.cpp">
<Filter>Tests\Types</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="..\..\library.properties">