home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c081_11 / 1.ddi / CLASSINC.ZIP / LIST.H < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-13  |  5.4 KB  |  254 lines

  1. // Borland C++ - (C) Copyright 1991 by Borland International
  2.  
  3. // Contents ----------------------------------------------------------------
  4. //
  5. //      List
  6. //
  7. //         ListIterator
  8. //      ListIterator::ListIterator                  constructor
  9. //
  10. // Description
  11. //
  12. //      Defines the class List.  Lists are used to link other objects
  13. //     together.
  14. //     Defines the ListIterator class.  A list iterator visits each
  15. //     of the items in a list.
  16. //
  17. // End ---------------------------------------------------------------------
  18.  
  19. // Interface Dependencies ---------------------------------------------------
  20.  
  21. #ifndef __LIST_H
  22. #define __LIST_H
  23.  
  24. #ifndef __IOSTREAM_H
  25. #include <iostream.h>
  26. #define __IOSTREAM_H
  27. #endif
  28.  
  29. #ifndef __CLSTYPES_H
  30. #include <clstypes.h>
  31. #endif
  32.  
  33. #ifndef __OBJECT_H
  34. #include <object.h>
  35. #endif
  36.  
  37. #ifndef __LSTELEM_H
  38. #include <lstelem.h>
  39. #endif
  40.  
  41. #ifndef __COLLECT_H
  42. #include <collect.h>
  43. #endif
  44.  
  45. #ifndef __CONTAIN_H
  46. #include <contain.h>
  47. #endif
  48.  
  49. // End Interface Dependencies ------------------------------------------------
  50.  
  51.  
  52. // Class //
  53.  
  54. class List:  public Collection
  55. {
  56. public:
  57.             List() { head = 0; }
  58.     virtual ~List();
  59.  
  60.             Object&         peekHead() const { return *(head->data); }
  61.  
  62.             void            add( Object& );
  63.             void            detach( const Object&, int = 0 );
  64.             void            destroy( const Object& l ) { detach( l, 1 ); }
  65.  
  66.     virtual classType       isA() const;
  67.     virtual char           *nameOf() const;
  68.     virtual hashValueType   hashValue() const;
  69.  
  70.     virtual ContainerIterator& initIterator() const;
  71.  
  72. private:
  73.             ListElement    *head;
  74.  
  75.     friend  class ListIterator;
  76. };
  77.  
  78. // Description -------------------------------------------------------------
  79. //
  80. //     Defines the container class List. 
  81. //
  82. //     List objects, i.e. objects instantiated of classes derived from
  83. //     List, are used in sequences where insertions and deletions
  84. //     are defined.  They operate soley on objects derived from
  85. //     class ListElement.
  86. //
  87. // Constructor
  88. //
  89. //     List
  90. //
  91. //     Constructor.
  92. //
  93. // Public Members
  94. //
  95. //      peekHead
  96. //
  97. //      Returns a reference to the object at the head of the list.
  98. //
  99. //      add
  100. //
  101. //      Adds an object to the list.
  102. //
  103. //      destroy
  104. //
  105. //      Detaches an object from the list and calls that object's destructor.
  106. //
  107. //      detach
  108. //
  109. //      Removes a reference to an object from the list.
  110. //
  111. //     hasMember
  112. //
  113. //     Determines whether the list has a given list element.
  114. //
  115. //     isA
  116. //
  117. //     Returns the class type of class list.
  118. //
  119. //     nameOf
  120. //
  121. //     Returns a pointer to the character string "List."
  122. //
  123. //     hashValue
  124. //
  125. //     Returns a pre-defined value for a list object.
  126. //
  127. // Inherited Members
  128. //      
  129. //     printOn
  130. //
  131. //     Inherited from Container.
  132. //
  133. //     isEmpty
  134. //
  135. //     Inherited from Container.
  136. //
  137. //     forEach
  138. //
  139. //     Inherited from Container.
  140. //
  141. //     firstThat
  142. //
  143. //     Inherited from Container.
  144. //
  145. //     lastThat
  146. //
  147. //     Inherited from Container.
  148. //
  149. //     isEqual
  150. //
  151. //     Inherited from Container.
  152. //
  153. //     isSortable
  154. //
  155. //     Inherited from Object.
  156. //
  157. //     isAssociation
  158. //
  159. //     Inherited from Object.
  160. //
  161. // Private Members
  162. //
  163. //     head
  164. //
  165. //     Maintains a pointer to the list element at the head of the list.
  166. //
  167. // End ---------------------------------------------------------------------
  168.  
  169.  
  170. // Class //
  171.  
  172. class ListIterator:  public ContainerIterator
  173. {
  174. public:
  175.             ListIterator( const List& );
  176.     virtual ~ListIterator();
  177.  
  178.     virtual             operator int();
  179.     virtual             operator Object&();
  180.     virtual Object&     operator ++();
  181.     virtual    void        restart();
  182.  
  183. private:
  184.             ListElement *currentElement;
  185.             ListElement *startingElement;
  186. };
  187.  
  188. // Description -------------------------------------------------------------
  189. //
  190. //         Defines the list iterator class.  Upon initialization, we set up
  191. //         an internal pointer to our current position in the list.  As
  192. //         the increment operator is called, we update this current position.
  193. //
  194. // Constructor
  195. //
  196. //      ListIterator( const List& )
  197. //
  198. //         Constructor for an iterator.  Note that this isn't a copy
  199. //         constructor, since it takes an object from a different class.
  200. //
  201. // Destructor
  202. //
  203. //         ~ListIterator
  204. //
  205. // Public Members
  206. //
  207. //         operator int
  208. //
  209. //         We are allowed one cast operator to a predefined type.  This
  210. //         operator defines an explicit or an implicit cast from a
  211. //         ListIterator to an integer.
  212. //
  213. //      operator Object&
  214. //
  215. //      Conversion to Object reference operator.
  216. //
  217. //         operator ++
  218. //
  219. //      The increment operator.
  220. //
  221. //         restart
  222. //
  223. //         List iterator restart mechanism.
  224. //
  225. // Private Members
  226. //
  227. //      currentElement
  228. //
  229. //         The current position in the iteration sequence.
  230. //
  231. //      startingElement
  232. //
  233. //         The starting position in the iteration sequence.
  234. //
  235. // End ---------------------------------------------------------------------
  236.  
  237.  
  238. // Constructor //
  239.  
  240. inline  ListIterator::ListIterator( const List& toIterate )
  241.  
  242. // Summary -----------------------------------------------------------------
  243. //
  244. //      Constructor for a list iterator object.
  245. //
  246. // End ---------------------------------------------------------------------
  247. {
  248.     currentElement = toIterate.head;
  249. }
  250. // End Constructor ListIterator::ListIterator //
  251.  
  252.  
  253. #endif // ifndef __LIST_H //
  254.