From c01262b5b5ad227e216b7c28a3bbcd6611d406b7 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sun, 26 May 2019 13:14:10 +0100 Subject: [PATCH] Variadic template etl::smallest --- include/etl/largest.h | 10 ++++- include/etl/largest_generator.h | 10 ++++- include/etl/smallest_generator.h | 64 +++++++++++++++++++++++++---- include/etl/type_traits_generator.h | 2 +- 4 files changed, 73 insertions(+), 13 deletions(-) diff --git a/include/etl/largest.h b/include/etl/largest.h index a406c550..556db65b 100644 --- a/include/etl/largest.h +++ b/include/etl/largest.h @@ -71,11 +71,15 @@ namespace etl ///\ingroup largest //*************************************************************************** template - struct largest_type + class largest_type { + private: + // Define 'largest_other' as 'largest_type' with all but the first parameter. using largest_other = typename largest_type::type; + public: + // Set 'type' to be the largest of the first parameter and any of the others. // This is recursive. using type = typename etl::conditional<(etl::size_of() > etl::size_of()), // Boolean @@ -94,8 +98,10 @@ namespace etl // Specialisation for one template parameter. //*************************************************************************** template - struct largest_type + class largest_type { + public: + using type = T1; enum diff --git a/include/etl/largest_generator.h b/include/etl/largest_generator.h index dfeebdc0..0dd78c85 100644 --- a/include/etl/largest_generator.h +++ b/include/etl/largest_generator.h @@ -83,11 +83,15 @@ namespace etl ///\ingroup largest //*************************************************************************** template - struct largest_type + class largest_type { + private: + // Define 'largest_other' as 'largest_type' with all but the first parameter. using largest_other = typename largest_type::type; + public: + // Set 'type' to be the largest of the first parameter and any of the others. // This is recursive. using type = typename etl::conditional<(etl::size_of() > etl::size_of()), // Boolean @@ -106,8 +110,10 @@ namespace etl // Specialisation for one template parameter. //*************************************************************************** template - struct largest_type + class largest_type { + public: + using type = T1; enum diff --git a/include/etl/smallest_generator.h b/include/etl/smallest_generator.h index dcc955a2..3b5c6d2c 100644 --- a/include/etl/smallest_generator.h +++ b/include/etl/smallest_generator.h @@ -75,6 +75,53 @@ cog.outl("//******************************************************************** namespace etl { +#if ETL_CPP11_SUPPORTED && !defined(ETL_SMALLEST_TYPE_FORCE_CPP03) + //*************************************************************************** + /// Template to determine the largest type and size. + /// Defines 'value_type' which is the type of the largest parameter. + /// Defines 'size' which is the size of the largest parameter. + ///\ingroup largest + //*************************************************************************** + template + class smallest_type + { + private: + + // Define 'smallest_other' as 'smallest_type' with all but the first parameter. + using smallest_other = typename smallest_type::type; + + public: + + // Set 'type' to be the smallest of the first parameter and any of the others. + // This is recursive. + using type = typename etl::conditional<(etl::size_of() < etl::size_of()), // Boolean + T1, // TrueType + smallest_other> // FalseType + ::type; // The smallest type of the two. + + // The size of the smallest type. + enum + { + size = etl::size_of() + }; + }; + + //*************************************************************************** + // Specialisation for one template parameter. + //*************************************************************************** + template + class smallest_type + { + public: + + using type = T1; + + enum + { + size = etl::size_of() + }; + }; +#else /*[[[cog import cog cog.outl("//***************************************************************************") @@ -161,7 +208,8 @@ namespace etl cog.outl("};") ]]]*/ /*[[[end]]]*/ - +#endif + namespace private_smallest { //************************************************************************* @@ -169,7 +217,7 @@ namespace etl //************************************************************************* template struct best_fit_uint_type; - + //************************************************************************* // Less than or equal to 8 bits. //************************************************************************* @@ -250,7 +298,7 @@ namespace etl } //*************************************************************************** - /// Template to determine the smallest unsigned int type that can contain a + /// Template to determine the smallest unsigned int type that can contain a /// value with the specified number of bits. /// Defines 'type' which is the type of the smallest unsigned integer. ///\ingroup smallest @@ -259,9 +307,9 @@ namespace etl struct smallest_uint_for_bits { private: - + // Determines the index of the best unsigned type for the required number of bits. - static const int TYPE_INDEX = ((NBITS > 8) ? 1 : 0) + + static const int TYPE_INDEX = ((NBITS > 8) ? 1 : 0) + ((NBITS > 16) ? 1 : 0) + ((NBITS > 32) ? 1 : 0); @@ -271,7 +319,7 @@ namespace etl }; //*************************************************************************** - /// Template to determine the smallest signed int type that can contain a + /// Template to determine the smallest signed int type that can contain a /// value with the specified number of bits. /// Defines 'type' which is the type of the smallest signed integer. ///\ingroup smallest @@ -304,7 +352,7 @@ namespace etl // Determines the index of the best unsigned type for the required value. static const int TYPE_INDEX = ((VALUE > UINT_LEAST8_MAX) ? 1 : 0) + - ((VALUE > UINT16_MAX) ? 1 : 0) + + ((VALUE > UINT16_MAX) ? 1 : 0) + ((VALUE > UINT32_MAX) ? 1 : 0); public: @@ -324,7 +372,7 @@ namespace etl private: // Determines the index of the best signed type for the required value. - static const int TYPE_INDEX = (((VALUE > INT_LEAST8_MAX) || (VALUE < INT_LEAST8_MIN)) ? 1 : 0) + + static const int TYPE_INDEX = (((VALUE > INT_LEAST8_MAX) || (VALUE < INT_LEAST8_MIN)) ? 1 : 0) + (((VALUE > INT16_MAX) || (VALUE < INT16_MIN)) ? 1 : 0) + (((VALUE > INT32_MAX) || (VALUE < INT32_MIN)) ? 1 : 0); diff --git a/include/etl/type_traits_generator.h b/include/etl/type_traits_generator.h index e620564c..b1cff3eb 100644 --- a/include/etl/type_traits_generator.h +++ b/include/etl/type_traits_generator.h @@ -633,7 +633,7 @@ namespace etl } template <> - constexpr size_t size_of() const + constexpr size_t size_of() { return 1; }