Added etl::parameter_pack

This commit is contained in:
John Wellbelove 2020-05-23 16:19:39 +01:00
parent 8c22c5cbc3
commit c68ed3dea1
11 changed files with 205 additions and 75 deletions

1
.gitignore vendored
View File

@ -255,3 +255,4 @@ unittest-cpp
build-test-Desktop_x86_windows_msvc2017_pe_32bit-Debug
test/Logs
build-test-Desktop_x86_windows_msvc2019_pe_32bit-Debug
test/vs2019/.vs

View File

@ -1555,42 +1555,6 @@ namespace etl
#if ETL_CPP17_SUPPORTED
template <typename T>
inline constexpr size_t size_of_v = etl::size_of<T>::value;
#endif
//***************************************************************************
/// index_of
//***************************************************************************
#if ETL_CPP11_SUPPORTED
template <typename T, typename... TTypes>
class index_of
{
private:
//***********************************
template <typename Type, typename T1, typename... TRest>
struct index_of_helper
{
static constexpr size_t value = etl::is_same<Type, T1>::value ? 1 : 1 + index_of_helper<Type, TRest...>::value;
};
//***********************************
template <typename Type, typename T1>
struct index_of_helper<Type, T1>
{
static constexpr size_t value = 1;
};
public:
static_assert(etl::is_one_of<T, TTypes...>::value, "T is not in typelist");
static constexpr size_t value = index_of_helper<T, TTypes...>::value - 1;
};
#if ETL_CPP17_SUPPORTED
template <typename T, typename... TTypes>
inline constexpr size_t index_of_v = index_of<T, TTypes...>::value;
#endif
#endif
}

View File

@ -0,0 +1,124 @@
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2017 jwellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#ifndef ETL_PARAMETER_PACK
#define ETL_PARAMETER_PACK
#include <stdint.h>
#include "platform.h"
#include "type_traits.h"
#if ETL_CPP11_NOT_SUPPORTED
#error NOT SUPPORTED FOR C++03 OR BELOW
#else
namespace etl
{
//***************************************************************************
/// parameter_pack
//***************************************************************************
template <typename... TTypes>
class parameter_pack
{
public:
static constexpr size_t size = sizeof...(TTypes);
//***************************************************************************
/// index_of_type
//***************************************************************************
template <typename T>
class index_of_type
{
private:
//***********************************
template <typename Type, typename T1, typename... TRest>
struct index_of_type_helper
{
static constexpr size_t value = std::is_same<Type, T1>::value ? 1 : 1 + index_of_type_helper<Type, TRest...>::value;
};
//***********************************
template <typename Type, typename T1>
struct index_of_type_helper<Type, T1>
{
static constexpr size_t value = 1;
};
public:
static_assert(etl::is_one_of<T, TTypes...>::value, "T is not in parameter pack");
/// The idex value.
static constexpr size_t value = index_of_type_helper<T, TTypes...>::value - 1;
};
#if ETL_CPP17_SUPPORTED
template <typename T>
static constexpr size_t index_of_type_v = index_of_type<T>::value;
#endif
//***************************************************************************
/// type_from_index
//***************************************************************************
template <size_t I>
class type_from_index
{
private:
//***********************************
template <size_t I, size_t N, typename T1, typename... TRest>
struct type_from_index_helper
{
using type = typename std::conditional<I == N, T1, typename type_from_index_helper<I, N + 1, TRest...>::type>::type;
};
//***********************************
template <size_t I, size_t N, typename T1>
struct type_from_index_helper<I, N, T1>
{
using type = T1;
};
public:
static_assert(I < sizeof...(TTypes), "Index out of bounds of parameter pack");
/// Template alias
using type = typename type_from_index_helper<I, 0, TTypes...>::type;
};
//***********************************
template <size_t I>
using type_from_index_t = typename type_from_index<I>::type;
};
}
#endif
#endif

View File

@ -1548,42 +1548,6 @@ namespace etl
#if ETL_CPP17_SUPPORTED
template <typename T>
inline constexpr size_t size_of_v = etl::size_of<T>::value;
#endif
//***************************************************************************
/// index_of
//***************************************************************************
#if ETL_CPP11_SUPPORTED
template <typename T, typename... TTypes>
class index_of
{
private:
//***********************************
template <typename Type, typename T1, typename... TRest>
struct index_of_helper
{
static constexpr size_t value = etl::is_same<Type, T1>::value ? 1 : 1 + index_of_helper<Type, TRest...>::value;
};
//***********************************
template <typename Type, typename T1>
struct index_of_helper<Type, T1>
{
static constexpr size_t value = 1;
};
public:
static_assert(etl::is_one_of<T, TTypes...>::value, "T is not in typelist");
static constexpr size_t value = index_of_helper<T, TTypes...>::value - 1;
};
#if ETL_CPP17_SUPPORTED
template <typename T, typename... TTypes>
inline constexpr size_t index_of_v = index_of<T, TTypes...>::value;
#endif
#endif
}

