Skip to content

Commit

Permalink
Added etl::type_list_type_at_index
Browse files Browse the repository at this point in the history
  • Loading branch information
John Wellbelove committed Mar 9, 2025
1 parent d4813df commit f1d5b16
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 9 deletions.
46 changes: 38 additions & 8 deletions include/etl/type_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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...>;
};

}

//***************************************************************************
Expand Down Expand Up @@ -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>
Expand All @@ -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>
{
};

Expand All @@ -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)>
Expand All @@ -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)>
{
};

Expand Down
14 changes: 14 additions & 0 deletions test/test_type_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
1 change: 1 addition & 0 deletions test/vs2022/etl.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
5 changes: 4 additions & 1 deletion test/vs2022/etl.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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">
Expand Down

0 comments on commit f1d5b16

Please sign in to comment.