Core/Misc: Modernize comparison operators

(cherry picked from commit f0a862e71b)
This commit is contained in:
Shauren
2023-08-24 11:48:45 +02:00
parent 343d09bc95
commit 451314241d
21 changed files with 77 additions and 106 deletions

View File

@@ -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);
}

View File

@@ -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;

View File

@@ -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;

View File

@@ -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); }

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -22,6 +22,7 @@
#include "EnumFlag.h"
#include "ObjectGuid.h"
#include "WaypointDefines.h"
#include "advstd.h"
#include <limits>
#include <map>
#include <string>
@@ -1578,9 +1579,19 @@ struct SmartScriptHolder
operator bool() const { return entryOrGuid != 0; }
// Default comparision operator using priority field as first ordering field
bool operator<(SmartScriptHolder const& other) const
std::strong_ordering operator<=>(SmartScriptHolder const& right) const
{
return std::tie(priority, entryOrGuid, source_type, event_id, link) < std::tie(other.priority, other.entryOrGuid, other.source_type, other.event_id, other.link);
if (std::strong_ordering cmp = priority <=> right.priority; advstd::is_neq(cmp))
return cmp;
if (std::strong_ordering cmp = entryOrGuid <=> right.entryOrGuid; advstd::is_neq(cmp))
return cmp;
if (std::strong_ordering cmp = source_type <=> right.source_type; advstd::is_neq(cmp))
return cmp;
if (std::strong_ordering cmp = event_id <=> right.event_id; advstd::is_neq(cmp))
return cmp;
if (std::strong_ordering cmp = link <=> right.link; advstd::is_neq(cmp))
return cmp;
return std::strong_ordering::equal;
}
static constexpr uint32 DEFAULT_PRIORITY = std::numeric_limits<uint32>::max();

View File

@@ -664,6 +664,8 @@ struct TC_GAME_API QuaternionData
bool isUnit() const;
void toEulerAnglesZYX(float& Z, float& Y, float& X) const;
static QuaternionData fromEulerAnglesZYX(float Z, float Y, float X);
friend bool operator==(QuaternionData const& left, QuaternionData const& right) = default;
};
// `gameobject_addon` table

View File

@@ -203,9 +203,8 @@ class TC_GAME_API ObjectGuid
TypeID GetTypeId() const { return GetTypeId(GetHigh()); }
bool operator!() const { return IsEmpty(); }
bool operator==(ObjectGuid const& guid) const { return GetRawValue() == guid.GetRawValue(); }
bool operator!=(ObjectGuid const& guid) const { return GetRawValue() != guid.GetRawValue(); }
bool operator< (ObjectGuid const& guid) const { return GetRawValue() < guid.GetRawValue(); }
bool operator==(ObjectGuid const& right) const = default;
std::strong_ordering operator<=>(ObjectGuid const& right) const = default;
static char const* GetTypeName(HighGuid high);
char const* GetTypeName() const { return !IsEmpty() ? GetTypeName(GetHigh()) : "None"; }

View File

@@ -62,7 +62,6 @@ private:
public:
bool operator==(Position const& a) const;
bool operator!=(Position const& a) const { return !(operator==(a)); }
void Relocate(float x, float y) { m_positionX = x; m_positionY = y; }
void Relocate(float x, float y, float z) { Relocate(x, y); m_positionZ = z; }

View File

@@ -77,11 +77,7 @@ struct TempSummonGroupKey
{
}
bool operator<(TempSummonGroupKey const& rhs) const
{
return std::tie(_summonerEntry, _summonerType, _summonGroup) <
std::tie(rhs._summonerEntry, rhs._summonerType, rhs._summonGroup);
}
std::strong_ordering operator<=>(TempSummonGroupKey const& right) const = default;
private:
uint32 _summonerEntry; ///< Summoner's entry
@@ -608,7 +604,6 @@ struct QuestRelationResult
}
bool operator==(Iterator const& other) const { return _it == other._it; }
bool operator!=(Iterator const& other) const { return _it != other._it; }
Iterator& operator++() { ++_it; skip(); return *this; }
Iterator operator++(int) { Iterator t = *this; ++*this; return t; }

View File

