From c68ed3dea1209f9ad1eb5d1a6da0d6dc88b51b13 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sat, 23 May 2020 16:19:39 +0100 Subject: [PATCH 1/2] Added etl::parameter_pack --- .gitignore | 1 + .../etl/generators/type_traits_generator.h | 36 ----- include/etl/parameter_pack.h | 124 ++++++++++++++++++ include/etl/type_traits.h | 36 ----- include/etl/version.h | 2 +- library.json | 2 +- library.properties | 2 +- support/Release notes.txt | 5 + test/test_parameter_pack.cpp | 64 +++++++++ test/vs2019/etl.vcxproj | 2 + test/vs2019/etl.vcxproj.filters | 6 + 11 files changed, 205 insertions(+), 75 deletions(-) create mode 100644 include/etl/parameter_pack.h create mode 100644 test/test_parameter_pack.cpp diff --git a/.gitignore b/.gitignore index b5c756a6..4bb62e0c 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/include/etl/generators/type_traits_generator.h b/include/etl/generators/type_traits_generator.h index a7520b8b..81196e02 100644 --- a/include/etl/generators/type_traits_generator.h +++ b/include/etl/generators/type_traits_generator.h @@ -1555,42 +1555,6 @@ namespace etl #if ETL_CPP17_SUPPORTED template inline constexpr size_t size_of_v = etl::size_of::value; -#endif - - //*************************************************************************** - /// index_of - //*************************************************************************** -#if ETL_CPP11_SUPPORTED - template - class index_of - { - private: - - //*********************************** - template - struct index_of_helper - { - static constexpr size_t value = etl::is_same::value ? 1 : 1 + index_of_helper::value; - }; - - //*********************************** - template - struct index_of_helper - { - static constexpr size_t value = 1; - }; - - public: - - static_assert(etl::is_one_of::value, "T is not in typelist"); - - static constexpr size_t value = index_of_helper::value - 1; - }; - -#if ETL_CPP17_SUPPORTED - template - inline constexpr size_t index_of_v = index_of::value; -#endif #endif } diff --git a/include/etl/parameter_pack.h b/include/etl/parameter_pack.h new file mode 100644 index 00000000..ab155aaf --- /dev/null +++ b/include/etl/parameter_pack.h @@ -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 + +#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 + class parameter_pack + { + public: + + static constexpr size_t size = sizeof...(TTypes); + + //*************************************************************************** + /// index_of_type + //*************************************************************************** + template + class index_of_type + { + private: + + //*********************************** + template + struct index_of_type_helper + { + static constexpr size_t value = std::is_same::value ? 1 : 1 + index_of_type_helper::value; + }; + + //*********************************** + template + struct index_of_type_helper + { + static constexpr size_t value = 1; + }; + + public: + + static_assert(etl::is_one_of::value, "T is not in parameter pack"); + + /// The idex value. + static constexpr size_t value = index_of_type_helper::value - 1; + }; + +#if ETL_CPP17_SUPPORTED + template + static constexpr size_t index_of_type_v = index_of_type::value; +#endif + + //*************************************************************************** + /// type_from_index + //*************************************************************************** + template + class type_from_index + { + private: + + //*********************************** + template + struct type_from_index_helper + { + using type = typename std::conditional::type>::type; + }; + + //*********************************** + template + struct type_from_index_helper + { + 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::type; + }; + + //*********************************** + template + using type_from_index_t = typename type_from_index::type; + }; +} +#endif +#endif diff --git a/include/etl/type_traits.h b/include/etl/type_traits.h index bf83e968..b4475a5a 100644 --- a/include/etl/type_traits.h +++ b/include/etl/type_traits.h @@ -1548,42 +1548,6 @@ namespace etl #if ETL_CPP17_SUPPORTED template inline constexpr size_t size_of_v = etl::size_of::value; -#endif - - //*************************************************************************** - /// index_of - //*************************************************************************** -#if ETL_CPP11_SUPPORTED - template - class index_of - { - private: - - //*********************************** - template - struct index_of_helper - { - static constexpr size_t value = etl::is_same::value ? 1 : 1 + index_of_helper::value; - }; - - //*********************************** - template - struct index_of_helper - { - static constexpr size_t value = 1; - }; - - public: - - static_assert(etl::is_one_of::value, "T is not in typelist"); - - static constexpr size_t value = index_of_helper::value - 1; - }; - -#if ETL_CPP17_SUPPORTED - template - inline constexpr size_t index_of_v = index_of::value; -#endif #endif } diff --git a/include/etl/version.h b/include/etl/version.h index e52d6e29..129be118 100644 --- a/include/etl/version.h +++ b/include/etl/version.h @@ -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) diff --git a/library.json b/library.json index e6e741bb..3cc77423 100644 --- a/library.json +++ b/library.json @@ -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" diff --git a/library.properties b/library.properties index a2c2b42f..50f72707 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Embedded Template Library -version=18.2.0 +version=18.3.0 author= John Wellbelove maintainer=John Wellbelove license=MIT diff --git a/support/Release notes.txt b/support/Release notes.txt index 3db2bbef..8c07ddce 100644 --- a/support/Release notes.txt +++ b/support/Release notes.txt @@ -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. diff --git a/test/test_parameter_pack.cpp b/test/test_parameter_pack.cpp new file mode 100644 index 00000000..078a1cf7 --- /dev/null +++ b/test/test_parameter_pack.cpp @@ -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 + +namespace +{ + using Pack = etl::parameter_pack; + + SUITE(test_type_lookup) + { + //************************************************************************* + TEST(test_index_of_type) + { + CHECK_EQUAL(0U, Pack::index_of_type_v); + CHECK_EQUAL(1U, Pack::index_of_type_v); + CHECK_EQUAL(2U, Pack::index_of_type_v); + + // Static assert + //CHECK_EQUAL(0U, Pack::index_of_type_v); + } + + //************************************************************************* + TEST(test_type_from_index) + { + CHECK((std::is_same_v>)); + CHECK((std::is_same_v>)); + CHECK((std::is_same_v>)); + + // Static assert + //CHECK((std::is_same_v>)); + } + }; +} diff --git a/test/vs2019/etl.vcxproj b/test/vs2019/etl.vcxproj index c2ab7677..f980b3da 100644 --- a/test/vs2019/etl.vcxproj +++ b/test/vs2019/etl.vcxproj @@ -1054,6 +1054,7 @@ + @@ -1582,6 +1583,7 @@ + diff --git a/test/vs2019/etl.vcxproj.filters b/test/vs2019/etl.vcxproj.filters index aba45339..c727bd0d 100644 --- a/test/vs2019/etl.vcxproj.filters +++ b/test/vs2019/etl.vcxproj.filters @@ -852,6 +852,9 @@ ETL\Containers\Generators + + ETL\Utilities + @@ -1325,6 +1328,9 @@ Source Files + + Source Files + From fefbfacb6f12520cccf88af1ab114757820cfad8 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sat, 23 May 2020 16:19:39 +0100 Subject: [PATCH 2/2] Added etl::parameter_pack --- include/etl/parameter_pack.h | 8 ++++---- test/CMakeLists.txt | 1 + test/codeblocks/ETL.cbp | 2 ++ test/test_type_traits.cpp | 13 ++++++------- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/include/etl/parameter_pack.h b/include/etl/parameter_pack.h index ab155aaf..0fef91e0 100644 --- a/include/etl/parameter_pack.h +++ b/include/etl/parameter_pack.h @@ -94,15 +94,15 @@ namespace etl private: //*********************************** - template + template struct type_from_index_helper { - using type = typename std::conditional::type>::type; + using type = typename std::conditional::type>::type; }; //*********************************** - template - struct type_from_index_helper + template + struct type_from_index_helper { using type = T1; }; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 953aab02..2db57c8e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -137,6 +137,7 @@ set(TEST_SOURCE_FILES test_queue_spsc_isr_small.cpp test_queue_spsc_locked.cpp test_queue_spsc_locked_small.cpp + test_parameter_pack test_scaled_rounding.cpp test_state_chart.cpp test_string_view.cpp diff --git a/test/codeblocks/ETL.cbp b/test/codeblocks/ETL.cbp index d5046582..fd4a0259 100644 --- a/test/codeblocks/ETL.cbp +++ b/test/codeblocks/ETL.cbp @@ -349,6 +349,7 @@ + @@ -542,6 +543,7 @@ + diff --git a/test/test_type_traits.cpp b/test/test_type_traits.cpp index ecae96c2..28127d17 100644 --- a/test/test_type_traits.cpp +++ b/test/test_type_traits.cpp @@ -809,13 +809,12 @@ namespace } //************************************************************************* - TEST(index_of) + TEST(is_rvalue_reference) { - CHECK_EQUAL(0U, (etl::index_of::value)); - CHECK_EQUAL(1U, (etl::index_of::value)); - CHECK_EQUAL(2U, (etl::index_of::value)); - - // Static assert - //CHECK_EQUAL(0U, (etl::index_of::value)); + CHECK_EQUAL(std::is_rvalue_reference_v, etl::is_rvalue_reference_v); + CHECK_EQUAL(std::is_rvalue_reference_v, etl::is_rvalue_reference_v); + CHECK_EQUAL(std::is_rvalue_reference_v, etl::is_rvalue_reference_v); + CHECK_EQUAL(std::is_rvalue_reference_v, etl::is_rvalue_reference_v); + CHECK_EQUAL(std::is_rvalue_reference_v, etl::is_rvalue_reference_v); } }