Skip to content

Commit

Permalink
AK: Make DistinctNumeric constexpr-capable
Browse files Browse the repository at this point in the history
  • Loading branch information
alimpfard authored and awesomekling committed May 4, 2021
1 parent 93f03c7 commit 415fd4d
Showing 1 changed file with 37 additions and 37 deletions.
74 changes: 37 additions & 37 deletions AK/DistinctNumeric.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,48 +52,48 @@ class DistinctNumeric {
using Self = DistinctNumeric<T, X, Incr, Cmp, Bool, Flags, Shift, Arith>;

public:
DistinctNumeric()
constexpr DistinctNumeric()
{
}

DistinctNumeric(T value)
constexpr DistinctNumeric(T value)
: m_value { value }
{
}

const T& value() const { return m_value; }
constexpr const T& value() const { return m_value; }

// Always implemented: identity.
bool operator==(const Self& other) const
constexpr bool operator==(const Self& other) const
{
return this->m_value == other.m_value;
}
bool operator!=(const Self& other) const
constexpr bool operator!=(const Self& other) const
{
return this->m_value != other.m_value;
}

// Only implemented when `Incr` is true:
Self& operator++()
constexpr Self& operator++()
{
static_assert(Incr, "'++a' is only available for DistinctNumeric types with 'Incr'.");
this->m_value += 1;
return *this;
}
Self operator++(int)
constexpr Self operator++(int)
{
static_assert(Incr, "'a++' is only available for DistinctNumeric types with 'Incr'.");
Self ret = this->m_value;
this->m_value += 1;
return ret;
}
Self& operator--()
constexpr Self& operator--()
{
static_assert(Incr, "'--a' is only available for DistinctNumeric types with 'Incr'.");
this->m_value -= 1;
return *this;
}
Self operator--(int)
constexpr Self operator--(int)
{
static_assert(Incr, "'a--' is only available for DistinctNumeric types with 'Incr'.");
Self ret = this->m_value;
Expand All @@ -102,30 +102,30 @@ class DistinctNumeric {
}

// Only implemented when `Cmp` is true:
bool operator>(const Self& other) const
constexpr bool operator>(const Self& other) const
{
static_assert(Cmp, "'a>b' is only available for DistinctNumeric types with 'Cmp'.");
return this->m_value > other.m_value;
}
bool operator<(const Self& other) const
constexpr bool operator<(const Self& other) const
{
static_assert(Cmp, "'a<b' is only available for DistinctNumeric types with 'Cmp'.");
return this->m_value < other.m_value;
}
bool operator>=(const Self& other) const
constexpr bool operator>=(const Self& other) const
{
static_assert(Cmp, "'a>=b' is only available for DistinctNumeric types with 'Cmp'.");
return this->m_value >= other.m_value;
}
bool operator<=(const Self& other) const
constexpr bool operator<=(const Self& other) const
{
static_assert(Cmp, "'a<=b' is only available for DistinctNumeric types with 'Cmp'.");
return this->m_value <= other.m_value;
}
// 'operator<=>' cannot be implemented. See class comment.

// Only implemented when `bool` is true:
bool operator!() const
constexpr bool operator!() const
{
static_assert(Bool, "'!a' is only available for DistinctNumeric types with 'Bool'.");
return !this->m_value;
Expand All @@ -136,39 +136,39 @@ class DistinctNumeric {
// `operator bool() const` would defy the entire point of this class.

// Only implemented when `Flags` is true:
Self operator~() const
constexpr Self operator~() const
{
static_assert(Flags, "'~a' is only available for DistinctNumeric types with 'Flags'.");
return ~this->m_value;
}
Self operator&(const Self& other) const
constexpr Self operator&(const Self& other) const
{
static_assert(Flags, "'a&b' is only available for DistinctNumeric types with 'Flags'.");
return this->m_value & other.m_value;
}
Self operator|(const Self& other) const
constexpr Self operator|(const Self& other) const
{
static_assert(Flags, "'a|b' is only available for DistinctNumeric types with 'Flags'.");
return this->m_value | other.m_value;
}
Self operator^(const Self& other) const
constexpr Self operator^(const Self& other) const
{
static_assert(Flags, "'a^b' is only available for DistinctNumeric types with 'Flags'.");
return this->m_value ^ other.m_value;
}
Self& operator&=(const Self& other)
constexpr Self& operator&=(const Self& other)
{
static_assert(Flags, "'a&=b' is only available for DistinctNumeric types with 'Flags'.");
this->m_value &= other.m_value;
return *this;
}
Self& operator|=(const Self& other)
constexpr Self& operator|=(const Self& other)
{
static_assert(Flags, "'a|=b' is only available for DistinctNumeric types with 'Flags'.");
this->m_value |= other.m_value;
return *this;
}
Self& operator^=(const Self& other)
constexpr Self& operator^=(const Self& other)
{
static_assert(Flags, "'a^=b' is only available for DistinctNumeric types with 'Flags'.");
this->m_value ^= other.m_value;
Expand All @@ -177,90 +177,90 @@ class DistinctNumeric {

// Only implemented when `Shift` is true:
// TODO: Should this take `int` instead?
Self operator<<(const Self& other) const
constexpr Self operator<<(const Self& other) const
{
static_assert(Shift, "'a<<b' is only available for DistinctNumeric types with 'Shift'.");
return this->m_value << other.m_value;
}
Self operator>>(const Self& other) const
constexpr Self operator>>(const Self& other) const
{
static_assert(Shift, "'a>>b' is only available for DistinctNumeric types with 'Shift'.");
return this->m_value >> other.m_value;
}
Self& operator<<=(const Self& other)
constexpr Self& operator<<=(const Self& other)
{
static_assert(Shift, "'a<<=b' is only available for DistinctNumeric types with 'Shift'.");
this->m_value <<= other.m_value;
return *this;
}
Self& operator>>=(const Self& other)
constexpr Self& operator>>=(const Self& other)
{
static_assert(Shift, "'a>>=b' is only available for DistinctNumeric types with 'Shift'.");
this->m_value >>= other.m_value;
return *this;
}

// Only implemented when `Arith` is true:
Self operator+(const Self& other) const
constexpr Self operator+(const Self& other) const
{
static_assert(Arith, "'a+b' is only available for DistinctNumeric types with 'Arith'.");
return this->m_value + other.m_value;
}
Self operator-(const Self& other) const
constexpr Self operator-(const Self& other) const
{
static_assert(Arith, "'a-b' is only available for DistinctNumeric types with 'Arith'.");
return this->m_value - other.m_value;
}
Self operator+() const
constexpr Self operator+() const
{
static_assert(Arith, "'+a' is only available for DistinctNumeric types with 'Arith'.");
return +this->m_value;
}
Self operator-() const
constexpr Self operator-() const
{
static_assert(Arith, "'-a' is only available for DistinctNumeric types with 'Arith'.");
return -this->m_value;
}
Self operator*(const Self& other) const
constexpr Self operator*(const Self& other) const
{
static_assert(Arith, "'a*b' is only available for DistinctNumeric types with 'Arith'.");
return this->m_value * other.m_value;
}
Self operator/(const Self& other) const
constexpr Self operator/(const Self& other) const
{
static_assert(Arith, "'a/b' is only available for DistinctNumeric types with 'Arith'.");
return this->m_value / other.m_value;
}
Self operator%(const Self& other) const
constexpr Self operator%(const Self& other) const
{
static_assert(Arith, "'a%b' is only available for DistinctNumeric types with 'Arith'.");
return this->m_value % other.m_value;
}
Self& operator+=(const Self& other)
constexpr Self& operator+=(const Self& other)
{
static_assert(Arith, "'a+=b' is only available for DistinctNumeric types with 'Arith'.");
this->m_value += other.m_value;
return *this;
}
Self& operator-=(const Self& other)
constexpr Self& operator-=(const Self& other)
{
static_assert(Arith, "'a+=b' is only available for DistinctNumeric types with 'Arith'.");
this->m_value += other.m_value;
return *this;
}
Self& operator*=(const Self& other)
constexpr Self& operator*=(const Self& other)
{
static_assert(Arith, "'a*=b' is only available for DistinctNumeric types with 'Arith'.");
this->m_value *= other.m_value;
return *this;
}
Self& operator/=(const Self& other)
constexpr Self& operator/=(const Self& other)
{
static_assert(Arith, "'a/=b' is only available for DistinctNumeric types with 'Arith'.");
this->m_value /= other.m_value;
return *this;
}
Self& operator%=(const Self& other)
constexpr Self& operator%=(const Self& other)
{
static_assert(Arith, "'a%=b' is only available for DistinctNumeric types with 'Arith'.");
this->m_value %= other.m_value;
Expand Down

0 comments on commit 415fd4d

Please sign in to comment.