@@ -91,7 +91,6 @@ struct Cell
}
bool operator == (Cell const& cell) const { return (data.All == cell.data.All); }
bool operator != (Cell const& cell) const { return !operator == (cell); }
union
{
struct

View File

@@ -155,22 +155,12 @@ struct CoordPair
return y_coord * LIMIT + x_coord;
}
friend bool operator==(CoordPair const& p1, CoordPair const& p2) = default;
uint32 x_coord;
uint32 y_coord;
};
template<uint32 LIMIT>
bool operator==(const CoordPair<LIMIT> &p1, const CoordPair<LIMIT> &p2)
{
return (p1.x_coord == p2.x_coord && p1.y_coord == p2.y_coord);
}
template<uint32 LIMIT>
bool operator!=(const CoordPair<LIMIT> &p1, const CoordPair<LIMIT> &p2)
{
return !(p1 == p2);
}
typedef CoordPair<MAX_NUMBER_OF_GRIDS> GridCoord;
typedef CoordPair<TOTAL_NUMBER_OF_CELLS_PER_MAP> CellCoord;

View File

@@ -64,10 +64,7 @@ struct CreatureTextId
{
CreatureTextId(uint32 e, uint32 g, uint32 i) : entry(e), textGroup(g), textId(i) { }
bool operator<(CreatureTextId const& right) const
{
return std::tie(entry, textGroup, textId) < std::tie(right.entry, right.textGroup, right.textId);
}
friend std::strong_ordering operator<=>(CreatureTextId const& left, CreatureTextId const& right) = default;
uint32 entry;
uint32 textGroup;

View File

@@ -45,7 +45,6 @@ public:
T const* operator*() const { return _index[_pos]; }
bool operator==(DBCStorageIterator const& right) const { /*ASSERT(_index == right._index, "Iterator belongs to a different container")*/ return _pos == right._pos; }
bool operator!=(DBCStorageIterator const& right) const { return !(*this == right); }
DBCStorageIterator& operator++()
{

View File

@@ -160,7 +160,7 @@ class LinkedListHead
{ // construct with null node pointer
}
Iterator(pointer _Pnode) : _Ptr(_Pnode)
explicit Iterator(pointer _Pnode) : _Ptr(_Pnode)
{ // construct with node pointer _Pnode
}
@@ -206,41 +206,19 @@ class LinkedListHead
return (_Tmp);
}
bool operator==(Iterator const& _Right) const
{ // test for iterator equality
return (_Ptr == _Right._Ptr);
}
bool operator!=(Iterator const& _Right) const
{ // test for iterator inequality
return (!(*this == _Right));
}
bool operator==(Iterator const& _Right) const = default;
// test for iterator equality
bool operator==(pointer const& _Right) const
{ // test for pointer equality
return (_Ptr != _Right);
}
bool operator!=(pointer const& _Right) const
{ // test for pointer equality
return (!(*this == _Right));
}
bool operator==(const_reference _Right) const
{ // test for reference equality
return (_Ptr == &_Right);
}
bool operator!=(const_reference _Right) const
{ // test for reference equality
return (_Ptr != &_Right);
}
pointer _Mynode()
{ // return node pointer
return (_Ptr);
}
protected:
pointer _Ptr; // pointer to node
};

View File

@@ -20,6 +20,7 @@
#include "Common.h"
#include "AsioHacksFwd.h"
#include <compare>
#include <memory>
enum RealmFlags
@@ -42,10 +43,7 @@ struct TC_SHARED_API RealmHandle
uint32 Realm; // primary key in `realmlist` table
bool operator<(RealmHandle const& r) const
{
return Realm < r.Realm;
}
std::strong_ordering operator<=>(RealmHandle const& r) const { return Realm <=> r.Realm; }
};
/// Type of server, this is values from second column of Cfg_Configs.dbc

View File

@@ -106,10 +106,6 @@ public:
{
return record.offset == b.record.offset;
}
bool operator!=(Iterator const& b) const
{
return record.offset != b.record.offset;
}
private:
Record record;

View File

@@ -45,7 +45,7 @@ namespace MMAP
uint32 m_mapId;
std::set<uint32>* m_tiles;
bool operator==(uint32 id)
bool operator==(uint32 id) const
{
return m_mapId == id;
}

View File

@@ -123,10 +123,6 @@ public:
{
return record.offset == b.record.offset;
}
bool operator!=(Iterator const& b) const
{
return record.offset != b.record.offset;
}
private:
Record record;
};