From 020f22de15b6f3b7e32be0359217f26df5ca6f75 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 6 Jan 2024 01:43:15 +0100 Subject: [PATCH] Define comparison and arithmetic operators as hidden friends Change these operators in all the other classes to hidden friends too. --- include/wx/longlong.h | 87 ++++++++++++++++++++++------------- include/wx/sharedptr.h | 24 +++++----- include/wx/windowid.h | 102 ++++++++++++++++++++--------------------- 3 files changed, 118 insertions(+), 95 deletions(-) diff --git a/include/wx/longlong.h b/include/wx/longlong.h index ff735a8fcd..cad5c250ae 100644 --- a/include/wx/longlong.h +++ b/include/wx/longlong.h @@ -197,6 +197,8 @@ public: { return wxLongLongNative(m_ll + ll.m_ll); } wxLongLongNative& operator+=(const wxLongLongNative& ll) { m_ll += ll.m_ll; return *this; } + friend wxLongLongNative operator+(long l, const wxLongLongNative& ll) + { return ll + l; } wxLongLongNative operator+(const wxLongLong_t ll) const { return wxLongLongNative(m_ll + ll); } @@ -221,6 +223,10 @@ public: { return wxLongLongNative(m_ll - ll.m_ll); } wxLongLongNative& operator-=(const wxLongLongNative& ll) { m_ll -= ll.m_ll; return *this; } + friend wxLongLongNative operator-(long l, const wxLongLongNative& ll) + { + return wxLongLongNative(l) - ll; + } wxLongLongNative operator-(const wxLongLong_t ll) const { return wxLongLongNative(m_ll - ll); } @@ -314,6 +320,13 @@ public: bool operator>=(long l) const { return m_ll >= l; } + friend bool operator<(long l, const wxLongLongNative& ll) { return ll > l; } + friend bool operator>(long l, const wxLongLongNative& ll) { return ll < l; } + friend bool operator<=(long l, const wxLongLongNative& ll) { return ll >= l; } + friend bool operator>=(long l, const wxLongLongNative& ll) { return ll <= l; } + friend bool operator==(long l, const wxLongLongNative& ll) { return ll == l; } + friend bool operator!=(long l, const wxLongLongNative& ll) { return ll != l; } + // miscellaneous // return the string representation of this number @@ -421,6 +434,8 @@ public: { return wxULongLongNative(m_ll + ll.m_ll); } wxULongLongNative& operator+=(const wxULongLongNative& ll) { m_ll += ll.m_ll; return *this; } + friend wxULongLongNative operator+(unsigned long l, const wxULongLongNative& ull) + { return ull + l; } wxULongLongNative operator+(const wxULongLong_t ll) const { return wxULongLongNative(m_ll + ll); } @@ -440,6 +455,10 @@ public: { return wxULongLongNative(m_ll - ll.m_ll); } wxULongLongNative& operator-=(const wxULongLongNative& ll) { m_ll -= ll.m_ll; return *this; } + friend wxULongLongNative operator-(unsigned long l, const wxULongLongNative& ull) + { + return wxULongLongNative(l - ull.m_ll); + } wxULongLongNative operator-(const wxULongLong_t ll) const { return wxULongLongNative(m_ll - ll); } @@ -533,6 +552,13 @@ public: bool operator>=(unsigned long l) const { return m_ll >= l; } + friend bool operator<(unsigned long l, const wxULongLongNative& ull) { return ull > l; } + friend bool operator>(unsigned long l, const wxULongLongNative& ull) { return ull < l; } + friend bool operator<=(unsigned long l, const wxULongLongNative& ull) { return ull >= l; } + friend bool operator>=(unsigned long l, const wxULongLongNative& ull) { return ull <= l; } + friend bool operator==(unsigned long l, const wxULongLongNative& ull) { return ull == l; } + friend bool operator!=(unsigned long l, const wxULongLongNative& ull) { return ull != l; } + // miscellaneous // return the string representation of this number @@ -696,6 +722,8 @@ public: wxLongLongWx operator+(long l) const; wxLongLongWx& operator+=(long l); + friend wxLongLongWx operator+(long l, const wxLongLongWx& ll) { return ll + l; } + // pre increment operator wxLongLongWx& operator++(); @@ -709,6 +737,10 @@ public: // subtraction wxLongLongWx operator-(const wxLongLongWx& ll) const; wxLongLongWx& operator-=(const wxLongLongWx& ll); + friend wxLongLongWx operator-(long l, const wxLongLongWx& ll) + { + return wxLongLongWx(l) - ll; + } // pre decrement operator wxLongLongWx& operator--(); @@ -761,6 +793,13 @@ public: bool operator<=(long l) const { return *this < l || *this == l; } bool operator>=(long l) const { return *this > l || *this == l; } + friend bool operator<(long l, const wxLongLongWx& ll) { return ll > l; } + friend bool operator>(long l, const wxLongLongWx& ll) { return ll < l; } + friend bool operator<=(long l, const wxLongLongWx& ll) { return ll >= l; } + friend bool operator>=(long l, const wxLongLongWx& ll) { return ll <= l; } + friend bool operator==(long l, const wxLongLongWx& ll) { return ll == l; } + friend bool operator!=(long l, const wxLongLongWx& ll) { return ll != l; } + // multiplication wxLongLongWx operator*(const wxLongLongWx& ll) const; wxLongLongWx& operator*=(const wxLongLongWx& ll); @@ -920,6 +959,8 @@ public: wxULongLongWx& operator+=(const wxULongLongWx& ll); wxULongLongWx operator+(unsigned long l) const; wxULongLongWx& operator+=(unsigned long l); + friend wxULongLongWx operator+(unsigned long l, const wxULongLongWx& ull) + { return ull + l; } // pre increment operator wxULongLongWx& operator++(); @@ -931,6 +972,13 @@ public: wxLongLongWx operator-(const wxULongLongWx& ll) const; wxULongLongWx& operator-=(const wxULongLongWx& ll); + friend wxLongLongWx operator-(unsigned long l, const wxULongLongWx& ull) + { + const wxULongLongWx ret = wxULongLongWx(l) - ull; + return wxLongLongWx((wxInt32)ret.GetHi(),ret.GetLo()); + } + + // pre decrement operator wxULongLongWx& operator--(); @@ -977,6 +1025,13 @@ public: bool operator<=(unsigned long l) const { return *this < l || *this == l; } bool operator>=(unsigned long l) const { return *this > l || *this == l; } + friend bool operator<(unsigned long l, const wxULongLongWx& ull) { return ull > l; } + friend bool operator>(unsigned long l, const wxULongLongWx& ull) { return ull < l; } + friend bool operator<=(unsigned long l, const wxULongLongWx& ull) { return ull >= l; } + friend bool operator>=(unsigned long l, const wxULongLongWx& ull) { return ull <= l; } + friend bool operator==(unsigned long l, const wxULongLongWx& ull) { return ull == l; } + friend bool operator!=(unsigned long l, const wxULongLongWx& ull) { return ull != l; } + // multiplication wxULongLongWx operator*(const wxULongLongWx& ll) const; wxULongLongWx& operator*=(const wxULongLongWx& ll); @@ -1031,38 +1086,6 @@ private: #endif // wxUSE_LONGLONG_WX -// ---------------------------------------------------------------------------- -// binary operators -// ---------------------------------------------------------------------------- - -inline bool operator<(long l, const wxLongLong& ll) { return ll > l; } -inline bool operator>(long l, const wxLongLong& ll) { return ll < l; } -inline bool operator<=(long l, const wxLongLong& ll) { return ll >= l; } -inline bool operator>=(long l, const wxLongLong& ll) { return ll <= l; } -inline bool operator==(long l, const wxLongLong& ll) { return ll == l; } -inline bool operator!=(long l, const wxLongLong& ll) { return ll != l; } - -inline wxLongLong operator+(long l, const wxLongLong& ll) { return ll + l; } -inline wxLongLong operator-(long l, const wxLongLong& ll) -{ - return wxLongLong(l) - ll; -} - -inline bool operator<(unsigned long l, const wxULongLong& ull) { return ull > l; } -inline bool operator>(unsigned long l, const wxULongLong& ull) { return ull < l; } -inline bool operator<=(unsigned long l, const wxULongLong& ull) { return ull >= l; } -inline bool operator>=(unsigned long l, const wxULongLong& ull) { return ull <= l; } -inline bool operator==(unsigned long l, const wxULongLong& ull) { return ull == l; } -inline bool operator!=(unsigned long l, const wxULongLong& ull) { return ull != l; } - -inline wxULongLong operator+(unsigned long l, const wxULongLong& ull) { return ull + l; } - -inline wxLongLong operator-(unsigned long l, const wxULongLong& ull) -{ - const wxULongLong ret = wxULongLong(l) - ull; - return wxLongLong((wxInt32)ret.GetHi(),ret.GetLo()); -} - #if wxUSE_LONGLONG_NATIVE && wxUSE_STREAMS WXDLLIMPEXP_BASE class wxTextOutputStream &operator<<(class wxTextOutputStream &stream, wxULongLong_t value); diff --git a/include/wx/sharedptr.h b/include/wx/sharedptr.h index ba332ac2cf..1b88a77ee6 100644 --- a/include/wx/sharedptr.h +++ b/include/wx/sharedptr.h @@ -110,6 +110,18 @@ public: bool unique() const { return (m_ref ? m_ref->m_count == 1 : true); } long use_count() const { return (m_ref ? (long)m_ref->m_count : 0); } + template + friend bool operator == (wxSharedPtr const &a, wxSharedPtr const &b ) + { + return a.get() == b.get(); + } + + template + friend bool operator != (wxSharedPtr const &a, wxSharedPtr const &b ) + { + return a.get() != b.get(); + } + private: struct reftype @@ -154,16 +166,4 @@ private: } }; -template -bool operator == (wxSharedPtr const &a, wxSharedPtr const &b ) -{ - return a.get() == b.get(); -} - -template -bool operator != (wxSharedPtr const &a, wxSharedPtr const &b ) -{ - return a.get() != b.get(); -} - #endif // _WX_SHAREDPTR_H_ diff --git a/include/wx/windowid.h b/include/wx/windowid.h index a6618b6188..2d448169d6 100644 --- a/include/wx/windowid.h +++ b/include/wx/windowid.h @@ -74,6 +74,57 @@ public: return *this; } + // comparison operators + friend bool operator==(const wxWindowIDRef& lhs, const wxWindowIDRef& rhs) + { + return lhs.GetValue() == rhs.GetValue(); + } + + friend bool operator==(const wxWindowIDRef& lhs, int rhs) + { + return lhs.GetValue() == rhs; + } + + friend bool operator==(const wxWindowIDRef& lhs, long rhs) + { + return lhs.GetValue() == rhs; + } + + friend bool operator==(int lhs, const wxWindowIDRef& rhs) + { + return rhs == lhs; + } + + friend bool operator==(long lhs, const wxWindowIDRef& rhs) + { + return rhs == lhs; + } + + friend bool operator!=(const wxWindowIDRef& lhs, const wxWindowIDRef& rhs) + { + return !(lhs == rhs); + } + + friend bool operator!=(const wxWindowIDRef& lhs, int rhs) + { + return !(lhs == rhs); + } + + friend bool operator!=(const wxWindowIDRef& lhs, long rhs) + { + return !(lhs == rhs); + } + + friend bool operator!=(int lhs, const wxWindowIDRef& rhs) + { + return !(lhs == rhs); + } + + friend bool operator!=(long lhs, const wxWindowIDRef& rhs) + { + return !(lhs == rhs); + } + // access to the stored id value wxWindowID GetValue() const { @@ -114,57 +165,6 @@ private: wxWindowID m_id; }; -// comparison operators -inline bool operator==(const wxWindowIDRef& lhs, const wxWindowIDRef& rhs) -{ - return lhs.GetValue() == rhs.GetValue(); -} - -inline bool operator==(const wxWindowIDRef& lhs, int rhs) -{ - return lhs.GetValue() == rhs; -} - -inline bool operator==(const wxWindowIDRef& lhs, long rhs) -{ - return lhs.GetValue() == rhs; -} - -inline bool operator==(int lhs, const wxWindowIDRef& rhs) -{ - return rhs == lhs; -} - -inline bool operator==(long lhs, const wxWindowIDRef& rhs) -{ - return rhs == lhs; -} - -inline bool operator!=(const wxWindowIDRef& lhs, const wxWindowIDRef& rhs) -{ - return !(lhs == rhs); -} - -inline bool operator!=(const wxWindowIDRef& lhs, int rhs) -{ - return !(lhs == rhs); -} - -inline bool operator!=(const wxWindowIDRef& lhs, long rhs) -{ - return !(lhs == rhs); -} - -inline bool operator!=(int lhs, const wxWindowIDRef& rhs) -{ - return !(lhs == rhs); -} - -inline bool operator!=(long lhs, const wxWindowIDRef& rhs) -{ - return !(lhs == rhs); -} - // ---------------------------------------------------------------------------- // wxIdManager // ----------------------------------------------------------------------------