diff --git a/include/etl/flags.h b/include/etl/flags.h index 6a83a298..f1c1a03a 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,9 +290,9 @@ namespace etl //************************************************************************* /// operator &= //************************************************************************* - flags& operator &=(value_type other) ETL_NOEXCEPT + ETL_CONSTEXPR flags& operator &=(value_type pattern) ETL_NOEXCEPT { - data &= other; + data &= pattern; return *this; } @@ -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 |= (pattern & 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 ^= (pattern & 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,9 +330,9 @@ namespace etl //************************************************************************* /// operator = //************************************************************************* - flags& operator =(value_type other) ETL_NOEXCEPT + ETL_CONSTEXPR flags& operator =(value_type pattern) ETL_NOEXCEPT { - data = (other & MASK); + data = (pattern & MASK); return *this; } @@ -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) {