From a2d3c415d79ab3a90239b2fcfe9767800e56462d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20Slav=C3=ADk?= Date: Sat, 27 Aug 2011 16:44:42 +0000 Subject: [PATCH] Remove DestroyTreeHelper(), make GetChildNodes() const, add RemoveChild(). There's no reason for DestroyTreeHelper() code, that's what destructors are for. On a related note, make removal of children from a node more explicit too. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@68930 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/generic/datavgen.cpp | 44 +++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/src/generic/datavgen.cpp b/src/generic/datavgen.cpp index c14a1dcd5a..6b9456a636 100644 --- a/src/generic/datavgen.cpp +++ b/src/generic/datavgen.cpp @@ -291,7 +291,18 @@ public: ~wxDataViewTreeNode() { - delete m_branchData; + if ( m_branchData ) + { + wxDataViewTreeNodes& nodes = m_branchData->children; + for ( wxDataViewTreeNodes::iterator i = nodes.begin(); + i != nodes.end(); + ++i ) + { + delete *i; + } + + delete m_branchData; + } } static wxDataViewTreeNode* CreateRootNode() @@ -304,7 +315,7 @@ public: wxDataViewTreeNode * GetParent() const { return m_parent; } - wxDataViewTreeNodes& GetChildNodes() + const wxDataViewTreeNodes& GetChildNodes() const { wxASSERT( m_branchData != NULL ); return m_branchData->children; @@ -321,6 +332,12 @@ public: m_branchData->children.Sort( &wxGenericTreeModelNodeCmp ); } + void RemoveChild(wxDataViewTreeNode *node) + { + wxCHECK_RET( m_branchData != NULL, "leaf node doesn't have children" ); + m_branchData->children.Remove(node); + } + const wxDataViewItem & GetItem() const { return m_item; } void SetItem( const wxDataViewItem & item ) { m_item = item; } @@ -2012,8 +2029,6 @@ bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem & parent, const wxData return true; } -static void DestroyTreeHelper( wxDataViewTreeNode * node); - bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem& parent, const wxDataViewItem& item) { @@ -2089,8 +2104,8 @@ bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem& parent, // Delete the item from wxDataViewTreeNode representation: const int itemsDeleted = 1 + itemNode->GetSubTreeCount(); - parentNode->GetChildNodes().Remove(itemNode); - ::DestroyTreeHelper(itemNode); + parentNode->RemoveChild(itemNode); + delete itemNode; parentNode->ChangeSubTreeCount(-itemsDeleted); // Make the row number invalid and get a new valid one when user call GetRowCount @@ -3257,25 +3272,12 @@ void wxDataViewMainWindow::BuildTree(wxDataViewModel * model) m_count = -1; } -static void DestroyTreeHelper( wxDataViewTreeNode * node ) -{ - if ( node->HasChildren() ) - { - wxDataViewTreeNodes& nodes = node->GetChildNodes(); - const int len = nodes.size(); - for (int i = 0; i < len; i++) - DestroyTreeHelper(nodes[i]); - } - delete node; -} - void wxDataViewMainWindow::DestroyTree() { if (!IsVirtualList()) { - ::DestroyTreeHelper(m_root); - m_count = 0; - m_root = NULL; + wxDELETE(m_root); + m_count = 0; } }