From 24d3bcb5903e07c6630c0313befaa49684dcc0cd Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 6 Oct 2022 18:45:28 +0400 Subject: [PATCH] Use operator<=> for some simplest data types. --- Telegram/SourceFiles/data/data_msg_id.h | 129 ++--------------------- Telegram/SourceFiles/data/data_peer_id.h | 73 ++----------- 2 files changed, 17 insertions(+), 185 deletions(-) diff --git a/Telegram/SourceFiles/data/data_msg_id.h b/Telegram/SourceFiles/data/data_msg_id.h index 9f884b429..95d36fb55 100644 --- a/Telegram/SourceFiles/data/data_msg_id.h +++ b/Telegram/SourceFiles/data/data_msg_id.h @@ -14,6 +14,8 @@ struct MsgId { constexpr MsgId(int64 value) noexcept : bare(value) { } + friend inline constexpr auto operator<=>(MsgId, MsgId) = default; + [[nodiscard]] constexpr explicit operator bool() const noexcept { return (bare != 0); } @@ -49,30 +51,6 @@ Q_DECLARE_METATYPE(MsgId); 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 EndClientMsgId = MsgId(-(1LL << 57)); constexpr auto ServerMaxMsgId = MsgId(1LL << 56); @@ -103,22 +81,12 @@ struct MsgRange { , till(till) { } + friend inline constexpr bool operator==(MsgRange, MsgRange) = default; + MsgId from = 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 { constexpr FullMsgId() noexcept = default; constexpr FullMsgId(PeerId peer, MsgId msg) noexcept @@ -126,6 +94,8 @@ struct FullMsgId { } FullMsgId(ChannelId channelId, MsgId msgId) = delete; + friend inline constexpr auto operator<=>(FullMsgId, FullMsgId) = default; + constexpr explicit operator bool() const noexcept { return msg != 0; } @@ -137,53 +107,16 @@ struct FullMsgId { 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); struct GlobalMsgId { FullMsgId itemId; uint64 sessionUniqueId = 0; + friend inline constexpr auto operator<=>( + GlobalMsgId, + GlobalMsgId) = default; + constexpr explicit operator bool() const noexcept { 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 { template <> diff --git a/Telegram/SourceFiles/data/data_peer_id.h b/Telegram/SourceFiles/data/data_peer_id.h index c59d1f635..1f99d955f 100644 --- a/Telegram/SourceFiles/data/data_peer_id.h +++ b/Telegram/SourceFiles/data/data_peer_id.h @@ -16,6 +16,7 @@ using PeerIdZero = void(PeerIdZeroHelper::*)(); template struct ChatIdType { BareId bare = 0; + static constexpr BareId kShift = Shift; static constexpr BareId kReservedBit = BareId(0x80); static_assert((Shift & kReservedBit) == 0, "Last bit is reserved."); @@ -28,6 +29,10 @@ struct ChatIdType { constexpr ChatIdType(MTPlong value) noexcept : bare(value.v) { } + friend inline constexpr auto operator<=>( + ChatIdType, + ChatIdType) = default; + [[nodiscard]] constexpr explicit operator bool() const noexcept { return (bare != 0); } @@ -37,48 +42,6 @@ struct ChatIdType { }; -template -[[nodiscard]] inline constexpr bool operator==( - ChatIdType a, - ChatIdType b) noexcept { - return (a.bare == b.bare); -} - -template -[[nodiscard]] inline constexpr bool operator!=( - ChatIdType a, - ChatIdType b) noexcept { - return (a.bare != b.bare); -} - -template -[[nodiscard]] inline constexpr bool operator<( - ChatIdType a, - ChatIdType b) noexcept { - return (a.bare < b.bare); -} - -template -[[nodiscard]] inline constexpr bool operator>( - ChatIdType a, - ChatIdType b) noexcept { - return (a.bare > b.bare); -} - -template -[[nodiscard]] inline constexpr bool operator<=( - ChatIdType a, - ChatIdType b) noexcept { - return (a.bare <= b.bare); -} - -template -[[nodiscard]] inline constexpr bool operator>=( - ChatIdType a, - ChatIdType b) noexcept { - return (a.bare >= b.bare); -} - template [[nodiscard]] inline constexpr bool operator==( ChatIdType a, @@ -158,6 +121,8 @@ struct PeerId { constexpr PeerId(PeerIdHelper value) noexcept : value(value.value) { } + friend inline constexpr auto operator<=>(PeerId, PeerId) = default; + template [[nodiscard]] constexpr bool is() const noexcept { 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==( PeerId a, PeerIdZero) noexcept {