View File

@ -38,7 +38,7 @@ SOFTWARE.
///\ingroup utilities
#define ETL_VERSION_MAJOR 18
#define ETL_VERSION_MINOR 2
#define ETL_VERSION_MINOR 3
#define ETL_VERSION_PATCH 0
#define ETL_VERSION ETL_STRINGIFY(ETL_VERSION_MAJOR) "." ETL_STRINGIFY(ETL_VERSION_MINOR) "." ETL_STRINGIFY(ETL_VERSION_PATCH)
#define ETL_VERSION_W ETL_STRINGIFY(ETL_VERSION_MAJOR) L"." ETL_STRINGIFY(ETL_VERSION_MINOR) L"." ETL_STRINGIFY(ETL_VERSION_PATCH)

View File

@ -1,6 +1,6 @@
{
"name": "Embedded Template Library",
"version": "18.2.0",
"version": "18.3.0",
"authors": {
"name": "John Wellbelove",
"email": "john.wellbelove@etlcpp.com"

View File

@ -1,5 +1,5 @@
name=Embedded Template Library
version=18.2.0
version=18.3.0
author= John Wellbelove <john.wellbelove@etlcpp.com>
maintainer=John Wellbelove <john.wellbelove@etlcpp.com>
license=MIT

View File

@ -1,3 +1,8 @@
===============================================================================
18.3.0
Added etl::parameter_pack to easily extract information about the types in a
template parameter pack.
===============================================================================
18.2.0
Variadic versions of etl::type_id_lookup and etl::type_type_lookup for C++11 and above.

View File

@ -0,0 +1,64 @@
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2020 jwellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#include "UnitTest++/UnitTest++.h"
#include "ExtraCheckMacros.h"
#include "etl/parameter_pack.h"
#include <type_traits>
namespace
{
using Pack = etl::parameter_pack<char, short, int>;
SUITE(test_type_lookup)
{
//*************************************************************************
TEST(test_index_of_type)
{
CHECK_EQUAL(0U, Pack::index_of_type_v<char>);
CHECK_EQUAL(1U, Pack::index_of_type_v<short>);
CHECK_EQUAL(2U, Pack::index_of_type_v<int>);
// Static assert
//CHECK_EQUAL(0U, Pack::index_of_type_v<long>);
}
//*************************************************************************
TEST(test_type_from_index)
{
CHECK((std::is_same_v<char, typename Pack::type_from_index_t<0U>>));
CHECK((std::is_same_v<short, typename Pack::type_from_index_t<1U>>));
CHECK((std::is_same_v<int, typename Pack::type_from_index_t<2U>>));
// Static assert
//CHECK((std::is_same_v<long, typename Pack::type_from_index_t<3U>>));
}
};
}

View File

@ -1054,6 +1054,7 @@
<ClInclude Include="..\..\include\etl\mutex\mutex_freertos.h" />
<ClInclude Include="..\..\include\etl\negative.h" />
<ClInclude Include="..\..\include\etl\null_type.h" />
<ClInclude Include="..\..\include\etl\parameter_pack.h" />
<ClInclude Include="..\..\include\etl\private\choose_namespace.h" />
<ClInclude Include="..\..\include\etl\private\crc16_poly_0x1021_.h" />
<ClInclude Include="..\..\include\etl\private\crc16_poly_0x8005.h" />
@ -1582,6 +1583,7 @@
</ClCompile>
<ClCompile Include="..\test_optional.cpp" />
<ClCompile Include="..\test_packet.cpp" />
<ClCompile Include="..\test_parameter_pack.cpp" />
<ClCompile Include="..\test_parameter_type.cpp" />
<ClCompile Include="..\test_parity_checksum.cpp" />
<ClCompile Include="..\test_pearson.cpp" />

View File

@ -852,6 +852,9 @@
<ClInclude Include="..\..\include\etl\generators\variant_pool_generator.h">
<Filter>ETL\Containers\Generators</Filter>
</ClInclude>
<ClInclude Include="..\..\include\etl\parameter_pack.h">
<Filter>ETL\Utilities</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\main.cpp">
@ -1325,6 +1328,9 @@
<ClCompile Include="..\test_string_u32_external_buffer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\test_parameter_pack.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="..\..\library.properties">