From 441e03172c58a7ebf1292fa5ef4a9e10637112d1 Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Thu, 16 Jan 2014 16:47:31 +0100 Subject: [PATCH] Added iterators for NETINFO_LIST (as net codes for existing net codes may be not consecutive). --- pcbnew/class_board.cpp | 7 +++-- pcbnew/class_board.h | 18 +++++++++++ pcbnew/class_netclass.cpp | 38 ++++++++++++----------- pcbnew/class_netinfo.h | 60 ++++++++++++++++++++++++++++++++++++ pcbnew/class_netinfolist.cpp | 4 +-- pcbnew/kicad_plugin.cpp | 17 ++++++---- 6 files changed, 115 insertions(+), 29 deletions(-) diff --git a/pcbnew/class_board.cpp b/pcbnew/class_board.cpp index acde874db3..5c435f634a 100644 --- a/pcbnew/class_board.cpp +++ b/pcbnew/class_board.cpp @@ -1440,10 +1440,11 @@ int BOARD::ReturnSortedNetnamesList( wxArrayString& aNames, bool aSortbyPadsCoun netBuffer.reserve( m_NetInfo.GetNetCount() ); - for( unsigned ii = 1; ii < m_NetInfo.GetNetCount(); ii++ ) + for( NETINFO_LIST::iterator net( m_NetInfo.begin() ), netEnd( m_NetInfo.end() ); + net != netEnd; ++net ) { - if( m_NetInfo.GetNetItem( ii )->GetNet() > 0 ) - netBuffer.push_back( m_NetInfo.GetNetItem( ii ) ); + if( net->GetNet() > 0 ) + netBuffer.push_back( *net ); } // sort the list diff --git a/pcbnew/class_board.h b/pcbnew/class_board.h index cd20413baa..c32d1d90e7 100644 --- a/pcbnew/class_board.h +++ b/pcbnew/class_board.h @@ -845,6 +845,24 @@ public: m_NetInfo.AppendNet( aNewNet ); } + /** + * Function BeginNets + * @return iterator to the first element of the NETINFO_ITEMs list + */ + NETINFO_LIST::iterator BeginNets() const + { + return m_NetInfo.begin(); + } + + /** + * Function EndNets + * @return iterator to the last element of the NETINFO_ITEMs list + */ + NETINFO_LIST::iterator EndNets() const + { + return m_NetInfo.end(); + } + /** * Function GetNetCount * @return the number of nets (NETINFO_ITEM) diff --git a/pcbnew/class_netclass.cpp b/pcbnew/class_netclass.cpp index f945963a95..655595a41d 100644 --- a/pcbnew/class_netclass.cpp +++ b/pcbnew/class_netclass.cpp @@ -203,12 +203,10 @@ void BOARD::SynchronizeNetsAndNetClasses() // set all NETs to the default NETCLASS, then later override some // as we go through the NETCLASSes. - int count = m_NetInfo.GetNetCount(); - for( int i=0; iSetClass( m_NetClasses.GetDefault() ); + net->SetClass( m_NetClasses.GetDefault() ); } // Add netclass name and pointer to nets. If a net is in more than one netclass, @@ -248,21 +246,18 @@ void BOARD::SynchronizeNetsAndNetClasses() m_NetClasses.GetDefault()->Clear(); - for( int i=0; iGetClassName(); + const wxString& classname = net->GetClassName(); - // because of the std:map<> this should be fast, and because of - // prior logic, netclass should not be NULL. - NETCLASS* netclass = m_NetClasses.Find( classname ); + // because of the std:map<> this should be fast, and because of + // prior logic, netclass should not be NULL. + NETCLASS* netclass = m_NetClasses.Find( classname ); - wxASSERT( netclass ); + wxASSERT( netclass ); - netclass->Add( net->GetNetname() ); - } + netclass->Add( net->GetNetname() ); } // D(printf("stop\n");) @@ -337,8 +332,15 @@ void NETCLASS::Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControl aFormatter->Print( aNestLevel+1, "(uvia_dia %s)\n", FMT_IU( GetuViaDiameter() ).c_str() ); aFormatter->Print( aNestLevel+1, "(uvia_drill %s)\n", FMT_IU( GetuViaDrill() ).c_str() ); - for( NETCLASS::const_iterator it = begin(); it!= end(); ++it ) - aFormatter->Print( aNestLevel+1, "(add_net %s)\n", aFormatter->Quotew( *it ).c_str() ); + for( NETCLASS::const_iterator it = begin(); it != end(); ++it ) + { + NETINFO_ITEM* netinfo = m_Parent->FindNet( *it ); + + if( netinfo && netinfo->GetNodesCount() > 0 ) + { + aFormatter->Print( aNestLevel+1, "(add_net %s)\n", aFormatter->Quotew( *it ).c_str() ); + } + } aFormatter->Print( aNestLevel, ")\n\n" ); } diff --git a/pcbnew/class_netinfo.h b/pcbnew/class_netinfo.h index 34f38188ae..326a0fa3b4 100644 --- a/pcbnew/class_netinfo.h +++ b/pcbnew/class_netinfo.h @@ -214,6 +214,66 @@ public: typedef boost::unordered_map NETNAMES_MAP; typedef boost::unordered_map NETCODES_MAP; + ///> Wrapper class, so you can iterate through NETINFO_ITEM*s, not + ///> std::pair + class iterator + { + public: + iterator( NETNAMES_MAP::const_iterator aIter ) : m_iterator( aIter ) + { + } + + /// pre-increment operator + const iterator& operator++() + { + ++m_iterator; + + return *this; + } + + /// post-increment operator + iterator operator++( int ) + { + iterator ret = *this; + ++m_iterator; + + return ret; + } + + NETINFO_ITEM* operator*() const + { + return m_iterator->second; + } + + NETINFO_ITEM* operator->() const + { + return m_iterator->second; + } + + bool operator!=( const iterator& aOther ) const + { + return m_iterator != aOther.m_iterator; + } + + bool operator==( const iterator& aOther ) const + { + return m_iterator == aOther.m_iterator; + } + + private: + NETNAMES_MAP::const_iterator m_iterator; + }; + + iterator begin() const + { + return iterator( m_netNames.begin() ); + } + + iterator end() const + { + return iterator( m_netNames.end() ); + } + private: /** * Function DeleteData diff --git a/pcbnew/class_netinfolist.cpp b/pcbnew/class_netinfolist.cpp index 556ed1d3d7..e57bbfed8b 100644 --- a/pcbnew/class_netinfolist.cpp +++ b/pcbnew/class_netinfolist.cpp @@ -90,8 +90,8 @@ void NETINFO_LIST::buildListOfNets() buildPadsFullList(); // Restore the initial state of NETINFO_ITEMs - for( unsigned i = 0; i < GetNetCount(); ++i ) - GetNetItem( i )->Clear(); + for( NETINFO_LIST::iterator net( begin() ), netEnd( end() ); net != netEnd; ++net ) + net->Clear(); // Assign pads to appropriate NETINFO_ITEMs for( unsigned ii = 0; ii < m_PadsFullList.size(); ii++ ) diff --git a/pcbnew/kicad_plugin.cpp b/pcbnew/kicad_plugin.cpp index ed6d7c062e..3cda985754 100644 --- a/pcbnew/kicad_plugin.cpp +++ b/pcbnew/kicad_plugin.cpp @@ -654,14 +654,19 @@ void PCB_IO::format( BOARD* aBoard, int aNestLevel ) const m_out->Print( aNestLevel, ")\n\n" ); - int netcount = aBoard->GetNetCount(); + // Unconditionally save the unconnected net + m_out->Print( aNestLevel, "(net 0 \"\")\n" ); - for( int i = 0; i < netcount; ++i ) + // and now the rest of nets + for( NETINFO_LIST::iterator net( aBoard->BeginNet() ), netEnd( aBoard->EndNet() ); + net != netEnd; ++net ) { - NETINFO_ITEM* net = aBoard->FindNet( i ); - m_out->Print( aNestLevel, "(net %d %s)\n", - net->GetNet(), - m_out->Quotew( net->GetNetname() ).c_str() ); + if( net->GetNodesCount() > 0 ) // save only not empty nets + { + m_out->Print( aNestLevel, "(net %d %s)\n", + net->GetNet(), + m_out->Quotew( net->GetNetname() ).c_str() ); + } } m_out->Print( 0, "\n" );