From 6291fd7cc9fd6570b71f6bd7db2861655e407d0a Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Sat, 29 Dec 2012 02:33:14 +0000 Subject: [PATCH] Present the math operators for Point, Size, Rect as if they were methods of the class instead of global functions. This helps disambiguate between Point and Size functions due to how both can be equivalent to 2-element sequences. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxPython/Phoenix/trunk@73302 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- etg/gdicmn.py | 104 ++++++++++++++++++++++----------------- unittests/test_gdicmn.py | 29 ++++++++++- 2 files changed, 86 insertions(+), 47 deletions(-) diff --git a/etg/gdicmn.py b/etg/gdicmn.py index 47f42867..624f2b47 100644 --- a/etg/gdicmn.py +++ b/etg/gdicmn.py @@ -79,23 +79,23 @@ def run(): for f in c.find('operator+=').all() + c.find('operator-=').all(): f.ignore(False) - # Add some stand-alone function declarations for the operators that really do - # exist. - wc = etgtools.WigCode("""\ - bool operator==(const wxPoint& p1, const wxPoint& p2); - bool operator!=(const wxPoint& p1, const wxPoint& p2); - wxPoint operator+(const wxPoint& p, const wxSize& s); - wxPoint operator+(const wxPoint& p1, const wxPoint& p2); - wxPoint operator+(const wxSize& s, const wxPoint& p); - wxPoint operator-(const wxPoint& p); - wxPoint operator-(const wxPoint& p, const wxSize& s); - wxPoint operator-(const wxPoint& p1, const wxPoint& p2); - wxPoint operator-(const wxSize& s, const wxPoint& p); - wxPoint operator*(const wxPoint& s, int i); - wxPoint operator*(int i, const wxPoint& s); - wxPoint operator/(const wxPoint& s, int i); - """) - module.insertItemAfter(c, wc) + # Add some method declarations for operators that really do exist. Note + # that these actually use C++ global operator functions, but we treat + # them as methods to help disambiguate implementations due to how + # multiple classes can be converted automatically to/from 2-element + # sequences. + c.addCppMethod('bool', '__eq__', '(const wxPoint& other)', + body="return *self == *other;") + c.addCppMethod('bool', '__neq__', '(const wxPoint& other)', + body="return *self != *other;") + + c.addItem(etgtools.WigCode("""\ + wxPoint operator+(const wxPoint& other); + wxPoint operator-(); + wxPoint operator-(const wxPoint& other); + wxPoint operator*(int i); + wxPoint operator/(int i); + """)) # wxPoint typemap @@ -143,23 +143,30 @@ def run(): c.addProperty("x GetWidth SetWidth") c.addProperty("y GetHeight SetHeight") - # take care of the same issues as wxPoint + # Take care of the same issues as wxPoint tools.ignoreAllOperators(c) for f in c.find('operator+=').all() + \ c.find('operator-=').all() + \ c.find('operator*=').all() + \ c.find('operator/=').all(): f.ignore(False) - wc = etgtools.WigCode("""\ - bool operator==(const wxSize& s1, const wxSize& s2); - bool operator!=(const wxSize& s1, const wxSize& s2); - wxSize operator*(const wxSize& s, int i); - wxSize operator*(int i, const wxSize& s); - wxSize operator+(const wxSize& s1, const wxSize& s2); - wxSize operator-(const wxSize& s1, const wxSize& s2); - wxSize operator/(const wxSize& s, int i); - """) - module.insertItemAfter(c, wc) + + c.addCppMethod('bool', '__eq__', '(const wxSize& other)', + body="return *self == *other;") + c.addCppMethod('bool', '__neq__', '(const wxSize& other)', + body="return *self != *other;") + + c.addItem(etgtools.WigCode("""\ + wxSize operator+(const wxSize& other); + wxSize operator-(const wxSize& other); + wxSize operator*(int i); + wxSize operator/(int i); + + wxPoint operator+(const wxPoint& other); + wxPoint operator-(const wxPoint& other); + wxRealPoint operator+(const wxRealPoint& other); + wxRealPoint operator-(const wxRealPoint& other); + """)) # wxSize typemap @@ -209,13 +216,17 @@ def run(): for f in c.find('operator+=').all() + \ c.find('operator*=').all(): f.ignore(False) - wc = etgtools.WigCode("""\ - bool operator==(const wxRect& r1, const wxRect& r2); - bool operator!=(const wxRect& r1, const wxRect& r2); - wxRect operator+(const wxRect& r1, const wxRect& r2); - wxRect operator*(const wxRect& r1, const wxRect& r2); - """) - module.insertItemAfter(c, wc) + + c.addCppMethod('bool', '__eq__', '(const wxRect& other)', + body="return *self == *other;") + c.addCppMethod('bool', '__neq__', '(const wxRect& other)', + body="return *self != *other;") + + c.addItem(etgtools.WigCode("""\ + wxRect operator+(const wxRect& other); + wxRect operator*(const wxRect& other); + """)) + # Because of our add-ons that make wx.Point and wx.Size act like 2-element # sequences, and also the typecheck code that allows 2-element sequences, then @@ -274,17 +285,20 @@ def run(): for f in c.find('operator+=').all() + \ c.find('operator-=').all(): f.ignore(False) - wc = etgtools.WigCode("""\ - bool operator==(const wxRealPoint& p1, const wxRealPoint& p2); - bool operator!=(const wxRealPoint& p1, const wxRealPoint& p2); - wxRealPoint operator*(const wxRealPoint& s, double i); - wxRealPoint operator*(double i, const wxRealPoint& s); - wxRealPoint operator+(const wxRealPoint& p1, const wxRealPoint& p2); - wxRealPoint operator-(const wxRealPoint& p1, const wxRealPoint& p2); - wxRealPoint operator/(const wxRealPoint& s, int i); - """) - module.insertItemAfter(c, wc) + + c.addCppMethod('bool', '__eq__', '(const wxRealPoint& other)', + body="return *self == *other;") + c.addCppMethod('bool', '__neq__', '(const wxRealPoint& other)', + body="return *self != *other;") + c.addItem(etgtools.WigCode("""\ + wxRealPoint operator+(const wxRealPoint& other); + wxRealPoint operator-(const wxRealPoint& other); + wxRealPoint operator*(int i); + wxRealPoint operator/(int i); + """)) + + # wxRealPoint typemap c.convertFromPyObject = tools.convertTwoDoublesTemplate('wxRealPoint') diff --git a/unittests/test_gdicmn.py b/unittests/test_gdicmn.py index 293fd338..02baa376 100644 --- a/unittests/test_gdicmn.py +++ b/unittests/test_gdicmn.py @@ -65,7 +65,7 @@ class Point(unittest.TestCase): p = p1 - p2 p = p1 - wx.Size(5,5) p = p1 * 5 - p = 5 * p1 + #p = 5 * p1 p = p1 / 5 p1 += p2 p1 -= p2 @@ -83,6 +83,31 @@ class Point(unittest.TestCase): with self.assertRaises(IndexError): p[2] + def test_math(self): + p = wx.Point(4,6) + p1 = p + wx.Point(4,2) + p2 = p + (4,2) + p3 = p * 2 + p4 = p / 2 + p5 = p - wx.Point(4,2) + p6 = -p + + self.assertTrue(isinstance(p1, wx.Point)) + self.assertTrue(isinstance(p2, wx.Point)) + self.assertTrue(isinstance(p3, wx.Point)) + self.assertTrue(isinstance(p4, wx.Point)) + self.assertTrue(isinstance(p5, wx.Point)) + self.assertTrue(isinstance(p6, wx.Point)) + + self.assertEqual(p1, (8,8)) + self.assertEqual(p2, (8,8)) + self.assertEqual(p3, (8,12)) + self.assertEqual(p4, (2,3)) + self.assertEqual(p5, (0,4)) + self.assertEqual(p6, (-4,-6)) + + + #--------------------------------------------------------------------------- @@ -159,7 +184,7 @@ class Size(unittest.TestCase): s1 == s2 s1 != s2 s = s1 * 5 - s = 5 * s1 + #s = 5 * s1 s = s1 + s2 s = s1 - s2 s = s1 / 5