Make TreeItemId hashable using the internal ID for the hash value,

so IDs pointing to the same item will compare as equal and have the same hash values. This enables TreeItemIDs to be dictionary keys in Py3.
This commit is contained in:
Robin Dunn
2017-05-18 08:19:08 -07:00
parent 37d67272b0
commit a1bafc04df
3 changed files with 20 additions and 1 deletions

View File

@@ -42,6 +42,18 @@ def run():
return self->IsOk();
""")
c.addCppMethod('bool', '__eq__', '(const wxTreeItemId* other)', """\
return *self == *other;
""")
c.addCppMethod('bool', '__neq__', '(const wxTreeItemId* other)', """\
return *self != *other;
""")
c.addPyMethod('__hash__', '(self)', """\
return hash(int(self.GetID()))
""")
td = etgtools.TypedefDef(name='wxTreeItemIdValue', type='void*')
module.insertItemBefore(c, td)

View File

@@ -14,7 +14,7 @@
%ModuleHeaderCode
#include <wx/treebase.h>
// A wxTreeItemData that knows what to do with PyObjects for maintianing the refcount
// A wxTreeItemData that knows what to do with PyObjects for maintaining the refcount
class wxPyTreeItemData : public wxPyUserDataHelper<wxTreeItemData>
{
public:

View File

@@ -28,6 +28,13 @@ class treectrl_Tests(wtc.WidgetTestCase):
self.assertTrue(child is not root)
self.assertTrue(child != root)
# Can TreeItemId be a dictionary key?
d = dict()
d[root] = 'root'
d[child] = 'child'
assert d[root] == 'root'
assert d[r] == 'root'
def test_treectrlTreeItemData(self):
value = 'Some Python Object'