Merge branch 'development'

This commit is contained in:
John Wellbelove 2020-11-01 12:17:12 +00:00
commit 4aed78e32c
2 changed files with 167 additions and 118 deletions

View File

@ -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<T, MASK>& initial) ETL_NOEXCEPT
: data(initial.value())
ETL_CONSTEXPR flags(const flags<T, MASK>& pattern) ETL_NOEXCEPT
: data(pattern.value())
{
}
//*************************************************************************
/// Tests bits.
//*************************************************************************
template <value_type position>
bool test() const ETL_NOEXCEPT
template <value_type pattern>
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 <value_type position, bool value>
flags<T, MASK>& set() ETL_NOEXCEPT
template <value_type pattern, bool value>
ETL_CONSTEXPR flags<T, MASK>& 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 <value_type position>
flags<T, MASK>& set(bool value) ETL_NOEXCEPT
template <value_type pattern>
ETL_CONSTEXPR flags<T, MASK>& set(bool value) ETL_NOEXCEPT
{
if (value)
{
data |= position;
}
else
{
data &= ~position;
}
data &= MASK;
value ? data |= (pattern & MASK) : data &= (~pattern & MASK);
return *this;
}
//*******************************************
template <value_type position>
flags<T, MASK>& set() ETL_NOEXCEPT
template <value_type pattern>
ETL_CONSTEXPR flags<T, MASK>& set() ETL_NOEXCEPT
{
data |= position;
data &= MASK;
data |= (pattern & MASK);
return *this;
}
//*******************************************
flags<T, MASK>& set(value_type position) ETL_NOEXCEPT
ETL_CONSTEXPR flags<T, MASK>& set(value_type pattern) ETL_NOEXCEPT
{
data |= position;
data &= MASK;
data |= (pattern & MASK);
return *this;
}
//*******************************************
flags<T, MASK>& set(value_type position, bool value) ETL_NOEXCEPT
ETL_CONSTEXPR flags<T, MASK>& 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<T, MASK>& clear() ETL_NOEXCEPT
ETL_CONSTEXPR flags<T, MASK>& 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 <value_type position>
flags<T, MASK>& reset() ETL_NOEXCEPT
template <value_type pattern>
ETL_CONSTEXPR flags<T, MASK>& reset() ETL_NOEXCEPT
{
data &= ~position;
data &= ~pattern;
return *this;
}
//*******************************************
flags<T, MASK>& reset(value_type position) ETL_NOEXCEPT
ETL_CONSTEXPR flags<T, MASK>& reset(value_type pattern) ETL_NOEXCEPT
{
data &= ~position;
data &= ~pattern;
return *this;
}
@ -203,29 +174,26 @@ namespace etl
//*************************************************************************
/// Flip bits.
//*************************************************************************
flags<T, MASK>& flip() ETL_NOEXCEPT
ETL_CONSTEXPR flags<T, MASK>& flip() ETL_NOEXCEPT
{
data = ~data;
data &= MASK;
data = (~data & MASK);
return *this;
}
//*******************************************
template <value_type position>
flags<T, MASK>& flip() ETL_NOEXCEPT
template <value_type pattern>
ETL_CONSTEXPR flags<T, MASK>& flip() ETL_NOEXCEPT
{
data ^= position;
data &= MASK;
data ^= pattern & MASK;
return *this;
}
//*******************************************
flags<T, MASK>& flip(value_type position) ETL_NOEXCEPT
ETL_CONSTEXPR flags<T, MASK>& 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 <value_type position>
bool all_of() const ETL_NOEXCEPT
template <value_type pattern>
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 <value_type position>
bool none_of() const ETL_NOEXCEPT
template <value_type pattern>
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 <value_type position>
bool any_of() const ETL_NOEXCEPT
template <value_type pattern>
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<T, MASK>& 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<T, MASK>& operator &=(value_type other) ETL_NOEXCEPT
ETL_CONSTEXPR flags<T, MASK>& operator &=(value_type pattern) ETL_NOEXCEPT
{
data &= other;
data &= pattern;
return *this;
}
@ -331,10 +300,9 @@ namespace etl
//*************************************************************************
/// operator |=
//*************************************************************************
flags<T, MASK>& operator |=(value_type other) ETL_NOEXCEPT
ETL_CONSTEXPR flags<T, MASK>& operator |=(value_type pattern) ETL_NOEXCEPT
{
data |= other;
data &= MASK;
data |= (pattern & MASK);
return *this;
}
@ -342,10 +310,9 @@ namespace etl
//*************************************************************************
/// operator ^=
//*************************************************************************
flags<T, MASK>& operator ^=(value_type other) ETL_NOEXCEPT
ETL_CONSTEXPR flags<T, MASK>& operator ^=(value_type pattern) ETL_NOEXCEPT
{
data ^= other;
data &= MASK;
data ^= (pattern & MASK);
return *this;
}
@ -353,12 +320,9 @@ namespace etl
//*************************************************************************
/// operator =
//*************************************************************************
flags<T, MASK>& operator =(flags<T, MASK> other) ETL_NOEXCEPT
ETL_CONSTEXPR flags<T, MASK>& operator =(flags<T, MASK> other) ETL_NOEXCEPT
{
if (this != &other)
{
data = other.data;
}
data = other.data;
return *this;
}
@ -366,9 +330,9 @@ namespace etl
//*************************************************************************
/// operator =
//*************************************************************************
flags<T, MASK>& operator =(value_type other) ETL_NOEXCEPT
ETL_CONSTEXPR flags<T, MASK>& operator =(value_type pattern) ETL_NOEXCEPT
{
data = (other & MASK);
data = (pattern & MASK);
return *this;
}
@ -390,7 +354,7 @@ namespace etl
/// operator ==
//***************************************************************************
template <typename T, T MASK>
bool operator == (flags<T, MASK> lhs, flags<T, MASK> rhs) ETL_NOEXCEPT
ETL_CONSTEXPR bool operator == (flags<T, MASK> lhs, flags<T, MASK> rhs) ETL_NOEXCEPT
{
return lhs.value() == rhs.value();
}
@ -399,7 +363,7 @@ namespace etl
/// operator !=
//***************************************************************************
template <typename T, T MASK>
bool operator != (flags<T, MASK> lhs, flags<T, MASK> rhs) ETL_NOEXCEPT
ETL_CONSTEXPR bool operator != (flags<T, MASK> lhs, flags<T, MASK> rhs) ETL_NOEXCEPT
{
return !(lhs == rhs);
}

