From fb52c2b67139da20b026475aa48d73522e6afe2b Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 22 Mar 2007 18:03:02 +0000 Subject: [PATCH] added macros to avoid code repetition when defining comparison operators; use them to replace existing operators in wxUniChar and wxUniCharRef (fixing bug in the operator== and != of the latter) and added comparison operators for const wxChar * and wxCStrData which are needed to compile existing code comparing pointers with s.c_str() git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@45019 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/defs.h | 46 ++++++++++ include/wx/string.h | 87 ++++++++----------- include/wx/unichar.h | 194 ++++++++++--------------------------------- 3 files changed, 124 insertions(+), 203 deletions(-) diff --git a/include/wx/defs.h b/include/wx/defs.h index e9ffe12880..4394a9776c 100644 --- a/include/wx/defs.h +++ b/include/wx/defs.h @@ -547,6 +547,52 @@ typedef int wxWindowID; /* integer on success as failure indicator */ #define wxNOT_FOUND (-1) +/* ---------------------------------------------------------------------------- */ +/* macros dealing with comparison operators */ +/* ---------------------------------------------------------------------------- */ + +/* + Expands into m(op, args...) for each op in the set { ==, !=, <, <=, >, >= }. + */ +#define wxFOR_ALL_COMPARISONS(m) \ + m(==) m(!=) m(>=) m(<=) m(>) m(<) + +#define wxFOR_ALL_COMPARISONS_1(m, x) \ + m(==,x) m(!=,x) m(>=,x) m(<=,x) m(>,x) m(<,x) + +#define wxFOR_ALL_COMPARISONS_2(m, x, y) \ + m(==,x,y) m(!=,x,y) m(>=,x,y) m(<=,x,y) m(>,x,y) m(<,x,y) + +#define wxFOR_ALL_COMPARISONS_3(m, x, y, z) \ + m(==,x,y,z) m(!=,x,y,z) m(>=,x,y,z) m(<=,x,y,z) m(>,x,y,z) m(<,x,y,z) + + +#define wxDEFINE_COMPARISON(op, T1, T2, cmp) \ + inline bool operator op(T1 x, T2 y) { return cmp(x, y, op); } + +#define wxDEFINE_COMPARISON_REV(op, T1, T2, cmp) \ + inline bool operator op(T2 y, T1 x) { return cmp(x, y, op); } + +/* + Define all 6 comparison operators (==, !=, <, <=, >, >=) for the given + types in the specified order. The implementation is provided by the cmp + macro. Normally wxDEFINE_ALL_COMPARISONS should be used as comparison + operators are usually symmetric. + */ +#define wxDEFINE_COMPARISONS(T1, T2, cmp) \ + wxFOR_ALL_COMPARISONS_3(wxDEFINE_COMPARISON, T1, T2, cmp) + +/* + This macro allows to define all 12 comparison operators (6 operators for + both orders of arguments) for the given types using the provided "cmp" + macro to implement the actual comparison: the macro is called with the 2 + arguments names, the first of type T1 and the second of type T2, and the + comparison operator being implemented. + */ +#define wxDEFINE_ALL_COMPARISONS(T1, T2, cmp) \ + wxFOR_ALL_COMPARISONS_3(wxDEFINE_COMPARISON, T1, T2, cmp) \ + wxFOR_ALL_COMPARISONS_3(wxDEFINE_COMPARISON_REV, T1, T2, cmp) + /* ---------------------------------------------------------------------------- */ /* macros to avoid compiler warnings */ /* ---------------------------------------------------------------------------- */ diff --git a/include/wx/string.h b/include/wx/string.h index 79731a8e8d..73a09e30cc 100644 --- a/include/wx/string.h +++ b/include/wx/string.h @@ -761,6 +761,9 @@ public: wxStringBase& operator+=(wchar_t ch) { return append(1, ch); } }; +// don't pollute the library user's name space +#undef wxASSERT_VALID_INDEX + #endif // !wxUSE_STL_BASED_WXSTRING // ---------------------------------------------------------------------------- @@ -1859,42 +1862,26 @@ private: // in compilation ambiguities when comparing std::string and wxString #if !wxUSE_STL_BASED_WXSTRING +#define wxCMP_WXCHAR_STRING(p, s, op) s.Cmp(p) op 0 + +wxDEFINE_ALL_COMPARISONS(const wxChar *, const wxString&, wxCMP_WXCHAR_STRING) + +#undef wxCMP_WXCHAR_STRING + +// note that there is an optimization in operator==() and !=(): we (quickly) +// checks the strings length first, before comparing their data inline bool operator==(const wxString& s1, const wxString& s2) { return (s1.Len() == s2.Len()) && (s1.Cmp(s2) == 0); } -inline bool operator==(const wxString& s1, const wxChar * s2) - { return s1.Cmp(s2) == 0; } -inline bool operator==(const wxChar * s1, const wxString& s2) - { return s2.Cmp(s1) == 0; } inline bool operator!=(const wxString& s1, const wxString& s2) { return (s1.Len() != s2.Len()) || (s1.Cmp(s2) != 0); } -inline bool operator!=(const wxString& s1, const wxChar * s2) - { return s1.Cmp(s2) != 0; } -inline bool operator!=(const wxChar * s1, const wxString& s2) - { return s2.Cmp(s1) != 0; } inline bool operator< (const wxString& s1, const wxString& s2) { return s1.Cmp(s2) < 0; } -inline bool operator< (const wxString& s1, const wxChar * s2) - { return s1.Cmp(s2) < 0; } -inline bool operator< (const wxChar * s1, const wxString& s2) - { return s2.Cmp(s1) > 0; } inline bool operator> (const wxString& s1, const wxString& s2) { return s1.Cmp(s2) > 0; } -inline bool operator> (const wxString& s1, const wxChar * s2) - { return s1.Cmp(s2) > 0; } -inline bool operator> (const wxChar * s1, const wxString& s2) - { return s2.Cmp(s1) < 0; } inline bool operator<=(const wxString& s1, const wxString& s2) { return s1.Cmp(s2) <= 0; } -inline bool operator<=(const wxString& s1, const wxChar * s2) - { return s1.Cmp(s2) <= 0; } -inline bool operator<=(const wxChar * s1, const wxString& s2) - { return s2.Cmp(s1) >= 0; } inline bool operator>=(const wxString& s1, const wxString& s2) { return s1.Cmp(s2) >= 0; } -inline bool operator>=(const wxString& s1, const wxChar * s2) - { return s1.Cmp(s2) >= 0; } -inline bool operator>=(const wxChar * s1, const wxString& s2) - { return s2.Cmp(s1) <= 0; } #if wxUSE_UNICODE inline bool operator==(const wxString& s1, const wxWCharBuffer& s2) @@ -1953,39 +1940,37 @@ inline bool operator!=(const wxString& s, wchar_t c) { return !s.IsSameAs(c); } // comparison with C string in Unicode build #if wxUSE_UNICODE -inline bool operator==(const wxString& s1, const char* s2) - { return s1 == wxString(s2); } -inline bool operator==(const char* s1, const wxString& s2) - { return wxString(s1) == s2; } -inline bool operator!=(const wxString& s1, const char* s2) - { return s1 != wxString(s2); } -inline bool operator!=(const char* s1, const wxString& s2) - { return wxString(s1) != s2; } -inline bool operator< (const wxString& s1, const char* s2) - { return s1 < wxString(s2); } -inline bool operator< (const char* s1, const wxString& s2) - { return wxString(s1) < s2; } -inline bool operator> (const wxString& s1, const char* s2) - { return s1 > wxString(s2); } -inline bool operator> (const char* s1, const wxString& s2) - { return wxString(s1) > s2; } -inline bool operator<=(const wxString& s1, const char* s2) - { return s1 <= wxString(s2); } -inline bool operator<=(const char* s1, const wxString& s2) - { return wxString(s1) <= s2; } -inline bool operator>=(const wxString& s1, const char* s2) - { return s1 >= wxString(s2); } -inline bool operator>=(const char* s1, const wxString& s2) - { return wxString(s1) >= s2; } + +#define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s + +wxDEFINE_ALL_COMPARISONS(const char *, const wxString&, wxCMP_CHAR_STRING) + +#undef wxCMP_CHAR_STRING + #endif // wxUSE_UNICODE +// we also need to provide the operators for comparison with wxCStrData to +// resolve ambiguity between operator(const wxChar *,const wxString &) and +// operator(const wxChar *, const wxChar *) for "p == s.c_str()" +// +// notice that these are (shallow) pointer comparisons, not (deep) string ones +#define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar() +#define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar() + +// FIXME: these ifdefs must be removed when wxCStrData has both conversions +#if wxUSE_UNICODE + wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData&, wxCMP_WCHAR_CSTRDATA) +#else + wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData&, wxCMP_CHAR_CSTRDATA) +#endif + +#undef wxCMP_CHAR_CSTRDATA +#undef wxCMP_WCHAR_CSTRDATA + // --------------------------------------------------------------------------- // Implementation only from here until the end of file // --------------------------------------------------------------------------- -// don't pollute the library user's name space -#undef wxASSERT_VALID_INDEX - #if wxUSE_STD_IOSTREAM #include "wx/iosfwrap.h" diff --git a/include/wx/unichar.h b/include/wx/unichar.h index 5c37f2df35..8917f569f6 100644 --- a/include/wx/unichar.h +++ b/include/wx/unichar.h @@ -80,49 +80,28 @@ public: wxUniChar& operator=(wint_t c) { m_value = c; return *this; } #endif - // Comparision operators: - bool operator==(const wxUniChar& c) const { return m_value == c.m_value; } - bool operator==(char c) const { return m_value == From8bit(c); } - bool operator==(wchar_t c) const { return m_value == (value_type)c; } -#ifndef wxWINT_T_IS_TYPEDEF - bool operator==(wint_t c) const { return m_value == (value_type)c; } -#endif + // Comparison operators: - bool operator!=(const wxUniChar& c) const { return m_value != c.m_value; } - bool operator!=(char c) const { return m_value != From8bit(c); } - bool operator!=(wchar_t c) const { return m_value != (value_type)c; } -#ifndef wxWINT_T_IS_TYPEDEF - bool operator!=(wint_t c) const { return m_value != (value_type)c; } -#endif - - bool operator>(const wxUniChar& c) const { return m_value > c.m_value; } - bool operator>(char c) const { return m_value > (value_type)c; } - bool operator>(wchar_t c) const { return m_value > (value_type)c; } -#ifndef wxWINT_T_IS_TYPEDEF - bool operator>(wint_t c) const { return m_value > (value_type)c; } -#endif + // define the given comparison operator for all the types +#define wxDEFINE_UNICHAR_OPERATOR_NO_WINT(op) \ + bool operator op(const wxUniChar& c) const { return m_value op c.m_value; }\ + bool operator op(char c) const { return m_value op From8bit(c); } \ + bool operator op(wchar_t c) const { return m_value op (value_type)c; } - bool operator<(const wxUniChar& c) const { return m_value < c.m_value; } - bool operator<(char c) const { return m_value < From8bit(c); } - bool operator<(wchar_t c) const { return m_value < (value_type)c; } -#ifndef wxWINT_T_IS_TYPEDEF - bool operator<(wint_t c) const { return m_value < (value_type)c; } +#ifdef wxWINT_T_IS_TYPEDEF + #define wxDEFINE_UNICHAR_OPERATOR wxDEFINE_UNICHAR_OPERATOR_NO_WINT +#else // wint_t is a separate type, need to overload for it too + #define wxDEFINE_UNICHAR_OPERATOR(op) \ + wxDEFINE_UNICHAR_OPERATOR_NO_WINT(op) \ + bool operator op(wint_t c) const { return m_value op (value_type)c; } #endif - bool operator>=(const wxUniChar& c) const { return m_value >= c.m_value; } - bool operator>=(char c) const { return m_value >= From8bit(c); } - bool operator>=(wchar_t c) const { return m_value >= (value_type)c; } -#ifndef wxWINT_T_IS_TYPEDEF - bool operator>=(wint_t c) const { return m_value >= (value_type)c; } -#endif + wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHAR_OPERATOR) - bool operator<=(const wxUniChar& c) const { return m_value <= c.m_value; } - bool operator<=(char c) const { return m_value <= From8bit(c); } - bool operator<=(wchar_t c) const { return m_value <= (value_type)c; } -#ifndef wxWINT_T_IS_TYPEDEF - bool operator<=(wint_t c) const { return m_value <= (value_type)c; } -#endif +#undef wxDEFINE_UNICHAR_OPERATOR_NO_WINT +#undef wxDEFINE_UNICHAR_OPERATOR + // this is needed for expressions like 'Z'-c int operator-(const wxUniChar& c) const { return m_value - c.m_value; } int operator-(char c) const { return m_value - From8bit(c); } int operator-(wchar_t c) const { return m_value - (value_type)c; } @@ -197,54 +176,25 @@ public: bool operator&&(bool v) const { return UniChar() && v; } #endif - // Comparision operators: - bool operator==(const wxUniCharRef& c) const { return m_pos == c.m_pos; } - bool operator==(const wxUniChar& c) const { return UniChar() == c; } - bool operator==(char c) const { return UniChar() == c; } - bool operator==(wchar_t c) const { return UniChar() == c; } -#ifndef wxWINT_T_IS_TYPEDEF - bool operator==(wint_t c) const { return UniChar() == c; } -#endif + // Comparison operators: +#define wxDEFINE_UNICHARREF_OPERATOR_NO_WINT(op) \ + bool operator op(const wxUniCharRef& c) const { return UniChar() op c.UniChar(); }\ + bool operator op(const wxUniChar& c) const { return UniChar() op c; } \ + bool operator op(char c) const { return UniChar() op c; } \ + bool operator op(wchar_t c) const { return UniChar() op c; } - bool operator!=(const wxUniCharRef& c) const { return m_pos != c.m_pos; } - bool operator!=(const wxUniChar& c) const { return UniChar() != c; } - bool operator!=(char c) const { return UniChar() != c; } - bool operator!=(wchar_t c) const { return UniChar() != c; } -#ifndef wxWINT_T_IS_TYPEDEF - bool operator!=(wint_t c) const { return UniChar() != c; } +#ifdef wxWINT_T_IS_TYPEDEF + #define wxDEFINE_UNICHARREF_OPERATOR wxDEFINE_UNICHARREF_OPERATOR_NO_WINT +#else // wint_t is a separate type, need to overload for it too + #define wxDEFINE_UNICHARREF_OPERATOR(op) \ + wxDEFINE_UNICHARREF_OPERATOR_NO_WINT(op) \ + bool operator op(wint_t c) const { return UniChar() op c; } #endif - bool operator>(const wxUniCharRef& c) const { return UniChar() > c.UniChar(); } - bool operator>(const wxUniChar& c) const { return UniChar() > c; } - bool operator>(char c) const { return UniChar() > c; } - bool operator>(wchar_t c) const { return UniChar() > c; } -#ifndef wxWINT_T_IS_TYPEDEF - bool operator>(wint_t c) const { return UniChar() > c; } -#endif + wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHARREF_OPERATOR) - bool operator<(const wxUniCharRef& c) const { return UniChar() < c.UniChar(); } - bool operator<(const wxUniChar& c) const { return UniChar() < c; } - bool operator<(char c) const { return UniChar() < c; } - bool operator<(wchar_t c) const { return UniChar() < c; } -#ifndef wxWINT_T_IS_TYPEDEF - bool operator<(wint_t c) const { return UniChar() < c; } -#endif - - bool operator>=(const wxUniCharRef& c) const { return UniChar() >= c.UniChar(); } - bool operator>=(const wxUniChar& c) const { return UniChar() >= c; } - bool operator>=(char c) const { return UniChar() >= c; } - bool operator>=(wchar_t c) const { return UniChar() >= c; } -#ifndef wxWINT_T_IS_TYPEDEF - bool operator>=(wint_t c) const { return UniChar() >= c; } -#endif - - bool operator<=(const wxUniCharRef& c) const { return UniChar() <= c.UniChar(); } - bool operator<=(const wxUniChar& c) const { return UniChar() <= c; } - bool operator<=(char c) const { return UniChar() <= c; } - bool operator<=(wchar_t c) const { return UniChar() <= c; } -#ifndef wxWINT_T_IS_TYPEDEF - bool operator<=(wint_t c) const { return UniChar() <= c; } -#endif +#undef wxDEFINE_UNICHARREF_OPERATOR_NO_WINT +#undef wxDEFINE_UNICHARREF_OPERATOR // for expressions like c-'A': int operator-(const wxUniCharRef& c) const { return UniChar() - c.UniChar(); } @@ -269,85 +219,25 @@ inline wxUniChar::wxUniChar(const wxUniCharRef& c) m_value = c.UniChar().m_value; } -// Comparision operators for the case when wxUniChar(Ref) is the second operand: -inline bool operator==(char c1, const wxUniChar& c2) { return c2 == c1; } -inline bool operator==(wchar_t c1, const wxUniChar& c2) { return c2 == c1; } -#ifndef wxWINT_T_IS_TYPEDEF -inline bool operator==(wint_t c1, const wxUniChar& c2) { return c2 == c1; } -#endif +// Comparison operators for the case when wxUniChar(Ref) is the second operand +// implemented in terms of member comparison functions -inline bool operator!=(char c1, const wxUniChar& c2) { return c2 != c1; } -inline bool operator!=(wchar_t c1, const wxUniChar& c2) { return c2 != c1; } -#ifndef wxWINT_T_IS_TYPEDEF -inline bool operator!=(wint_t c1, const wxUniChar& c2) { return c2 != c1; } -#endif - -inline bool operator>(char c1, const wxUniChar& c2) { return c2 < c1; } -inline bool operator>(wchar_t c1, const wxUniChar& c2) { return c2 < c1; } -#ifndef wxWINT_T_IS_TYPEDEF -inline bool operator>(wint_t c1, const wxUniChar& c2) { return c2 < c1; } -#endif - -inline bool operator<(char c1, const wxUniChar& c2) { return c2 > c1; } -inline bool operator<(wchar_t c1, const wxUniChar& c2) { return c2 > c1; } -#ifndef wxWINT_T_IS_TYPEDEF -inline bool operator<(wint_t c1, const wxUniChar& c2) { return c2 > c1; } -#endif - -inline bool operator>=(char c1, const wxUniChar& c2) { return c2 <= c1; } -inline bool operator>=(wchar_t c1, const wxUniChar& c2) { return c2 <= c1; } -#ifndef wxWINT_T_IS_TYPEDEF -inline bool operator>=(wint_t c1, const wxUniChar& c2) { return c2 <= c1; } -#endif - -inline bool operator<=(char c1, const wxUniChar& c2) { return c2 >= c1; } -inline bool operator<=(wchar_t c1, const wxUniChar& c2) { return c2 >= c1; } -#ifndef wxWINT_T_IS_TYPEDEF -inline bool operator<=(wint_t c1, const wxUniChar& c2) { return c2 >= c1; } -#endif +#define wxCMP_REVERSE(c1, c2, op) c2 op c1 +wxDEFINE_COMPARISONS(char, const wxUniChar&, wxCMP_REVERSE) +wxDEFINE_COMPARISONS(char, const wxUniCharRef&, wxCMP_REVERSE) -inline bool operator==(char c1, const wxUniCharRef& c2) { return c2 == c1; } -inline bool operator==(wchar_t c1, const wxUniCharRef& c2) { return c2 == c1; } -#ifndef wxWINT_T_IS_TYPEDEF -inline bool operator==(wint_t c1, const wxUniCharRef& c2) { return c2 == c1; } -#endif -inline bool operator==(const wxUniChar& c1, const wxUniCharRef& c2) { return c2 == c1; } +wxDEFINE_COMPARISONS(wchar_t, const wxUniChar&, wxCMP_REVERSE) +wxDEFINE_COMPARISONS(wchar_t, const wxUniCharRef&, wxCMP_REVERSE) -inline bool operator!=(char c1, const wxUniCharRef& c2) { return c2 != c1; } -inline bool operator!=(wchar_t c1, const wxUniCharRef& c2) { return c2 != c1; } #ifndef wxWINT_T_IS_TYPEDEF -inline bool operator!=(wint_t c1, const wxUniCharRef& c2) { return c2 != c1; } +wxDEFINE_COMPARISONS(wint_t, const wxUniChar&, wxCMP_REVERSE) +wxDEFINE_COMPARISONS(wint_t, const wxUniCharRef&, wxCMP_REVERSE) #endif -inline bool operator!=(const wxUniChar& c1, const wxUniCharRef& c2) { return c2 != c1; } -inline bool operator>(char c1, const wxUniCharRef& c2) { return c2 < c1; } -inline bool operator>(wchar_t c1, const wxUniCharRef& c2) { return c2 < c1; } -#ifndef wxWINT_T_IS_TYPEDEF -inline bool operator>(wint_t c1, const wxUniCharRef& c2) { return c2 < c1; } -#endif -inline bool operator>(const wxUniChar& c1, const wxUniCharRef& c2) { return c2 < c1; } +wxDEFINE_COMPARISONS(const wxUniChar&, const wxUniCharRef&, wxCMP_REVERSE) -inline bool operator<(char c1, const wxUniCharRef& c2) { return c2 > c1; } -inline bool operator<(wchar_t c1, const wxUniCharRef& c2) { return c2 > c1; } -#ifndef wxWINT_T_IS_TYPEDEF -inline bool operator<(wint_t c1, const wxUniCharRef& c2) { return c2 > c1; } -#endif -inline bool operator<(const wxUniChar& c1, const wxUniCharRef& c2) { return c2 > c1; } - -inline bool operator>=(char c1, const wxUniCharRef& c2) { return c2 <= c1; } -inline bool operator>=(wchar_t c1, const wxUniCharRef& c2) { return c2 <= c1; } -#ifndef wxWINT_T_IS_TYPEDEF -inline bool operator>=(wint_t c1, const wxUniCharRef& c2) { return c2 <= c1; } -#endif -inline bool operator>=(const wxUniChar& c1, const wxUniCharRef& c2) { return c2 <= c1; } - -inline bool operator<=(char c1, const wxUniCharRef& c2) { return c2 >= c1; } -inline bool operator<=(wchar_t c1, const wxUniCharRef& c2) { return c2 >= c1; } -#ifndef wxWINT_T_IS_TYPEDEF -inline bool operator<=(wint_t c1, const wxUniCharRef& c2) { return c2 >= c1; } -#endif -inline bool operator<=(const wxUniChar& c1, const wxUniCharRef& c2) { return c2 >= c1; } +#undef wxCMP_REVERSE // for expressions like c-'A': inline int operator-(char c1, const wxUniCharRef& c2) { return -(c2 - c1); } -- 2.45.2