From 8fb468e708e67f457c079a88a1ab2a15b7d865ee Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Wed, 15 May 2019 22:17:53 +0100 Subject: [PATCH 1/2] Added size_of<> --- include/etl/type_traits.h | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/include/etl/type_traits.h b/include/etl/type_traits.h index ee01e614..f75fb0f1 100644 --- a/include/etl/type_traits.h +++ b/include/etl/type_traits.h @@ -335,7 +335,7 @@ namespace etl struct conditional_integral_constant { ETL_STATIC_ASSERT(etl::is_integral::value, "Not an integral type"); - static const T value = FALSE_VALUE; + static const T value = FALSE_VALUE; }; /// make_signed @@ -485,14 +485,14 @@ namespace etl ///\ingroup types //*************************************************************************** template struct is_one_of { - static const bool value = + static const bool value = etl::is_same::value || etl::is_same::value || etl::is_same::value || @@ -586,6 +586,21 @@ namespace etl typedef const type_t* const_pointer; typedef const type_t* const const_pointer_const; }; + + //*************************************************************************** + // size_of + //*************************************************************************** + template + struct size_of + { + static const size_t size = sizeof(T); + }; + + template <> + struct size_of + { + static const size_t size = 1; + }; } #endif From e3fa5b912c68ff5e90cbce214b8906cfcbbf77c5 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Wed, 15 May 2019 22:31:30 +0100 Subject: [PATCH 2/2] Added size_of<> --- include/etl/type_traits_generator.h | 15 +++++++++++++++ test/test_type_traits.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/include/etl/type_traits_generator.h b/include/etl/type_traits_generator.h index 83b982bd..a48a78c6 100644 --- a/include/etl/type_traits_generator.h +++ b/include/etl/type_traits_generator.h @@ -598,6 +598,21 @@ namespace etl typedef const type_t* const_pointer; typedef const type_t* const const_pointer_const; }; + + //*************************************************************************** + // size_of + //*************************************************************************** + template + struct size_of + { + static const size_t size = sizeof(T); + }; + + template <> + struct size_of + { + static const size_t size = 1; + }; } #endif diff --git a/test/test_type_traits.cpp b/test/test_type_traits.cpp index 4ef72f27..e1aba926 100644 --- a/test/test_type_traits.cpp +++ b/test/test_type_traits.cpp @@ -41,6 +41,20 @@ namespace std #include "etl/type_traits.h" #include +namespace +{ + struct TestData { }; +} + +namespace etl +{ + template <> + struct etl::size_of + { + static const size_t size = 20; + }; +} + namespace { // A class to test non-fundamental types. @@ -728,4 +742,14 @@ namespace CHECK_EQUAL(1, v1); CHECK_EQUAL(2, v2); } + + //************************************************************************* + TEST(size_of) + { + CHECK_EQUAL(1, etl::size_of::size); + CHECK_EQUAL(1, etl::size_of::size); + CHECK_EQUAL(2, etl::size_of::size); + CHECK_EQUAL(4, etl::size_of::size); + CHECK_EQUAL(20, etl::size_of::size); + } }