Add comparison and hash operators to wx.WindowIDRef

This commit is contained in:
Robin Dunn
2018-06-25 09:06:21 -07:00
parent a55c2fc685
commit 8a53aa610b
2 changed files with 31 additions and 1 deletions

View File

@@ -72,14 +72,22 @@ def run():
return self->GetValue();
""")
klass.addCppMethod('int', '__int__', '()',
doc="Alias for GetValue allowing the IDRef to be passed as the WindowID parameter when creating widgets or etc.",
body="""\
return self->GetValue();
""")
klass.addCppMethod('bool', '__eq__', '(wxWindowID id)', "return self->GetValue() == id;")
klass.addCppMethod('bool', '__ne__', '(wxWindowID id)', "return self->GetValue() != id;")
klass.addCppMethod('bool', '__lt__', '(wxWindowID id)', "return self->GetValue() < id;")
klass.addCppMethod('bool', '__gt__', '(wxWindowID id)', "return self->GetValue() > id;")
klass.addCppMethod('bool', '__le__', '(wxWindowID id)', "return self->GetValue() <= id;")
klass.addCppMethod('bool', '__ge__', '(wxWindowID id)', "return self->GetValue() >= id;")
klass.addPyMethod('__repr__', '(self)', 'return "WindowIDRef: {}".format(self.GetId())')
klass.addPyMethod('__hash__', '(self)', 'return hash(self.GetValue())')
# and finish it up by adding it to the module
module.addItem(klass)

View File

@@ -58,7 +58,29 @@ class IdManagerTest(wtc.WidgetTestCase):
b.Destroy()
def test_WindowIDRef01(self):
ref1 = wx.WindowIDRef(wx.IdManager.ReserveId())
ref2 = wx.WindowIDRef(wx.IdManager.ReserveId())
val = ref1 == ref2
assert type(val) == bool
val = ref1 != ref2
assert type(val) == bool
val = ref1 > ref2
assert type(val) == bool
val = ref1 < ref2
assert type(val) == bool
val = ref1 >= ref2
assert type(val) == bool
val = ref1 <= ref2
assert type(val) == bool
def test_WindowIDRef02(self):
d = {wx.NewIdRef(): 'one',
wx.NewIdRef(): 'two'}
keys = sorted(d.keys())
for k in keys:
val = d[k]
#---------------------------------------------------------------------------