diff options
| author | Shauren <shauren.trinity@gmail.com> | 2023-08-24 11:48:45 +0200 |
|---|---|---|
| committer | Shauren <shauren.trinity@gmail.com> | 2023-08-24 11:48:45 +0200 |
| commit | 451314241dc40c4e3be600ef95a4f4fbd322f701 (patch) | |
| tree | 1c55abe5e0027df578a792015254e4b95b8e6d80 /src/common | |
| parent | 343d09bc95ade0cc34f953b56cbe666baca387fc (diff) | |
Core/Misc: Modernize comparison operators
(cherry picked from commit f0a862e71bc12d86a898901ef773475a7c964832)
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/Cryptography/BigNumber.cpp | 2 | ||||
| -rw-r--r-- | src/common/Cryptography/BigNumber.h | 15 | ||||
| -rw-r--r-- | src/common/Debugging/WheatyExceptionReport.h | 8 | ||||
| -rw-r--r-- | src/common/Utilities/SmartEnum.h | 9 | ||||
| -rw-r--r-- | src/common/Utilities/TaskScheduler.h | 13 | ||||
| -rw-r--r-- | src/common/Utilities/Util.h | 12 | ||||
| -rw-r--r-- | src/common/Utilities/advstd.h | 29 |
7 files changed, 50 insertions, 38 deletions
diff --git a/src/common/Cryptography/BigNumber.cpp b/src/common/Cryptography/BigNumber.cpp index 62e459c6dce..35bfc91224d 100644 --- a/src/common/Cryptography/BigNumber.cpp +++ b/src/common/Cryptography/BigNumber.cpp @@ -146,7 +146,7 @@ BigNumber& BigNumber::operator<<=(int n) return *this; } -int BigNumber::CompareTo(BigNumber const& bn) const +int32 BigNumber::CompareTo(BigNumber const& bn) const { return BN_cmp(_bn, bn._bn); } diff --git a/src/common/Cryptography/BigNumber.h b/src/common/Cryptography/BigNumber.h index e61a8073c31..bbe2bebb42d 100644 --- a/src/common/Cryptography/BigNumber.h +++ b/src/common/Cryptography/BigNumber.h @@ -94,12 +94,17 @@ class TC_COMMON_API BigNumber return t <<= n; } - int CompareTo(BigNumber const& bn) const; - bool operator<=(BigNumber const& bn) const { return (CompareTo(bn) <= 0); } + int32 CompareTo(BigNumber const& bn) const; bool operator==(BigNumber const& bn) const { return (CompareTo(bn) == 0); } - bool operator>=(BigNumber const& bn) const { return (CompareTo(bn) >= 0); } - bool operator<(BigNumber const& bn) const { return (CompareTo(bn) < 0); } - bool operator>(BigNumber const& bn) const { return (CompareTo(bn) > 0); } + std::strong_ordering operator<=>(BigNumber const& other) const + { + int32 cmp = CompareTo(other); + if (cmp < 0) + return std::strong_ordering::less; + if (cmp > 0) + return std::strong_ordering::greater; + return std::strong_ordering::equal; + } bool IsZero() const; bool IsNegative() const; diff --git a/src/common/Debugging/WheatyExceptionReport.h b/src/common/Debugging/WheatyExceptionReport.h index 2b99dcbb83c..b1876528a78 100644 --- a/src/common/Debugging/WheatyExceptionReport.h +++ b/src/common/Debugging/WheatyExceptionReport.h @@ -7,6 +7,7 @@ #include <winnt.h> #include <winternl.h> #include <dbghelp.h> +#include <compare> #include <set> #include <cstdlib> #include <cstdio> @@ -101,11 +102,8 @@ struct SymbolPair _offset = offset; } - bool operator<(SymbolPair const& other) const - { - return _offset < other._offset || - (_offset == other._offset && _type < other._type); - } + bool operator==(SymbolPair const& other) const = default; + std::strong_ordering operator<=>(SymbolPair const& other) const = default; DWORD _type; DWORD_PTR _offset; diff --git a/src/common/Utilities/SmartEnum.h b/src/common/Utilities/SmartEnum.h index 24e3c60bd30..3c58d433406 100644 --- a/src/common/Utilities/SmartEnum.h +++ b/src/common/Utilities/SmartEnum.h @@ -85,13 +85,10 @@ class EnumUtils Iterator() : _index(EnumUtils::Count<Enum>()) {} explicit Iterator(size_t index) : _index(index) { } - bool operator==(const Iterator& other) const { return other._index == _index; } - bool operator!=(const Iterator& other) const { return !operator==(other); } + bool operator==(const Iterator& other) const = default; + std::strong_ordering operator<=>(const Iterator& other) const = default; + difference_type operator-(Iterator const& other) const { return _index - other._index; } - bool operator<(const Iterator& other) const { return _index < other._index; } - bool operator<=(const Iterator& other) const { return _index <= other._index; } - bool operator>(const Iterator& other) const { return _index > other._index; } - bool operator>=(const Iterator& other) const { return _index >= other._index; } value_type operator[](difference_type d) const { return FromIndex<Enum>(_index + d); } value_type operator*() const { return operator[](0); } diff --git a/src/common/Utilities/TaskScheduler.h b/src/common/Utilities/TaskScheduler.h index 4d6761780be..3af5fc70a12 100644 --- a/src/common/Utilities/TaskScheduler.h +++ b/src/common/Utilities/TaskScheduler.h @@ -95,18 +95,13 @@ class TC_COMMON_API TaskScheduler Task& operator= (Task&& right) = delete; // Order tasks by its end - inline bool operator< (Task const& other) const + std::weak_ordering operator<=> (Task const& other) const { - return _end < other._end; - } - - inline bool operator> (Task const& other) const - { - return _end > other._end; + return _end <=> other._end; } // Compare tasks with its end - inline bool operator== (Task const& other) + bool operator== (Task const& other) const { return _end == other._end; } @@ -126,7 +121,7 @@ class TC_COMMON_API TaskScheduler bool operator() (TaskContainer const& left, TaskContainer const& right) const { return (*left.get()) < (*right.get()); - }; + } }; class TC_COMMON_API TaskQueue diff --git a/src/common/Utilities/Util.h b/src/common/Utilities/Util.h index d37b86f85e5..62c8655b65f 100644 --- a/src/common/Utilities/Util.h +++ b/src/common/Utilities/Util.h @@ -417,18 +417,6 @@ public: part[2] = p3; } - inline bool operator<(flag96 const& right) const - { - for (uint8 i = 3; i > 0; --i) - { - if (part[i - 1] < right.part[i - 1]) - return true; - else if (part[i - 1] > right.part[i - 1]) - return false; - } - return false; - } - inline bool operator==(flag96 const& right) const { return diff --git a/src/common/Utilities/advstd.h b/src/common/Utilities/advstd.h index ff2717c9755..4a26e00df52 100644 --- a/src/common/Utilities/advstd.h +++ b/src/common/Utilities/advstd.h @@ -18,9 +18,38 @@ #ifndef TRINITY_ADVSTD_H #define TRINITY_ADVSTD_H +#include <version> + +#ifdef __cpp_lib_bit_cast +#include <bit> +#else +#include <cstring> // for memcpy +#endif +#include <compare> + // this namespace holds implementations of upcoming stdlib features that our c++ version doesn't have yet namespace advstd { +// libc++ is missing these two +[[nodiscard]] constexpr bool is_eq(std::partial_ordering cmp) noexcept { return cmp == 0; } +[[nodiscard]] constexpr bool is_neq(std::partial_ordering cmp) noexcept { return cmp != 0; } + +#ifdef __cpp_lib_bit_cast +using std::bit_cast; +#else +// libstdc++ v10 is missing this +template <typename To, typename From, + std::enable_if_t<std::conjunction_v< + std::bool_constant<sizeof(To) == sizeof(From)>, + std::is_trivially_copyable<To>, + std::is_trivially_copyable<From>>, int> = 0> + [[nodiscard]] constexpr To bit_cast(From const& from) noexcept +{ + To to; + std::memcpy(&to, &from, sizeof(To)); + return to; +} +#endif } #endif |
