diff --git a/samples/dataview/mymodels.cpp b/samples/dataview/mymodels.cpp index 30d8f71328..4c719a0785 100644 --- a/samples/dataview/mymodels.cpp +++ b/samples/dataview/mymodels.cpp @@ -135,12 +135,15 @@ void MyMusicTreeModel::Delete( const wxDataViewItem &item ) m_ninth = nullptr; // first remove the node from the parent's array of children; - // NOTE: MyMusicTreeModelNodePtrArray is only an array of _pointers_ - // thus removing the node from it doesn't result in freeing it - node->GetParent()->GetChildren().Remove( node ); - - // free the node - delete node; + auto& siblings = node->GetParent()->GetChildren(); + for ( auto it = siblings.begin(); it != siblings.end(); ++it ) + { + if ( it->get() == node ) + { + siblings.erase(it); + break; + } + } // notify control ItemDeleted( parent, item ); @@ -151,12 +154,7 @@ void MyMusicTreeModel::Clear() m_classical = nullptr; m_ninth = nullptr; - while (!m_root->GetChildren().IsEmpty()) - { - MyMusicTreeModelNode* node = m_root->GetNthChild(0); - m_root->GetChildren().Remove(node); - delete node; - } + m_root->GetChildren().clear(); Cleared(); } @@ -320,14 +318,12 @@ unsigned int MyMusicTreeModel::GetChildren( const wxDataViewItem &parent, return 0; } - unsigned int count = node->GetChildren().GetCount(); - for (unsigned int pos = 0; pos < count; pos++) + for ( const auto& child : node->GetChildren() ) { - MyMusicTreeModelNode *child = node->GetChildren().Item( pos ); - array.Add( wxDataViewItem( (void*) child ) ); + array.Add( wxDataViewItem( child.get() ) ); } - return count; + return array.size(); } @@ -514,7 +510,7 @@ void MyListModel::GetValueByRow( wxVariant &variant, case Col_Custom: { - IntToStringMap::const_iterator it = m_customColValues.find(row); + const auto it = m_customColValues.find(row); if ( it != m_customColValues.end() ) variant = it->second; else diff --git a/samples/dataview/mymodels.h b/samples/dataview/mymodels.h index 7a52e0f03f..ad113d83c2 100644 --- a/samples/dataview/mymodels.h +++ b/samples/dataview/mymodels.h @@ -8,18 +8,17 @@ // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -#include "wx/hashmap.h" -#include "wx/vector.h" - -WX_DECLARE_HASH_MAP(unsigned, wxString, wxIntegerHash, wxIntegerEqual, - IntToStringMap); +#include +#include +#include // ---------------------------------------------------------------------------- // MyMusicTreeModelNode: a node inside MyMusicTreeModel // ---------------------------------------------------------------------------- class MyMusicTreeModelNode; -WX_DEFINE_ARRAY_PTR( MyMusicTreeModelNode*, MyMusicTreeModelNodePtrArray ); +using MyMusicTreeModelNodePtr = std::unique_ptr; +using MyMusicTreeModelNodePtrArray = std::vector; class MyMusicTreeModelNode { @@ -49,16 +48,7 @@ public: m_container = true; } - ~MyMusicTreeModelNode() - { - // free all our children nodes - size_t count = m_children.GetCount(); - for (size_t i = 0; i < count; i++) - { - MyMusicTreeModelNode *child = m_children[i]; - delete child; - } - } + ~MyMusicTreeModelNode() = default; bool IsContainer() const { return m_container; } @@ -68,13 +58,13 @@ public: MyMusicTreeModelNodePtrArray& GetChildren() { return m_children; } MyMusicTreeModelNode* GetNthChild( unsigned int n ) - { return m_children.Item( n ); } + { return m_children.at( n ).get(); } void Insert( MyMusicTreeModelNode* child, unsigned int n) - { m_children.Insert( child, n); } + { m_children.insert( m_children.begin() + n, MyMusicTreeModelNodePtr(child) ); } void Append( MyMusicTreeModelNode* child ) - { m_children.Add( child ); } + { m_children.push_back( MyMusicTreeModelNodePtr(child) ); } unsigned int GetChildCount() const - { return m_children.GetCount(); } + { return m_children.size(); } public: // public to avoid getters/setters wxString m_title; @@ -87,7 +77,7 @@ public: // public to avoid getters/setters // needs to know in advance if a node is or _will be_ a container. // Thus implementing: // bool IsContainer() const - // { return m_children.GetCount()>0; } + // { return !m_children.empty(); } // doesn't work with wxGTK when MyMusicTreeModel::AddToClassical is called // AND the classical node was removed (a new node temporary without children // would be added to the control) @@ -223,10 +213,10 @@ public: unsigned int row, unsigned int col ) override; private: - wxVector m_toggleColValues; + std::vector m_toggleColValues; wxArrayString m_textColValues; wxArrayString m_iconColValues; - IntToStringMap m_customColValues; + std::unordered_map m_customColValues; wxBitmapBundle m_icon[2]; };