KiCad PCB EDA Suite
dlist.h
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 #ifndef DLIST_H_
27 #define DLIST_H_
28 
29 
30 #include <stdio.h> // NULL definition.
31 
32 
33 class EDA_ITEM;
34 
35 
40 class DHEAD
41 {
42 protected:
45  unsigned count;
46  bool meOwner;
47 
53  DHEAD() :
54  first(0),
55  last(0),
56  count(0),
57  meOwner(true)
58  {
59  }
60 
61  ~DHEAD();
62 
68  void append( EDA_ITEM* aNewElement );
69 
75  void append( DHEAD& aList );
76 
85  void insert( EDA_ITEM* aNewElement, EDA_ITEM* aElementAfterMe );
86 
92  void insert( EDA_ITEM* aNewElement )
93  {
94  insert( aNewElement, first );
95  }
96 
102  void remove( EDA_ITEM* aElement );
103 
104 
105 public:
106 
112  void DeleteAll();
113 
119  void SetOwnership( bool Iown ) { meOwner = Iown; }
120 
121 
126  unsigned GetCount() const { return count; }
127 
128 #if defined(DEBUG)
129  void VerifyListIntegrity();
130 #endif
131 };
132 
133 
141 template <class T>
142 class DLIST : public DHEAD
143 {
144 public:
145 
150  operator T* () const { return GetFirst(); }
151 
156  T* operator -> () const { return GetFirst(); }
157 
163  T* GetFirst() const { return (T*) first; }
164 
170  T* GetLast() const { return (T*) last; }
171 
177  void Append( T* aNewElement )
178  {
179  append( aNewElement );
180  }
181 
187  void Append( DLIST& aList )
188  {
189  append( aList );
190  }
191 
200  void Insert( T* aNewElement, T* aElementAfterMe )
201  {
202  insert( aNewElement, aElementAfterMe );
203  }
204 
211  T* Remove( T* aElement )
212  {
213  remove( aElement );
214  return aElement;
215  }
216 
217  //-----< STL like functions >---------------------------------------
218  T* begin() const { return GetFirst(); }
219  T* end() const { return NULL; }
220 
222  {
223  if( GetFirst() )
224  return Remove( GetFirst() );
225  return NULL;
226  }
227 
229  {
230  if( GetLast() )
231  return Remove( GetLast() );
232  return NULL;
233  }
234 
240  void PushFront( T* aNewElement )
241  {
242  insert( aNewElement );
243  }
244 
250  void PushBack( T* aNewElement )
251  {
252  append( aNewElement );
253  }
254 
255  //-----</ STL like functions >--------------------------------------
256 };
257 
258 #endif // DLIST_H_
T * Remove(T *aElement)
Function Remove removes aElement from the list, but does not delete it.
Definition: dlist.h:211
void PushFront(T *aNewElement)
Function PushFront puts aNewElement at front of list sequence.
Definition: dlist.h:240
void Append(DLIST &aList)
Function Append adds aList to the end of the list.
Definition: dlist.h:187
Class DHEAD is only for use by template class DLIST, use that instead.
Definition: dlist.h:40
T * end() const
Definition: dlist.h:219
T
enum T contains all this lexer's tokens.
void Append(T *aNewElement)
Function Append adds aNewElement to the end of the list.
Definition: dlist.h:177
T * begin() const
Definition: dlist.h:218
Class DLIST is the head of a doubly linked list.
Definition: dlist.h:142
void DeleteAll()
Function DeleteAll deletes all items on the list and leaves the list empty.
Definition: dlist.cpp:41
void insert(EDA_ITEM *aNewElement, EDA_ITEM *aElementAfterMe)
Function insert puts aNewElement just in front of aElementAfterMe in the list sequence.
Definition: dlist.cpp:119
void PushBack(T *aNewElement)
Function PushBack puts aNewElement at the end of the list sequence.
Definition: dlist.h:250
T * GetLast() const
Function GetLast returns the last T* in the list without removing it, or NULL if the list is empty...
Definition: dlist.h:170
T * GetFirst() const
Function GetFirst returns the first T* in the list without removing it, or NULL if the list is empty...
Definition: dlist.h:163
T * operator->() const
operator -> is a dereferencing operator that returns GetFirst(), a T*
Definition: dlist.h:156
unsigned count
how many elements are in the list, automatically maintained.
Definition: dlist.h:45
~DHEAD()
Definition: dlist.cpp:34
T * PopBack()
Definition: dlist.h:228
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
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:178
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:59
unsigned GetCount() const
Function GetCount returns the number of elements in the list.
Definition: dlist.h:126
void SetOwnership(bool Iown)
Function SetOwnership controls whether the list owns the objects and is responsible for deleteing the...
Definition: dlist.h:119
DHEAD()
Constructor DHEAD is protected so that a DHEAD can only be instantiated from within a DLIST template...
Definition: dlist.h:53
void Insert(T *aNewElement, T *aElementAfterMe)
Function Insert puts aNewElement just in front of aElementAfterMe in the list sequence.
Definition: dlist.h:200
T * PopFront()
Definition: dlist.h:221
void insert(EDA_ITEM *aNewElement)
Function insert puts aNewElement in front of list sequence.
Definition: dlist.h:92