KiCad PCB EDA Suite
dlist.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2008 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
5  * Copyright (C) 1992-2008 KiCad Developers, see change_log.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 
26 #include <fctsys.h>
27 #include <dlist.h>
28 #include <base_struct.h>
29 
30 // verifies conditions for setting a parent list for an element, i.e.
31 // element either does not belong to any list or it already belongs to the same list
32 #define CHECK_OWNERSHIP(a) wxASSERT( !a->GetList() || a->GetList() == this );
33 
34 /* Implement the class DHEAD from dlist.h */
35 
36 
38 {
39  if( meOwner )
40  DeleteAll();
41 }
42 
43 
45 {
46  wxCHECK( meOwner, /*void*/ ); // only owning lists may delete the contents
47  EDA_ITEM* next;
48  EDA_ITEM* item = first;
49 
50  while( item )
51  {
52  next = item->Next();
53  delete item; // virtual destructor, class specific
54  item = next;
55  }
56 
57  first = 0;
58  last = 0;
59  count = 0;
60 }
61 
62 
63 void DHEAD::append( EDA_ITEM* aNewElement )
64 {
65  wxCHECK( aNewElement, /*void*/ );
66 
67  if( first ) // list is not empty, first is not touched
68  {
69  wxASSERT( count > 0 );
70  wxCHECK( last, /*void*/ ); // 'first' is non-null, so 'last' should be non-null too
71 
72  aNewElement->SetNext( 0 );
73  aNewElement->SetBack( last );
74 
75  wxASSERT( !last->Next() ); // the last element should point to nullptr
76  last->SetNext( aNewElement );
77  last = aNewElement;
78  }
79  else // list is empty, first and last are changed
80  {
81  wxASSERT( count == 0 );
82  wxASSERT( !last ); // 'first' is null, then 'last' should be too
83  aNewElement->SetNext( 0 );
84  aNewElement->SetBack( 0 );
85 
86  first = aNewElement;
87  last = aNewElement;
88  }
89 
90  CHECK_OWNERSHIP( aNewElement );
91  aNewElement->SetList( this );
92 
93  ++count;
94 }
95 
96 
97 void DHEAD::append( DHEAD& aList )
98 {
99  if( aList.first )
100  {
101  // Change the item's list to me.
102  for( EDA_ITEM* item = aList.first; item; item = item->Next() )
103  {
104  wxASSERT( item->GetList() == &aList );
105  item->SetList( this );
106  }
107 
108  if( first ) // this list is not empty, set last item's next to the first item in aList
109  {
110  wxCHECK_RET( last != NULL, wxT( "Last list element not set." ) );
111 
112  last->SetNext( aList.first );
113  aList.first->SetBack( last );
114  last = aList.last;
115  }
116  else // this list is empty, first and last are same as aList
117  {
118  first = aList.first;
119  last = aList.last;
120  }
121 
122  count += aList.count;
123 
124  aList.count = 0;
125  aList.first = NULL;
126  aList.last = NULL;
127  }
128 }
129 
130 
131 void DHEAD::insert( EDA_ITEM* aNewElement, EDA_ITEM* aAfterMe )
132 {
133  wxCHECK( aNewElement, /*void*/ );
134 
135  if( !aAfterMe )
136  append( aNewElement );
137  else
138  {
139  wxCHECK( aAfterMe->GetList() == this, /*void*/ );
140 
141  // the list cannot be empty if aAfterMe is supposedly on the list
142  wxASSERT( first && last && count > 0 );
143 
144  if( first == aAfterMe )
145  {
146  aAfterMe->SetBack( aNewElement );
147 
148  aNewElement->SetBack( 0 ); // first in list does not point back
149  aNewElement->SetNext( aAfterMe );
150 
151  first = aNewElement;
152  }
153  else
154  {
155  EDA_ITEM* oldBack = aAfterMe->Back();
156 
157  aAfterMe->SetBack( aNewElement );
158 
159  aNewElement->SetBack( oldBack );
160  aNewElement->SetNext( aAfterMe );
161 
162  oldBack->SetNext( aNewElement );
163  }
164 
165  CHECK_OWNERSHIP( aNewElement );
166  aNewElement->SetList( this );
167 
168  ++count;
169  }
170 }
171 
172 
173 void DHEAD::remove( EDA_ITEM* aElement )
174 {
175  wxCHECK( aElement && aElement->GetList() == this, /*void*/ );
176 
177  if( aElement->Next() )
178  {
179  aElement->Next()->SetBack( aElement->Back() );
180  }
181  else // element being removed is last
182  {
183  wxASSERT( last == aElement );
184  last = aElement->Back();
185  }
186 
187  if( aElement->Back() )
188  {
189  aElement->Back()->SetNext( aElement->Next() );
190  }
191  else // element being removed is first
192  {
193  wxASSERT( first == aElement );
194  first = aElement->Next();
195  }
196 
197  aElement->SetBack( 0 );
198  aElement->SetNext( 0 );
199  aElement->SetList( 0 );
200 
201  --count;
202  wxASSERT( ( first && last ) || count == 0 );
203 }
204 
205 #if defined(DEBUG)
206 
207 void DHEAD::VerifyListIntegrity()
208 {
209  EDA_ITEM* item;
210  unsigned i = 0;
211 
212  for( item = first; item && i<count; ++i, item = item->Next() )
213  {
214  if( i < count-1 )
215  {
216  wxASSERT( item->Next() );
217  }
218 
219  wxASSERT( item->GetList() == this );
220  }
221 
222  wxASSERT( item == NULL );
223  wxASSERT( i == count );
224 
225  i = 0;
226  for( item = last; item && i<count; ++i, item = item->Back() )
227  {
228  if( i < count-1 )
229  {
230  wxASSERT( item->Back() );
231  }
232  }
233 
234  wxASSERT( item == NULL );
235  wxASSERT( i == count );
236 
237  // printf("list %p has %d items.\n", this, count );
238 }
239 
240 #endif
241 
DHEAD * GetList() const
Definition: base_struct.h:212
CITER next(CITER it)
Definition: ptree.cpp:130
void SetBack(EDA_ITEM *aBack)
Definition: base_struct.h:215
Class DHEAD is only for use by template class DLIST, use that instead.
Definition: dlist.h:40
EDA_ITEM * Back() const
Definition: base_struct.h:210
void remove(EDA_ITEM *aElement)
Function remove removes aElement from the list, but does not delete it.
Definition: dlist.cpp:173
void SetList(DHEAD *aList)
Definition: base_struct.h:217
EDA_ITEM * Next() const
Definition: base_struct.h:209
void DeleteAll()
Function DeleteAll deletes all items on the list and leaves the list empty.
Definition: dlist.cpp:44
void insert(EDA_ITEM *aNewElement, EDA_ITEM *aElementAfterMe)
Function insert puts aNewElement just in front of aElementAfterMe in the list sequence.
Definition: dlist.cpp:131
unsigned count
how many elements are in the list, automatically maintained.
Definition: dlist.h:45
~DHEAD()
Definition: dlist.cpp:37
void SetNext(EDA_ITEM *aNext)
Definition: base_struct.h:214
EDA_ITEM * last
last elment in list, or NULL if empty
Definition: dlist.h:44
bool meOwner
I must delete the objects I hold in my destructor.
Definition: dlist.h:46
size_t i
Definition: json11.cpp:597
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:154
EDA_ITEM * first
first element in list, or NULL if list empty
Definition: dlist.h:43
void append(EDA_ITEM *aNewElement)
Function append adds aNewElement to the end of the list.
Definition: dlist.cpp:63
Basic classes for most KiCad items.
#define CHECK_OWNERSHIP(a)
Definition: dlist.cpp:32