2008-11-24 04:34:58 +00:00
|
|
|
/*
|
2011-09-30 18:15:37 +00:00
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
2008-11-24 04:34:58 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2008 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
|
2011-09-30 18:15:37 +00:00
|
|
|
* Copyright (C) 1992-2008 KiCad Developers, see change_log.txt for contributors.
|
2008-11-24 04:34:58 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <fctsys.h>
|
|
|
|
#include <dlist.h>
|
|
|
|
#include <base_struct.h>
|
2008-11-24 04:34:58 +00:00
|
|
|
|
2018-05-04 14:10:18 +00:00
|
|
|
// verifies conditions for setting a parent list for an element, i.e.
|
|
|
|
// element either does not belong to any list or it already belongs to the same list
|
|
|
|
#define CHECK_OWNERSHIP(a) wxASSERT( !a->GetList() || a->GetList() == this );
|
2008-11-24 04:34:58 +00:00
|
|
|
|
|
|
|
/* Implement the class DHEAD from dlist.h */
|
|
|
|
|
|
|
|
|
|
|
|
DHEAD::~DHEAD()
|
|
|
|
{
|
|
|
|
if( meOwner )
|
2008-12-02 09:13:28 +00:00
|
|
|
DeleteAll();
|
2008-11-24 04:34:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-02 09:13:28 +00:00
|
|
|
void DHEAD::DeleteAll()
|
2008-11-24 04:34:58 +00:00
|
|
|
{
|
2018-05-04 14:10:18 +00:00
|
|
|
wxCHECK( meOwner, /*void*/ ); // only owning lists may delete the contents
|
2010-12-08 20:12:46 +00:00
|
|
|
EDA_ITEM* next;
|
|
|
|
EDA_ITEM* item = first;
|
2008-11-24 04:34:58 +00:00
|
|
|
|
|
|
|
while( item )
|
|
|
|
{
|
|
|
|
next = item->Next();
|
|
|
|
delete item; // virtual destructor, class specific
|
|
|
|
item = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
first = 0;
|
|
|
|
last = 0;
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-08 20:12:46 +00:00
|
|
|
void DHEAD::append( EDA_ITEM* aNewElement )
|
2008-11-24 04:34:58 +00:00
|
|
|
{
|
2018-05-04 13:10:44 +00:00
|
|
|
wxCHECK( aNewElement, /*void*/ );
|
2008-11-24 04:34:58 +00:00
|
|
|
|
|
|
|
if( first ) // list is not empty, first is not touched
|
|
|
|
{
|
2018-05-04 14:10:18 +00:00
|
|
|
wxASSERT( count > 0 );
|
|
|
|
wxCHECK( last, /*void*/ ); // 'first' is non-null, so 'last' should be non-null too
|
2008-11-24 04:34:58 +00:00
|
|
|
|
|
|
|
aNewElement->SetNext( 0 );
|
|
|
|
aNewElement->SetBack( last );
|
|
|
|
|
2018-05-04 14:10:18 +00:00
|
|
|
wxASSERT( !last->Next() ); // the last element should point to nullptr
|
2008-11-24 04:34:58 +00:00
|
|
|
last->SetNext( aNewElement );
|
|
|
|
last = aNewElement;
|
|
|
|
}
|
|
|
|
else // list is empty, first and last are changed
|
|
|
|
{
|
2018-05-04 14:10:18 +00:00
|
|
|
wxASSERT( count == 0 );
|
|
|
|
wxASSERT( !last ); // 'first' is null, then 'last' should be too
|
2008-11-24 04:34:58 +00:00
|
|
|
aNewElement->SetNext( 0 );
|
|
|
|
aNewElement->SetBack( 0 );
|
|
|
|
|
|
|
|
first = aNewElement;
|
|
|
|
last = aNewElement;
|
|
|
|
}
|
|
|
|
|
2018-05-04 14:10:18 +00:00
|
|
|
CHECK_OWNERSHIP( aNewElement );
|
2008-11-24 04:34:58 +00:00
|
|
|
aNewElement->SetList( this );
|
|
|
|
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-26 18:39:39 +00:00
|
|
|
void DHEAD::append( DHEAD& aList )
|
|
|
|
{
|
2013-12-26 22:36:43 +00:00
|
|
|
if( aList.first )
|
2012-02-26 18:39:39 +00:00
|
|
|
{
|
2013-12-26 22:36:43 +00:00
|
|
|
// Change the item's list to me.
|
|
|
|
for( EDA_ITEM* item = aList.first; item; item = item->Next() )
|
2018-05-04 14:10:18 +00:00
|
|
|
{
|
2018-05-11 08:23:40 +00:00
|
|
|
wxASSERT( item->GetList() == &aList );
|
2013-12-26 22:36:43 +00:00
|
|
|
item->SetList( this );
|
2018-05-04 14:10:18 +00:00
|
|
|
}
|
2013-12-26 22:36:43 +00:00
|
|
|
|
|
|
|
if( first ) // this list is not empty, set last item's next to the first item in aList
|
|
|
|
{
|
|
|
|
wxCHECK_RET( last != NULL, wxT( "Last list element not set." ) );
|
|
|
|
|
|
|
|
last->SetNext( aList.first );
|
|
|
|
aList.first->SetBack( last );
|
|
|
|
last = aList.last;
|
|
|
|
}
|
|
|
|
else // this list is empty, first and last are same as aList
|
|
|
|
{
|
|
|
|
first = aList.first;
|
|
|
|
last = aList.last;
|
|
|
|
}
|
|
|
|
|
|
|
|
count += aList.count;
|
|
|
|
|
|
|
|
aList.count = 0;
|
|
|
|
aList.first = NULL;
|
|
|
|
aList.last = NULL;
|
2012-02-26 18:39:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-08 20:12:46 +00:00
|
|
|
void DHEAD::insert( EDA_ITEM* aNewElement, EDA_ITEM* aAfterMe )
|
2008-11-24 04:34:58 +00:00
|
|
|
{
|
2018-05-04 13:10:44 +00:00
|
|
|
wxCHECK( aNewElement, /*void*/ );
|
2008-11-24 04:34:58 +00:00
|
|
|
|
|
|
|
if( !aAfterMe )
|
2008-11-24 21:03:40 +00:00
|
|
|
append( aNewElement );
|
2008-11-24 04:34:58 +00:00
|
|
|
else
|
|
|
|
{
|
2018-05-04 13:10:44 +00:00
|
|
|
wxCHECK( aAfterMe->GetList() == this, /*void*/ );
|
2008-11-24 04:34:58 +00:00
|
|
|
|
|
|
|
// the list cannot be empty if aAfterMe is supposedly on the list
|
2018-05-04 14:10:18 +00:00
|
|
|
wxASSERT( first && last && count > 0 );
|
2008-11-24 04:34:58 +00:00
|
|
|
|
|
|
|
if( first == aAfterMe )
|
|
|
|
{
|
|
|
|
aAfterMe->SetBack( aNewElement );
|
|
|
|
|
|
|
|
aNewElement->SetBack( 0 ); // first in list does not point back
|
|
|
|
aNewElement->SetNext( aAfterMe );
|
|
|
|
|
|
|
|
first = aNewElement;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-12-08 20:12:46 +00:00
|
|
|
EDA_ITEM* oldBack = aAfterMe->Back();
|
2008-11-24 04:34:58 +00:00
|
|
|
|
|
|
|
aAfterMe->SetBack( aNewElement );
|
|
|
|
|
|
|
|
aNewElement->SetBack( oldBack );
|
|
|
|
aNewElement->SetNext( aAfterMe );
|
|
|
|
|
|
|
|
oldBack->SetNext( aNewElement );
|
|
|
|
}
|
|
|
|
|
2018-05-04 14:10:18 +00:00
|
|
|
CHECK_OWNERSHIP( aNewElement );
|
2008-11-24 04:34:58 +00:00
|
|
|
aNewElement->SetList( this );
|
|
|
|
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-08 20:12:46 +00:00
|
|
|
void DHEAD::remove( EDA_ITEM* aElement )
|
2008-11-24 04:34:58 +00:00
|
|
|
{
|
2018-05-04 13:10:44 +00:00
|
|
|
wxCHECK( aElement && aElement->GetList() == this, /*void*/ );
|
2008-11-24 04:34:58 +00:00
|
|
|
|
|
|
|
if( aElement->Next() )
|
|
|
|
{
|
|
|
|
aElement->Next()->SetBack( aElement->Back() );
|
|
|
|
}
|
2008-12-02 18:20:49 +00:00
|
|
|
else // element being removed is last
|
2008-11-24 04:34:58 +00:00
|
|
|
{
|
|
|
|
wxASSERT( last == aElement );
|
|
|
|
last = aElement->Back();
|
|
|
|
}
|
|
|
|
|
|
|
|
if( aElement->Back() )
|
|
|
|
{
|
2008-12-02 18:20:49 +00:00
|
|
|
aElement->Back()->SetNext( aElement->Next() );
|
2008-11-24 04:34:58 +00:00
|
|
|
}
|
2008-12-02 18:20:49 +00:00
|
|
|
else // element being removed is first
|
2008-11-24 04:34:58 +00:00
|
|
|
{
|
|
|
|
wxASSERT( first == aElement );
|
|
|
|
first = aElement->Next();
|
|
|
|
}
|
|
|
|
|
|
|
|
aElement->SetBack( 0 );
|
|
|
|
aElement->SetNext( 0 );
|
|
|
|
aElement->SetList( 0 );
|
|
|
|
|
|
|
|
--count;
|
2018-05-04 14:10:18 +00:00
|
|
|
wxASSERT( ( first && last ) || count == 0 );
|
2008-11-24 04:34:58 +00:00
|
|
|
}
|
|
|
|
|
2008-12-02 18:20:49 +00:00
|
|
|
#if defined(DEBUG)
|
|
|
|
|
|
|
|
void DHEAD::VerifyListIntegrity()
|
|
|
|
{
|
2010-12-08 20:12:46 +00:00
|
|
|
EDA_ITEM* item;
|
2008-12-02 18:20:49 +00:00
|
|
|
unsigned i = 0;
|
|
|
|
|
|
|
|
for( item = first; item && i<count; ++i, item = item->Next() )
|
|
|
|
{
|
|
|
|
if( i < count-1 )
|
|
|
|
{
|
|
|
|
wxASSERT( item->Next() );
|
|
|
|
}
|
|
|
|
|
|
|
|
wxASSERT( item->GetList() == this );
|
|
|
|
}
|
|
|
|
|
|
|
|
wxASSERT( item == NULL );
|
|
|
|
wxASSERT( i == count );
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
for( item = last; item && i<count; ++i, item = item->Back() )
|
|
|
|
{
|
|
|
|
if( i < count-1 )
|
|
|
|
{
|
|
|
|
wxASSERT( item->Back() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wxASSERT( item == NULL );
|
|
|
|
wxASSERT( i == count );
|
2008-12-04 04:28:11 +00:00
|
|
|
|
|
|
|
// printf("list %p has %d items.\n", this, count );
|
2008-12-02 18:20:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|