]> git.saurik.com Git - wxWidgets.git/commitdiff
added macros to avoid code repetition when defining comparison operators; use them...
authorVadim Zeitlin <vadim@wxwidgets.org>
Thu, 22 Mar 2007 18:03:02 +0000 (18:03 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Thu, 22 Mar 2007 18:03:02 +0000 (18:03 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@45019 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/defs.h
include/wx/string.h
include/wx/unichar.h

index e9ffe12880b5b347258f2372b7c08179a23ec10c..4394a9776cd67e3c70c7060765722c2c58085c07 100644 (file)
@@ -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 */
 /*  ---------------------------------------------------------------------------- */
index 79731a8e8dec15e22f569b47ddd5f7b61f636c25..73a09e30ccceb3d6ce1b9a15a4e0602f17c2bcbe 100644 (file)
@@ -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"
index 5c37f2df35bb1c394f27ed00a18ebc8ca64b199c..8917f569f6d0ae3b2ae14644480d47dcec2496f8 100644 (file)
@@ -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); }