diff --git a/eeschema/lib_items.cpp b/eeschema/lib_items.cpp index d1e7df0399..fec8f46e6f 100644 --- a/eeschema/lib_items.cpp +++ b/eeschema/lib_items.cpp @@ -35,7 +35,7 @@ LIB_ITEMS_LIST::ITERATOR::ITERATOR( LIB_ITEMS_MAP& aItems, int aType ) m_type = m_filter ? aType : FIRST_TYPE; m_it = (*m_parent)[m_type].begin(); - // be sure the enum order is correct for the type assert + // be sure the enum order is correct for the type static_assert( (int) ( ITERATOR::FIRST_TYPE ) < (int) ( ITERATOR::LAST_TYPE ), "fix FIRST_TYPE and LAST_TYPE definitions" ); wxASSERT( m_type >= ITERATOR::FIRST_TYPE && m_type <= ITERATOR::LAST_TYPE ); @@ -107,10 +107,7 @@ LIB_ITEMS_LIST::ITERATOR LIB_ITEMS_LIST::erase( const ITERATOR& aIterator ) { LIB_ITEMS_LIST::ITERATOR it( aIterator ); it.m_it = (*aIterator.m_parent)[aIterator.m_type].erase( aIterator.m_it ); - - // if we reached the end, then move to the next type - if( it.m_it == (*it.m_parent)[it.m_type].end() && !it.m_filter ) - ++it; + it.validate(); return it; } @@ -183,7 +180,26 @@ LIB_ITEMS_LIST::ITERATOR& LIB_ITEMS_LIST::ITERATOR::operator++() if( m_it != (*m_parent)[m_type].end() ) ++m_it; - // should the iterator move to the next type? + validate(); + + return *this; +} + + +bool LIB_ITEMS_LIST::ITERATOR::operator!=( const LIB_ITEMS_LIST::ITERATOR& aOther ) const +{ + wxASSERT( aOther.m_parent == m_parent ); + wxASSERT( aOther.m_filter == m_filter ); + wxASSERT( !m_filter || aOther.m_type == m_type ); + + return aOther.m_it != m_it; +} + + +void LIB_ITEMS_LIST::ITERATOR::validate() +{ + // for all-items iterators (unfiltered): check if this is the end of the + // current type container, if so switch to the next non-empty container if( m_it == (*m_parent)[m_type].end() && !m_filter ) { auto typeIt = m_parent->find( m_type ); @@ -202,16 +218,4 @@ LIB_ITEMS_LIST::ITERATOR& LIB_ITEMS_LIST::ITERATOR::operator++() m_type = typeIt->first; } } - - return *this; -} - - -bool LIB_ITEMS_LIST::ITERATOR::operator!=( const LIB_ITEMS_LIST::ITERATOR& aOther ) const -{ - wxASSERT( aOther.m_parent == m_parent ); - wxASSERT( aOther.m_filter == m_filter ); - wxASSERT( !m_filter || aOther.m_type == m_type ); - - return aOther.m_it != m_it; } diff --git a/eeschema/lib_items.h b/eeschema/lib_items.h index a1c207ed63..1a584ba0c0 100644 --- a/eeschema/lib_items.h +++ b/eeschema/lib_items.h @@ -81,6 +81,9 @@ public: */ ITERATOR( LIB_ITEMS_MAP& aItems, int aType = TYPE_NOT_INIT ); + ///> Assures the iterator is in a valid state. + void validate(); + ///> Wrapped container LIB_ITEMS_MAP* m_parent;