From 55bc8da7260ebc391562a960e3a656ba4a4ef365 Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Sun, 17 Sep 2017 11:09:56 +0200 Subject: [PATCH] LIB_ITEMS_CONTAINER code formatting --- eeschema/class_libentry.cpp | 18 ++++++------- eeschema/lib_collectors.cpp | 2 ++ eeschema/lib_items.cpp | 50 ++++++++++++++++++++++++++++--------- 3 files changed, 49 insertions(+), 21 deletions(-) diff --git a/eeschema/class_libentry.cpp b/eeschema/class_libentry.cpp index 81f8c47acc..bc5a056cc1 100644 --- a/eeschema/class_libentry.cpp +++ b/eeschema/class_libentry.cpp @@ -580,18 +580,16 @@ void LIB_PART::AddDrawItem( LIB_ITEM* aItem ) LIB_ITEM* LIB_PART::GetNextDrawItem( LIB_ITEM* aItem, KICAD_T aType ) { - /* Return the next draw object pointer. - * If item is NULL return the first item of type in the list. - */ if( m_drawings.empty( aType ) ) return NULL; if( aItem == NULL ) return &( *( m_drawings.begin( aType ) ) ); - // Search for last item, assume aItem is of type aType + // Search for the last item, assume aItem is of type aType wxASSERT( ( aType == TYPE_NOT_INIT ) || ( aType == aItem->Type() ) ); LIB_ITEMS_CONTAINER::ITERATOR it = m_drawings.begin( aType ); + while( ( it != m_drawings.end( aType ) ) && ( aItem != &( *it ) ) ) ++it; @@ -599,6 +597,7 @@ LIB_ITEM* LIB_PART::GetNextDrawItem( LIB_ITEM* aItem, KICAD_T aType ) if( it != m_drawings.end( aType ) ) { ++it; + if( it != m_drawings.end( aType ) ) return &( *it ); } @@ -1492,7 +1491,7 @@ void LIB_PART::CopySelectedItems( const wxPoint& aOffset ) tmp.push_back( newItem ); } - for( auto item : tmp) + for( auto item : tmp ) m_drawings.push_back( item ); MoveSelectedItems( aOffset ); @@ -1598,7 +1597,11 @@ void LIB_PART::SetUnitCount( int aCount ) { int prevCount = m_unitCount; + // Temporary storage for new items, as adding new items directly to + // m_drawings may cause the buffer reallocation which invalidates the + // iterators std::vector< LIB_ITEM* > tmp; + for( LIB_ITEM& item : m_drawings ) { if( item.m_Unit != 1 ) @@ -1608,13 +1611,10 @@ void LIB_PART::SetUnitCount( int aCount ) { LIB_ITEM* newItem = (LIB_ITEM*) item.Clone(); newItem->m_Unit = j; - - // We cannot use push_back here, because when adding items in vector - // the buffer can be reallocated, that change the previous value of - // .begin() and .end() iterators and invalidate others iterators tmp.push_back( newItem ); } } + for( auto item : tmp ) m_drawings.push_back( item ); } diff --git a/eeschema/lib_collectors.cpp b/eeschema/lib_collectors.cpp index 1f922ab01b..f4110800bd 100644 --- a/eeschema/lib_collectors.cpp +++ b/eeschema/lib_collectors.cpp @@ -125,6 +125,8 @@ void LIB_COLLECTOR::Collect( LIB_ITEMS_CONTAINER& aItems, const KICAD_T aFilterL m_data.m_convert = aConvert; for( LIB_ITEM& item : aItems ) + { if( SEARCH_QUIT == item.Visit( m_inspector, NULL, m_ScanTypes ) ) break; + } } diff --git a/eeschema/lib_items.cpp b/eeschema/lib_items.cpp index e245bfb81b..99d5c34ab3 100644 --- a/eeschema/lib_items.cpp +++ b/eeschema/lib_items.cpp @@ -29,12 +29,14 @@ #include -void LIB_ITEMS_CONTAINER::push_back( LIB_ITEM *aItem ) +void LIB_ITEMS_CONTAINER::push_back( LIB_ITEM* aItem ) { operator[]( aItem->Type() ).push_back( aItem ); } -LIB_ITEMS_CONTAINER::ITERATOR LIB_ITEMS_CONTAINER::erase(const LIB_ITEMS_CONTAINER::ITERATOR &aIterator) + +LIB_ITEMS_CONTAINER::ITERATOR LIB_ITEMS_CONTAINER::erase( + const LIB_ITEMS_CONTAINER::ITERATOR& aIterator ) { LIB_ITEMS_CONTAINER::ITERATOR it( aIterator ); it.m_it = (*aIterator.m_parent)[ aIterator.m_curType ].erase( aIterator.m_it ); @@ -43,30 +45,35 @@ LIB_ITEMS_CONTAINER::ITERATOR LIB_ITEMS_CONTAINER::erase(const LIB_ITEMS_CONTAIN return it; } + LIB_ITEMS_CONTAINER::ITERATOR LIB_ITEMS_CONTAINER::begin( int aType ) { size_t bucket = ( aType != TYPE_NOT_INIT ) ? aType : first(); return ITERATOR( this, operator[]( bucket ).begin(), bucket, aType ); } -LIB_ITEMS_CONTAINER::ITERATOR LIB_ITEMS_CONTAINER::end(int aType) + +LIB_ITEMS_CONTAINER::ITERATOR LIB_ITEMS_CONTAINER::end( int aType ) { size_t bucket = ( aType != TYPE_NOT_INIT ) ? aType : last(); return ITERATOR( this, operator[]( bucket ).end(), bucket, aType ); } + LIB_ITEMS_CONTAINER::CONST_ITERATOR LIB_ITEMS_CONTAINER::begin( int aType ) const { size_t bucket = ( aType != TYPE_NOT_INIT ) ? aType : first(); return CONST_ITERATOR( this, operator[]( bucket ).begin(), bucket, aType ); } + LIB_ITEMS_CONTAINER::CONST_ITERATOR LIB_ITEMS_CONTAINER::end( int aType ) const { size_t bucket = ( aType != TYPE_NOT_INIT ) ? aType : last(); return CONST_ITERATOR( this, operator[]( bucket ).end(), bucket, aType ); } + size_t LIB_ITEMS_CONTAINER::size( int aType ) const { if( aType != TYPE_NOT_INIT ) @@ -76,6 +83,7 @@ size_t LIB_ITEMS_CONTAINER::size( int aType ) const else { size_t cnt = 0; + for( int i = 0; i < TYPES_COUNT; ++i) cnt += m_data[ i ].size(); @@ -83,24 +91,28 @@ size_t LIB_ITEMS_CONTAINER::size( int aType ) const } } + bool LIB_ITEMS_CONTAINER::empty( int aType ) const { return ( size( aType ) == 0 ); } + void LIB_ITEMS_CONTAINER::sort() { for( int i = 0; i < TYPES_COUNT; ++i ) m_data[ i ].sort(); } + void LIB_ITEMS_CONTAINER::unique() { for( int i = 0; i < TYPES_COUNT; ++i ) m_data[ i ].unique(); } -LIB_ITEMS &LIB_ITEMS_CONTAINER::operator[]( int aType ) + +LIB_ITEMS& LIB_ITEMS_CONTAINER::operator[]( int aType ) { if( ( aType < FIRST_TYPE ) || ( aType > LAST_TYPE ) ) throw std::out_of_range( "LIB_ITEMS_CONTAINER out of range" ); @@ -108,7 +120,8 @@ LIB_ITEMS &LIB_ITEMS_CONTAINER::operator[]( int aType ) return m_data[ aType - FIRST_TYPE ]; } -const LIB_ITEMS &LIB_ITEMS_CONTAINER::operator[]( int aType ) const + +const LIB_ITEMS& LIB_ITEMS_CONTAINER::operator[]( int aType ) const { if( ( aType < FIRST_TYPE ) || ( aType > LAST_TYPE ) ) throw std::out_of_range( "LIB_ITEMS_CONTAINER out of range" ); @@ -116,57 +129,69 @@ const LIB_ITEMS &LIB_ITEMS_CONTAINER::operator[]( int aType ) const return m_data[ aType - FIRST_TYPE ]; } + size_t LIB_ITEMS_CONTAINER::first() const { int i = 0; + while( ( i < TYPES_COUNT ) && ( m_data[ i ].empty() ) ) ++i; return ( i == TYPES_COUNT ) ? FIRST_TYPE : FIRST_TYPE + i; } + size_t LIB_ITEMS_CONTAINER::last() const { int i = TYPES_COUNT - 1; + while( ( i >= 0 ) && ( m_data[ i ].empty() ) ) --i; return ( i < 0 ) ? FIRST_TYPE : FIRST_TYPE + i; } + template< typename ITEM_TYPE > LIB_ITEMS_CONTAINER::ITERATOR_BASE& LIB_ITEMS_CONTAINER::ITERATOR_BASE::operator++() { if( m_it != (*m_parent)[ m_curType ].end() ) ++m_it; + validate(); return *this; } + template< typename ITEM_TYPE > -bool LIB_ITEMS_CONTAINER::ITERATOR_BASE::operator!=(const LIB_ITEMS_CONTAINER::ITERATOR_BASE &aOther) const +bool LIB_ITEMS_CONTAINER::ITERATOR_BASE::operator!=( + const LIB_ITEMS_CONTAINER::ITERATOR_BASE& aOther ) const { if( aOther.m_parent != m_parent ) return true; + if( aOther.m_filter != m_filter ) return true; + if( aOther.m_curType != m_curType ) return true; + return aOther.m_it != m_it; } + template< typename ITEM_TYPE > -LIB_ITEMS_CONTAINER::ITERATOR_BASE::ITERATOR_BASE( typename LIB_ITEMS_CONTAINER::ITERATOR_ADAPTER< ITEM_TYPE >::CONTAINER* aItems, - typename LIB_ITEMS_CONTAINER::ITERATOR_ADAPTER< ITEM_TYPE >::ITERATOR aIt, - int aBucket, int aType ) - : m_parent( aItems ) - , m_it( aIt ) - , m_curType( aBucket ) +LIB_ITEMS_CONTAINER::ITERATOR_BASE::ITERATOR_BASE( + typename LIB_ITEMS_CONTAINER::ITERATOR_ADAPTER< ITEM_TYPE >::CONTAINER* aItems, + typename LIB_ITEMS_CONTAINER::ITERATOR_ADAPTER< ITEM_TYPE >::ITERATOR aIt, + int aBucket, int aType ) + : m_parent( aItems ), m_it( aIt ), m_curType( aBucket ) { m_filter = ( aType != TYPE_NOT_INIT ); } + template< typename ITEM_TYPE > void LIB_ITEMS_CONTAINER::ITERATOR_BASE::validate() { @@ -176,6 +201,7 @@ void LIB_ITEMS_CONTAINER::ITERATOR_BASE::validate() { // switch to the next type (look for a not empty container) int nextType = m_curType; + do ++nextType; while( ( nextType <= LAST_TYPE ) && (*m_parent)[ nextType ].empty() );