From 55995e4c310a7e22b1f93fd3857fd6ba4fd6678b Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Mon, 4 Dec 2017 10:10:14 +0000 Subject: [PATCH 01/24] Corrected comment --- src/sqrt.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sqrt.h b/src/sqrt.h index faeb8806..cbfdb2ce 100644 --- a/src/sqrt.h +++ b/src/sqrt.h @@ -40,7 +40,7 @@ SOFTWARE. namespace etl { //*************************************************************************** - /// Calculates the smallest value that, when squared, will be greater than or equal to VALUE. + /// Calculates the smallest value that, when squared, will be not greater than VALUE. //*************************************************************************** template struct sqrt From 71b963d404710d72c3d12e97e5a84c3d2d2c8b3e Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Mon, 4 Dec 2017 10:10:56 +0000 Subject: [PATCH 02/24] Removed explicit from constructor from underlying value type. --- src/type_def.h | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/type_def.h b/src/type_def.h index 69e89442..f3c0907d 100644 --- a/src/type_def.h +++ b/src/type_def.h @@ -64,7 +64,7 @@ namespace etl } //********************************************************************* - explicit type_def(TValue value_) + type_def(TValue value_) : value(value_) { } @@ -237,11 +237,18 @@ namespace etl return *this; } + //********************************************************************* + type_def& operator =(TValue rhs) + { + value = rhs; + return *this; + } + //********************************************************************* type_def& operator =(const type_def& rhs) { - value = rhs.value; - return *this; + value = rhs.value; + return *this; } //********************************************************************* From 1021a847c46a7580fd946a499aa7a1455a929916 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Mon, 4 Dec 2017 10:11:17 +0000 Subject: [PATCH 03/24] Removed redundant comment --- src/function.h | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/function.h b/src/function.h index 7b13b82b..2e07390b 100644 --- a/src/function.h +++ b/src/function.h @@ -336,11 +336,6 @@ namespace etl typedef TObject object_type; ///< The type of object. typedef void parameter_type; ///< The type of parameter sent to the function. - //************************************************************************* - /// Constructor. - ///\param object Reference to the object - //************************************************************************* - //************************************************************************* /// The function operator that calls the destination function. ///\param data The data to pass to the function. From e3c6865bd1c28f5a2e1275366417281146ef477f Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Mon, 4 Dec 2017 10:21:41 +0000 Subject: [PATCH 04/24] Added has_successor() and get_successor() Move successor pointer from protected to private --- src/message_router.h | 80 +++++++++++++++++++--------------- src/message_router_generator.h | 24 +++++++--- 2 files changed, 64 insertions(+), 40 deletions(-) diff --git a/src/message_router.h b/src/message_router.h index 4c8744c2..d7c7b5c7 100644 --- a/src/message_router.h +++ b/src/message_router.h @@ -133,6 +133,18 @@ namespace etl successor = &successor_; } + //******************************************** + imessage_router& get_successor() const + { + return *successor; + } + + //******************************************** + bool has_successor() const + { + return (successor != nullptr); + } + enum { NULL_MESSAGE_ROUTER = 255, @@ -156,14 +168,14 @@ namespace etl { } - etl::imessage_router* successor; - private: // Disabled. imessage_router(const imessage_router&); imessage_router& operator =(const imessage_router&); + etl::imessage_router* successor; + etl::message_router_id_t message_router_id; }; @@ -354,9 +366,9 @@ namespace etl case T16::ID: static_cast(this)->on_receive(source, static_cast(msg)); break; default: { - if (successor != nullptr) + if (has_successor()) { - successor->receive(source, msg); + get_successor().receive(source, msg); } else { @@ -512,9 +524,9 @@ namespace etl case T15::ID: static_cast(this)->on_receive(source, static_cast(msg)); break; default: { - if (successor != nullptr) + if (has_successor()) { - successor->receive(source, msg); + get_successor().receive(source, msg); } else { @@ -668,9 +680,9 @@ namespace etl case T14::ID: static_cast(this)->on_receive(source, static_cast(msg)); break; default: { - if (successor != nullptr) + if (has_successor()) { - successor->receive(source, msg); + get_successor().receive(source, msg); } else { @@ -822,9 +834,9 @@ namespace etl case T13::ID: static_cast(this)->on_receive(source, static_cast(msg)); break; default: { - if (successor != nullptr) + if (has_successor()) { - successor->receive(source, msg); + get_successor().receive(source, msg); } else { @@ -973,9 +985,9 @@ namespace etl case T12::ID: static_cast(this)->on_receive(source, static_cast(msg)); break; default: { - if (successor != nullptr) + if (has_successor()) { - successor->receive(source, msg); + get_successor().receive(source, msg); } else { @@ -1122,9 +1134,9 @@ namespace etl case T11::ID: static_cast(this)->on_receive(source, static_cast(msg)); break; default: { - if (successor != nullptr) + if (has_successor()) { - successor->receive(source, msg); + get_successor().receive(source, msg); } else { @@ -1269,9 +1281,9 @@ namespace etl case T10::ID: static_cast(this)->on_receive(source, static_cast(msg)); break; default: { - if (successor != nullptr) + if (has_successor()) { - successor->receive(source, msg); + get_successor().receive(source, msg); } else { @@ -1414,9 +1426,9 @@ namespace etl case T9::ID: static_cast(this)->on_receive(source, static_cast(msg)); break; default: { - if (successor != nullptr) + if (has_successor()) { - successor->receive(source, msg); + get_successor().receive(source, msg); } else { @@ -1556,9 +1568,9 @@ namespace etl case T8::ID: static_cast(this)->on_receive(source, static_cast(msg)); break; default: { - if (successor != nullptr) + if (has_successor()) { - successor->receive(source, msg); + get_successor().receive(source, msg); } else { @@ -1696,9 +1708,9 @@ namespace etl case T7::ID: static_cast(this)->on_receive(source, static_cast(msg)); break; default: { - if (successor != nullptr) + if (has_successor()) { - successor->receive(source, msg); + get_successor().receive(source, msg); } else { @@ -1833,9 +1845,9 @@ namespace etl case T6::ID: static_cast(this)->on_receive(source, static_cast(msg)); break; default: { - if (successor != nullptr) + if (has_successor()) { - successor->receive(source, msg); + get_successor().receive(source, msg); } else { @@ -1968,9 +1980,9 @@ namespace etl case T5::ID: static_cast(this)->on_receive(source, static_cast(msg)); break; default: { - if (successor != nullptr) + if (has_successor()) { - successor->receive(source, msg); + get_successor().receive(source, msg); } else { @@ -2100,9 +2112,9 @@ namespace etl case T4::ID: static_cast(this)->on_receive(source, static_cast(msg)); break; default: { - if (successor != nullptr) + if (has_successor()) { - successor->receive(source, msg); + get_successor().receive(source, msg); } else { @@ -2230,9 +2242,9 @@ namespace etl case T3::ID: static_cast(this)->on_receive(source, static_cast(msg)); break; default: { - if (successor != nullptr) + if (has_successor()) { - successor->receive(source, msg); + get_successor().receive(source, msg); } else { @@ -2358,9 +2370,9 @@ namespace etl case T2::ID: static_cast(this)->on_receive(source, static_cast(msg)); break; default: { - if (successor != nullptr) + if (has_successor()) { - successor->receive(source, msg); + get_successor().receive(source, msg); } else { @@ -2484,9 +2496,9 @@ namespace etl case T1::ID: static_cast(this)->on_receive(source, static_cast(msg)); break; default: { - if (successor != nullptr) + if (has_successor()) { - successor->receive(source, msg); + get_successor().receive(source, msg); } else { diff --git a/src/message_router_generator.h b/src/message_router_generator.h index 8353ee48..d0c525b4 100644 --- a/src/message_router_generator.h +++ b/src/message_router_generator.h @@ -145,6 +145,18 @@ namespace etl successor = &successor_; } + //******************************************** + imessage_router& get_successor() const + { + return *successor; + } + + //******************************************** + bool has_successor() const + { + return (has_successor()); + } + enum { NULL_MESSAGE_ROUTER = 255, @@ -168,14 +180,14 @@ namespace etl { } - etl::imessage_router* successor; - private: // Disabled. imessage_router(const imessage_router&); imessage_router& operator =(const imessage_router&); + etl::imessage_router* successor; + etl::message_router_id_t message_router_id; }; @@ -362,9 +374,9 @@ namespace etl cog.outl(" break;") cog.outl(" default:") cog.outl(" {") - cog.outl(" if (successor != nullptr)") + cog.outl(" if (has_successor())") cog.outl(" {") - cog.outl(" successor->receive(source, msg);") + cog.outl(" get_successor().receive(source, msg);") cog.outl(" }") cog.outl(" else") cog.outl(" {") @@ -534,9 +546,9 @@ namespace etl cog.outl(" break;") cog.outl(" default:") cog.outl(" {") - cog.outl(" if (successor != nullptr)") + cog.outl(" if (has_successor())") cog.outl(" {") - cog.outl(" successor->receive(source, msg);") + cog.outl(" get_successor().receive(source, msg);") cog.outl(" }") cog.outl(" else") cog.outl(" {") From 636554812fe7dd267f3045e606f41c9fd0fed277 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Tue, 5 Dec 2017 18:47:20 +0000 Subject: [PATCH 05/24] Minor tweak to conditional_integral_constant --- src/type_traits.h | 13 +++++++------ src/type_traits_generator.h | 6 +++++- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/type_traits.h b/src/type_traits.h index 350c8129..492296a8 100644 --- a/src/type_traits.h +++ b/src/type_traits.h @@ -54,6 +54,7 @@ SOFTWARE. #define __ETL_TYPE_TRAITS__ #include +#include #include "platform.h" #include "nullptr.h" @@ -334,7 +335,7 @@ namespace etl struct conditional_integral_constant { STATIC_ASSERT(etl::is_integral::value, "Not an integral type"); - static const T value = FALSE_VALUE; + static const T value = FALSE_VALUE; }; /// make_signed @@ -484,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 || diff --git a/src/type_traits_generator.h b/src/type_traits_generator.h index ef409202..bef84ee5 100644 --- a/src/type_traits_generator.h +++ b/src/type_traits_generator.h @@ -66,6 +66,7 @@ cog.outl("//******************************************************************** #define __ETL_TYPE_TRAITS__ #include +#include #include "platform.h" #include "nullptr.h" @@ -333,7 +334,10 @@ namespace etl /// conditional_integral_constant ///\ingroup type_traits template - struct conditional_integral_constant + struct conditional_integral_constant; + + template + struct conditional_integral_constant { STATIC_ASSERT(etl::is_integral::value, "Not an integral type"); static const T value = TRUE_VALUE; From c0169188cabdd13123d6127f774de16b7ec8b96e Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Tue, 5 Dec 2017 18:49:50 +0000 Subject: [PATCH 06/24] Made some templates into normal functions and moved to binary.cpp Added bit<> template constant Added bit enumerations --- src/binary.cpp | 470 ++++++++++++++++++++++++++++++++ src/binary.h | 618 +++++++++---------------------------------- test/test_binary.cpp | 98 ++++++- 3 files changed, 689 insertions(+), 497 deletions(-) create mode 100644 src/binary.cpp diff --git a/src/binary.cpp b/src/binary.cpp new file mode 100644 index 00000000..52035a5c --- /dev/null +++ b/src/binary.cpp @@ -0,0 +1,470 @@ +///\file + +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +http://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. +******************************************************************************/ + +#include "platform.h" +#include "binary.h" + +namespace etl +{ +#if ETL_8BIT_SUPPORT + //*************************************************************************** + /// Reverse 8 bits. + //*************************************************************************** + uint8_t reverse_bits(uint8_t value) + { + value = ((value & 0xAA) >> 1) | ((value & 0x55) << 1); + value = ((value & 0xCC) >> 2) | ((value & 0x33) << 2); + value = (value >> 4) | (value << 4); + + return value; + } +#endif + + //*************************************************************************** + /// Reverse 16 bits. + //*************************************************************************** + uint16_t reverse_bits(uint16_t value) + { + value = ((value & 0xAAAA) >> 1) | ((value & 0x5555) << 1); + value = ((value & 0xCCCC) >> 2) | ((value & 0x3333) << 2); + value = ((value & 0xF0F0) >> 4) | ((value & 0x0F0F) << 4); + value = (value >> 8) | (value << 8); + + return value; + } + + //*************************************************************************** + /// Reverse 32 bits. + //*************************************************************************** + uint32_t reverse_bits(uint32_t value) + { + value = ((value & 0xAAAAAAAA) >> 1) | ((value & 0x55555555) << 1); + value = ((value & 0xCCCCCCCC) >> 2) | ((value & 0x33333333) << 2); + value = ((value & 0xF0F0F0F0) >> 4) | ((value & 0x0F0F0F0F) << 4); + value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8); + value = (value >> 16) | (value << 16); + + return value; + } + + //*************************************************************************** + /// Reverse 64 bits. + //*************************************************************************** + uint64_t reverse_bits(uint64_t value) + { + value = ((value & 0xAAAAAAAAAAAAAAAA) >> 1) | ((value & 0x5555555555555555) << 1); + value = ((value & 0xCCCCCCCCCCCCCCCC) >> 2) | ((value & 0x3333333333333333) << 2); + value = ((value & 0xF0F0F0F0F0F0F0F0) >> 4) | ((value & 0x0F0F0F0F0F0F0F0F) << 4); + value = ((value & 0xFF00FF00FF00FF00) >> 8) | ((value & 0x00FF00FF00FF00FF) << 8); + value = ((value & 0xFFFF0000FFFF0000) >> 16) | ((value & 0x0000FFFF0000FFFF) << 16); + value = (value >> 32) | (value << 32); + + return value; + } + + //*************************************************************************** + /// Reverse bytes 16 bit. + //*************************************************************************** + uint16_t reverse_bytes(uint16_t value) + { + value = (value >> 8) | (value << 8); + + return value; + } + + //*************************************************************************** + /// Reverse bytes 32 bit. + //*************************************************************************** + uint32_t reverse_bytes(uint32_t value) + { + value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8); + value = (value >> 16) | (value << 16); + + return value; + } + + //*************************************************************************** + /// Reverse bytes 64 bit. + //*************************************************************************** + uint64_t reverse_bytes(uint64_t value) + { + value = ((value & 0xFF00FF00FF00FF00) >> 8) | ((value & 0x00FF00FF00FF00FF) << 8); + value = ((value & 0xFFFF0000FFFF0000) >> 16) | ((value & 0x0000FFFF0000FFFF) << 16); + value = (value >> 32) | (value << 32); + + return value; + } + +#if ETL_8BIT_SUPPORT + //*************************************************************************** + /// Converts Gray code to binary. + //*************************************************************************** + uint8_t gray_to_binary(uint8_t value) + { + value ^= (value >> 4); + value ^= (value >> 2); + value ^= (value >> 1); + + return value; + } +#endif + + //*************************************************************************** + /// Converts Gray code to binary. + //*************************************************************************** + uint16_t gray_to_binary(uint16_t value) + { + value ^= (value >> 8); + value ^= (value >> 4); + value ^= (value >> 2); + value ^= (value >> 1); + + return value; + } + + //*************************************************************************** + /// Converts Gray code to binary. + //*************************************************************************** + uint32_t gray_to_binary(uint32_t value) + { + value ^= (value >> 16); + value ^= (value >> 8); + value ^= (value >> 4); + value ^= (value >> 2); + value ^= (value >> 1); + + return value; + } + + //*************************************************************************** + /// Converts Gray code to binary. + //*************************************************************************** + uint64_t gray_to_binary(uint64_t value) + { + value ^= (value >> 32); + value ^= (value >> 16); + value ^= (value >> 8); + value ^= (value >> 4); + value ^= (value >> 2); + value ^= (value >> 1); + + return value; + } + +#if ETL_8BIT_SUPPORT + //*************************************************************************** + /// Count set bits. 8 bits. + //*************************************************************************** + uint_least8_t count_bits(uint8_t value) + { + uint32_t count; + static const int S[] = { 1, 2, 4 }; + static const uint8_t B[] = { 0x55, 0x33, 0x0F }; + + count = value - ((value >> 1) & B[0]); + count = ((count >> S[1]) & B[1]) + (count & B[1]); + count = ((count >> S[2]) + count) & B[2]; + + return uint_least8_t(count); + } +#endif + + //*************************************************************************** + /// Count set bits. 16 bits. + //*************************************************************************** + uint_least8_t count_bits(uint16_t value) + { + uint32_t count; + static const int S[] = { 1, 2, 4, 8 }; + static const uint16_t B[] = { 0x5555, 0x3333, 0x0F0F, 0x00FF }; + + count = value - ((value >> 1) & B[0]); + count = ((count >> S[1]) & B[1]) + (count & B[1]); + count = ((count >> S[2]) + count) & B[2]; + count = ((count >> S[3]) + count) & B[3]; + + return count; + } + + //*************************************************************************** + /// Count set bits. 32 bits. + //*************************************************************************** + uint_least8_t count_bits(uint32_t value) + { + uint32_t count; + + value = value - ((value >> 1) & 0x55555555); + value = (value & 0x33333333) + ((value >> 2) & 0x33333333); + count = (((value + (value >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24; + + return uint_least8_t(count); + } + + //*************************************************************************** + /// Count set bits. 64 bits. + //*************************************************************************** + uint_least8_t count_bits(uint64_t value) + { + uint64_t count; + static const int S[] = { 1, 2, 4, 8, 16, 32 }; + static const uint64_t B[] = { 0x5555555555555555, 0x3333333333333333, 0x0F0F0F0F0F0F0F0F, 0x00FF00FF00FF00FF, 0x0000FFFF0000FFFF, 0x00000000FFFFFFFF }; + + count = value - ((value >> 1) & B[0]); + count = ((count >> S[1]) & B[1]) + (count & B[1]); + count = ((count >> S[2]) + count) & B[2]; + count = ((count >> S[3]) + count) & B[3]; + count = ((count >> S[4]) + count) & B[4]; + count = ((count >> S[5]) + count) & B[5]; + + return uint_least8_t(count); + } + +#if ETL_8BIT_SUPPORT + //*************************************************************************** + /// Parity. 8bits. 0 = even, 1 = odd + //*************************************************************************** + uint_least8_t parity(uint8_t value) + { + value ^= value >> 4; + value &= 0x0F; + return (0x6996 >> value) & 1; + } +#endif + + //*************************************************************************** + /// Parity. 16bits. 0 = even, 1 = odd + //*************************************************************************** + uint_least8_t parity(uint16_t value) + { + value ^= value >> 8; + value ^= value >> 4; + value &= 0x0F; + return (0x6996 >> value) & 1; + } + + //*************************************************************************** + /// Parity. 32bits. 0 = even, 1 = odd + //*************************************************************************** + uint_least8_t parity(uint32_t value) + { + value ^= value >> 16; + value ^= value >> 8; + value ^= value >> 4; + value &= 0x0F; + return (0x6996 >> value) & 1; + } + + //*************************************************************************** + /// Parity. 64bits. 0 = even, 1 = odd + //*************************************************************************** + uint_least8_t parity(uint64_t value) + { + value ^= value >> 32; + value ^= value >> 16; + value ^= value >> 8; + value ^= value >> 4; + value &= 0x0F; + return (0x69966996 >> value) & 1; + } + +#if ETL_8BIT_SUPPORT + //*************************************************************************** + /// Count trailing zeros. bit. + /// Uses a binary search. + //*************************************************************************** + uint_least8_t count_trailing_zeros(uint8_t value) + { + uint_least8_t count; + + if (value & 0x1) + { + count = 0; + } + else + { + count = 1; + + if ((value & 0xF) == 0) + { + value >>= 4; + count += 4; + } + + if ((value & 0x3) == 0) + { + value >>= 2; + count += 2; + } + + count -= value & 0x1; + } + + return count; + } +#endif + + //*************************************************************************** + /// Count trailing zeros. 16bit. + /// Uses a binary search. + //*************************************************************************** + uint_least8_t count_trailing_zeros(uint16_t value) + { + uint_least8_t count; + + if (value & 0x1) + { + count = 0; + } + else + { + count = 1; + + if ((value & 0xFF) == 0) + { + value >>= 8; + count += 8; + } + + if ((value & 0xF) == 0) + { + value >>= 4; + count += 4; + } + + if ((value & 0x3) == 0) + { + value >>= 2; + count += 2; + } + + count -= value & 0x1; + } + + return count; + } + + //*************************************************************************** + /// Count trailing zeros. 32bit. + /// Uses a binary search. + //*************************************************************************** + uint_least8_t count_trailing_zeros(uint32_t value) + { + uint_least8_t count; + + if (value & 0x1) + { + count = 0; + } + else + { + count = 1; + + if ((value & 0xFFFF) == 0) + { + value >>= 16; + count += 16; + } + + if ((value & 0xFF) == 0) + { + value >>= 8; + count += 8; + } + + if ((value & 0xF) == 0) + { + value >>= 4; + count += 4; + } + + if ((value & 0x3) == 0) + { + value >>= 2; + count += 2; + } + + count -= value & 0x1; + } + + return count; + } + + //*************************************************************************** + /// Count trailing zeros. 64bit. + /// Uses a binary search. + //*************************************************************************** + uint_least8_t count_trailing_zeros(uint64_t value) + { + uint_least8_t count; + + if (value & 0x1) + { + count = 0; + } + else + { + count = 1; + + if ((value & 0xFFFFFFFF) == 0) + { + value >>= 32; + count += 32; + } + + if ((value & 0xFFFF) == 0) + { + value >>= 16; + count += 16; + } + + if ((value & 0xFF) == 0) + { + value >>= 8; + count += 8; + } + + if ((value & 0xF) == 0) + { + value >>= 4; + count += 4; + } + + if ((value & 0x3) == 0) + { + value >>= 2; + count += 2; + } + + count -= value & 0x1; + } + + return count; + } +} diff --git a/src/binary.h b/src/binary.h index 1717168d..7fccd64e 100644 --- a/src/binary.h +++ b/src/binary.h @@ -52,34 +52,12 @@ namespace etl //*************************************************************************** /// Maximum value that can be contained in N bits. //*************************************************************************** - namespace __private_binary__ - { - /// Helper definition for non-zero NBITS. - template - struct max_value_for_nbits_helper - { - typedef typename etl::smallest_uint_for_bits::type value_type; - static const value_type value = (uint64_t(1) << (NBITS - 1)) | max_value_for_nbits_helper::value; - }; - - /// Specialisation for when NBITS == 0. - template <> - struct max_value_for_nbits_helper<0> - { - typedef etl::smallest_uint_for_bits<0>::type value_type; - static const value_type value = 1; - }; - - template - const typename max_value_for_nbits_helper::value_type max_value_for_nbits_helper::value; - } - /// Definition for non-zero NBITS. template struct max_value_for_nbits { typedef typename etl::smallest_uint_for_bits::type value_type; - static const value_type value = __private_binary__::max_value_for_nbits_helper::value; + static const value_type value = (value_type(1) << (NBITS - 1)) | max_value_for_nbits::value; }; /// Specialisation for when NBITS == 0. @@ -172,108 +150,33 @@ namespace etl return result; } + //*************************************************************************** + /// Reverse bits. + //*************************************************************************** #if ETL_8BIT_SUPPORT - //*************************************************************************** - /// Reverse 8 bits. - //*************************************************************************** - template - typename etl::enable_if::type, uint8_t>::value, T>::type - reverse_bits(T value) - { - value = ((value & 0xAA) >> 1) | ((value & 0x55) << 1); - value = ((value & 0xCC) >> 2) | ((value & 0x33) << 2); - value = (value >> 4) | (value << 4); - - return value; - } + uint8_t reverse_bits(uint8_t value); + inline int8_t reverse_bits(int8_t value) { return int8_t(reverse_bits(uint8_t(value))); } #endif + uint16_t reverse_bits(uint16_t value); + inline int16_t reverse_bits(int16_t value) { return int16_t(reverse_bits(uint16_t(value))); } + uint32_t reverse_bits(uint32_t value); + inline int32_t reverse_bits(int32_t value) { return int32_t(reverse_bits(uint32_t(value))); } + uint64_t reverse_bits(uint64_t value); + inline int64_t reverse_bits(int64_t value) { return int64_t(reverse_bits(uint64_t(value))); } //*************************************************************************** - /// Reverse 16 bits. + /// Reverse bytes. //*************************************************************************** - template - typename etl::enable_if::type, uint16_t>::value, T>::type - reverse_bits(T value) - { - value = ((value & 0xAAAA) >> 1) | ((value & 0x5555) << 1); - value = ((value & 0xCCCC) >> 2) | ((value & 0x3333) << 2); - value = ((value & 0xF0F0) >> 4) | ((value & 0x0F0F) << 4); - value = (value >> 8) | (value << 8); - - return value; - } - - //*************************************************************************** - /// Reverse 32 bits. - //*************************************************************************** - template - typename etl::enable_if::type, uint32_t>::value, T>::type - reverse_bits(T value) - { - value = ((value & 0xAAAAAAAA) >> 1) | ((value & 0x55555555) << 1); - value = ((value & 0xCCCCCCCC) >> 2) | ((value & 0x33333333) << 2); - value = ((value & 0xF0F0F0F0) >> 4) | ((value & 0x0F0F0F0F) << 4); - value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8); - value = (value >> 16) | (value << 16); - - return value; - } - - //*************************************************************************** - /// Reverse 64 bits. - //*************************************************************************** - template - typename etl::enable_if::type, uint64_t>::value, T>::type - reverse_bits(T value) - { - value = ((value & 0xAAAAAAAAAAAAAAAA) >> 1) | ((value & 0x5555555555555555) << 1); - value = ((value & 0xCCCCCCCCCCCCCCCC) >> 2) | ((value & 0x3333333333333333) << 2); - value = ((value & 0xF0F0F0F0F0F0F0F0) >> 4) | ((value & 0x0F0F0F0F0F0F0F0F) << 4); - value = ((value & 0xFF00FF00FF00FF00) >> 8) | ((value & 0x00FF00FF00FF00FF) << 8); - value = ((value & 0xFFFF0000FFFF0000) >> 16) | ((value & 0x0000FFFF0000FFFF) << 16); - value = (value >> 32) | (value << 32); - - return value; - } - - //*************************************************************************** - /// Reverse bytes 16 bit. - //*************************************************************************** - template - typename etl::enable_if::type, uint16_t>::value, T>::type - reverse_bytes(T value) - { - value = (value >> 8) | (value << 8); - - return value; - } - - //*************************************************************************** - /// Reverse bytes 32 bit. - //*************************************************************************** - template - typename etl::enable_if::type, uint32_t>::value, T>::type - reverse_bytes(T value) - { - value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8); - value = (value >> 16) | (value << 16); - - return value; - } - - //*************************************************************************** - /// Reverse bytes 64 bit. - //*************************************************************************** - template - typename etl::enable_if::type, uint64_t>::value, T>::type - reverse_bytes(T value) - { - value = ((value & 0xFF00FF00FF00FF00) >> 8) | ((value & 0x00FF00FF00FF00FF) << 8); - value = ((value & 0xFFFF0000FFFF0000) >> 16) | ((value & 0x0000FFFF0000FFFF) << 16); - value = (value >> 32) | (value << 32); - - return value; - } +#if ETL_8BIT_SUPPORT + inline uint8_t reverse_bytes(uint8_t value) { return value; } + inline int8_t reverse_bytes(int8_t value) { return value; } +#endif + uint16_t reverse_bytes(uint16_t value); + inline int16_t reverse_bytes(int16_t value) { return int16_t(reverse_bytes(uint16_t(value))); } + uint32_t reverse_bytes(uint32_t value); + inline int32_t reverse_bytes(int32_t value) { return int32_t(reverse_bytes(uint32_t(value))); } + uint64_t reverse_bytes(uint64_t value); + inline int64_t reverse_bytes(int64_t value) { return int64_t(reverse_bytes(uint64_t(value))); } //*************************************************************************** /// Converts binary to Gray code. @@ -286,201 +189,47 @@ namespace etl return (value >> 1) ^ value; } + //*************************************************************************** + /// Converts Gray code to binary. + //*************************************************************************** #if ETL_8BIT_SUPPORT - //*************************************************************************** - /// Converts Gray code to binary. - //*************************************************************************** - template - typename etl::enable_if::type, uint8_t>::value, T>::type - gray_to_binary(T value) - { - value ^= (value >> 4); - value ^= (value >> 2); - value ^= (value >> 1); - - return value; - } + uint8_t gray_to_binary(uint8_t value); + inline int8_t gray_to_binary(int8_t value) { return int8_t(gray_to_binary(uint8_t(value))); } #endif + uint16_t gray_to_binary(uint16_t value); + inline int16_t gray_to_binary(int16_t value) { return int16_t(gray_to_binary(uint16_t(value))); } + uint32_t gray_to_binary(uint32_t value); + inline int32_t gray_to_binary(int32_t value) { return int32_t(gray_to_binary(uint32_t(value))); } + uint64_t gray_to_binary(uint64_t value); + inline int64_t gray_to_binary(int64_t value) { return int64_t(gray_to_binary(uint64_t(value))); } //*************************************************************************** - /// Converts Gray code to binary. + /// Count set bits. //*************************************************************************** - template - typename etl::enable_if::type, uint16_t>::value, T>::type - gray_to_binary(T value) - { - value ^= (value >> 8); - value ^= (value >> 4); - value ^= (value >> 2); - value ^= (value >> 1); - - return value; - } - - //*************************************************************************** - /// Converts Gray code to binary. - //*************************************************************************** - template - typename etl::enable_if::type, uint32_t>::value, T>::type - gray_to_binary(T value) - { - value ^= (value >> 16); - value ^= (value >> 8); - value ^= (value >> 4); - value ^= (value >> 2); - value ^= (value >> 1); - - return value; - } - - //*************************************************************************** - /// Converts Gray code to binary. - //*************************************************************************** - template - typename etl::enable_if::type, uint64_t>::value, T>::type - gray_to_binary(T value) - { - value ^= (value >> 32); - value ^= (value >> 16); - value ^= (value >> 8); - value ^= (value >> 4); - value ^= (value >> 2); - value ^= (value >> 1); - - return value; - } - #if ETL_8BIT_SUPPORT - //*************************************************************************** - /// Count set bits. 8 bits. - //*************************************************************************** - template - typename etl::enable_if::type, uint8_t>::value, size_t>::type - count_bits(T value) - { - uint32_t count; - static const int S[] = { 1, 2, 4 }; - static const uint8_t B[] = { 0x55, 0x33, 0x0F }; - - count = value - ((value >> 1) & B[0]); - count = ((count >> S[1]) & B[1]) + (count & B[1]); - count = ((count >> S[2]) + count) & B[2]; - - return count; - } + uint_least8_t count_bits(uint8_t value); + inline uint_least8_t count_bits(int8_t value) { return count_bits(uint8_t(value)); } #endif + uint_least8_t count_bits(uint16_t value); + inline uint_least8_t count_bits(int16_t value) { return count_bits(uint16_t(value)); } + uint_least8_t count_bits(uint32_t value); + inline uint_least8_t count_bits(int32_t value) { return count_bits(uint32_t(value)); } + uint_least8_t count_bits(uint64_t value); + inline uint_least8_t count_bits(int64_t value) { return count_bits(uint64_t(value)); } //*************************************************************************** - /// Count set bits. 16 bits. + /// Parity. 0 = even, 1 = odd //*************************************************************************** - template - typename etl::enable_if::type, uint16_t>::value, size_t>::type - count_bits(T value) - { - uint32_t count; - static const int S[] = { 1, 2, 4, 8 }; - static const uint16_t B[] = { 0x5555, 0x3333, 0x0F0F, 0x00FF }; - - count = value - ((value >> 1) & B[0]); - count = ((count >> S[1]) & B[1]) + (count & B[1]); - count = ((count >> S[2]) + count) & B[2]; - count = ((count >> S[3]) + count) & B[3]; - - return count; - } - - //*************************************************************************** - /// Count set bits. 32 bits. - //*************************************************************************** - template - typename etl::enable_if::type, uint32_t>::value, size_t>::type - count_bits(T value) - { - uint32_t count; - - value = value - ((value >> 1) & 0x55555555); - value = (value & 0x33333333) + ((value >> 2) & 0x33333333); - count = (((value + (value >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24; - - return count; - } - - //*************************************************************************** - /// Count set bits. 64 bits. - //*************************************************************************** - template - typename etl::enable_if::type, uint64_t>::value, size_t>::type - count_bits(T value) - { - uint64_t count; - static const int S[] = { 1, 2, 4, 8, 16, 32 }; - static const uint64_t B[] = { 0x5555555555555555, 0x3333333333333333, 0x0F0F0F0F0F0F0F0F, 0x00FF00FF00FF00FF, 0x0000FFFF0000FFFF, 0x00000000FFFFFFFF }; - - count = value - ((value >> 1) & B[0]); - count = ((count >> S[1]) & B[1]) + (count & B[1]); - count = ((count >> S[2]) + count) & B[2]; - count = ((count >> S[3]) + count) & B[3]; - count = ((count >> S[4]) + count) & B[4]; - count = ((count >> S[5]) + count) & B[5]; - - return size_t(count); - } - #if ETL_8BIT_SUPPORT - //*************************************************************************** - /// Parity. 8bits. 0 = even, 1 = odd - //*************************************************************************** - template - typename etl::enable_if::type, uint8_t>::value, size_t>::type - parity(T value) - { - value ^= value >> 4; - value &= 0x0F; - return (0x6996 >> value) & 1; - } + uint_least8_t parity(uint8_t value); + inline uint_least8_t parity(int8_t value) { return parity(uint8_t(value)); } #endif - - //*************************************************************************** - /// Parity. 16bits. 0 = even, 1 = odd - //*************************************************************************** - template - typename etl::enable_if::type, uint16_t>::value, size_t>::type - parity(T value) - { - value ^= value >> 8; - value ^= value >> 4; - value &= 0x0F; - return (0x6996 >> value) & 1; - } - - //*************************************************************************** - /// Parity. 32bits. 0 = even, 1 = odd - //*************************************************************************** - template - typename etl::enable_if::type, uint32_t>::value, size_t>::type - parity(T value) - { - value ^= value >> 16; - value ^= value >> 8; - value ^= value >> 4; - value &= 0x0F; - return (0x6996 >> value) & 1; - } - - //*************************************************************************** - /// Parity. 64bits. 0 = even, 1 = odd - //*************************************************************************** - template - typename etl::enable_if::type, uint64_t>::value, size_t>::type - parity(T value) - { - value ^= value >> 32; - value ^= value >> 16; - value ^= value >> 8; - value ^= value >> 4; - value &= 0x0F; - return (0x69966996 >> value) & 1; - } + uint_least8_t parity(uint16_t value); + inline uint_least8_t parity(int16_t value) { return parity(uint16_t(value)); } + uint_least8_t parity(uint32_t value); + inline uint_least8_t parity(int32_t value) { return parity(uint32_t(value)); } + uint_least8_t parity(uint64_t value); + inline uint_least8_t parity(int64_t value) { return parity(uint64_t(value)); } //*************************************************************************** /// Fold a binary number down to a set number of bits using XOR. @@ -521,14 +270,12 @@ namespace etl STATIC_ASSERT(etl::is_signed::value, "TReturn not a signed type"); STATIC_ASSERT(NBITS <= std::numeric_limits::digits, "NBITS too large for return type"); - typedef typename etl::make_unsigned::type mask_t; - - mask_t negative = (1 << (NBITS - 1)); + const TReturn negative = (TReturn(1) << (NBITS - 1)); TReturn signed_value = value; if ((signed_value & negative) != 0) { - mask_t sign_bits = ~((1 << NBITS) - 1); + const TReturn sign_bits = ~((TReturn(1) << NBITS) - 1); signed_value = value | sign_bits; } @@ -547,201 +294,32 @@ namespace etl STATIC_ASSERT(etl::is_signed::value, "TReturn not a signed type"); assert(NBITS <= std::numeric_limits::digits); - typedef typename etl::make_unsigned::type mask_t; - - mask_t negative = (TReturn(1) << (NBITS - 1)); + const TReturn negative = (TReturn(1) << (NBITS - 1)); TReturn signed_value = value; if ((signed_value & negative) != 0) { - mask_t sign_bits = ~((1 << NBITS) - 1); + const TReturn sign_bits = ~((TReturn(1) << NBITS) - 1); signed_value = value | sign_bits; } return signed_value; } -#if ETL_8BIT_SUPPORT //*************************************************************************** /// Count trailing zeros. bit. /// Uses a binary search. //*************************************************************************** - template - typename etl::enable_if::type, uint8_t>::value, uint_least8_t>::type - count_trailing_zeros(T value) - { - uint_least8_t count; - - if (value & 0x1) - { - count = 0; - } - else - { - count = 1; - - if ((value & 0xF) == 0) - { - value >>= 4; - count += 4; - } - - if ((value & 0x3) == 0) - { - value >>= 2; - count += 2; - } - - count -= value & 0x1; - } - - return count; - } +#if ETL_8BIT_SUPPORT + uint_least8_t count_trailing_zeros(uint8_t value); + inline uint_least8_t count_trailing_zeros(int8_t value) { return count_trailing_zeros(uint8_t(value)); } #endif - - //*************************************************************************** - /// Count trailing zeros. 16bit. - /// Uses a binary search. - //*************************************************************************** - template - typename etl::enable_if::type, uint16_t>::value, uint_least8_t>::type - count_trailing_zeros(T value) - { - uint_least8_t count; - - if (value & 0x1) - { - count = 0; - } - else - { - count = 1; - - if ((value & 0xFF) == 0) - { - value >>= 8; - count += 8; - } - - if ((value & 0xF) == 0) - { - value >>= 4; - count += 4; - } - - if ((value & 0x3) == 0) - { - value >>= 2; - count += 2; - } - - count -= value & 0x1; - } - - return count; - } - - //*************************************************************************** - /// Count trailing zeros. 32bit. - /// Uses a binary search. - //*************************************************************************** - template - typename etl::enable_if::type, uint32_t>::value, uint_least8_t>::type - count_trailing_zeros(T value) - { - uint_least8_t count; - - if (value & 0x1) - { - count = 0; - } - else - { - count = 1; - - if ((value & 0xFFFF) == 0) - { - value >>= 16; - count += 16; - } - - if ((value & 0xFF) == 0) - { - value >>= 8; - count += 8; - } - - if ((value & 0xF) == 0) - { - value >>= 4; - count += 4; - } - - if ((value & 0x3) == 0) - { - value >>= 2; - count += 2; - } - - count -= value & 0x1; - } - - return count; - } - - //*************************************************************************** - /// Count trailing zeros. 64bit. - /// Uses a binary search. - //*************************************************************************** - template - typename etl::enable_if::type, uint64_t>::value, uint_least8_t>::type - count_trailing_zeros(T value) - { - uint_least8_t count; - - if (value & 0x1) - { - count = 0; - } - else - { - count = 1; - - if ((value & 0xFFFFFFFF) == 0) - { - value >>= 32; - count += 32; - } - - if ((value & 0xFFFF) == 0) - { - value >>= 16; - count += 16; - } - - if ((value & 0xFF) == 0) - { - value >>= 8; - count += 8; - } - - if ((value & 0xF) == 0) - { - value >>= 4; - count += 4; - } - - if ((value & 0x3) == 0) - { - value >>= 2; - count += 2; - } - - count -= value & 0x1; - } - - return count; - } + uint_least8_t count_trailing_zeros(uint16_t value); + inline uint_least8_t count_trailing_zeros(int16_t value) { return count_trailing_zeros(uint16_t(value)); } + uint_least8_t count_trailing_zeros(uint32_t value); + inline uint_least8_t count_trailing_zeros(int32_t value) { return count_trailing_zeros(uint32_t(value)); } + uint_least8_t count_trailing_zeros(uint64_t value); + inline uint_least8_t count_trailing_zeros(int64_t value) { return count_trailing_zeros(uint64_t(value)); } //*************************************************************************** /// Find the position of the first set bit. @@ -750,7 +328,7 @@ namespace etl template uint_least8_t first_set_bit_position(T value) { - return count_trailing_zeros(value); + return count_trailing_zeros(value); } //*************************************************************************** @@ -760,8 +338,8 @@ namespace etl template uint_least8_t first_clear_bit_position(T value) { - value = ~value; - return count_trailing_zeros(value); + value = ~value; + return count_trailing_zeros(value); } //*************************************************************************** @@ -771,14 +349,28 @@ namespace etl template uint_least8_t first_bit_position(bool state, T value) { - if (!state) - { - value = ~value; - } + if (!state) + { + value = ~value; + } - return count_trailing_zeros(value); + return count_trailing_zeros(value); } + //*************************************************************************** + /// Gets the value of the bit at POSITION + /// Starts from LSB. + //*************************************************************************** + template + struct bit + { + typedef typename etl::smallest_uint_for_bits::type value_type; + static const value_type value = value_type(1) << POSITION; + }; + + template + const typename bit::value_type bit::value; + //*************************************************************************** /// 8 bit binary constants. //*************************************************************************** @@ -1041,6 +633,42 @@ namespace etl b11111110 = 254, b11111111 = 255 }; + + enum bit_constant + { + b0 = 0x1, + b1 = 0x2, + b2 = 0x4, + b3 = 0x8, + b4 = 0x10, + b5 = 0x20, + b6 = 0x40, + b7 = 0x80, + b8 = 0x100, + b9 = 0x200, + b10 = 0x400, + b11 = 0x800, + b12 = 0x1000, + b13 = 0x2000, + b14 = 0x4000, + b15 = 0x8000, + b16 = 0x10000, + b17 = 0x20000, + b18 = 0x40000, + b19 = 0x80000, + b20 = 0x100000, + b21 = 0x200000, + b22 = 0x400000, + b23 = 0x800000, + b24 = 0x1000000, + b25 = 0x2000000, + b26 = 0x4000000, + b27 = 0x8000000, + b28 = 0x10000000, + b29 = 0x20000000, + b30 = 0x40000000, + b31 = 0x80000000 + }; } #endif diff --git a/test/test_binary.cpp b/test/test_binary.cpp index 261b4601..8bcc935a 100644 --- a/test/test_binary.cpp +++ b/test/test_binary.cpp @@ -99,8 +99,12 @@ TReturn test_fold_bits(uint64_t value, int size) // Slow gray to binary template -T compare_gray_to_binary(T value) +T compare_gray_to_binary(T value_) { + typedef typename std::make_unsigned::type type; + + type value = type(value_); + T mask; for (mask = value >> 1; mask != 0; mask = mask >> 1) { @@ -647,6 +651,7 @@ namespace for (size_t i = 1; i <= std::numeric_limits::max(); ++i) { CHECK_EQUAL(compare_gray_to_binary(uint8_t(i)), etl::gray_to_binary(uint8_t(i))); + CHECK_EQUAL(compare_gray_to_binary(int8_t(i)), etl::gray_to_binary(int8_t(i))); } } @@ -656,6 +661,7 @@ namespace for (size_t i = 1; i <= std::numeric_limits::max(); ++i) { CHECK_EQUAL(compare_gray_to_binary(uint16_t(i)), etl::gray_to_binary(uint16_t(i))); + CHECK_EQUAL(compare_gray_to_binary(int16_t(i)), etl::gray_to_binary(int16_t(i))); } } @@ -670,6 +676,7 @@ namespace { uint32_t value = hash.value(); CHECK_EQUAL(compare_gray_to_binary(value), etl::gray_to_binary(value)); + CHECK_EQUAL(int32_t(compare_gray_to_binary(value)), etl::gray_to_binary(int32_t(value))); } } @@ -684,6 +691,7 @@ namespace { uint64_t value = hash.value(); CHECK_EQUAL(compare_gray_to_binary(value), etl::gray_to_binary(value)); + CHECK_EQUAL(int64_t(compare_gray_to_binary(value)), etl::gray_to_binary(int64_t(value))); } } @@ -693,6 +701,7 @@ namespace for (size_t i = 1; i <= std::numeric_limits::max(); ++i) { CHECK_EQUAL(test_count(i), etl::count_bits(uint8_t(i))); + CHECK_EQUAL(test_count(i), etl::count_bits(int8_t(i))); } } @@ -702,6 +711,7 @@ namespace for (size_t i = 1; i <= std::numeric_limits::max(); ++i) { CHECK_EQUAL(test_count(i), etl::count_bits(uint16_t(i))); + CHECK_EQUAL(test_count(i), etl::count_bits(int16_t(i))); } } @@ -717,6 +727,7 @@ namespace uint32_t value = hash.value(); CHECK_EQUAL(test_count(value), etl::count_bits(value)); + CHECK_EQUAL(test_count(value), etl::count_bits(int32_t(value))); } } @@ -732,6 +743,7 @@ namespace uint64_t value = hash.value(); CHECK_EQUAL(test_count(value), etl::count_bits(value)); + CHECK_EQUAL(test_count(value), etl::count_bits(int64_t(value))); } } @@ -741,6 +753,7 @@ namespace for (size_t i = 1; i <= std::numeric_limits::max(); ++i) { CHECK_EQUAL(test_parity(i), etl::parity(uint8_t(i))); + CHECK_EQUAL(test_parity(i), etl::parity(int8_t(i))); } } @@ -750,6 +763,7 @@ namespace for (size_t i = 1; i <= std::numeric_limits::max(); ++i) { CHECK_EQUAL(test_parity(i), etl::parity(uint16_t(i))); + CHECK_EQUAL(test_parity(i), etl::parity(int16_t(i))); } } @@ -765,6 +779,7 @@ namespace uint32_t value = hash.value(); CHECK_EQUAL(test_parity(value), etl::parity(value)); + CHECK_EQUAL(test_parity(value), etl::parity(int32_t(value))); } } @@ -780,6 +795,7 @@ namespace uint64_t value = hash.value(); CHECK_EQUAL(test_parity(value), etl::parity(value)); + CHECK_EQUAL(test_parity(value), etl::parity(int64_t(value))); } } @@ -857,7 +873,7 @@ namespace TEST(test_max_value_for_bits) { // Check that the values are correct. - //CHECK_EQUAL(0, etl::max_value_for_nbits<0>::value); + //CHECK_EQUAL(0U, etl::max_value_for_nbits<0>::value); CHECK_EQUAL(1U, etl::max_value_for_nbits<1>::value); CHECK_EQUAL(3U, etl::max_value_for_nbits<2>::value); CHECK_EQUAL(7U, etl::max_value_for_nbits<3>::value); @@ -1082,6 +1098,84 @@ namespace CHECK_EQUAL(178956970, (etl::sign_extend(value32, 30))); CHECK_EQUAL(178956970, (etl::sign_extend(value32, 30))); } + + //************************************************************************* + TEST(test_bit) + { + const uint32_t N = 1; + + CHECK_EQUAL(N << 0, etl::bit<0>::value); + CHECK_EQUAL(N << 1, etl::bit<1>::value); + CHECK_EQUAL(N << 2, etl::bit<2>::value); + CHECK_EQUAL(N << 3, etl::bit<3>::value); + CHECK_EQUAL(N << 4, etl::bit<4>::value); + CHECK_EQUAL(N << 5, etl::bit<5>::value); + CHECK_EQUAL(N << 6, etl::bit<6>::value); + CHECK_EQUAL(N << 7, etl::bit<7>::value); + CHECK_EQUAL(N << 8, etl::bit<8>::value); + CHECK_EQUAL(N << 9, etl::bit<9>::value); + CHECK_EQUAL(N << 10, etl::bit<10>::value); + CHECK_EQUAL(N << 11, etl::bit<11>::value); + CHECK_EQUAL(N << 12, etl::bit<12>::value); + CHECK_EQUAL(N << 13, etl::bit<13>::value); + CHECK_EQUAL(N << 14, etl::bit<14>::value); + CHECK_EQUAL(N << 15, etl::bit<15>::value); + CHECK_EQUAL(N << 16, etl::bit<16>::value); + CHECK_EQUAL(N << 17, etl::bit<17>::value); + CHECK_EQUAL(N << 18, etl::bit<18>::value); + CHECK_EQUAL(N << 19, etl::bit<19>::value); + CHECK_EQUAL(N << 20, etl::bit<20>::value); + CHECK_EQUAL(N << 21, etl::bit<21>::value); + CHECK_EQUAL(N << 22, etl::bit<22>::value); + CHECK_EQUAL(N << 23, etl::bit<23>::value); + CHECK_EQUAL(N << 24, etl::bit<24>::value); + CHECK_EQUAL(N << 25, etl::bit<25>::value); + CHECK_EQUAL(N << 26, etl::bit<26>::value); + CHECK_EQUAL(N << 27, etl::bit<27>::value); + CHECK_EQUAL(N << 28, etl::bit<28>::value); + CHECK_EQUAL(N << 29, etl::bit<29>::value); + CHECK_EQUAL(N << 30, etl::bit<30>::value); + CHECK_EQUAL(N << 31, etl::bit<31>::value); + } + + //************************************************************************* + TEST(test_bits_constants) + { + const uint32_t N = 1; + + CHECK_EQUAL(N << 0, etl::b0); + CHECK_EQUAL(N << 1, etl::b1); + CHECK_EQUAL(N << 2, etl::b2); + CHECK_EQUAL(N << 3, etl::b3); + CHECK_EQUAL(N << 4, etl::b4); + CHECK_EQUAL(N << 5, etl::b5); + CHECK_EQUAL(N << 6, etl::b6); + CHECK_EQUAL(N << 7, etl::b7); + CHECK_EQUAL(N << 8, etl::b8); + CHECK_EQUAL(N << 9, etl::b9); + CHECK_EQUAL(N << 10, etl::b10); + CHECK_EQUAL(N << 11, etl::b11); + CHECK_EQUAL(N << 12, etl::b12); + CHECK_EQUAL(N << 13, etl::b13); + CHECK_EQUAL(N << 14, etl::b14); + CHECK_EQUAL(N << 15, etl::b15); + CHECK_EQUAL(N << 16, etl::b16); + CHECK_EQUAL(N << 17, etl::b17); + CHECK_EQUAL(N << 18, etl::b18); + CHECK_EQUAL(N << 19, etl::b19); + CHECK_EQUAL(N << 20, etl::b20); + CHECK_EQUAL(N << 21, etl::b21); + CHECK_EQUAL(N << 22, etl::b22); + CHECK_EQUAL(N << 23, etl::b23); + CHECK_EQUAL(N << 24, etl::b24); + CHECK_EQUAL(N << 25, etl::b25); + CHECK_EQUAL(N << 26, etl::b26); + CHECK_EQUAL(N << 27, etl::b27); + CHECK_EQUAL(N << 28, etl::b28); + CHECK_EQUAL(N << 29, etl::b29); + CHECK_EQUAL(N << 30, etl::b30); + CHECK_EQUAL(N << 31, uint32_t(etl::b31)); + } }; } From accd863dcd565d8520e90026ce0ac0b016f19c7c Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Tue, 5 Dec 2017 18:50:18 +0000 Subject: [PATCH 07/24] Fixed global find/replace error. --- src/message_router_generator.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/message_router_generator.h b/src/message_router_generator.h index d0c525b4..26d67413 100644 --- a/src/message_router_generator.h +++ b/src/message_router_generator.h @@ -154,7 +154,7 @@ namespace etl //******************************************** bool has_successor() const { - return (has_successor()); + return (successor != nullptr); } enum From c84815e9d25157240412845f1748590c49b1f5c3 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Tue, 5 Dec 2017 18:51:19 +0000 Subject: [PATCH 08/24] Parameter named in definition. --- src/fsm.h | 82 +++++++++++++++++++++++++++---------------------------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/src/fsm.h b/src/fsm.h index 60edfce7..afa3422e 100644 --- a/src/fsm.h +++ b/src/fsm.h @@ -324,7 +324,7 @@ namespace etl /// Does this FSM accept the message id? /// Yes, it accepts everything! //******************************************* - bool accepts(etl::message_id_t) const + bool accepts(etl::message_id_t id) const { return true; } @@ -383,10 +383,10 @@ namespace etl //*************************************************************************** // The definition for all 16 message types. //*************************************************************************** - template class fsm_state : public ifsm_state { @@ -442,10 +442,10 @@ namespace etl //*************************************************************************** // Specialisation for 15 message types. //*************************************************************************** - template class fsm_state : public ifsm_state { @@ -499,10 +499,10 @@ namespace etl //*************************************************************************** // Specialisation for 14 message types. //*************************************************************************** - template class fsm_state : public ifsm_state { @@ -555,10 +555,10 @@ namespace etl //*************************************************************************** // Specialisation for 13 message types. //*************************************************************************** - template class fsm_state : public ifsm_state { @@ -610,9 +610,9 @@ namespace etl //*************************************************************************** // Specialisation for 12 message types. //*************************************************************************** - template class fsm_state : public ifsm_state { @@ -663,9 +663,9 @@ namespace etl //*************************************************************************** // Specialisation for 11 message types. //*************************************************************************** - template class fsm_state : public ifsm_state { @@ -715,9 +715,9 @@ namespace etl //*************************************************************************** // Specialisation for 10 message types. //*************************************************************************** - template class fsm_state : public ifsm_state { @@ -766,9 +766,9 @@ namespace etl //*************************************************************************** // Specialisation for 9 message types. //*************************************************************************** - template class fsm_state : public ifsm_state { @@ -816,8 +816,8 @@ namespace etl //*************************************************************************** // Specialisation for 8 message types. //*************************************************************************** - template class fsm_state : public ifsm_state { @@ -864,8 +864,8 @@ namespace etl //*************************************************************************** // Specialisation for 7 message types. //*************************************************************************** - template class fsm_state : public ifsm_state { @@ -911,8 +911,8 @@ namespace etl //*************************************************************************** // Specialisation for 6 message types. //*************************************************************************** - template class fsm_state : public ifsm_state { @@ -957,8 +957,8 @@ namespace etl //*************************************************************************** // Specialisation for 5 message types. //*************************************************************************** - template class fsm_state : public ifsm_state { @@ -1002,7 +1002,7 @@ namespace etl //*************************************************************************** // Specialisation for 4 message types. //*************************************************************************** - template class fsm_state : public ifsm_state { @@ -1045,7 +1045,7 @@ namespace etl //*************************************************************************** // Specialisation for 3 message types. //*************************************************************************** - template class fsm_state : public ifsm_state { @@ -1087,7 +1087,7 @@ namespace etl //*************************************************************************** // Specialisation for 2 message types. //*************************************************************************** - template class fsm_state : public ifsm_state { @@ -1128,7 +1128,7 @@ namespace etl //*************************************************************************** // Specialisation for 1 message type. //*************************************************************************** - template class fsm_state : public ifsm_state { From 316ea015078d876f9be18426496332154d892c04 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Tue, 5 Dec 2017 18:51:47 +0000 Subject: [PATCH 09/24] Added new binary.cpp --- test/vs2017/etl.vcxproj | 1 + test/vs2017/etl.vcxproj.filters | 3 +++ 2 files changed, 4 insertions(+) diff --git a/test/vs2017/etl.vcxproj b/test/vs2017/etl.vcxproj index d7b2c1dd..c168f880 100644 --- a/test/vs2017/etl.vcxproj +++ b/test/vs2017/etl.vcxproj @@ -298,6 +298,7 @@ + diff --git a/test/vs2017/etl.vcxproj.filters b/test/vs2017/etl.vcxproj.filters index 1b3f3f85..e3895e76 100644 --- a/test/vs2017/etl.vcxproj.filters +++ b/test/vs2017/etl.vcxproj.filters @@ -932,6 +932,9 @@ Source Files\ECL + + ETL\Utilities + From 45c9e81c5498aba62cfd967380ca0abbc53d2701 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Wed, 13 Dec 2017 09:57:52 +0000 Subject: [PATCH 10/24] Simplified codebase --- src/type_lookup.h | 1949 +---------------------------------- src/type_lookup_generator.h | 188 +--- 2 files changed, 61 insertions(+), 2076 deletions(-) diff --git a/src/type_lookup.h b/src/type_lookup.h index a00c4e76..1d8a98dc 100644 --- a/src/type_lookup.h +++ b/src/type_lookup.h @@ -48,15 +48,12 @@ SOFTWARE. namespace etl { - namespace __private_type_lookup__ - { - struct null_type {}; - } + struct null_type {}; //*************************************************************************** /// The type/id pair type to use for type/id lookup template parameters. //*************************************************************************** - template + template struct type_id_pair { typedef T type; @@ -70,7 +67,7 @@ namespace etl //*************************************************************************** /// The type/type pair type to use for type/type lookup template parameters. //*************************************************************************** - template + template struct type_type_pair { typedef T1 type1; @@ -78,34 +75,30 @@ namespace etl }; //*************************************************************************** - // Default for 16 types. + // For 16 types. //*************************************************************************** template , - typename T3 = etl::type_id_pair<>, - typename T4 = etl::type_id_pair<>, - typename T5 = etl::type_id_pair<>, - typename T6 = etl::type_id_pair<>, - typename T7 = etl::type_id_pair<>, - typename T8 = etl::type_id_pair<>, - typename T9 = etl::type_id_pair<>, - typename T10 = etl::type_id_pair<>, - typename T11 = etl::type_id_pair<>, - typename T12 = etl::type_id_pair<>, - typename T13 = etl::type_id_pair<>, - typename T14 = etl::type_id_pair<>, - typename T15 = etl::type_id_pair<>, - typename T16 = etl::type_id_pair<> > + typename T2 = etl::type_id_pair, + typename T3 = etl::type_id_pair, + typename T4 = etl::type_id_pair, + typename T5 = etl::type_id_pair, + typename T6 = etl::type_id_pair, + typename T7 = etl::type_id_pair, + typename T8 = etl::type_id_pair, + typename T9 = etl::type_id_pair, + typename T10 = etl::type_id_pair, + typename T11 = etl::type_id_pair, + typename T12 = etl::type_id_pair, + typename T13 = etl::type_id_pair, + typename T14 = etl::type_id_pair, + typename T15 = etl::type_id_pair, + typename T16 = etl::type_id_pair > struct type_id_lookup { - private: - - typedef __private_type_lookup__::null_type null_type; - public: //************************************ - template + template struct type_from_id { typedef @@ -125,12 +118,12 @@ namespace etl typename etl::conditional::type>::type>::type>::type> - ::type>::type>::type>::type> - ::type>::type>::type>::type> - ::type>::type>::type>::type type; + etl::null_type>::type>::type>::type>::type> + ::type>::type>::type>::type> + ::type>::type>::type>::type> + ::type>::type>::type>::type type; - STATIC_ASSERT(!(etl::is_same::value), "Invalid id"); + STATIC_ASSERT(!(etl::is_same::value), "Invalid id"); }; //************************************ @@ -183,1255 +176,26 @@ namespace etl }; //*************************************************************************** - // Specialisation for 15 types. + // For 16 types. //*************************************************************************** template - struct type_id_lookup > - { - private: - - typedef __private_type_lookup__::null_type null_type; - - public: - - //************************************ - template - struct type_from_id - { - typedef - typename etl::conditional::type>::type>::type>::type>::type>::type>::type>::type> - ::type>::type>::type>::type>::type>::type>::type type; - - STATIC_ASSERT(!(etl::is_same::value), "Invalid id"); - }; - - //************************************ - enum - { - UNKNOWN = UINT_MAX - }; - - template - struct id_from_type - { - enum - { - value = - (unsigned int) etl::is_same::value ? T1::ID : - (unsigned int) etl::is_same::value ? T2::ID : - (unsigned int) etl::is_same::value ? T3::ID : - (unsigned int) etl::is_same::value ? T4::ID : - (unsigned int) etl::is_same::value ? T5::ID : - (unsigned int) etl::is_same::value ? T6::ID : - (unsigned int) etl::is_same::value ? T7::ID : - (unsigned int) etl::is_same::value ? T8::ID : - (unsigned int) etl::is_same::value ? T9::ID : - (unsigned int) etl::is_same::value ? T10::ID : - (unsigned int) etl::is_same::value ? T11::ID : - (unsigned int) etl::is_same::value ? T12::ID : - (unsigned int) etl::is_same::value ? T13::ID : - (unsigned int) etl::is_same::value ? T14::ID : - (unsigned int) etl::is_same::value ? T15::ID : - (unsigned int) UNKNOWN - }; - - STATIC_ASSERT(((unsigned int)value != (unsigned int)UNKNOWN), "Invalid type"); - }; - - //************************************ - template - static unsigned int get_id_from_type(const T&) - { - return get_id_from_type(); - } - - //************************************ - template - static unsigned int get_id_from_type() - { - return id_from_type::value; - } - }; - - //*************************************************************************** - // Specialisation for 14 types. - //*************************************************************************** - template - struct type_id_lookup, etl::type_id_pair<> > - { - private: - - typedef __private_type_lookup__::null_type null_type; - - public: - - //************************************ - template - struct type_from_id - { - typedef - typename etl::conditional::type>::type>::type>::type>::type>::type>::type>::type> - ::type>::type>::type>::type>::type>::type type; - - STATIC_ASSERT(!(etl::is_same::value), "Invalid id"); - }; - - //************************************ - enum - { - UNKNOWN = UINT_MAX - }; - - template - struct id_from_type - { - enum - { - value = - (unsigned int) etl::is_same::value ? T1::ID : - (unsigned int) etl::is_same::value ? T2::ID : - (unsigned int) etl::is_same::value ? T3::ID : - (unsigned int) etl::is_same::value ? T4::ID : - (unsigned int) etl::is_same::value ? T5::ID : - (unsigned int) etl::is_same::value ? T6::ID : - (unsigned int) etl::is_same::value ? T7::ID : - (unsigned int) etl::is_same::value ? T8::ID : - (unsigned int) etl::is_same::value ? T9::ID : - (unsigned int) etl::is_same::value ? T10::ID : - (unsigned int) etl::is_same::value ? T11::ID : - (unsigned int) etl::is_same::value ? T12::ID : - (unsigned int) etl::is_same::value ? T13::ID : - (unsigned int) etl::is_same::value ? T14::ID : - (unsigned int) UNKNOWN - }; - - STATIC_ASSERT(((unsigned int)value != (unsigned int)UNKNOWN), "Invalid type"); - }; - - //************************************ - template - static unsigned int get_id_from_type(const T&) - { - return get_id_from_type(); - } - - //************************************ - template - static unsigned int get_id_from_type() - { - return id_from_type::value; - } - }; - - //*************************************************************************** - // Specialisation for 13 types. - //*************************************************************************** - template - struct type_id_lookup, etl::type_id_pair<>, etl::type_id_pair<> > - { - private: - - typedef __private_type_lookup__::null_type null_type; - - public: - - //************************************ - template - struct type_from_id - { - typedef - typename etl::conditional::type>::type>::type>::type>::type>::type>::type>::type> - ::type>::type>::type>::type>::type type; - - STATIC_ASSERT(!(etl::is_same::value), "Invalid id"); - }; - - //************************************ - enum - { - UNKNOWN = UINT_MAX - }; - - template - struct id_from_type - { - enum - { - value = - (unsigned int) etl::is_same::value ? T1::ID : - (unsigned int) etl::is_same::value ? T2::ID : - (unsigned int) etl::is_same::value ? T3::ID : - (unsigned int) etl::is_same::value ? T4::ID : - (unsigned int) etl::is_same::value ? T5::ID : - (unsigned int) etl::is_same::value ? T6::ID : - (unsigned int) etl::is_same::value ? T7::ID : - (unsigned int) etl::is_same::value ? T8::ID : - (unsigned int) etl::is_same::value ? T9::ID : - (unsigned int) etl::is_same::value ? T10::ID : - (unsigned int) etl::is_same::value ? T11::ID : - (unsigned int) etl::is_same::value ? T12::ID : - (unsigned int) etl::is_same::value ? T13::ID : - (unsigned int) UNKNOWN - }; - - STATIC_ASSERT(((unsigned int)value != (unsigned int)UNKNOWN), "Invalid type"); - }; - - //************************************ - template - static unsigned int get_id_from_type(const T&) - { - return get_id_from_type(); - } - - //************************************ - template - static unsigned int get_id_from_type() - { - return id_from_type::value; - } - }; - - //*************************************************************************** - // Specialisation for 12 types. - //*************************************************************************** - template - struct type_id_lookup, - etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<> > - { - private: - - typedef __private_type_lookup__::null_type null_type; - - public: - - //************************************ - template - struct type_from_id - { - typedef - typename etl::conditional::type>::type>::type>::type>::type>::type>::type>::type> - ::type>::type>::type>::type type; - - STATIC_ASSERT(!(etl::is_same::value), "Invalid id"); - }; - - //************************************ - enum - { - UNKNOWN = UINT_MAX - }; - - template - struct id_from_type - { - enum - { - value = - (unsigned int) etl::is_same::value ? T1::ID : - (unsigned int) etl::is_same::value ? T2::ID : - (unsigned int) etl::is_same::value ? T3::ID : - (unsigned int) etl::is_same::value ? T4::ID : - (unsigned int) etl::is_same::value ? T5::ID : - (unsigned int) etl::is_same::value ? T6::ID : - (unsigned int) etl::is_same::value ? T7::ID : - (unsigned int) etl::is_same::value ? T8::ID : - (unsigned int) etl::is_same::value ? T9::ID : - (unsigned int) etl::is_same::value ? T10::ID : - (unsigned int) etl::is_same::value ? T11::ID : - (unsigned int) etl::is_same::value ? T12::ID : - (unsigned int) UNKNOWN - }; - - STATIC_ASSERT(((unsigned int)value != (unsigned int)UNKNOWN), "Invalid type"); - }; - - //************************************ - template - static unsigned int get_id_from_type(const T&) - { - return get_id_from_type(); - } - - //************************************ - template - static unsigned int get_id_from_type() - { - return id_from_type::value; - } - }; - - //*************************************************************************** - // Specialisation for 11 types. - //*************************************************************************** - template - struct type_id_lookup, etl::type_id_pair<>, - etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<> > - { - private: - - typedef __private_type_lookup__::null_type null_type; - - public: - - //************************************ - template - struct type_from_id - { - typedef - typename etl::conditional::type>::type>::type>::type>::type>::type>::type>::type> - ::type>::type>::type type; - - STATIC_ASSERT(!(etl::is_same::value), "Invalid id"); - }; - - //************************************ - enum - { - UNKNOWN = UINT_MAX - }; - - template - struct id_from_type - { - enum - { - value = - (unsigned int) etl::is_same::value ? T1::ID : - (unsigned int) etl::is_same::value ? T2::ID : - (unsigned int) etl::is_same::value ? T3::ID : - (unsigned int) etl::is_same::value ? T4::ID : - (unsigned int) etl::is_same::value ? T5::ID : - (unsigned int) etl::is_same::value ? T6::ID : - (unsigned int) etl::is_same::value ? T7::ID : - (unsigned int) etl::is_same::value ? T8::ID : - (unsigned int) etl::is_same::value ? T9::ID : - (unsigned int) etl::is_same::value ? T10::ID : - (unsigned int) etl::is_same::value ? T11::ID : - (unsigned int) UNKNOWN - }; - - STATIC_ASSERT(((unsigned int)value != (unsigned int)UNKNOWN), "Invalid type"); - }; - - //************************************ - template - static unsigned int get_id_from_type(const T&) - { - return get_id_from_type(); - } - - //************************************ - template - static unsigned int get_id_from_type() - { - return id_from_type::value; - } - }; - - //*************************************************************************** - // Specialisation for 10 types. - //*************************************************************************** - template - struct type_id_lookup, etl::type_id_pair<>, etl::type_id_pair<>, - etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<> > - { - private: - - typedef __private_type_lookup__::null_type null_type; - - public: - - //************************************ - template - struct type_from_id - { - typedef - typename etl::conditional::type>::type>::type>::type>::type>::type>::type>::type> - ::type>::type type; - - STATIC_ASSERT(!(etl::is_same::value), "Invalid id"); - }; - - //************************************ - enum - { - UNKNOWN = UINT_MAX - }; - - template - struct id_from_type - { - enum - { - value = - (unsigned int) etl::is_same::value ? T1::ID : - (unsigned int) etl::is_same::value ? T2::ID : - (unsigned int) etl::is_same::value ? T3::ID : - (unsigned int) etl::is_same::value ? T4::ID : - (unsigned int) etl::is_same::value ? T5::ID : - (unsigned int) etl::is_same::value ? T6::ID : - (unsigned int) etl::is_same::value ? T7::ID : - (unsigned int) etl::is_same::value ? T8::ID : - (unsigned int) etl::is_same::value ? T9::ID : - (unsigned int) etl::is_same::value ? T10::ID : - (unsigned int) UNKNOWN - }; - - STATIC_ASSERT(((unsigned int)value != (unsigned int)UNKNOWN), "Invalid type"); - }; - - //************************************ - template - static unsigned int get_id_from_type(const T&) - { - return get_id_from_type(); - } - - //************************************ - template - static unsigned int get_id_from_type() - { - return id_from_type::value; - } - }; - - //*************************************************************************** - // Specialisation for 9 types. - //*************************************************************************** - template - struct type_id_lookup, etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<>, - etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<> > - { - private: - - typedef __private_type_lookup__::null_type null_type; - - public: - - //************************************ - template - struct type_from_id - { - typedef - typename etl::conditional::type>::type>::type>::type>::type>::type>::type>::type> - ::type type; - - STATIC_ASSERT(!(etl::is_same::value), "Invalid id"); - }; - - //************************************ - enum - { - UNKNOWN = UINT_MAX - }; - - template - struct id_from_type - { - enum - { - value = - (unsigned int) etl::is_same::value ? T1::ID : - (unsigned int) etl::is_same::value ? T2::ID : - (unsigned int) etl::is_same::value ? T3::ID : - (unsigned int) etl::is_same::value ? T4::ID : - (unsigned int) etl::is_same::value ? T5::ID : - (unsigned int) etl::is_same::value ? T6::ID : - (unsigned int) etl::is_same::value ? T7::ID : - (unsigned int) etl::is_same::value ? T8::ID : - (unsigned int) etl::is_same::value ? T9::ID : - (unsigned int) UNKNOWN - }; - - STATIC_ASSERT(((unsigned int)value != (unsigned int)UNKNOWN), "Invalid type"); - }; - - //************************************ - template - static unsigned int get_id_from_type(const T&) - { - return get_id_from_type(); - } - - //************************************ - template - static unsigned int get_id_from_type() - { - return id_from_type::value; - } - }; - - //*************************************************************************** - // Specialisation for 8 types. - //*************************************************************************** - template - struct type_id_lookup, - etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<>, - etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<> > - { - private: - - typedef __private_type_lookup__::null_type null_type; - - public: - - //************************************ - template - struct type_from_id - { - typedef - typename etl::conditional::type>::type>::type>::type>::type>::type>::type>::type type; - - - STATIC_ASSERT(!(etl::is_same::value), "Invalid id"); - }; - - //************************************ - enum - { - UNKNOWN = UINT_MAX - }; - - template - struct id_from_type - { - enum - { - value = - (unsigned int) etl::is_same::value ? T1::ID : - (unsigned int) etl::is_same::value ? T2::ID : - (unsigned int) etl::is_same::value ? T3::ID : - (unsigned int) etl::is_same::value ? T4::ID : - (unsigned int) etl::is_same::value ? T5::ID : - (unsigned int) etl::is_same::value ? T6::ID : - (unsigned int) etl::is_same::value ? T7::ID : - (unsigned int) etl::is_same::value ? T8::ID : - (unsigned int) UNKNOWN - }; - - STATIC_ASSERT(((unsigned int)value != (unsigned int)UNKNOWN), "Invalid type"); - }; - - //************************************ - template - static unsigned int get_id_from_type(const T&) - { - return get_id_from_type(); - } - - //************************************ - template - static unsigned int get_id_from_type() - { - return id_from_type::value; - } - }; - - //*************************************************************************** - // Specialisation for 7 types. - //*************************************************************************** - template - struct type_id_lookup, etl::type_id_pair<>, - etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<>, - etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<> > - { - private: - - typedef __private_type_lookup__::null_type null_type; - - public: - - //************************************ - template - struct type_from_id - { - typedef - typename etl::conditional::type>::type>::type>::type>::type>::type>::type type; - - STATIC_ASSERT(!(etl::is_same::value), "Invalid id"); - }; - - //************************************ - enum - { - UNKNOWN = UINT_MAX - }; - - template - struct id_from_type - { - enum - { - value = - (unsigned int) etl::is_same::value ? T1::ID : - (unsigned int) etl::is_same::value ? T2::ID : - (unsigned int) etl::is_same::value ? T3::ID : - (unsigned int) etl::is_same::value ? T4::ID : - (unsigned int) etl::is_same::value ? T5::ID : - (unsigned int) etl::is_same::value ? T6::ID : - (unsigned int) etl::is_same::value ? T7::ID : - (unsigned int) UNKNOWN - }; - - STATIC_ASSERT(((unsigned int)value != (unsigned int)UNKNOWN), "Invalid type"); - }; - - //************************************ - template - static unsigned int get_id_from_type(const T&) - { - return get_id_from_type(); - } - - //************************************ - template - static unsigned int get_id_from_type() - { - return id_from_type::value; - } - }; - - //*************************************************************************** - // Specialisation for 6 types. - //*************************************************************************** - template - struct type_id_lookup, etl::type_id_pair<>, etl::type_id_pair<>, - etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<>, - etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<> > - { - private: - - typedef __private_type_lookup__::null_type null_type; - - public: - - //************************************ - template - struct type_from_id - { - typedef - typename etl::conditional::type>::type>::type>::type>::type>::type type; - - STATIC_ASSERT(!(etl::is_same::value), "Invalid id"); - }; - - //************************************ - enum - { - UNKNOWN = UINT_MAX - }; - - template - struct id_from_type - { - enum - { - value = - (unsigned int) etl::is_same::value ? T1::ID : - (unsigned int) etl::is_same::value ? T2::ID : - (unsigned int) etl::is_same::value ? T3::ID : - (unsigned int) etl::is_same::value ? T4::ID : - (unsigned int) etl::is_same::value ? T5::ID : - (unsigned int) etl::is_same::value ? T6::ID : - (unsigned int) UNKNOWN - }; - - STATIC_ASSERT(((unsigned int)value != (unsigned int)UNKNOWN), "Invalid type"); - }; - - //************************************ - template - static unsigned int get_id_from_type(const T&) - { - return get_id_from_type(); - } - - //************************************ - template - static unsigned int get_id_from_type() - { - return id_from_type::value; - } - }; - - //*************************************************************************** - // Specialisation for 5 types. - //*************************************************************************** - template - struct type_id_lookup, etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<>, - etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<>, - etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<> > - { - private: - - typedef __private_type_lookup__::null_type null_type; - - public: - - //************************************ - template - struct type_from_id - { - typedef - typename etl::conditional::type>::type>::type>::type>::type type; - - STATIC_ASSERT(!(etl::is_same::value), "Invalid id"); - }; - - //************************************ - enum - { - UNKNOWN = UINT_MAX - }; - - template - struct id_from_type - { - enum - { - value = - (unsigned int) etl::is_same::value ? T1::ID : - (unsigned int) etl::is_same::value ? T2::ID : - (unsigned int) etl::is_same::value ? T3::ID : - (unsigned int) etl::is_same::value ? T4::ID : - (unsigned int) etl::is_same::value ? T5::ID : - (unsigned int) UNKNOWN - }; - - STATIC_ASSERT(((unsigned int)value != (unsigned int)UNKNOWN), "Invalid type"); - }; - - //************************************ - template - static unsigned int get_id_from_type(const T&) - { - return get_id_from_type(); - } - - //************************************ - template - static unsigned int get_id_from_type() - { - return id_from_type::value; - } - }; - - //*************************************************************************** - // Specialisation for 4 types. - //*************************************************************************** - template - struct type_id_lookup, - etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<>, - etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<>, - etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<> > - { - private: - - typedef __private_type_lookup__::null_type null_type; - - public: - - //************************************ - template - struct type_from_id - { - typedef - typename etl::conditional::type>::type>::type>::type type; - - STATIC_ASSERT(!(etl::is_same::value), "Invalid id"); - }; - - //************************************ - enum - { - UNKNOWN = UINT_MAX - }; - - template - struct id_from_type - { - enum - { - value = - (unsigned int) etl::is_same::value ? T1::ID : - (unsigned int) etl::is_same::value ? T2::ID : - (unsigned int) etl::is_same::value ? T3::ID : - (unsigned int) etl::is_same::value ? T4::ID : - (unsigned int) UNKNOWN - }; - - STATIC_ASSERT(((unsigned int)value != (unsigned int)UNKNOWN), "Invalid type"); - }; - - //************************************ - template - static unsigned int get_id_from_type(const T&) - { - return get_id_from_type(); - } - - //************************************ - template - static unsigned int get_id_from_type() - { - return id_from_type::value; - } - }; - - //*************************************************************************** - // Specialisation for 3 types. - //*************************************************************************** - template - struct type_id_lookup, etl::type_id_pair<>, - etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<>, - etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<>, - etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<> > - { - private: - - typedef __private_type_lookup__::null_type null_type; - - public: - - //************************************ - template - struct type_from_id - { - typedef - typename etl::conditional::type>::type>::type type; - - STATIC_ASSERT(!(etl::is_same::value), "Invalid id"); - }; - - //************************************ - enum - { - UNKNOWN = UINT_MAX - }; - - template - struct id_from_type - { - enum - { - value = - (unsigned int) etl::is_same::value ? T1::ID : - (unsigned int) etl::is_same::value ? T2::ID : - (unsigned int) etl::is_same::value ? T3::ID : - (unsigned int) UNKNOWN - }; - - STATIC_ASSERT(((unsigned int)value != (unsigned int)UNKNOWN), "Invalid type"); - }; - - //************************************ - template - static unsigned int get_id_from_type(const T&) - { - return get_id_from_type(); - } - - //************************************ - template - static unsigned int get_id_from_type() - { - return id_from_type::value; - } - }; - - //*************************************************************************** - // Specialisation for 2 types. - //*************************************************************************** - template - struct type_id_lookup, etl::type_id_pair<>, etl::type_id_pair<>, - etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<>, - etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<>, - etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<> > - { - private: - - typedef __private_type_lookup__::null_type null_type; - - public: - - //************************************ - template - struct type_from_id - { - typedef - typename etl::conditional::type>::type type; - - STATIC_ASSERT(!(etl::is_same::value), "Invalid id"); - }; - - //************************************ - enum - { - UNKNOWN = UINT_MAX - }; - - template - struct id_from_type - { - enum - { - value = - (unsigned int) etl::is_same::value ? T1::ID : - (unsigned int) etl::is_same::value ? T2::ID : - (unsigned int) UNKNOWN - }; - - STATIC_ASSERT(((unsigned int)value != (unsigned int)UNKNOWN), "Invalid type"); - }; - - //************************************ - template - static unsigned int get_id_from_type(const T&) - { - return get_id_from_type(); - } - - //************************************ - template - static unsigned int get_id_from_type() - { - return id_from_type::value; - } - }; - - //*************************************************************************** - // Specialisation for 1 type. - //*************************************************************************** - template - struct type_id_lookup, etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<>, - etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<>, - etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<>, - etl::type_id_pair<>, etl::type_id_pair<>, etl::type_id_pair<> > - { - private: - - typedef __private_type_lookup__::null_type null_type; - - public: - - //************************************ - template - struct type_from_id - { - typedef - typename etl::conditional::type type; - - STATIC_ASSERT(!(etl::is_same::value), "Invalid id"); - }; - - //************************************ - enum - { - UNKNOWN = UINT_MAX - }; - - template - struct id_from_type - { - enum - { - value = - (unsigned int) etl::is_same::value ? T1::ID : - (unsigned int) UNKNOWN - }; - - STATIC_ASSERT(((unsigned int)value != (unsigned int)UNKNOWN), "Invalid type"); - }; - - //************************************ - template - static unsigned int get_id_from_type(const T&) - { - return get_id_from_type(); - } - - //************************************ - template - static unsigned int get_id_from_type() - { - return id_from_type::value; - } - }; - //*************************************************************************** - // Default for 16 types. - //*************************************************************************** - template , - typename T3 = etl::type_type_pair<>, - typename T4 = etl::type_type_pair<>, - typename T5 = etl::type_type_pair<>, - typename T6 = etl::type_type_pair<>, - typename T7 = etl::type_type_pair<>, - typename T8 = etl::type_type_pair<>, - typename T9 = etl::type_type_pair<>, - typename T10 = etl::type_type_pair<>, - typename T11 = etl::type_type_pair<>, - typename T12 = etl::type_type_pair<>, - typename T13 = etl::type_type_pair<>, - typename T14 = etl::type_type_pair<>, - typename T15 = etl::type_type_pair<>, - typename T16 = etl::type_type_pair<> > + typename T2 = etl::type_type_pair, + typename T3 = etl::type_type_pair, + typename T4 = etl::type_type_pair, + typename T5 = etl::type_type_pair, + typename T6 = etl::type_type_pair, + typename T7 = etl::type_type_pair, + typename T8 = etl::type_type_pair, + typename T9 = etl::type_type_pair, + typename T10 = etl::type_type_pair, + typename T11 = etl::type_type_pair, + typename T12 = etl::type_type_pair, + typename T13 = etl::type_type_pair, + typename T14 = etl::type_type_pair, + typename T15 = etl::type_type_pair, + typename T16 = etl::type_type_pair > struct type_type_lookup { - private: - - typedef __private_type_lookup__::null_type null_type; - public: //************************************ @@ -1455,635 +219,10 @@ namespace etl typename etl::conditional::value, typename T14::type2, typename etl::conditional::value, typename T15::type2, typename etl::conditional::value, typename T16::type2, - null_type>::type>::type>::type>::type>::type>::type>::type>::type> - ::type>::type>::type>::type>::type>::type>::type>::type type; + etl::null_type>::type>::type>::type>::type>::type>::type>::type>::type> + ::type>::type>::type>::type>::type>::type>::type>::type type; - STATIC_ASSERT(!(etl::is_same::value), "Invalid type"); - }; - }; - - //*************************************************************************** - // Specialisation for 15 types. - //*************************************************************************** - template - struct type_type_lookup > - { - private: - - typedef __private_type_lookup__::null_type null_type; - - public: - - //************************************ - template - struct type_from_type - { - typedef - typename etl::conditional::value, typename T1::type2, - typename etl::conditional::value, typename T2::type2, - typename etl::conditional::value, typename T3::type2, - typename etl::conditional::value, typename T4::type2, - typename etl::conditional::value, typename T5::type2, - typename etl::conditional::value, typename T6::type2, - typename etl::conditional::value, typename T7::type2, - typename etl::conditional::value, typename T8::type2, - typename etl::conditional::value, typename T9::type2, - typename etl::conditional::value, typename T10::type2, - typename etl::conditional::value, typename T11::type2, - typename etl::conditional::value, typename T12::type2, - typename etl::conditional::value, typename T13::type2, - typename etl::conditional::value, typename T14::type2, - typename etl::conditional::value, typename T15::type2, - null_type>::type>::type>::type>::type>::type>::type>::type>::type> - ::type>::type>::type>::type>::type>::type>::type type; - - STATIC_ASSERT(!(etl::is_same::value), "Invalid type"); - }; - }; - - //*************************************************************************** - // Specialisation for 14 types. - //*************************************************************************** - template - struct type_type_lookup, etl::type_type_pair<> > - { - private: - - typedef __private_type_lookup__::null_type null_type; - - public: - - //************************************ - template - struct type_from_type - { - typedef - typename etl::conditional::value, typename T1::type2, - typename etl::conditional::value, typename T2::type2, - typename etl::conditional::value, typename T3::type2, - typename etl::conditional::value, typename T4::type2, - typename etl::conditional::value, typename T5::type2, - typename etl::conditional::value, typename T6::type2, - typename etl::conditional::value, typename T7::type2, - typename etl::conditional::value, typename T8::type2, - typename etl::conditional::value, typename T9::type2, - typename etl::conditional::value, typename T10::type2, - typename etl::conditional::value, typename T11::type2, - typename etl::conditional::value, typename T12::type2, - typename etl::conditional::value, typename T13::type2, - typename etl::conditional::value, typename T14::type2, - null_type>::type>::type>::type>::type>::type>::type>::type>::type> - ::type>::type>::type>::type>::type>::type type; - - STATIC_ASSERT(!(etl::is_same::value), "Invalid type"); - }; - }; - - //*************************************************************************** - // Specialisation for 13 types. - //*************************************************************************** - template - struct type_type_lookup, etl::type_type_pair<>, etl::type_type_pair<> > - { - private: - - typedef __private_type_lookup__::null_type null_type; - - public: - - //************************************ - template - struct type_from_type - { - typedef - typename etl::conditional::value, typename T1::type2, - typename etl::conditional::value, typename T2::type2, - typename etl::conditional::value, typename T3::type2, - typename etl::conditional::value, typename T4::type2, - typename etl::conditional::value, typename T5::type2, - typename etl::conditional::value, typename T6::type2, - typename etl::conditional::value, typename T7::type2, - typename etl::conditional::value, typename T8::type2, - typename etl::conditional::value, typename T9::type2, - typename etl::conditional::value, typename T10::type2, - typename etl::conditional::value, typename T11::type2, - typename etl::conditional::value, typename T12::type2, - typename etl::conditional::value, typename T13::type2, - null_type>::type>::type>::type>::type>::type>::type>::type>::type> - ::type>::type>::type>::type>::type type; - - STATIC_ASSERT(!(etl::is_same::value), "Invalid type"); - }; - }; - - //*************************************************************************** - // Specialisation for 12 types. - //*************************************************************************** - template - struct type_type_lookup, - etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<> > - { - private: - - typedef __private_type_lookup__::null_type null_type; - - public: - - //************************************ - template - struct type_from_type - { - typedef - typename etl::conditional::value, typename T1::type2, - typename etl::conditional::value, typename T2::type2, - typename etl::conditional::value, typename T3::type2, - typename etl::conditional::value, typename T4::type2, - typename etl::conditional::value, typename T5::type2, - typename etl::conditional::value, typename T6::type2, - typename etl::conditional::value, typename T7::type2, - typename etl::conditional::value, typename T8::type2, - typename etl::conditional::value, typename T9::type2, - typename etl::conditional::value, typename T10::type2, - typename etl::conditional::value, typename T11::type2, - typename etl::conditional::value, typename T12::type2, - null_type>::type>::type>::type>::type>::type>::type>::type>::type> - ::type>::type>::type>::type type; - - STATIC_ASSERT(!(etl::is_same::value), "Invalid type"); - }; - }; - - //*************************************************************************** - // Specialisation for 11 types. - //*************************************************************************** - template - struct type_type_lookup, etl::type_type_pair<>, - etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<> > - { - private: - - typedef __private_type_lookup__::null_type null_type; - - public: - - //************************************ - template - struct type_from_type - { - typedef - typename etl::conditional::value, typename T1::type2, - typename etl::conditional::value, typename T2::type2, - typename etl::conditional::value, typename T3::type2, - typename etl::conditional::value, typename T4::type2, - typename etl::conditional::value, typename T5::type2, - typename etl::conditional::value, typename T6::type2, - typename etl::conditional::value, typename T7::type2, - typename etl::conditional::value, typename T8::type2, - typename etl::conditional::value, typename T9::type2, - typename etl::conditional::value, typename T10::type2, - typename etl::conditional::value, typename T11::type2, - null_type>::type>::type>::type>::type>::type>::type>::type>::type> - ::type>::type>::type type; - - STATIC_ASSERT(!(etl::is_same::value), "Invalid type"); - }; - }; - - //*************************************************************************** - // Specialisation for 10 types. - //*************************************************************************** - template - struct type_type_lookup, etl::type_type_pair<>, etl::type_type_pair<>, - etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<> > - { - private: - - typedef __private_type_lookup__::null_type null_type; - - public: - - //************************************ - template - struct type_from_type - { - typedef - typename etl::conditional::value, typename T1::type2, - typename etl::conditional::value, typename T2::type2, - typename etl::conditional::value, typename T3::type2, - typename etl::conditional::value, typename T4::type2, - typename etl::conditional::value, typename T5::type2, - typename etl::conditional::value, typename T6::type2, - typename etl::conditional::value, typename T7::type2, - typename etl::conditional::value, typename T8::type2, - typename etl::conditional::value, typename T9::type2, - typename etl::conditional::value, typename T10::type2, - null_type>::type>::type>::type>::type>::type>::type>::type>::type> - ::type>::type type; - - STATIC_ASSERT(!(etl::is_same::value), "Invalid type"); - }; - }; - - //*************************************************************************** - // Specialisation for 9 types. - //*************************************************************************** - template - struct type_type_lookup, etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<>, - etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<> > - { - private: - - typedef __private_type_lookup__::null_type null_type; - - public: - - //************************************ - template - struct type_from_type - { - typedef - typename etl::conditional::value, typename T1::type2, - typename etl::conditional::value, typename T2::type2, - typename etl::conditional::value, typename T3::type2, - typename etl::conditional::value, typename T4::type2, - typename etl::conditional::value, typename T5::type2, - typename etl::conditional::value, typename T6::type2, - typename etl::conditional::value, typename T7::type2, - typename etl::conditional::value, typename T8::type2, - typename etl::conditional::value, typename T9::type2, - null_type>::type>::type>::type>::type>::type>::type>::type>::type> - ::type type; - - STATIC_ASSERT(!(etl::is_same::value), "Invalid type"); - }; - }; - - //*************************************************************************** - // Specialisation for 8 types. - //*************************************************************************** - template - struct type_type_lookup, - etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<>, - etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<> > - { - private: - - typedef __private_type_lookup__::null_type null_type; - - public: - - //************************************ - template - struct type_from_type - { - typedef - typename etl::conditional::value, typename T1::type2, - typename etl::conditional::value, typename T2::type2, - typename etl::conditional::value, typename T3::type2, - typename etl::conditional::value, typename T4::type2, - typename etl::conditional::value, typename T5::type2, - typename etl::conditional::value, typename T6::type2, - typename etl::conditional::value, typename T7::type2, - typename etl::conditional::value, typename T8::type2, - null_type>::type>::type>::type>::type>::type>::type>::type>::type type; - - STATIC_ASSERT(!(etl::is_same::value), "Invalid type"); - }; - }; - - //*************************************************************************** - // Specialisation for 7 types. - //*************************************************************************** - template - struct type_type_lookup, etl::type_type_pair<>, - etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<>, - etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<> > - { - private: - - typedef __private_type_lookup__::null_type null_type; - - public: - - //************************************ - template - struct type_from_type - { - typedef - typename etl::conditional::value, typename T1::type2, - typename etl::conditional::value, typename T2::type2, - typename etl::conditional::value, typename T3::type2, - typename etl::conditional::value, typename T4::type2, - typename etl::conditional::value, typename T5::type2, - typename etl::conditional::value, typename T6::type2, - typename etl::conditional::value, typename T7::type2, - null_type>::type>::type>::type>::type>::type>::type>::type type; - - STATIC_ASSERT(!(etl::is_same::value), "Invalid type"); - }; - }; - - //*************************************************************************** - // Specialisation for 6 types. - //*************************************************************************** - template - struct type_type_lookup, etl::type_type_pair<>, etl::type_type_pair<>, - etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<>, - etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<> > - { - private: - - typedef __private_type_lookup__::null_type null_type; - - public: - - //************************************ - template - struct type_from_type - { - typedef - typename etl::conditional::value, typename T1::type2, - typename etl::conditional::value, typename T2::type2, - typename etl::conditional::value, typename T3::type2, - typename etl::conditional::value, typename T4::type2, - typename etl::conditional::value, typename T5::type2, - typename etl::conditional::value, typename T6::type2, - null_type>::type>::type>::type>::type>::type>::type type; - - STATIC_ASSERT(!(etl::is_same::value), "Invalid type"); - }; - }; - - //*************************************************************************** - // Specialisation for 5 types. - //*************************************************************************** - template - struct type_type_lookup, etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<>, - etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<>, - etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<> > - { - private: - - typedef __private_type_lookup__::null_type null_type; - - public: - - //************************************ - template - struct type_from_type - { - typedef - typename etl::conditional::value, typename T1::type2, - typename etl::conditional::value, typename T2::type2, - typename etl::conditional::value, typename T3::type2, - typename etl::conditional::value, typename T4::type2, - typename etl::conditional::value, typename T5::type2, - null_type>::type>::type>::type>::type>::type type; - - STATIC_ASSERT(!(etl::is_same::value), "Invalid type"); - }; - }; - - //*************************************************************************** - // Specialisation for 4 types. - //*************************************************************************** - template - struct type_type_lookup, - etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<>, - etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<>, - etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<> > - { - private: - - typedef __private_type_lookup__::null_type null_type; - - public: - - //************************************ - template - struct type_from_type - { - typedef - typename etl::conditional::value, typename T1::type2, - typename etl::conditional::value, typename T2::type2, - typename etl::conditional::value, typename T3::type2, - typename etl::conditional::value, typename T4::type2, - null_type>::type>::type>::type>::type type; - - STATIC_ASSERT(!(etl::is_same::value), "Invalid type"); - }; - }; - - //*************************************************************************** - // Specialisation for 3 types. - //*************************************************************************** - template - struct type_type_lookup, etl::type_type_pair<>, - etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<>, - etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<>, - etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<> > - { - private: - - typedef __private_type_lookup__::null_type null_type; - - public: - - //************************************ - template - struct type_from_type - { - typedef - typename etl::conditional::value, typename T1::type2, - typename etl::conditional::value, typename T2::type2, - typename etl::conditional::value, typename T3::type2, - null_type>::type>::type>::type type; - - STATIC_ASSERT(!(etl::is_same::value), "Invalid type"); - }; - }; - - //*************************************************************************** - // Specialisation for 2 types. - //*************************************************************************** - template - struct type_type_lookup, etl::type_type_pair<>, etl::type_type_pair<>, - etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<>, - etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<>, - etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<> > - { - private: - - typedef __private_type_lookup__::null_type null_type; - - public: - - //************************************ - template - struct type_from_type - { - typedef - typename etl::conditional::value, typename T1::type2, - typename etl::conditional::value, typename T2::type2, - null_type>::type>::type type; - - STATIC_ASSERT(!(etl::is_same::value), "Invalid type"); - }; - }; - - //*************************************************************************** - // Specialisation for 1 type. - //*************************************************************************** - template - struct type_type_lookup, etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<>, - etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<>, - etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<>, - etl::type_type_pair<>, etl::type_type_pair<>, etl::type_type_pair<> > - { - private: - - typedef __private_type_lookup__::null_type null_type; - - public: - - //************************************ - template - struct type_from_type - { - typedef - typename etl::conditional::value, typename T1::type2, - null_type>::type type; - - STATIC_ASSERT(!(etl::is_same::value), "Invalid type"); + STATIC_ASSERT(!(etl::is_same::value), "Invalid type"); }; }; } diff --git a/src/type_lookup_generator.h b/src/type_lookup_generator.h index 7e363cb0..851bfec3 100644 --- a/src/type_lookup_generator.h +++ b/src/type_lookup_generator.h @@ -60,15 +60,12 @@ cog.outl("//******************************************************************** namespace etl { - namespace __private_type_lookup__ - { - struct null_type {}; - } + struct null_type {}; //*************************************************************************** /// The type/id pair type to use for type/id lookup template parameters. //*************************************************************************** - template + template struct type_id_pair { typedef T type; @@ -82,7 +79,7 @@ namespace etl //*************************************************************************** /// The type/type pair type to use for type/type lookup template parameters. //*************************************************************************** - template + template struct type_type_pair { typedef T1 type1; @@ -92,28 +89,24 @@ namespace etl /*[[[cog import cog cog.outl("//***************************************************************************") - cog.outl("// Default for %s types." % int(NTypes)) + cog.outl("// For %s types." % int(NTypes)) cog.outl("//***************************************************************************") cog.outl("template ," %n) - cog.outl(" typename T%s = etl::type_id_pair<> >" %NTypes) + cog.outl(" typename T%s = etl::type_id_pair," %(n, n)) + cog.outl(" typename T%s = etl::type_id_pair >" %(NTypes, NTypes)) cog.outl("struct type_id_lookup") cog.outl("{") - cog.outl("private:") - cog.outl("") - cog.outl(" typedef __private_type_lookup__::null_type null_type;") - cog.outl("") cog.outl("public:") cog.outl("") cog.outl(" //************************************") - cog.outl(" template ") + cog.outl(" template ") cog.outl(" struct type_from_id") cog.outl(" {") cog.outl(" typedef ") for n in range(1, int(NTypes) + 1): cog.outl(" typename etl::conditional") + cog.out(" etl::null_type>") for n in range(1, int(NTypes) + 1): if n == int(NTypes): cog.outl("::type type;") @@ -122,9 +115,9 @@ namespace etl if n % 4 == 0: if n != int(NTypes): cog.outl("") - cog.out(" ") + cog.out(" ") cog.outl("") - cog.outl(" STATIC_ASSERT(!(etl::is_same::value), \"Invalid id\");") + cog.outl(" STATIC_ASSERT(!(etl::is_same::value), \"Invalid id\");") cog.outl(" };") cog.outl("") cog.outl(" //************************************") @@ -161,108 +154,16 @@ namespace etl cog.outl(" return id_from_type::value;") cog.outl(" }") cog.outl("};") - for n in range(int(NTypes) - 1, 0, -1): - cog.outl("") - cog.outl("//***************************************************************************") - if n == 1: - cog.outl("// Specialisation for %d type." % n) - else: - cog.outl("// Specialisation for %d types." % n) - cog.outl("//***************************************************************************") - cog.out("template <") - for t in range(1, n + 1): - cog.out("typename T%s" %t) - if t == n: - cog.outl(">") - else: - cog.outl(",") - cog.out(" ") - cog.out("struct type_id_lookup<") - for t in range(1, n + 1): - cog.out("T%s, " %t) - if t % 4 == 0: - cog.outl("") - cog.out(" ") - for t in range(n, int(NTypes) - 1): - cog.out("etl::type_id_pair<>, ") - if t % 4 == 0: - cog.outl("") - cog.out(" ") - cog.outl("etl::type_id_pair<> >") - cog.outl("{") - cog.outl("private:") - cog.outl("") - cog.outl(" typedef __private_type_lookup__::null_type null_type;") - cog.outl("") - cog.outl("public:") - cog.outl("") - cog.outl(" //************************************") - cog.outl(" template ") - cog.outl(" struct type_from_id") - cog.outl(" {") - cog.outl(" typedef ") - for t in range(1, n + 1): - cog.outl(" typename etl::conditional") - for t in range(1, n + 1): - if t == n: - cog.outl("::type type;") - else: - cog.out("::type>") - if t % 8 == 0: - cog.outl("") - cog.out(" ") - cog.outl("") - cog.outl(" STATIC_ASSERT(!(etl::is_same::value), \"Invalid id\");") - cog.outl(" };") - cog.outl("") - cog.outl(" //************************************") - cog.outl(" enum") - cog.outl(" {") - cog.outl(" UNKNOWN = UINT_MAX") - cog.outl(" };") - cog.outl("") - cog.outl(" template ") - cog.outl(" struct id_from_type") - cog.outl(" {") - cog.outl(" enum") - cog.outl(" {") - cog.outl(" value =") - for t in range(1, n + 1) : - cog.outl(" (unsigned int) etl::is_same::value ? T%s::ID :" % (t, t)) - cog.outl(" (unsigned int) UNKNOWN") - cog.outl(" };") - cog.outl("") - cog.outl(" STATIC_ASSERT(((unsigned int)value != (unsigned int)UNKNOWN), \"Invalid type\");") - cog.outl(" };") - cog.outl("") - cog.outl(" //************************************") - cog.outl(" template ") - cog.outl(" static unsigned int get_id_from_type(const T&)") - cog.outl(" {") - cog.outl(" return get_id_from_type();") - cog.outl(" }") - cog.outl("") - cog.outl(" //************************************") - cog.outl(" template ") - cog.outl(" static unsigned int get_id_from_type()") - cog.outl(" {") - cog.outl(" return id_from_type::value;") - cog.outl(" }") - cog.outl("};") + cog.outl("") cog.outl("//***************************************************************************") - cog.outl("// Default for %s types." % int(NTypes)) + cog.outl("// For %s types." % int(NTypes)) cog.outl("//***************************************************************************") cog.outl("template ," %n) - cog.outl(" typename T%s = etl::type_type_pair<> >" %NTypes) + cog.outl(" typename T%s = etl::type_type_pair," %n) + cog.outl(" typename T%s = etl::type_type_pair >" %NTypes) cog.outl("struct type_type_lookup") cog.outl("{") - cog.outl("private:") - cog.outl("") - cog.outl(" typedef __private_type_lookup__::null_type null_type;") - cog.outl("") cog.outl("public:") cog.outl("") cog.outl(" //************************************") @@ -272,7 +173,7 @@ namespace etl cog.outl(" typedef ") for n in range(1, int(NTypes) + 1): cog.outl(" typename etl::conditional::value, typename T%s::type2," %(n, n)) - cog.out(" null_type>") + cog.out(" etl::null_type>") for n in range(1, int(NTypes) + 1): if n == int(NTypes): cog.outl("::type type;") @@ -281,66 +182,11 @@ namespace etl if n % 8 == 0: if n != int(NTypes): cog.outl("") - cog.out(" ") + cog.out(" ") cog.outl("") - cog.outl(" STATIC_ASSERT(!(etl::is_same::value), \"Invalid type\");") + cog.outl(" STATIC_ASSERT(!(etl::is_same::value), \"Invalid type\");") cog.outl(" };") cog.outl("};") - for n in range(int(NTypes) - 1, 0, -1): - cog.outl("") - cog.outl("//***************************************************************************") - if n == 1: - cog.outl("// Specialisation for %d type." % n) - else: - cog.outl("// Specialisation for %d types." % n) - cog.outl("//***************************************************************************") - cog.out("template <") - for t in range(1, n + 1): - cog.out("typename T%s" %t) - if t == n: - cog.outl(">") - else: - cog.outl(",") - cog.out(" ") - cog.out("struct type_type_lookup<") - for t in range(1, n + 1): - cog.out("T%s, " %t) - if t % 4 == 0: - cog.outl("") - cog.out(" ") - for t in range(n, int(NTypes) - 1): - cog.out("etl::type_type_pair<>, ") - if t % 4 == 0: - cog.outl("") - cog.out(" ") - cog.outl("etl::type_type_pair<> >") - cog.outl("{") - cog.outl("private:") - cog.outl("") - cog.outl(" typedef __private_type_lookup__::null_type null_type;") - cog.outl("") - cog.outl("public:") - cog.outl("") - cog.outl(" //************************************") - cog.outl(" template ") - cog.outl(" struct type_from_type") - cog.outl(" {") - cog.outl(" typedef ") - for t in range(1, n + 1): - cog.outl(" typename etl::conditional::value, typename T%s::type2," %(t, t)) - cog.out(" null_type>") - for t in range(1, n + 1): - if t == n: - cog.outl("::type type;") - else: - cog.out("::type>") - if t % 8 == 0: - cog.outl("") - cog.out(" ") - cog.outl("") - cog.outl(" STATIC_ASSERT(!(etl::is_same::value), \"Invalid type\");") - cog.outl(" };") - cog.outl("};") ]]]*/ /*[[[end]]]*/ } From ece1174861be4e113578a502249fc607dda07402 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Wed, 13 Dec 2017 10:44:41 +0000 Subject: [PATCH 11/24] Added clear() --- src/debug_count.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/debug_count.h b/src/debug_count.h index 45c55414..ab80a4c4 100644 --- a/src/debug_count.h +++ b/src/debug_count.h @@ -93,6 +93,11 @@ namespace etl return count; } + inline void clear() + { + count = 0; + } + private: int32_t count; @@ -129,6 +134,11 @@ namespace etl { return 0; } + + inline void clear() + { + } + #endif }; } From 5752151b54e7ed9f7d7404dd24a816323db9c32e Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Wed, 13 Dec 2017 10:45:33 +0000 Subject: [PATCH 12/24] Changes from due to type_lookup.h updates --- src/factory.h | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/factory.h b/src/factory.h index b4d62b2c..b7c8c6db 100644 --- a/src/factory.h +++ b/src/factory.h @@ -81,21 +81,21 @@ namespace etl //*************************************************************************** template , - typename T3 = etl::type_id_pair<>, - typename T4 = etl::type_id_pair<>, - typename T5 = etl::type_id_pair<>, - typename T6 = etl::type_id_pair<>, - typename T7 = etl::type_id_pair<>, - typename T8 = etl::type_id_pair<>, - typename T9 = etl::type_id_pair<>, - typename T10 = etl::type_id_pair<>, - typename T11 = etl::type_id_pair<>, - typename T12 = etl::type_id_pair<>, - typename T13 = etl::type_id_pair<>, - typename T14 = etl::type_id_pair<>, - typename T15 = etl::type_id_pair<>, - typename T16 = etl::type_id_pair<>> + typename T2 = etl::type_id_pair, + typename T3 = etl::type_id_pair, + typename T4 = etl::type_id_pair, + typename T5 = etl::type_id_pair, + typename T6 = etl::type_id_pair, + typename T7 = etl::type_id_pair, + typename T8 = etl::type_id_pair, + typename T9 = etl::type_id_pair, + typename T10 = etl::type_id_pair, + typename T11 = etl::type_id_pair, + typename T12 = etl::type_id_pair, + typename T13 = etl::type_id_pair, + typename T14 = etl::type_id_pair, + typename T15 = etl::type_id_pair, + typename T16 = etl::type_id_pair > class factory { private: From 1d6058ffac6bcb7ea9155567a90895ba46320038 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Fri, 15 Dec 2017 13:11:08 +0000 Subject: [PATCH 13/24] Modified factory to use pool instead of custom internal allocator. --- src/factory.h | 221 +++++++++++++++++++------------------------------- 1 file changed, 82 insertions(+), 139 deletions(-) diff --git a/src/factory.h b/src/factory.h index b7c8c6db..6957fe85 100644 --- a/src/factory.h +++ b/src/factory.h @@ -39,6 +39,7 @@ SOFTWARE. #include "alignment.h" #include "static_assert.h" #include "type_lookup.h" +#include #undef ETL_FILE #define ETL_FILE "40" @@ -125,12 +126,9 @@ namespace etl static const size_t MAX_SIZE = MAX_SIZE_; //************************************************************************* - /// Constructor + /// Default constructor. //************************************************************************* factory() - : p_next(reinterpret_cast(&buffer[0])), - items_allocated(0), - items_initialised(0) { } @@ -142,11 +140,20 @@ namespace etl { STATIC_ASSERT((etl::is_one_of::value), "Unsupported type"); - T* p = reinterpret_cast(allocate_item()); + T* p = nullptr; - if (p != nullptr) + if (pool.full()) { - new (p) T(); + ETL_ASSERT(false, ETL_ERROR(etl::factory_cannot_create)); + } + else + { + p = pool.allocate(); + + if (p != nullptr) + { + new (p) T(); + } } return p; @@ -160,11 +167,20 @@ namespace etl { STATIC_ASSERT((etl::is_one_of::value), "Unsupported type"); - T* p = reinterpret_cast(allocate_item()); + T* p = nullptr; - if (p != nullptr) + if (pool.full()) { - new (p) T(p1); + ETL_ASSERT(false, ETL_ERROR(etl::factory_cannot_create)); + } + else + { + p = pool.allocate(); + + if (p != nullptr) + { + new (p) T(p1); + } } return p; @@ -178,11 +194,20 @@ namespace etl { STATIC_ASSERT((etl::is_one_of::value), "Unsupported type"); - T* p = reinterpret_cast(allocate_item()); + T* p = nullptr; - if (p != nullptr) + if (pool.full()) { - new (p) T(p1, p2); + ETL_ASSERT(false, ETL_ERROR(etl::factory_cannot_create)); + } + else + { + p = pool.allocate(); + + if (p != nullptr) + { + new (p) T(p1, p2); + } } return p; @@ -196,11 +221,20 @@ namespace etl { STATIC_ASSERT((etl::is_one_of::value), "Unsupported type"); - T* p = reinterpret_cast(allocate_item()); + T* p = nullptr; - if (p != nullptr) + if (pool.full()) { - new (p) T(p1, p2, p3); + ETL_ASSERT(false, ETL_ERROR(etl::factory_cannot_create)); + } + else + { + p = pool.allocate(); + + if (p != nullptr) + { + new (p) T(p1, p2, p3); + } } return p; @@ -214,11 +248,20 @@ namespace etl { STATIC_ASSERT((etl::is_one_of::value), "Unsupported type"); - T* p = reinterpret_cast(allocate_item()); + T* p = nullptr; - if (p != nullptr) + if (pool.full()) { - new (p) T(p1, p2, p3, p4); + ETL_ASSERT(false, ETL_ERROR(etl::factory_cannot_create)); + } + else + { + p = pool.allocate(); + + if (p != nullptr) + { + new (p) T(p1, p2, p3, p4); + } } return p; @@ -305,7 +348,18 @@ namespace etl p->~T(); - return release_item(reinterpret_cast(const_cast(p))); + void* vp = reinterpret_cast(const_cast(p)); + + if (pool.is_in_pool(vp)) + { + pool.release(reinterpret_cast(const_cast(p))); + return true; + } + else + { + ETL_ASSERT(false, ETL_ERROR(factory_did_not_create)); + return false; + } } //************************************************************************* @@ -321,7 +375,7 @@ namespace etl //************************************************************************* size_t available() const { - return MAX_SIZE - items_allocated; + return pool.available(); } //************************************************************************* @@ -329,7 +383,7 @@ namespace etl //************************************************************************* size_t size() const { - return items_allocated; + return pool.size(); } //************************************************************************* @@ -338,7 +392,7 @@ namespace etl //************************************************************************* bool empty() const { - return items_allocated == 0; + return pool.empty(); } //************************************************************************* @@ -347,7 +401,7 @@ namespace etl //************************************************************************* bool full() const { - return items_allocated == MAX_SIZE;; + return pool.full(); } private: @@ -355,121 +409,10 @@ namespace etl factory(const factory&); factory& operator =(const factory&); - //************************************************************************* - /// Allocate an item from the pool. - //************************************************************************* - char* allocate_item() - { - char* p_value = nullptr; - - // Any free space left? - if (items_allocated < MAX_SIZE) - { - // Initialise another one if necessary. - if (items_initialised < MAX_SIZE) - { - uintptr_t p = reinterpret_cast(reinterpret_cast(&buffer[0]) + (items_initialised * ITEM_SIZE)); - *reinterpret_cast(p) = p + ITEM_SIZE; - ++items_initialised; - } - - // Get the address of new allocated item. - p_value = p_next; - - ++items_allocated; - if (items_allocated != MAX_SIZE) - { - // Set up the pointer to the next free item - p_next = *reinterpret_cast(p_next); - } - else - { - // No more left! - p_next = nullptr; - } - } - else - { - ETL_ASSERT(false, ETL_ERROR(etl::factory_cannot_create)); - } - - return p_value; - } - - //************************************************************************* - /// Release an item back to the pool. - //************************************************************************* - bool release_item(char* p_value) - { - if (is_item_in_pool(p_value)) - { - if (p_next != nullptr) - { - // Point it to the current free item. - *(uintptr_t*)p_value = reinterpret_cast(p_next); - } - else - { - // This is the only free item. - *((uintptr_t*)p_value) = 0; - } - - p_next = p_value; - - --items_allocated; - - return true; - } - else - { - ETL_ASSERT(false, ETL_ERROR(factory_did_not_create)); - } - - return false; - } - - //************************************************************************* - /// Check if the item belongs to this pool. - //************************************************************************* - bool is_item_in_pool(char* p) const - { - if (empty() || (p == nullptr)) - { - return false; - } - - // Within the range of the buffer? - intptr_t distance = p - reinterpret_cast(&buffer[0]); - bool is_within_range = (distance >= 0) && (distance <= intptr_t((ITEM_SIZE * MAX_SIZE) - ITEM_SIZE)); - - // Modulus and division can be slow on some architectures, so only do this in debug. -#if defined(ETL_DEBUG) - // Is the address on a valid object boundary? - bool is_valid_address = ((distance % ITEM_SIZE) == 0); -#else - bool is_valid_address = true; -#endif - - return is_within_range && is_valid_address; - } - - // The pool element. - union Element - { - uintptr_t next; ///< Pointer to the next free element. - char value[etl::largest::size]; ///< Storage for value type. - typename etl::type_with_alignment::alignment>::type dummy; ///< Dummy item to get correct alignment. - }; - - ///< The memory for the pool of objects. - typename etl::aligned_storage::value>::type buffer[MAX_SIZE]; - - static const uint32_t ITEM_SIZE = sizeof(Element); - - char* p_next; - - uint32_t items_allocated; ///< The number of items allocated. - uint32_t items_initialised; ///< The number of items initialised. + // The pool. + etl::generic_pool::size, + etl::largest::alignment, + MAX_SIZE> pool; }; } From 39ad0fe8088d580d83cbd8d5f782c950ad6cc729 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Fri, 15 Dec 2017 13:40:49 +0000 Subject: [PATCH 14/24] Added sign_extend functions with shift parameter. --- src/binary.h | 59 +++++++++++++++++++++++-- src/factory.h | 40 ++++++++--------- test/test_binary.cpp | 102 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 177 insertions(+), 24 deletions(-) diff --git a/src/binary.h b/src/binary.h index 7fccd64e..df7f73b9 100644 --- a/src/binary.h +++ b/src/binary.h @@ -271,12 +271,38 @@ namespace etl STATIC_ASSERT(NBITS <= std::numeric_limits::digits, "NBITS too large for return type"); const TReturn negative = (TReturn(1) << (NBITS - 1)); - TReturn signed_value = value; + TReturn signed_value = value & ((1 << NBITS) - 1); if ((signed_value & negative) != 0) { const TReturn sign_bits = ~((TReturn(1) << NBITS) - 1); - signed_value = value | sign_bits; + signed_value |= sign_bits; + } + + return signed_value; + } + + //*************************************************************************** + /// Sign extend. + /// Converts an N bit binary number, where bit N-1 is the sign bit, and SHIFT + /// is the right shift amount, to a signed integral type. + //*************************************************************************** + template + TReturn sign_extend(TValue value) + { + STATIC_ASSERT(etl::is_integral::value, "TValue not an integral type"); + STATIC_ASSERT(etl::is_integral::value, "TReturn not an integral type"); + STATIC_ASSERT(etl::is_signed::value, "TReturn not a signed type"); + STATIC_ASSERT(NBITS <= std::numeric_limits::digits, "NBITS too large for return type"); + STATIC_ASSERT(SHIFT <= std::numeric_limits::digits, "SHIFT too large"); + + const TReturn negative = (TReturn(1) << (NBITS - 1)); + TReturn signed_value = (value >> SHIFT) & ((1 << NBITS) - 1); + + if ((signed_value & negative) != 0) + { + const TReturn sign_bits = ~((TReturn(1) << NBITS) - 1); + signed_value |= sign_bits; } return signed_value; @@ -295,12 +321,37 @@ namespace etl assert(NBITS <= std::numeric_limits::digits); const TReturn negative = (TReturn(1) << (NBITS - 1)); - TReturn signed_value = value; + TReturn signed_value = value & ((1 << NBITS) - 1); if ((signed_value & negative) != 0) { const TReturn sign_bits = ~((TReturn(1) << NBITS) - 1); - signed_value = value | sign_bits; + signed_value |= sign_bits; + } + + return signed_value; + } + + //*************************************************************************** + /// Sign extend. + /// Converts an N bit binary number, where bit N-1 is the sign bit, and SHIFT + /// is the right shift amount, to a signed integral type. + //*************************************************************************** + template + TReturn sign_extend(TValue value, const size_t NBITS, const size_t SHIFT) + { + STATIC_ASSERT(etl::is_integral::value, "TValue not an integral type"); + STATIC_ASSERT(etl::is_integral::value, "TReturn not an integral type"); + STATIC_ASSERT(etl::is_signed::value, "TReturn not a signed type"); + assert(NBITS <= std::numeric_limits::digits); + + const TReturn negative = (TReturn(1) << (NBITS - 1)); + TReturn signed_value = (value >> SHIFT) & ((1 << NBITS) - 1); + + if ((signed_value & negative) != 0) + { + const TReturn sign_bits = ~((TReturn(1) << NBITS) - 1); + signed_value |= sign_bits; } return signed_value; diff --git a/src/factory.h b/src/factory.h index 6957fe85..30886ec9 100644 --- a/src/factory.h +++ b/src/factory.h @@ -148,7 +148,7 @@ namespace etl } else { - p = pool.allocate(); + p = pool.template allocate(); if (p != nullptr) { @@ -175,7 +175,7 @@ namespace etl } else { - p = pool.allocate(); + p = pool.template allocate(); if (p != nullptr) { @@ -202,7 +202,7 @@ namespace etl } else { - p = pool.allocate(); + p = pool.template allocate(); if (p != nullptr) { @@ -229,7 +229,7 @@ namespace etl } else { - p = pool.allocate(); + p = pool.template allocate(); if (p != nullptr) { @@ -256,7 +256,7 @@ namespace etl } else { - p = pool.allocate(); + p = pool.template allocate(); if (p != nullptr) { @@ -329,23 +329,23 @@ namespace etl bool destroy(const T* const p) { STATIC_ASSERT((etl::is_one_of::value || - etl::is_base_of::value || - etl::is_base_of::value || - etl::is_base_of::value || + etl::is_base_of::value || + etl::is_base_of::value || + etl::is_base_of::value || etl::is_base_of::value || - etl::is_base_of::value || - etl::is_base_of::value || - etl::is_base_of::value || - etl::is_base_of::value || - etl::is_base_of::value || - etl::is_base_of::value || - etl::is_base_of::value || - etl::is_base_of::value || - etl::is_base_of::value || - etl::is_base_of::value || - etl::is_base_of::value || + etl::is_base_of::value || + etl::is_base_of::value || + etl::is_base_of::value || + etl::is_base_of::value || + etl::is_base_of::value || + etl::is_base_of::value || + etl::is_base_of::value || + etl::is_base_of::value || + etl::is_base_of::value || + etl::is_base_of::value || + etl::is_base_of::value || etl::is_base_of::value), "Invalid type"); - + p->~T(); void* vp = reinterpret_cast(const_cast(p)); diff --git a/test/test_binary.cpp b/test/test_binary.cpp index 8bcc935a..5689d400 100644 --- a/test/test_binary.cpp +++ b/test/test_binary.cpp @@ -1053,6 +1053,57 @@ namespace CHECK_EQUAL(178956970, (etl::sign_extend(value32))); } + //************************************************************************* + TEST(test_sign_extend_template1b) + { + uint8_t value8 = 0x2A; + uint8_t value8mask = 0x3F; + + const uint32_t value_initial = 0x55555555; + + uint32_t value; + + // Shift 0 + value = value_initial; + value &= ~value8mask; + value |= value8; + + CHECK_EQUAL(-22, (etl::sign_extend(value))); + CHECK_EQUAL(-22, (etl::sign_extend(value))); + + // Shift 3 + value = value_initial; + value &= ~(value8mask << 3); + value |= (value8 << 3); + + CHECK_EQUAL(-22, (etl::sign_extend(value))); + CHECK_EQUAL(-22, (etl::sign_extend(value))); + + // Shift 6 + value = value_initial; + value &= ~(value8mask << 6); + value |= (value8 << 6); + + CHECK_EQUAL(-22, (etl::sign_extend(value))); + CHECK_EQUAL(-22, (etl::sign_extend(value))); + + // Shift 12 + value = value_initial; + value &= ~(value8mask << 12); + value |= (value8 << 12); + + CHECK_EQUAL(-22, (etl::sign_extend(value))); + CHECK_EQUAL(-22, (etl::sign_extend(value))); + + // Shift 26 + value = value_initial; + value &= ~(value8mask << 26); + value |= (value8 << 26); + + CHECK_EQUAL(-22, (etl::sign_extend(value))); + CHECK_EQUAL(-22, (etl::sign_extend(value))); + } + //************************************************************************* TEST(test_sign_extend_template2) { @@ -1099,6 +1150,57 @@ namespace CHECK_EQUAL(178956970, (etl::sign_extend(value32, 30))); } + //************************************************************************* + TEST(test_sign_extend_template2b) + { + uint8_t value8 = 0x2A; + uint8_t value8mask = 0x3F; + + const uint32_t value_initial = 0x55555555; + + uint32_t value; + + // Shift 0 + value = value_initial; + value &= ~value8mask; + value |= value8; + + CHECK_EQUAL(-22, (etl::sign_extend(value, 6, 0))); + CHECK_EQUAL(-22, (etl::sign_extend(value, 6, 0))); + + // Shift 3 + value = value_initial; + value &= ~(value8mask << 3); + value |= (value8 << 3); + + CHECK_EQUAL(-22, (etl::sign_extend(value, 6, 3))); + CHECK_EQUAL(-22, (etl::sign_extend(value, 6, 3))); + + // Shift 6 + value = value_initial; + value &= ~(value8mask << 6); + value |= (value8 << 6); + + CHECK_EQUAL(-22, (etl::sign_extend(value, 6, 6))); + CHECK_EQUAL(-22, (etl::sign_extend(value, 6, 6))); + + // Shift 12 + value = value_initial; + value &= ~(value8mask << 12); + value |= (value8 << 12); + + CHECK_EQUAL(-22, (etl::sign_extend(value, 6, 12))); + CHECK_EQUAL(-22, (etl::sign_extend(value, 6, 12))); + + // Shift 26 + value = value_initial; + value &= ~(value8mask << 26); + value |= (value8 << 26); + + CHECK_EQUAL(-22, (etl::sign_extend(value, 6, 26))); + CHECK_EQUAL(-22, (etl::sign_extend(value, 6, 26))); + } + //************************************************************************* TEST(test_bit) { From ec6d1db186e1ab2826c4cfa605855215187c4e07 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Fri, 15 Dec 2017 20:40:12 +0000 Subject: [PATCH 15/24] Modified factory to use pool instead of custom internal allocator. --- src/factory.h | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/src/factory.h b/src/factory.h index 30886ec9..bf287777 100644 --- a/src/factory.h +++ b/src/factory.h @@ -30,6 +30,7 @@ SOFTWARE. #define __ETL_FACTORY__ #include +#include #include "platform.h" #include "error_handler.h" @@ -132,6 +133,7 @@ namespace etl { } +#if !ETL_CPP11_SUPPORTED //************************************************************************* /// Creates the object. Default constructor. //************************************************************************* @@ -321,6 +323,45 @@ namespace etl STATIC_ASSERT((!etl::is_same::value), "Invalid index"); return create_from_type(p1, p2, p3, p4); } +#else + //************************************************************************* + /// Creates the object from a type. Variadic parameter constructor. + //************************************************************************* + template + T* create_from_type(Args&&... args) + { + STATIC_ASSERT((etl::is_one_of::value), "Unsupported type"); + + T* p = nullptr; + + if (pool.full()) + { + ETL_ASSERT(false, ETL_ERROR(etl::factory_cannot_create)); + } + else + { + p = pool.template allocate(); + + if (p != nullptr) + { + new (p) T(std::forward(args)...); + } + } + + return p; + } + + //************************************************************************* + /// Creates the object from an index. Variadic parameter constructor. + //************************************************************************* + template + typename lookup_t::template type_from_id::type* create_from_id(Args&&... args) + { + typedef typename lookup_t::template type_from_id::type type; + STATIC_ASSERT((!etl::is_same::value), "Invalid index"); + return create_from_type(std::forward(args)...); + } +#endif //************************************************************************* /// Destroys the object. @@ -352,7 +393,7 @@ namespace etl if (pool.is_in_pool(vp)) { - pool.release(reinterpret_cast(const_cast(p))); + pool.release(vp); return true; } else From 2877bf7f2357a5c8248388b29f391c2a0d6c8d28 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Mon, 18 Dec 2017 21:36:41 +0000 Subject: [PATCH 16/24] Deprecated etl::factory --- src/factory.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/factory.h b/src/factory.h index bf287777..6c4e46f0 100644 --- a/src/factory.h +++ b/src/factory.h @@ -42,6 +42,12 @@ SOFTWARE. #include "type_lookup.h" #include +#if defined(ETL_COMPILER_GCC) + #warning THIS CLASS IS DEPRECATED!USE VARIANT_POOL INSTEAD. +#elif defined(ETL_COMPILER_MICROSOFT) + #pragma message ("THIS CLASS IS DEPRECATED! USE VARIANT_POOL INSTEAD.") +#endif + #undef ETL_FILE #define ETL_FILE "40" From 9ff47d024c75b3630c17b15912eef9957a4cbe7c Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Mon, 18 Dec 2017 21:37:33 +0000 Subject: [PATCH 17/24] Moved get_fsm_context() from public to protected in etl::state --- src/fsm.h | 47 +++++++++++++++++++++++++++++++++++++++++++++ src/fsm_generator.h | 5 +++++ 2 files changed, 52 insertions(+) diff --git a/src/fsm.h b/src/fsm.h index afa3422e..4ccf9904 100644 --- a/src/fsm.h +++ b/src/fsm.h @@ -402,6 +402,8 @@ namespace etl { } + protected: + inline TContext& get_fsm_context() const { return static_cast(ifsm_state::get_fsm_context()); @@ -461,10 +463,13 @@ namespace etl { } + protected: + inline TContext& get_fsm_context() const { return static_cast(ifsm_state::get_fsm_context()); } + private: etl::fsm_state_id_t process_event(etl::imessage_router& source, const etl::imessage& message) @@ -518,10 +523,13 @@ namespace etl { } + protected: + inline TContext& get_fsm_context() const { return static_cast(ifsm_state::get_fsm_context()); } + private: etl::fsm_state_id_t process_event(etl::imessage_router& source, const etl::imessage& message) @@ -574,10 +582,13 @@ namespace etl { } + protected: + inline TContext& get_fsm_context() const { return static_cast(ifsm_state::get_fsm_context()); } + private: etl::fsm_state_id_t process_event(etl::imessage_router& source, const etl::imessage& message) @@ -628,10 +639,13 @@ namespace etl { } + protected: + inline TContext& get_fsm_context() const { return static_cast(ifsm_state::get_fsm_context()); } + private: etl::fsm_state_id_t process_event(etl::imessage_router& source, const etl::imessage& message) @@ -681,10 +695,13 @@ namespace etl { } + protected: + inline TContext& get_fsm_context() const { return static_cast(ifsm_state::get_fsm_context()); } + private: etl::fsm_state_id_t process_event(etl::imessage_router& source, const etl::imessage& message) @@ -733,10 +750,13 @@ namespace etl { } + protected: + inline TContext& get_fsm_context() const { return static_cast(ifsm_state::get_fsm_context()); } + private: etl::fsm_state_id_t process_event(etl::imessage_router& source, const etl::imessage& message) @@ -784,10 +804,13 @@ namespace etl { } + protected: + inline TContext& get_fsm_context() const { return static_cast(ifsm_state::get_fsm_context()); } + private: etl::fsm_state_id_t process_event(etl::imessage_router& source, const etl::imessage& message) @@ -833,10 +856,13 @@ namespace etl { } + protected: + inline TContext& get_fsm_context() const { return static_cast(ifsm_state::get_fsm_context()); } + private: etl::fsm_state_id_t process_event(etl::imessage_router& source, const etl::imessage& message) @@ -881,10 +907,13 @@ namespace etl { } + protected: + inline TContext& get_fsm_context() const { return static_cast(ifsm_state::get_fsm_context()); } + private: etl::fsm_state_id_t process_event(etl::imessage_router& source, const etl::imessage& message) @@ -928,10 +957,13 @@ namespace etl { } + protected: + inline TContext& get_fsm_context() const { return static_cast(ifsm_state::get_fsm_context()); } + private: etl::fsm_state_id_t process_event(etl::imessage_router& source, const etl::imessage& message) @@ -974,10 +1006,13 @@ namespace etl { } + protected: + inline TContext& get_fsm_context() const { return static_cast(ifsm_state::get_fsm_context()); } + private: etl::fsm_state_id_t process_event(etl::imessage_router& source, const etl::imessage& message) @@ -1018,10 +1053,13 @@ namespace etl { } + protected: + inline TContext& get_fsm_context() const { return static_cast(ifsm_state::get_fsm_context()); } + private: etl::fsm_state_id_t process_event(etl::imessage_router& source, const etl::imessage& message) @@ -1061,10 +1099,13 @@ namespace etl { } + protected: + inline TContext& get_fsm_context() const { return static_cast(ifsm_state::get_fsm_context()); } + private: etl::fsm_state_id_t process_event(etl::imessage_router& source, const etl::imessage& message) @@ -1103,10 +1144,13 @@ namespace etl { } + protected: + inline TContext& get_fsm_context() const { return static_cast(ifsm_state::get_fsm_context()); } + private: etl::fsm_state_id_t process_event(etl::imessage_router& source, const etl::imessage& message) @@ -1144,10 +1188,13 @@ namespace etl { } + protected: + inline TContext& get_fsm_context() const { return static_cast(ifsm_state::get_fsm_context()); } + private: etl::fsm_state_id_t process_event(etl::imessage_router& source, const etl::imessage& message) diff --git a/src/fsm_generator.h b/src/fsm_generator.h index 203c1b77..50c67cad 100644 --- a/src/fsm_generator.h +++ b/src/fsm_generator.h @@ -422,6 +422,8 @@ namespace etl cog.outl(" {") cog.outl(" }") cog.outl("") + cog.outl("protected:") + cog.outl("") cog.outl(" inline TContext& get_fsm_context() const") cog.outl(" {") cog.outl(" return static_cast(ifsm_state::get_fsm_context());") @@ -493,10 +495,13 @@ namespace etl cog.outl(" {") cog.outl(" }") cog.outl("") + cog.outl("protected:") + cog.outl("") cog.outl(" inline TContext& get_fsm_context() const") cog.outl(" {") cog.outl(" return static_cast(ifsm_state::get_fsm_context());") cog.outl(" }") + cog.outl("") cog.outl("private:") cog.outl("") cog.outl(" etl::fsm_state_id_t process_event(etl::imessage_router& source, const etl::imessage& message)") From b4598fbd5cfb78d0c9001055ac8627a5e8667f1b Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Mon, 18 Dec 2017 21:38:00 +0000 Subject: [PATCH 18/24] Updated version number --- library.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.properties b/library.properties index b983848a..24c1d7e5 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Embedded Template Library -version=10.6.0 +version=10.8.0 author= John Wellbelove maintainer=John Wellbelove sentence=A C++ template library tailored for embedded systems. From 696247c0f5ddf13101f30d9bff58c970c1d073ae Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Thu, 21 Dec 2017 13:52:43 +0000 Subject: [PATCH 19/24] Removed default function template parameter value. --- src/cstring.h | 8 ++++---- src/hash.h | 6 +++--- src/u16string.h | 8 ++++---- src/u32string.h | 8 ++++---- src/wstring.h | 8 ++++---- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/cstring.h b/src/cstring.h index aae1c86a..36e60233 100644 --- a/src/cstring.h +++ b/src/cstring.h @@ -195,8 +195,8 @@ namespace etl { size_t operator()(const etl::istring& text) const { - return etl::__private_hash__::generic_hash<>(reinterpret_cast(&text[0]), - reinterpret_cast(&text[text.size()])); + return etl::__private_hash__::generic_hash(reinterpret_cast(&text[0]), + reinterpret_cast(&text[text.size()])); } }; @@ -205,8 +205,8 @@ namespace etl { size_t operator()(const etl::string& text) const { - return etl::__private_hash__::generic_hash<>(reinterpret_cast(&text[0]), - reinterpret_cast(&text[text.size()])); + return etl::__private_hash__::generic_hash(reinterpret_cast(&text[0]), + reinterpret_cast(&text[text.size()])); } }; #endif diff --git a/src/hash.h b/src/hash.h index 505b75a9..891b8518 100644 --- a/src/hash.h +++ b/src/hash.h @@ -52,7 +52,7 @@ namespace etl /// Hash to use when size_t is 16 bits. /// T is always expected to be size_t. //************************************************************************* - template + template typename enable_if::type generic_hash(const uint8_t* begin, const uint8_t* end) { @@ -65,7 +65,7 @@ namespace etl /// Hash to use when size_t is 32 bits. /// T is always expected to be size_t. //************************************************************************* - template + template typename enable_if::type generic_hash(const uint8_t* begin, const uint8_t* end) { @@ -76,7 +76,7 @@ namespace etl /// Hash to use when size_t is 64 bits. /// T is always expected to be size_t. //************************************************************************* - template + template typename enable_if::type generic_hash(const uint8_t* begin, const uint8_t* end) { diff --git a/src/u16string.h b/src/u16string.h index 45e1ec93..107ccfcb 100644 --- a/src/u16string.h +++ b/src/u16string.h @@ -199,8 +199,8 @@ namespace etl { size_t operator()(const etl::iu16string& text) const { - return etl::__private_hash__::generic_hash<>(reinterpret_cast(&text[0]), - reinterpret_cast(&text[text.size()])); + return etl::__private_hash__::generic_hash(reinterpret_cast(&text[0]), + reinterpret_cast(&text[text.size()])); } }; @@ -209,8 +209,8 @@ namespace etl { size_t operator()(const etl::u16string& text) const { - return etl::__private_hash__::generic_hash<>(reinterpret_cast(&text[0]), - reinterpret_cast(&text[text.size()])); + return etl::__private_hash__::generic_hash(reinterpret_cast(&text[0]), + reinterpret_cast(&text[text.size()])); } }; #endif diff --git a/src/u32string.h b/src/u32string.h index 2cf0ceac..590c7534 100644 --- a/src/u32string.h +++ b/src/u32string.h @@ -199,8 +199,8 @@ namespace etl { size_t operator()(const etl::iu32string& text) const { - return etl::__private_hash__::generic_hash<>(reinterpret_cast(&text[0]), - reinterpret_cast(&text[text.size()])); + return etl::__private_hash__::generic_hash(reinterpret_cast(&text[0]), + reinterpret_cast(&text[text.size()])); } }; @@ -209,8 +209,8 @@ namespace etl { size_t operator()(const etl::u32string& text) const { - return etl::__private_hash__::generic_hash<>(reinterpret_cast(&text[0]), - reinterpret_cast(&text[text.size()])); + return etl::__private_hash__::generic_hash(reinterpret_cast(&text[0]), + reinterpret_cast(&text[text.size()])); } }; #endif diff --git a/src/wstring.h b/src/wstring.h index 3d8c4832..99b1979a 100644 --- a/src/wstring.h +++ b/src/wstring.h @@ -200,8 +200,8 @@ namespace etl { size_t operator()(const etl::iwstring& text) const { - return etl::__private_hash__::generic_hash<>(reinterpret_cast(&text[0]), - reinterpret_cast(&text[text.size()])); + return etl::__private_hash__::generic_hash(reinterpret_cast(&text[0]), + reinterpret_cast(&text[text.size()])); } }; @@ -210,8 +210,8 @@ namespace etl { size_t operator()(const etl::wstring& text) const { - return etl::__private_hash__::generic_hash<>(reinterpret_cast(&text[0]), - reinterpret_cast(&text[text.size()])); + return etl::__private_hash__::generic_hash(reinterpret_cast(&text[0]), + reinterpret_cast(&text[text.size()])); } }; #endif From 483f322f892b2528b34450ab9435859b7f8b8928 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Thu, 21 Dec 2017 13:53:32 +0000 Subject: [PATCH 20/24] Changed static const to enum. --- src/multimap.h | 9 ++++++--- src/multiset.h | 9 ++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/multimap.h b/src/multimap.h index 016a4513..1d2e7682 100644 --- a/src/multimap.h +++ b/src/multimap.h @@ -178,9 +178,12 @@ namespace etl protected: - static const uint_least8_t kLeft = 0; - static const uint_least8_t kRight = 1; - static const uint_least8_t kNeither = 2; + enum + { + kLeft, + kRight, + kNeither + }; //************************************************************************* /// The node element in the multimap. diff --git a/src/multiset.h b/src/multiset.h index e01e1d59..d2911291 100644 --- a/src/multiset.h +++ b/src/multiset.h @@ -178,9 +178,12 @@ namespace etl protected: - static const uint_least8_t kLeft = 0; - static const uint_least8_t kRight = 1; - static const uint_least8_t kNeither = 2; + enum + { + kLeft, + kRight, + kNeither + }; //************************************************************************* /// The node element in the multiset. From f366d26a26b7a2444a31af4f7bc3277af8bbce37 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Thu, 21 Dec 2017 13:54:06 +0000 Subject: [PATCH 21/24] Removed factory. Added variant_pool --- src/file_error_numbers.txt | 2 +- test/codeblocks/ETL.cbp | 5 +- test/codeblocks/ETL.layout | 812 ++++++++++++++++---------------- test/vs2017/etl.vcxproj | 4 +- test/vs2017/etl.vcxproj.filters | 12 +- 5 files changed, 418 insertions(+), 417 deletions(-) diff --git a/src/file_error_numbers.txt b/src/file_error_numbers.txt index 6ce37665..818a7958 100644 --- a/src/file_error_numbers.txt +++ b/src/file_error_numbers.txt @@ -37,4 +37,4 @@ 37 task 38 message 39 message_bus -40 factory +40 factory / variant_pool diff --git a/test/codeblocks/ETL.cbp b/test/codeblocks/ETL.cbp index 11c28d9b..74d3fdb8 100644 --- a/test/codeblocks/ETL.cbp +++ b/test/codeblocks/ETL.cbp @@ -60,6 +60,7 @@ + @@ -99,7 +100,6 @@ - @@ -205,6 +205,7 @@ + @@ -304,7 +305,6 @@ - @@ -369,6 +369,7 @@ + diff --git a/test/codeblocks/ETL.layout b/test/codeblocks/ETL.layout index 79201827..aab3c739 100644 --- a/test/codeblocks/ETL.layout +++ b/test/codeblocks/ETL.layout @@ -2,379 +2,114 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - + - + - + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -382,9 +117,24 @@ - + - + + + + + + + + + + + + + + + + @@ -392,9 +142,164 @@ - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -402,9 +307,49 @@ - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -414,52 +359,47 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - - - - + @@ -467,49 +407,59 @@ - + - + - + - + - + - + - + - + - + - + - - - - - - + - + - + - + - + + + + + + + + + + + + + + + + @@ -517,14 +467,64 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + + + + + + + + + + + + + + + + + + + + + diff --git a/test/vs2017/etl.vcxproj b/test/vs2017/etl.vcxproj index c168f880..a01e6d2a 100644 --- a/test/vs2017/etl.vcxproj +++ b/test/vs2017/etl.vcxproj @@ -136,7 +136,6 @@ - @@ -166,6 +165,7 @@ + @@ -358,7 +358,6 @@ - true @@ -453,6 +452,7 @@ + diff --git a/test/vs2017/etl.vcxproj.filters b/test/vs2017/etl.vcxproj.filters index e3895e76..6201abf2 100644 --- a/test/vs2017/etl.vcxproj.filters +++ b/test/vs2017/etl.vcxproj.filters @@ -489,9 +489,6 @@ ETL\Maths - - ETL\Containers - ETL\Utilities @@ -561,6 +558,9 @@ Source Files\ECL + + ETL\Containers + @@ -854,9 +854,6 @@ Source Files - - Source Files - Source Files @@ -935,6 +932,9 @@ ETL\Utilities + + Source Files + From 14a18d3336e76a9fc4ac1919875fd6fcbe56091d Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Thu, 21 Dec 2017 13:54:42 +0000 Subject: [PATCH 22/24] Renamed include guard to avoid clash with list. --- src/forward_list.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/forward_list.h b/src/forward_list.h index 2a2cf156..116846a2 100644 --- a/src/forward_list.h +++ b/src/forward_list.h @@ -28,8 +28,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ******************************************************************************/ -#ifndef __ETL_LIST__ -#define __ETL_LIST__ +#ifndef __ETL_FORWARD_LIST__ +#define __ETL_FORWARD_LIST__ #include #include From 02c96ba7916e0237d8ca20ac707f2007737ca514 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Thu, 21 Dec 2017 13:55:48 +0000 Subject: [PATCH 23/24] Added etl::variant_pool as a replacement for etl::factory. --- src/variant_pool.h | 374 +++++++++++++++++++++++++++++++++++++ test/test_variant_pool.cpp | 315 +++++++++++++++++++++++++++++++ 2 files changed, 689 insertions(+) create mode 100644 src/variant_pool.h create mode 100644 test/test_variant_pool.cpp diff --git a/src/variant_pool.h b/src/variant_pool.h new file mode 100644 index 00000000..f671776a --- /dev/null +++ b/src/variant_pool.h @@ -0,0 +1,374 @@ +/****************************************************************************** +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_VARIANT_POOL__ +#define __ETL_VARIANT_POOL__ + +#include +#include + +#include "platform.h" +#include "error_handler.h" +#include "exception.h" +#include "largest.h" +#include "type_traits.h" +#include "alignment.h" +#include "static_assert.h" +#include "type_lookup.h" +#include + +#undef ETL_FILE +#define ETL_FILE "40" + +namespace etl +{ + //*************************************************************************** + class variant_pool_exception : public etl::exception + { + public: + + variant_pool_exception(string_type reason_, string_type file_name_, numeric_type line_number_) + : exception(reason_, file_name_, line_number_) + { + } + }; + + //*************************************************************************** + class variant_pool_cannot_create : public etl::variant_pool_exception + { + public: + + variant_pool_cannot_create(string_type file_name_, numeric_type line_number_) + : variant_pool_exception(ETL_ERROR_TEXT("variant_pool:cannot create", ETL_FILE"A"), file_name_, line_number_) + { + } + }; + + //*************************************************************************** + class variant_pool_did_not_create : public etl::variant_pool_exception + { + public: + + variant_pool_did_not_create(string_type file_name_, numeric_type line_number_) + : variant_pool_exception(ETL_ERROR_TEXT("variant_pool:did not create", ETL_FILE"B"), file_name_, line_number_) + { + } + }; + + //*************************************************************************** + template + class variant_pool + { + public: + + static const size_t MAX_SIZE = MAX_SIZE_; + + //************************************************************************* + /// Default constructor. + //************************************************************************* + variant_pool() + { + } + +#if !ETL_CPP11_SUPPORTED + //************************************************************************* + /// Creates the object. Default constructor. + //************************************************************************* + template + T* create() + { + STATIC_ASSERT((etl::is_one_of::value), "Unsupported type"); + + T* p = nullptr; + + if (pool.full()) + { + ETL_ASSERT(false, ETL_ERROR(etl::variant_pool_cannot_create)); + } + else + { + p = pool.template allocate(); + + if (p != nullptr) + { + new (p) T(); + } + } + + return p; + } + + //************************************************************************* + /// Creates the object. One parameter constructor. + //************************************************************************* + template + T* create(const TP1& p1) + { + STATIC_ASSERT((etl::is_one_of::value), "Unsupported type"); + + T* p = nullptr; + + if (pool.full()) + { + ETL_ASSERT(false, ETL_ERROR(etl::variant_pool_cannot_create)); + } + else + { + p = pool.template allocate(); + + if (p != nullptr) + { + new (p) T(p1); + } + } + + return p; + } + + //************************************************************************* + /// Creates the object. Two parameter constructor. + //************************************************************************* + template + T* create(const TP1& p1, const TP2& p2) + { + STATIC_ASSERT((etl::is_one_of::value), "Unsupported type"); + + T* p = nullptr; + + if (pool.full()) + { + ETL_ASSERT(false, ETL_ERROR(etl::variant_pool_cannot_create)); + } + else + { + p = pool.template allocate(); + + if (p != nullptr) + { + new (p) T(p1, p2); + } + } + + return p; + } + + //************************************************************************* + /// Creates the object. Three parameter constructor. + //************************************************************************* + template + T* create(const TP1& p1, const TP2& p2, const TP3& p3) + { + STATIC_ASSERT((etl::is_one_of::value), "Unsupported type"); + + T* p = nullptr; + + if (pool.full()) + { + ETL_ASSERT(false, ETL_ERROR(etl::variant_pool_cannot_create)); + } + else + { + p = pool.template allocate(); + + if (p != nullptr) + { + new (p) T(p1, p2, p3); + } + } + + return p; + } + + //************************************************************************* + /// Creates the object. Four parameter constructor. + //************************************************************************* + template + T* create(const TP1& p1, const TP2& p2, const TP3& p3, const TP4& p4) + { + STATIC_ASSERT((etl::is_one_of::value), "Unsupported type"); + + T* p = nullptr; + + if (pool.full()) + { + ETL_ASSERT(false, ETL_ERROR(etl::variant_pool_cannot_create)); + } + else + { + p = pool.template allocate(); + + if (p != nullptr) + { + new (p) T(p1, p2, p3, p4); + } + } + + return p; + } +#else + //************************************************************************* + /// Creates the object from a type. Variadic parameter constructor. + //************************************************************************* + template + T* create(Args&&... args) + { + STATIC_ASSERT((etl::is_one_of::value), "Unsupported type"); + + T* p = nullptr; + + if (pool.full()) + { + ETL_ASSERT(false, ETL_ERROR(etl::variant_pool_cannot_create)); + } + else + { + p = pool.template allocate(); + + if (p != nullptr) + { + new (p) T(std::forward(args)...); + } + } + + return p; + } +#endif + + //************************************************************************* + /// Destroys the object. + //************************************************************************* + template + bool destroy(const T* const p) + { + STATIC_ASSERT((etl::is_one_of::value || + etl::is_base_of::value || + etl::is_base_of::value || + etl::is_base_of::value || + etl::is_base_of::value || + etl::is_base_of::value || + etl::is_base_of::value || + etl::is_base_of::value || + etl::is_base_of::value || + etl::is_base_of::value || + etl::is_base_of::value || + etl::is_base_of::value || + etl::is_base_of::value || + etl::is_base_of::value || + etl::is_base_of::value || + etl::is_base_of::value || + etl::is_base_of::value), "Invalid type"); + + p->~T(); + + void* vp = reinterpret_cast(const_cast(p)); + + if (pool.is_in_pool(vp)) + { + pool.release(vp); + return true; + } + else + { + ETL_ASSERT(false, ETL_ERROR(variant_pool_did_not_create)); + return false; + } + } + + //************************************************************************* + /// Returns the maximum number of items in the variant_pool. + //************************************************************************* + size_t max_size() const + { + return MAX_SIZE; + } + + //************************************************************************* + /// Returns the number of free items in the variant_pool. + //************************************************************************* + size_t available() const + { + return pool.available(); + } + + //************************************************************************* + /// Returns the number of allocated items in the variant_pool. + //************************************************************************* + size_t size() const + { + return pool.size(); + } + + //************************************************************************* + /// Checks to see if there are no allocated items in the variant_pool. + /// \return true if there are none allocated. + //************************************************************************* + bool empty() const + { + return pool.empty(); + } + + //************************************************************************* + /// Checks to see if there are no free items in the variant_pool. + /// \return true if there are none free. + //************************************************************************* + bool full() const + { + return pool.full(); + } + + private: + + variant_pool(const variant_pool&); + variant_pool& operator =(const variant_pool&); + + // The pool. + etl::generic_pool::size, + etl::largest::alignment, + MAX_SIZE> pool; + }; +} + +#undef ETL_FILE + +#endif diff --git a/test/test_variant_pool.cpp b/test/test_variant_pool.cpp new file mode 100644 index 00000000..4578c8d3 --- /dev/null +++ b/test/test_variant_pool.cpp @@ -0,0 +1,315 @@ +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +http://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. +******************************************************************************/ + +#include "UnitTest++.h" +#include "ExtraCheckMacros.h" + +#include "variant_pool.h" + +#include +#include + +namespace +{ + bool destructor; + + //*********************************** + struct Base + { + Base() + { + destructor = false; + } + + virtual ~Base() + { + } + + virtual void Set() = 0; + }; + + //*********************************** + struct Derived1 : public Base + { + int i; + + Derived1() + : i(0) + { + } + + ~Derived1() + { + destructor = true; + } + + void Set() + { + i = 1; + } + }; + + //*********************************** + struct Derived2 : public Base + { + double d; + + Derived2() + : d(0.0) + { + } + + ~Derived2() + { + destructor = true; + } + + void Set() + { + d = 1.2; + } + }; + + //*********************************** + struct Derived3 : public Base + { + std::string s; + + Derived3() + : s("constructed") + { + } + + Derived3(const char* p1) + : s("constructed") + { + s.append(p1); + } + + Derived3(const char* p1, const std::string& p2) + : s("constructed") + { + s.append(p1); + s.append(p2); + } + + Derived3(const char* p1, const std::string& p2, const char* p3) + : s("constructed") + { + s.append(p1); + s.append(p2); + s.append(p3); + } + + Derived3(const char* p1, const std::string& p2, const char* p3, const std::string& p4) + : s("constructed") + { + s.append(p1); + s.append(p2); + s.append(p3); + s.append(p4); + } + + ~Derived3() + { + destructor = true; + } + + void Set() + { + s = "set"; + } + }; + + //*********************************** + struct NonDerived + { + NonDerived() + : s("constructed") + { + } + + ~NonDerived() + { + destructor = true; + } + + void Set() + { + s = "set"; + } + + std::string s; + }; + + const size_t SIZE = 5; + + // Notice that the type declaration order is not important. + typedef etl::variant_pool Factory; + + SUITE(test_variant_pool) + { + //************************************************************************* + TEST(test_sizes) + { + Factory variant_pool; + + size_t ms = Factory::MAX_SIZE; + CHECK_EQUAL(SIZE, ms); + CHECK_EQUAL(SIZE, variant_pool.max_size()); + CHECK_EQUAL(SIZE, variant_pool.available()); + CHECK_EQUAL(0U, variant_pool.size()); + CHECK(variant_pool.empty()); + CHECK(!variant_pool.full()); + + variant_pool.create(); + CHECK_EQUAL(SIZE - 1U, variant_pool.available()); + CHECK_EQUAL(1U, variant_pool.size()); + CHECK(!variant_pool.empty()); + CHECK(!variant_pool.full()); + + variant_pool.create(); + variant_pool.create(); + variant_pool.create(); + variant_pool.create(); + CHECK_EQUAL(0U, variant_pool.available()); + CHECK_EQUAL(SIZE, variant_pool.size()); + CHECK(!variant_pool.empty()); + CHECK(variant_pool.full()); + + CHECK_THROW(variant_pool.create(), etl::variant_pool_cannot_create); + } + + //************************************************************************* + TEST(test_create_release) + { + Factory variant_pool; + + Base* p; + + // Derived 1 + p = variant_pool.create(); + Derived1* pd1 = static_cast(p); + CHECK_EQUAL(0, pd1->i); + p->Set(); + CHECK_EQUAL(1, pd1->i); + variant_pool.destroy(p); + CHECK(destructor); + + // Derived 2 + destructor = false; + p = variant_pool.create(); + Derived2* pd2 = static_cast(p); + CHECK_EQUAL(0.0, pd2->d); + p->Set(); + CHECK_EQUAL(1.2, pd2->d); + variant_pool.destroy(p); + CHECK(destructor); + + // Derived 3 + destructor = false; + p = variant_pool.create(); + Derived3* pd3 = static_cast(p); + CHECK_EQUAL("constructed", pd3->s); + p->Set(); + CHECK_EQUAL("set", pd3->s); + variant_pool.destroy(p); + CHECK(destructor); + + // Non Derived + destructor = false; + NonDerived* pnd = variant_pool.create(); + CHECK_EQUAL("constructed", pnd->s); + pnd->Set(); + CHECK_EQUAL("set", pnd->s); + variant_pool.destroy(pnd); + CHECK(destructor); + + // Integral + int* pi = variant_pool.create(); + variant_pool.destroy(pi); + } + + //************************************************************************* + TEST(test_create_release_const) + { + Factory variant_pool; + + const Derived1& d = *variant_pool.create(); + + CHECK_EQUAL(0, d.i); + variant_pool.destroy(&d); + CHECK(destructor); + + } + + //************************************************************************* + TEST(test_create_emplace) + { + Factory variant_pool; + + Base* p; + Derived3* pd3; + + p = variant_pool.create("1"); + pd3 = static_cast(p); + CHECK_EQUAL("constructed1", pd3->s); + variant_pool.destroy(p); + + p = variant_pool.create("1", "2"); + pd3 = static_cast(p); + CHECK_EQUAL("constructed12", pd3->s); + variant_pool.destroy(p); + + p = variant_pool.create("1", "2", "3"); + pd3 = static_cast(p); + CHECK_EQUAL("constructed123", pd3->s); + variant_pool.destroy(p); + + p = variant_pool.create("1", "2", "3", "4"); + pd3 = static_cast(p); + CHECK_EQUAL("constructed1234", pd3->s); + variant_pool.destroy(p); + } + + //************************************************************************* + TEST(test_did_not_create) + { + Factory variant_pool1; + Factory variant_pool2; + + Base* p; + + p = variant_pool1.create(); + CHECK_NO_THROW(variant_pool1.destroy(p)); + + p = variant_pool2.create(); + CHECK_THROW(variant_pool1.destroy(p), etl::variant_pool_did_not_create); + } + }; +} From f57e59fdc3aacedc6d2505d44403a67d89663c58 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Thu, 21 Dec 2017 14:10:55 +0000 Subject: [PATCH 24/24] Added release notes --- support/Release notes.txt | 41 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 support/Release notes.txt diff --git a/support/Release notes.txt b/support/Release notes.txt new file mode 100644 index 00000000..6eaa2cd6 --- /dev/null +++ b/support/Release notes.txt @@ -0,0 +1,41 @@ +10.8.0 + +Added etl::variant_pool as a replacement for etl::factory. +Deprecated etl::factory + +etl::forward_list + Renamed include guard to avoid clash with list. + +Removed factory from unit tests. + +multimap & multiset + Changed static const to enum. + +hash & strings + Removed default function template parameter value. + +FSM + Moved get_fsm_context() from public to protected in etl::state + +Binary + Added sign_extend functions with shift parameter. + Added new binary.cpp for non-template functions. + Added bit<> template constant + Added bit enumerations + +Debug count + Added clear() + +Type lookup + Simplified codebase + +Message router + Added 'successor' member to allow implementation 'Chain Of Responsibility' pattern. + +IO Ports + Changes value() to read(). + Rewrite of classes to improve efficiency by removing iterator indirection step. + Fixed inconsistencies in the dynamic IO port API. + +Type traits + Added 'conditional_integral_constant' to complement 'conditional' \ No newline at end of file