You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

258 lines
6.9 KiB

17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
  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. #ifndef DLIST_H_
  25. #define DLIST_H_
  26. #include <stdio.h> // NULL definition.
  27. class EDA_ITEM;
  28. /**
  29. * Class DHEAD
  30. * is only for use by template class DLIST, use that instead.
  31. */
  32. class DHEAD
  33. {
  34. protected:
  35. EDA_ITEM* first; ///< first element in list, or NULL if list empty
  36. EDA_ITEM* last; ///< last elment in list, or NULL if empty
  37. unsigned count; ///< how many elements are in the list, automatically maintained.
  38. bool meOwner; ///< I must delete the objects I hold in my destructor
  39. /**
  40. * Constructor DHEAD
  41. * is protected so that a DHEAD can only be instantiated from within a
  42. * DLIST template.
  43. */
  44. DHEAD() :
  45. first(0),
  46. last(0),
  47. count(0),
  48. meOwner(true)
  49. {
  50. }
  51. ~DHEAD();
  52. /**
  53. * Function append
  54. * adds \a aNewElement to the end of the list.
  55. * @param aNewElement The element to insert.
  56. */
  57. void append( EDA_ITEM* aNewElement );
  58. /**
  59. * Function append
  60. * adds \a aList to the end of the list.
  61. * @param aList The list to aList.
  62. */
  63. void append( DHEAD& aList );
  64. /**
  65. * Function insert
  66. * puts \a aNewElement just in front of \a aElementAfterMe in the list sequence.
  67. * If \a aElementAfterMe is NULL, then simply append().
  68. * @param aNewElement The element to insert.
  69. * @param aElementAfterMe The element to insert \a aNewElement before,
  70. * if NULL then append \a aNewElement onto end of list.
  71. */
  72. void insert( EDA_ITEM* aNewElement, EDA_ITEM* aElementAfterMe );
  73. /**
  74. * Function insert
  75. * puts \a aNewElement in front of list sequence.
  76. * @param aNewElement The element to insert.
  77. */
  78. void insert( EDA_ITEM* aNewElement )
  79. {
  80. insert( aNewElement, first );
  81. }
  82. /**
  83. * Function remove
  84. * removes \a aElement from the list, but does not delete it.
  85. * @param aElement The element to remove.
  86. */
  87. void remove( EDA_ITEM* aElement );
  88. public:
  89. /**
  90. * Function DeleteAll
  91. * deletes all items on the list and leaves the list empty. The destructor
  92. * for each item is called.
  93. */
  94. void DeleteAll();
  95. /**
  96. * Function SetOwnership
  97. * controls whether the list owns the objects and is responsible for
  98. * deleteing their memory at time of this object's destruction.
  99. */
  100. void SetOwnership( bool Iown ) { meOwner = Iown; }
  101. /**
  102. * Function GetCount
  103. * returns the number of elements in the list.
  104. */
  105. unsigned GetCount() const { return count; }
  106. #if defined(DEBUG)
  107. void VerifyListIntegrity();
  108. #endif
  109. };
  110. /**
  111. * Class DLIST
  112. * is the head of a doubly linked list. It contains pointers to the first
  113. * and last elements in a doubly linked list. The elements in the list must
  114. * be of class T or derived from T, and T must be derived from EDA_ITEM.
  115. * @see DHEAD for additional public functions.
  116. */
  117. template <class T>
  118. class DLIST : public DHEAD
  119. {
  120. public:
  121. /**
  122. * operator T*
  123. * is a casting operator that returns \a GetFirst(), a T*
  124. */
  125. operator T* () const { return GetFirst(); }
  126. /**
  127. * operator ->
  128. * is a dereferencing operator that returns \a GetFirst(), a T*
  129. */
  130. T* operator -> () const { return GetFirst(); }
  131. /**
  132. * Function GetFirst
  133. * returns the first T* in the list without removing it, or NULL if
  134. * the list is empty.
  135. */
  136. T* GetFirst() const { return (T*) first; }
  137. /**
  138. * Function GetLast
  139. * returns the last T* in the list without removing it,
  140. * or NULL if the list is empty.
  141. */
  142. T* GetLast() const { return (T*) last; }
  143. /**
  144. * Function Append
  145. * adds \a aNewElement to the end of the list.
  146. * @param aNewElement The element to insert.
  147. */
  148. void Append( T* aNewElement )
  149. {
  150. append( aNewElement );
  151. }
  152. /**
  153. * Function Append
  154. * adds \a aList to the end of the list.
  155. * @param aList The list to append to the end of the list.
  156. */
  157. void Append( DLIST& aList )
  158. {
  159. append( aList );
  160. }
  161. /**
  162. * Function Insert
  163. * puts \a aNewElement just in front of \a aElementAfterMe in the list sequence.
  164. * If aElementAfterMe is NULL, then simply Append()
  165. * @param aNewElement The element to insert.
  166. * @param aElementAfterMe The element to insert \a aNewElement before,
  167. * if NULL then append \a aNewElement onto end of list.
  168. */
  169. void Insert( T* aNewElement, T* aElementAfterMe )
  170. {
  171. insert( aNewElement, aElementAfterMe );
  172. }
  173. /**
  174. * Function Remove
  175. * removes \a aElement from the list, but does not delete it.
  176. * @param aElement The element to remove from the list.
  177. * @return T* - the removed element, so you can easily delete it upon return.
  178. */
  179. T* Remove( T* aElement )
  180. {
  181. remove( aElement );
  182. return aElement;
  183. }
  184. //-----< STL like functions >---------------------------------------
  185. T* begin() const { return GetFirst(); }
  186. T* end() const { return NULL; }
  187. T* PopFront()
  188. {
  189. if( GetFirst() )
  190. return Remove( GetFirst() );
  191. return NULL;
  192. }
  193. T* PopBack()
  194. {
  195. if( GetLast() )
  196. return Remove( GetLast() );
  197. return NULL;
  198. }
  199. /**
  200. * Function PushFront
  201. * puts aNewElement at front of list sequence.
  202. * @param aNewElement The element to insert at the front of the list.
  203. */
  204. void PushFront( T* aNewElement )
  205. {
  206. insert( aNewElement );
  207. }
  208. /**
  209. * Function PushBack
  210. * puts aNewElement at the end of the list sequence.
  211. * @param aNewElement The element to push to the end of the list.
  212. */
  213. void PushBack( T* aNewElement )
  214. {
  215. append( aNewElement );
  216. }
  217. //-----</ STL like functions >--------------------------------------
  218. };
  219. #endif // DLIST_H_