diff options
author | Ali Mohammad Pur <ali.mpfard@gmail.com> | 2021-04-25 22:27:54 +0430 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-05-04 21:32:15 +0200 |
commit | 415fd4d2ecd8d846f15f1e4dd1c9865f85a15347 (patch) | |
tree | 36da1b4ac639eda70d46bcf814acc7e3d18a3935 | |
parent | 93f03c73d9cdd38abc0a4d02a2e811f1ce92262e (diff) | |
download | serenity-415fd4d2ecd8d846f15f1e4dd1c9865f85a15347.zip |
AK: Make DistinctNumeric constexpr-capable
-rw-r--r-- | AK/DistinctNumeric.h | 74 |
1 files changed, 37 insertions, 37 deletions
diff --git a/AK/DistinctNumeric.h b/AK/DistinctNumeric.h index 44af5a1154..f6c2c67dbe 100644 --- a/AK/DistinctNumeric.h +++ b/AK/DistinctNumeric.h @@ -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; @@ -102,22 +102,22 @@ public: } // 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; @@ -125,7 +125,7 @@ public: // '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; @@ -136,39 +136,39 @@ public: // `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; @@ -177,23 +177,23 @@ public: // 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; @@ -201,66 +201,66 @@ public: } // 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; |