View File

@ -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<F1>();
constexpr bool is_all = cef2.all();
constexpr bool is_all_of1= cef2.all_of(F1357);
constexpr bool is_all_of2 = cef2.all_of<F1357>();
constexpr bool is_any = cef2.any();
constexpr bool is_any_of1 = cef2.any_of(F1357);
constexpr bool is_any_of2 = cef2.any_of<F1357>();
constexpr bool is_none = cef2.none();
constexpr bool is_none_of1 = cef2.none_of(F1357);
constexpr bool is_none_of2 = cef2.none_of<F1357>();
constexpr uint8_t value = cef2;
constexpr Flags cef5(Flags(F1357).flip());
constexpr Flags cef6(Flags(F01234567).flip(F1357));
constexpr Flags cef7(Flags(F01234567).flip<F1357>());
constexpr Flags cef8(Flags(F01234567).reset(F1357));
constexpr Flags cef9(Flags(F01234567).reset<F1357>());
constexpr Flags cef10(Flags(F01234567).clear());
constexpr Flags cef11(Flags(F1357).set(F0246));
constexpr Flags cef12(Flags(F1357).set<F0246>());
constexpr Flags cef13(Flags(F1357).set(F0246, true));
constexpr Flags cef14(Flags(F1357).set<F0246>(true));
constexpr Flags cef15(Flags(F1357).set<F0246, true>());
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)
{