From a1aaa5dbb6975a87bad0d72023eac0a255a2b4dd Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sat, 31 Oct 2020 18:38:08 +0000 Subject: [PATCH 1/3] Added ETL_CONSTEXPR --- include/etl/flags.h | 182 ++++++++++++++++++-------------------------- test/test_flags.cpp | 99 ++++++++++++++++++++++-- 2 files changed, 165 insertions(+), 116 deletions(-) diff --git a/include/etl/flags.h b/include/etl/flags.h index 6a83a298..e6b902fb 100644 --- a/include/etl/flags.h +++ b/include/etl/flags.h @@ -67,114 +67,85 @@ namespace etl //************************************************************************* /// Constructor //************************************************************************* - flags() ETL_NOEXCEPT + ETL_CONSTEXPR flags() ETL_NOEXCEPT : data(value_type(0)) { } - flags(value_type initial) ETL_NOEXCEPT - : data(initial & MASK) + ETL_CONSTEXPR flags(value_type pattern) ETL_NOEXCEPT + : data(pattern & MASK) { } - flags(const flags& initial) ETL_NOEXCEPT - : data(initial.value()) + ETL_CONSTEXPR flags(const flags& pattern) ETL_NOEXCEPT + : data(pattern.value()) { } //************************************************************************* /// Tests bits. //************************************************************************* - template - bool test() const ETL_NOEXCEPT + template + ETL_CONSTEXPR bool test() const ETL_NOEXCEPT { - return (data & position) != value_type(0); + return (data & pattern) != value_type(0); } //******************************************* - bool test(value_type position) const + ETL_CONSTEXPR bool test(value_type pattern) const ETL_NOEXCEPT { - return (data & position) != value_type(0); + return (data & pattern) != value_type(0); } //************************************************************************* /// Set the bits. //************************************************************************* - template - flags& set() ETL_NOEXCEPT + template + ETL_CONSTEXPR flags& set() ETL_NOEXCEPT { - if ETL_IF_CONSTEXPR(value) - { - data |= position; - } - else - { - data &= ~position; - } - - data &= MASK; + value ? data |= (pattern & MASK) : data &= (~pattern & MASK); return *this; } //******************************************* - template - flags& set(bool value) ETL_NOEXCEPT + template + ETL_CONSTEXPR flags& set(bool value) ETL_NOEXCEPT { - if (value) - { - data |= position; - } - else - { - data &= ~position; - } - - data &= MASK; + value ? data |= (pattern & MASK) : data &= (~pattern & MASK); return *this; } //******************************************* - template - flags& set() ETL_NOEXCEPT + template + ETL_CONSTEXPR flags& set() ETL_NOEXCEPT { - data |= position; - data &= MASK; + data |= (pattern & MASK); return *this; } //******************************************* - flags& set(value_type position) ETL_NOEXCEPT + ETL_CONSTEXPR flags& set(value_type pattern) ETL_NOEXCEPT { - data |= position; - data &= MASK; + data |= (pattern & MASK); return *this; } //******************************************* - flags& set(value_type position, bool value) ETL_NOEXCEPT + ETL_CONSTEXPR flags& set(value_type pattern, bool value) ETL_NOEXCEPT { - if (value) - { - data |= position; - } - else - { - data &= ~position; - } - - data &= MASK; - + value ? data |= (pattern & MASK) : data &= (~pattern & MASK); + return *this; } //************************************************************************* /// Clear all of the flags. //************************************************************************* - flags& clear() ETL_NOEXCEPT + ETL_CONSTEXPR flags& clear() ETL_NOEXCEPT { data = ALL_CLEAR; @@ -182,20 +153,20 @@ namespace etl } //************************************************************************* - /// Reset the bit at the position. + /// Reset the bit at the pattern. //************************************************************************* - template - flags& reset() ETL_NOEXCEPT + template + ETL_CONSTEXPR flags& reset() ETL_NOEXCEPT { - data &= ~position; + data &= ~pattern; return *this; } //******************************************* - flags& reset(value_type position) ETL_NOEXCEPT + ETL_CONSTEXPR flags& reset(value_type pattern) ETL_NOEXCEPT { - data &= ~position; + data &= ~pattern; return *this; } @@ -203,29 +174,26 @@ namespace etl //************************************************************************* /// Flip bits. //************************************************************************* - flags& flip() ETL_NOEXCEPT + ETL_CONSTEXPR flags& flip() ETL_NOEXCEPT { - data = ~data; - data &= MASK; + data = (~data & MASK); return *this; } //******************************************* - template - flags& flip() ETL_NOEXCEPT + template + ETL_CONSTEXPR flags& flip() ETL_NOEXCEPT { - data ^= position; - data &= MASK; - + data ^= pattern & MASK; + return *this; } //******************************************* - flags& flip(value_type position) ETL_NOEXCEPT + ETL_CONSTEXPR flags& flip(value_type pattern) ETL_NOEXCEPT { - data ^= position; - data &= MASK; + data ^= pattern & MASK; return *this; } @@ -233,71 +201,70 @@ namespace etl //************************************************************************* // Are all the bits sets? //************************************************************************* - bool all() const ETL_NOEXCEPT + ETL_CONSTEXPR bool all() const ETL_NOEXCEPT { return data == MASK; } //******************************************* - template - bool all_of() const ETL_NOEXCEPT + template + ETL_CONSTEXPR bool all_of() const ETL_NOEXCEPT { - return (data & (position & MASK)) == (position & MASK); + return (data & (pattern & MASK)) == (pattern & MASK); } //******************************************* - bool all_of(value_type position) const ETL_NOEXCEPT + ETL_CONSTEXPR bool all_of(value_type pattern) const ETL_NOEXCEPT { - position &= MASK; - return (data & position) == position; + return (data & (pattern &= MASK)) == pattern; } //************************************************************************* /// Are none of the bits set? //************************************************************************* - bool none() const ETL_NOEXCEPT + ETL_CONSTEXPR bool none() const ETL_NOEXCEPT { return (data & MASK) == ALL_CLEAR; } //******************************************* - template - bool none_of() const ETL_NOEXCEPT + template + ETL_CONSTEXPR bool none_of() const ETL_NOEXCEPT { - return !any_of(position); + return !any_of(pattern); } //******************************************* - bool none_of(value_type position) const ETL_NOEXCEPT + ETL_CONSTEXPR bool none_of(value_type pattern) const ETL_NOEXCEPT { - return !any_of(position); + return !any_of(pattern); } //************************************************************************* /// Are any of the bits set? //************************************************************************* - bool any() const ETL_NOEXCEPT + ETL_CONSTEXPR bool any() const ETL_NOEXCEPT { return (data & MASK) != value_type(0); } //******************************************* - template - bool any_of() const ETL_NOEXCEPT + template + ETL_CONSTEXPR bool any_of() const ETL_NOEXCEPT { - return (data & (position & MASK)) != value_type(0); + return (data & (pattern & MASK)) != value_type(0); } //******************************************* - bool any_of(value_type position) const + ETL_CONSTEXPR bool any_of(value_type pattern) const { - return (data & (position & MASK)) != value_type(0); + return (data & (pattern & MASK)) != value_type(0); } //************************************************************************* /// Return the value of the flags. //************************************************************************* - value_type value() const ETL_NOEXCEPT + ETL_CONSTEXPR value_type value() const ETL_NOEXCEPT { return data; } @@ -305,15 +272,17 @@ namespace etl //************************************************************************* /// Set the value of the flags. //************************************************************************* - void value(value_type initial) ETL_NOEXCEPT + ETL_CONSTEXPR flags& value(value_type pattern) ETL_NOEXCEPT { - data = initial & MASK; + data = pattern & MASK; + + return *this; } //************************************************************************* /// Return the value of the flags. //************************************************************************* - operator value_type() const ETL_NOEXCEPT + ETL_CONSTEXPR operator value_type() const ETL_NOEXCEPT { return data; } @@ -321,7 +290,7 @@ namespace etl //************************************************************************* /// operator &= //************************************************************************* - flags& operator &=(value_type other) ETL_NOEXCEPT + ETL_CONSTEXPR flags& operator &=(value_type pattern) ETL_NOEXCEPT { data &= other; @@ -331,10 +300,9 @@ namespace etl //************************************************************************* /// operator |= //************************************************************************* - flags& operator |=(value_type other) ETL_NOEXCEPT + ETL_CONSTEXPR flags& operator |=(value_type pattern) ETL_NOEXCEPT { - data |= other; - data &= MASK; + data |= (other & MASK); return *this; } @@ -342,10 +310,9 @@ namespace etl //************************************************************************* /// operator ^= //************************************************************************* - flags& operator ^=(value_type other) ETL_NOEXCEPT + ETL_CONSTEXPR flags& operator ^=(value_type pattern) ETL_NOEXCEPT { - data ^= other; - data &= MASK; + data ^= (other & MASK); return *this; } @@ -353,12 +320,9 @@ namespace etl //************************************************************************* /// operator = //************************************************************************* - flags& operator =(flags other) ETL_NOEXCEPT + ETL_CONSTEXPR flags& operator =(flags other) ETL_NOEXCEPT { - if (this != &other) - { - data = other.data; - } + data = other.data; return *this; } @@ -366,7 +330,7 @@ namespace etl //************************************************************************* /// operator = //************************************************************************* - flags& operator =(value_type other) ETL_NOEXCEPT + ETL_CONSTEXPR flags& operator =(value_type pattern) ETL_NOEXCEPT { data = (other & MASK); @@ -390,7 +354,7 @@ namespace etl /// operator == //*************************************************************************** template - bool operator == (flags lhs, flags rhs) ETL_NOEXCEPT + ETL_CONSTEXPR bool operator == (flags lhs, flags rhs) ETL_NOEXCEPT { return lhs.value() == rhs.value(); } @@ -399,7 +363,7 @@ namespace etl /// operator != //*************************************************************************** template - bool operator != (flags lhs, flags rhs) ETL_NOEXCEPT + ETL_CONSTEXPR bool operator != (flags lhs, flags rhs) ETL_NOEXCEPT { return !(lhs == rhs); } diff --git a/test/test_flags.cpp b/test/test_flags.cpp index 6f8b8bc3..6a18074c 100644 --- a/test/test_flags.cpp +++ b/test/test_flags.cpp @@ -75,23 +75,108 @@ namespace { Flags flags; - CHECK_EQUAL(0U, flags); - CHECK_EQUAL(0U, flags.value()); + CHECK_EQUAL(int(FNONE), flags); + CHECK_EQUAL(int(FNONE), flags.value()); } //************************************************************************* TEST(test_value) { - Flags flags; + Flags flags(F1346); - CHECK_EQUAL(int(FNONE), int(flags)); - CHECK_EQUAL(int(FNONE), int(flags.value())); - - flags.value(int(F1346)); CHECK_EQUAL(int(F1346), int(flags)); CHECK_EQUAL(int(F1346), int(flags.value())); } + //************************************************************************* + TEST(test_constexpr) + { + constexpr Flags cef1; + constexpr Flags cef2(F01234567); + constexpr Flags cef3(cef2); + constexpr Flags cef4(cef2.value()); + constexpr bool is_f1a = cef2.test(F1); + constexpr bool is_f1b = cef2.test(); + constexpr bool is_all = cef2.all(); + constexpr bool is_all_of1= cef2.all_of(F1357); + constexpr bool is_all_of2 = cef2.all_of(); + + constexpr bool is_any = cef2.any(); + constexpr bool is_any_of1 = cef2.any_of(F1357); + constexpr bool is_any_of2 = cef2.any_of(); + + constexpr bool is_none = cef2.none(); + constexpr bool is_none_of1 = cef2.none_of(F1357); + constexpr bool is_none_of2 = cef2.none_of(); + + constexpr uint8_t value = cef2; + constexpr Flags cef5(Flags(F1357).flip()); + constexpr Flags cef6(Flags(F01234567).flip(F1357)); + constexpr Flags cef7(Flags(F01234567).flip()); + + constexpr Flags cef8(Flags(F01234567).reset(F1357)); + constexpr Flags cef9(Flags(F01234567).reset()); + + constexpr Flags cef10(Flags(F01234567).clear()); + + constexpr Flags cef11(Flags(F1357).set(F0246)); + constexpr Flags cef12(Flags(F1357).set()); + constexpr Flags cef13(Flags(F1357).set(F0246, true)); + constexpr Flags cef14(Flags(F1357).set(true)); + constexpr Flags cef15(Flags(F1357).set()); + + constexpr Flags cef16(Flags(F1357).value(F0246)); + + constexpr Flags cef17(Flags(F1357).operator =(F01234567)); + constexpr Flags cef18(Flags(F1357).operator =(cef2)); + + constexpr bool is_same1 = (cef3 == cef4); + constexpr bool is_same2 = (cef3 == F01234567); + constexpr bool is_same3 = (F01234567 == cef3); + + constexpr bool is_not_same1 = (cef3 != cef4); + constexpr bool is_not_same2 = (cef3 != F01234567); + constexpr bool is_not_same3 = (F01234567 != cef3); + + CHECK_EQUAL(int(FNONE), int(cef1)); + CHECK_EQUAL(int(F01234567), int(cef2)); + CHECK_EQUAL(int(F01234567), int(cef3)); + CHECK_EQUAL(int(F01234567), int(cef4)); + CHECK_EQUAL(int(F0246), int(cef5)); + CHECK_EQUAL(int(F0246), int(cef6)); + CHECK_EQUAL(int(F0246), int(cef7)); + CHECK_EQUAL(int(F0246), int(cef8)); + CHECK_EQUAL(int(F0246), int(cef9)); + CHECK_EQUAL(int(FNONE), int(cef10)); + CHECK_EQUAL(int(F01234567), int(cef11)); + CHECK_EQUAL(int(F01234567), int(cef12)); + CHECK_EQUAL(int(F01234567), int(cef13)); + CHECK_EQUAL(int(F01234567), int(cef14)); + CHECK_EQUAL(int(F01234567), int(cef15)); + CHECK_EQUAL(int(F0246), int(cef16)); + CHECK_EQUAL(int(F01234567), int(cef17)); + CHECK_EQUAL(int(cef2), int(cef18)); + CHECK_EQUAL(int(cef2), int(value)); + CHECK(is_f1a); + CHECK(is_f1b); + CHECK(is_all); + CHECK(is_all_of1); + CHECK(is_all_of2); + CHECK(is_any); + CHECK(is_any_of1); + CHECK(is_any_of2); + CHECK(!is_none); + CHECK(!is_none_of1); + CHECK(!is_none_of2); + CHECK_EQUAL(int(cef2), int(value)); + CHECK(is_same1); + CHECK(is_same2); + CHECK(is_same3); + CHECK(!is_not_same1); + CHECK(!is_not_same2); + CHECK(!is_not_same3); + } + //************************************************************************* TEST(test_value_masked) { From 7539dacfcc6ea61f1a076461758dfd663e6b2898 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sun, 1 Nov 2020 11:29:57 +0000 Subject: [PATCH 2/3] Added ETL_CONSTEXPR --- include/etl/flags.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/etl/flags.h b/include/etl/flags.h index e6b902fb..f1c1a03a 100644 --- a/include/etl/flags.h +++ b/include/etl/flags.h @@ -292,7 +292,7 @@ namespace etl //************************************************************************* ETL_CONSTEXPR flags& operator &=(value_type pattern) ETL_NOEXCEPT { - data &= other; + data &= pattern; return *this; } @@ -302,7 +302,7 @@ namespace etl //************************************************************************* ETL_CONSTEXPR flags& operator |=(value_type pattern) ETL_NOEXCEPT { - data |= (other & MASK); + data |= (pattern & MASK); return *this; } @@ -312,7 +312,7 @@ namespace etl //************************************************************************* ETL_CONSTEXPR flags& operator ^=(value_type pattern) ETL_NOEXCEPT { - data ^= (other & MASK); + data ^= (pattern & MASK); return *this; } @@ -332,7 +332,7 @@ namespace etl //************************************************************************* ETL_CONSTEXPR flags& operator =(value_type pattern) ETL_NOEXCEPT { - data = (other & MASK); + data = (pattern & MASK); return *this; } From 7645fd1359499bad2bf61944ed8ba2776d489eb9 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sun, 1 Nov 2020 12:14:35 +0000 Subject: [PATCH 3/3] Updated version numbers --- include/etl/version.h | 2 +- library.json | 4 ++-- library.properties | 2 +- support/Release notes.txt | 4 ++++ 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/include/etl/version.h b/include/etl/version.h index fda41bfb..629372bb 100644 --- a/include/etl/version.h +++ b/include/etl/version.h @@ -39,7 +39,7 @@ SOFTWARE. #define ETL_VERSION_MAJOR 18 #define ETL_VERSION_MINOR 19 -#define ETL_VERSION_PATCH 1 +#define ETL_VERSION_PATCH 2 #define ETL_VERSION ETL_STRINGIFY(ETL_VERSION_MAJOR) "." ETL_STRINGIFY(ETL_VERSION_MINOR) "." ETL_STRINGIFY(ETL_VERSION_PATCH) #define ETL_VERSION_W ETL_STRINGIFY(ETL_VERSION_MAJOR) L"." ETL_STRINGIFY(ETL_VERSION_MINOR) L"." ETL_STRINGIFY(ETL_VERSION_PATCH) #define ETL_VERSION_U16 ETL_STRINGIFY(ETL_VERSION_MAJOR) u"." ETL_STRINGIFY(ETL_VERSION_MINOR) u"." ETL_STRINGIFY(ETL_VERSION_PATCH) diff --git a/library.json b/library.json index c5ca6306..5e025f14 100644 --- a/library.json +++ b/library.json @@ -1,7 +1,7 @@ { "name": "Embedded Template Library", - "version": "18.19.1", - "authors": { + "version": "18.19.2", + "author s": { "name": "John Wellbelove", "email": "john.wellbelove@etlcpp.com" }, diff --git a/library.properties b/library.properties index d58f5e1f..bf412908 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Embedded Template Library -version=18.19.1 +version=18.19.2 author= John Wellbelove maintainer=John Wellbelove license=MIT diff --git a/support/Release notes.txt b/support/Release notes.txt index 1da4f619..5e1e0f77 100644 --- a/support/Release notes.txt +++ b/support/Release notes.txt @@ -1,3 +1,7 @@ +=============================================================================== +18.19.2 +Expanded the use of constexpr in etl::flags + =============================================================================== 18.19.1 Added ETL_CONSTEXPR for state_chart, transision and state constructors.