Use operator<=> for some simplest data types.

This commit is contained in:
John Preston 2022-10-06 18:45:28 +04:00
parent 2c50f7b18c
commit 24d3bcb590
2 changed files with 17 additions and 185 deletions

View file

@ -14,6 +14,8 @@ struct MsgId {
constexpr MsgId(int64 value) noexcept : bare(value) { constexpr MsgId(int64 value) noexcept : bare(value) {
} }
friend inline constexpr auto operator<=>(MsgId, MsgId) = default;
[[nodiscard]] constexpr explicit operator bool() const noexcept { [[nodiscard]] constexpr explicit operator bool() const noexcept {
return (bare != 0); return (bare != 0);
} }
@ -49,30 +51,6 @@ Q_DECLARE_METATYPE(MsgId);
return MsgId(a.bare - b.bare); return MsgId(a.bare - b.bare);
} }
[[nodiscard]] inline constexpr bool operator==(MsgId a, MsgId b) noexcept {
return (a.bare == b.bare);
}
[[nodiscard]] inline constexpr bool operator!=(MsgId a, MsgId b) noexcept {
return (a.bare != b.bare);
}
[[nodiscard]] inline constexpr bool operator<(MsgId a, MsgId b) noexcept {
return (a.bare < b.bare);
}
[[nodiscard]] inline constexpr bool operator>(MsgId a, MsgId b) noexcept {
return (a.bare > b.bare);
}
[[nodiscard]] inline constexpr bool operator<=(MsgId a, MsgId b) noexcept {
return (a.bare <= b.bare);
}
[[nodiscard]] inline constexpr bool operator>=(MsgId a, MsgId b) noexcept {
return (a.bare >= b.bare);
}
constexpr auto StartClientMsgId = MsgId(0x01 - (1LL << 58)); constexpr auto StartClientMsgId = MsgId(0x01 - (1LL << 58));
constexpr auto EndClientMsgId = MsgId(-(1LL << 57)); constexpr auto EndClientMsgId = MsgId(-(1LL << 57));
constexpr auto ServerMaxMsgId = MsgId(1LL << 56); constexpr auto ServerMaxMsgId = MsgId(1LL << 56);
@ -103,22 +81,12 @@ struct MsgRange {
, till(till) { , till(till) {
} }
friend inline constexpr bool operator==(MsgRange, MsgRange) = default;
MsgId from = 0; MsgId from = 0;
MsgId till = 0; MsgId till = 0;
}; };
[[nodiscard]] inline constexpr bool operator==(
MsgRange a,
MsgRange b) noexcept {
return (a.from == b.from) && (a.till == b.till);
}
[[nodiscard]] inline constexpr bool operator!=(
MsgRange a,
MsgRange b) noexcept {
return !(a == b);
}
struct FullMsgId { struct FullMsgId {
constexpr FullMsgId() noexcept = default; constexpr FullMsgId() noexcept = default;
constexpr FullMsgId(PeerId peer, MsgId msg) noexcept constexpr FullMsgId(PeerId peer, MsgId msg) noexcept
@ -126,6 +94,8 @@ struct FullMsgId {
} }
FullMsgId(ChannelId channelId, MsgId msgId) = delete; FullMsgId(ChannelId channelId, MsgId msgId) = delete;
friend inline constexpr auto operator<=>(FullMsgId, FullMsgId) = default;
constexpr explicit operator bool() const noexcept { constexpr explicit operator bool() const noexcept {
return msg != 0; return msg != 0;
} }
@ -137,53 +107,16 @@ struct FullMsgId {
MsgId msg = 0; MsgId msg = 0;
}; };
[[nodiscard]] inline constexpr bool operator<(
const FullMsgId &a,
const FullMsgId &b) noexcept {
if (a.peer < b.peer) {
return true;
} else if (a.peer > b.peer) {
return false;
}
return a.msg < b.msg;
}
[[nodiscard]] inline constexpr bool operator>(
const FullMsgId &a,
const FullMsgId &b) noexcept {
return b < a;
}
[[nodiscard]] inline constexpr bool operator<=(
const FullMsgId &a,
const FullMsgId &b) noexcept {
return !(b < a);
}
[[nodiscard]] inline constexpr bool operator>=(
const FullMsgId &a,
const FullMsgId &b) noexcept {
return !(a < b);
}
[[nodiscard]] inline constexpr bool operator==(
const FullMsgId &a,
const FullMsgId &b) noexcept {
return (a.peer == b.peer) && (a.msg == b.msg);
}
[[nodiscard]] inline constexpr bool operator!=(
const FullMsgId &a,
const FullMsgId &b) noexcept {
return !(a == b);
}
Q_DECLARE_METATYPE(FullMsgId); Q_DECLARE_METATYPE(FullMsgId);
struct GlobalMsgId { struct GlobalMsgId {
FullMsgId itemId; FullMsgId itemId;
uint64 sessionUniqueId = 0; uint64 sessionUniqueId = 0;
friend inline constexpr auto operator<=>(
GlobalMsgId,
GlobalMsgId) = default;
constexpr explicit operator bool() const noexcept { constexpr explicit operator bool() const noexcept {
return itemId && sessionUniqueId; return itemId && sessionUniqueId;
} }
@ -192,48 +125,6 @@ struct GlobalMsgId {
} }
}; };
[[nodiscard]] inline constexpr bool operator<(
const GlobalMsgId &a,
const GlobalMsgId &b) noexcept {
if (a.itemId < b.itemId) {
return true;
} else if (a.itemId > b.itemId) {
return false;
}
return a.sessionUniqueId < b.sessionUniqueId;
}
[[nodiscard]] inline constexpr bool operator>(
const GlobalMsgId &a,
const GlobalMsgId &b) noexcept {
return b < a;
}
[[nodiscard]] inline constexpr bool operator<=(
const GlobalMsgId &a,
const GlobalMsgId &b) noexcept {
return !(b < a);
}
[[nodiscard]] inline constexpr bool operator>=(
const GlobalMsgId &a,
const GlobalMsgId &b) noexcept {
return !(a < b);
}
[[nodiscard]] inline constexpr bool operator==(
const GlobalMsgId &a,
const GlobalMsgId &b) noexcept {
return (a.itemId == b.itemId)
&& (a.sessionUniqueId == b.sessionUniqueId);
}
[[nodiscard]] inline constexpr bool operator!=(
const GlobalMsgId &a,
const GlobalMsgId &b) noexcept {
return !(a == b);
}
namespace std { namespace std {
template <> template <>

View file

@ -16,6 +16,7 @@ using PeerIdZero = void(PeerIdZeroHelper::*)();
template <uint8 Shift> template <uint8 Shift>
struct ChatIdType { struct ChatIdType {
BareId bare = 0; BareId bare = 0;
static constexpr BareId kShift = Shift; static constexpr BareId kShift = Shift;
static constexpr BareId kReservedBit = BareId(0x80); static constexpr BareId kReservedBit = BareId(0x80);
static_assert((Shift & kReservedBit) == 0, "Last bit is reserved."); static_assert((Shift & kReservedBit) == 0, "Last bit is reserved.");
@ -28,6 +29,10 @@ struct ChatIdType {
constexpr ChatIdType(MTPlong value) noexcept : bare(value.v) { constexpr ChatIdType(MTPlong value) noexcept : bare(value.v) {
} }
friend inline constexpr auto operator<=>(
ChatIdType,
ChatIdType) = default;
[[nodiscard]] constexpr explicit operator bool() const noexcept { [[nodiscard]] constexpr explicit operator bool() const noexcept {
return (bare != 0); return (bare != 0);
} }
@ -37,48 +42,6 @@ struct ChatIdType {
}; };
template <uchar Shift>
[[nodiscard]] inline constexpr bool operator==(
ChatIdType<Shift> a,
ChatIdType<Shift> b) noexcept {
return (a.bare == b.bare);
}
template <uchar Shift>
[[nodiscard]] inline constexpr bool operator!=(
ChatIdType<Shift> a,
ChatIdType<Shift> b) noexcept {
return (a.bare != b.bare);
}
template <uchar Shift>
[[nodiscard]] inline constexpr bool operator<(
ChatIdType<Shift> a,
ChatIdType<Shift> b) noexcept {
return (a.bare < b.bare);
}
template <uchar Shift>
[[nodiscard]] inline constexpr bool operator>(
ChatIdType<Shift> a,
ChatIdType<Shift> b) noexcept {
return (a.bare > b.bare);
}
template <uchar Shift>
[[nodiscard]] inline constexpr bool operator<=(
ChatIdType<Shift> a,
ChatIdType<Shift> b) noexcept {
return (a.bare <= b.bare);
}
template <uchar Shift>
[[nodiscard]] inline constexpr bool operator>=(
ChatIdType<Shift> a,
ChatIdType<Shift> b) noexcept {
return (a.bare >= b.bare);
}
template <uchar Shift> template <uchar Shift>
[[nodiscard]] inline constexpr bool operator==( [[nodiscard]] inline constexpr bool operator==(
ChatIdType<Shift> a, ChatIdType<Shift> a,
@ -158,6 +121,8 @@ struct PeerId {
constexpr PeerId(PeerIdHelper value) noexcept : value(value.value) { constexpr PeerId(PeerIdHelper value) noexcept : value(value.value) {
} }
friend inline constexpr auto operator<=>(PeerId, PeerId) = default;
template <typename SomeChatIdType, BareId = SomeChatIdType::kShift> template <typename SomeChatIdType, BareId = SomeChatIdType::kShift>
[[nodiscard]] constexpr bool is() const noexcept { [[nodiscard]] constexpr bool is() const noexcept {
return ((value >> 48) & BareId(0xFF)) == SomeChatIdType::kShift; return ((value >> 48) & BareId(0xFF)) == SomeChatIdType::kShift;
@ -177,30 +142,6 @@ struct PeerId {
}; };
[[nodiscard]] inline constexpr bool operator==(PeerId a, PeerId b) noexcept {
return (a.value == b.value);
}
[[nodiscard]] inline constexpr bool operator!=(PeerId a, PeerId b) noexcept {
return (a.value != b.value);
}
[[nodiscard]] inline constexpr bool operator<(PeerId a, PeerId b) noexcept {
return (a.value < b.value);
}
[[nodiscard]] inline constexpr bool operator>(PeerId a, PeerId b) noexcept {
return (a.value > b.value);
}
[[nodiscard]] inline constexpr bool operator<=(PeerId a, PeerId b) noexcept {
return (a.value <= b.value);
}
[[nodiscard]] inline constexpr bool operator>=(PeerId a, PeerId b) noexcept {
return (a.value >= b.value);
}
[[nodiscard]] inline constexpr bool operator==( [[nodiscard]] inline constexpr bool operator==(
PeerId a, PeerId a,
PeerIdZero) noexcept { PeerIdZero) noexcept {