home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c081_11 / 11.ddi / CLASSSRC.ZIP / CONTAIN.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-13  |  12.6 KB  |  468 lines

  1. // Borland C++ - (C) Copyright 1991 by Borland International
  2.  
  3. // Contents ----------------------------------------------------------------
  4. //
  5. //      Container::Container                        copy constructor
  6. //      Container::forEach
  7. //      Container::firstThat
  8. //      Container::lastThat
  9. //      Container::isEqual
  10. //      Container::printOn
  11. //      Container::printHeader
  12. //      Container::printSeparator
  13. //      Container::printTrailer
  14. //
  15. // Description
  16. //
  17. //      Implementation of class Container member functions.
  18. //
  19. // End ---------------------------------------------------------------------
  20.  
  21. // Interface Dependencies ---------------------------------------------------
  22.  
  23. #ifndef __IOSTREAM_H
  24. #include <iostream.h>
  25. #define __IOSTREAM_H
  26. #endif
  27.  
  28. #ifndef __CLSTYPES_H
  29. #include <clstypes.h>
  30. #endif
  31.  
  32. #ifndef __CONTAIN_H
  33. #include <contain.h>
  34. #endif
  35.  
  36. // End Interface Dependencies ------------------------------------------------
  37.  
  38. // Implementation Dependencies ----------------------------------------------
  39. // End Implementation Dependencies -------------------------------------------
  40.  
  41.  
  42. // Constructor //
  43.  
  44. Container::Container( const Container& toCopy )
  45.  
  46. // Summary -----------------------------------------------------------------
  47. //
  48. //      Constructors a container and copies the given container into it.
  49. //
  50. // Parameters
  51. //
  52. //      toCopy
  53. //
  54. //      The container which we are to copy.
  55. //
  56. // Functional Description
  57. //
  58. //      We initialize an iterator, then iterate through each object in the
  59. //      container we are copying, constructing and copying as we go.
  60. //
  61. // Remarks
  62. //
  63. //  warnings:
  64. //      We must be sure to delete the container iterator, since it was
  65. //      allocated on the heap.
  66. //
  67. // End ---------------------------------------------------------------------
  68. {
  69.     ContainerIterator ©Iterator = toCopy.initIterator();
  70.  
  71.     while( int(copyIterator) != 0 )
  72.     {
  73.     } // end while //
  74.  
  75.     delete ©Iterator;
  76. }
  77. // End Constructor Container::Container //
  78.  
  79.  
  80. // Member Function //
  81.  
  82. Container::~Container()
  83.  
  84. // Summary -----------------------------------------------------------------
  85. //
  86. //      Destructor for a Container object.
  87. //
  88. //      We don't do anything here, because the derived class will have
  89. //      taken care of deleting all the contained objects.
  90. //
  91. // End ---------------------------------------------------------------------
  92. {
  93. }
  94. // End Destructor //
  95.  
  96.  
  97. // Member Function //
  98.  
  99. void Container::forEach( iterFuncType actionPtr, void *paramListPtr )
  100.  
  101. // Summary -----------------------------------------------------------------
  102. //
  103. //      Calls the given iterator function on each object in this container.
  104. //
  105. // Parameters
  106. //
  107. //      actionPtr
  108. //
  109. //      Pointer to the action routine which is to be called for each object.
  110. //
  111. //      paramListPtr
  112. //
  113. //      Pointer to the list of parameters which will be passed along to
  114. //      the action routine.
  115. //
  116. // Functional Description
  117. //
  118. //      We initialize an iterator, then iterator through each object,
  119. //      asking the objects to perform the forEach function on themselves.
  120. //
  121. // Remarks
  122. //
  123. //  warnings:
  124. //      The action routine must have a prototype of the form:
  125. //          void action( Object&, void * );
  126. //
  127. //      We must be sure to delete the container iterator, since it was
  128. //      allocated on the heap.
  129. //
  130. // End ---------------------------------------------------------------------
  131. {
  132.     ContainerIterator& containerIterator = initIterator();
  133.  
  134.     while( int(containerIterator) != 0 )
  135.     {
  136.         containerIterator++.forEach( actionPtr, paramListPtr );
  137.     }
  138.     delete &containerIterator;
  139. }
  140. // End Member Function Container::forEach //
  141.  
  142.  
  143. // Member Function //
  144.  
  145. Object& Container::firstThat( condFuncType testFuncPtr, void *paramListPtr ) const
  146.  
  147. // Summary -----------------------------------------------------------------
  148. //
  149. //      Finds the first object in the container which satisfies the
  150. //      given condition function.
  151. //
  152. // Parameters
  153. //
  154. //      testFuncPtr
  155. //
  156. //      Pointer to the conditional test routine which is to be called 
  157. //      for this container.
  158. //
  159. //      paramListPtr
  160. //
  161. //      Pointer to the list of parameters which will be passed along to
  162. //      the conditional test routine.
  163. //
  164. // Return Value
  165. //
  166. //      Returns the first object in the container which satisfies the 
  167. //      condition.  Returns NOOBJECT otherwise.
  168. //
  169. // Functional Description
  170. //
  171. //      We initialize an iterator, then iterator through each object,
  172. //      asking the objects to perform the firstThat function on themselves.
  173. //
  174. // Remarks
  175. //
  176. //  warnings:
  177. //      The conditional test routine must have a prototype of the form:
  178. //          int test( Object&, void * );
  179. //
  180. //      The conditional test routine must return 1 if the given object
  181. //      satisfies the condition.
  182. //
  183. //      We must be sure to delete the container iterator, since it was
  184. //      allocated on the heap.
  185. //
  186. // End ---------------------------------------------------------------------
  187. {
  188.     ContainerIterator &containerIterator = initIterator();
  189.  
  190.     while( int(containerIterator) != 0 )
  191.     {
  192.         Object& testResult = 
  193.                     containerIterator++.firstThat( testFuncPtr, paramListPtr );
  194.         if ( testResult != NOOBJECT )
  195.         {
  196.             delete &containerIterator;
  197.             return testResult;
  198.         }
  199.     } // end while //
  200.     delete &containerIterator;
  201.     return NOOBJECT;
  202. }
  203. // End Member Function Container::firstThat //
  204.  
  205.  
  206. // Member Function //
  207.  
  208. Object& Container::lastThat( condFuncType testFuncPtr, void *paramListPtr ) const
  209.  
  210. // Summary -----------------------------------------------------------------
  211. //
  212. //      Finds the last object in the container which satisfies the
  213. //      given condition function.
  214. //
  215. // Parameters
  216. //
  217. //      testFuncPtr
  218. //
  219. //      Pointer to the conditional test routine which is to be called 
  220. //      for this object.
  221. //
  222. //      paramListPtr
  223. //
  224. //      Pointer to the list of parameters which will be passed along to
  225. //      the conditional test routine.
  226. //
  227. // Functional Description
  228. //
  229. //      We initialize an iterator, then iterator through each object,
  230. //      asking the objects to perform the lastThat function on themselves.
  231. //      As we iterate, if we find an object which satisfies the condition,
  232. //      we make that object the last object which met the condition.  Note
  233. //      that there is no short-circuiting the search, since we must search
  234. //      through every object in the container to find the last one.
  235. //
  236. // Remarks
  237. //
  238. //  warnings:
  239. //      The conditional test routine must have a prototype of the form:
  240. //          int test( Object&, void * );
  241. //
  242. //      The conditional test routine must return 1 if the given object
  243. //      satisfies the condition.
  244. //
  245. //      We must be sure to delete the container iterator, since it was
  246. //      allocated on the heap.
  247. //
  248. // End ---------------------------------------------------------------------
  249. {
  250.     ContainerIterator& containerIterator = initIterator();
  251.  
  252.     Object *lastMet = &(containerIterator++.lastThat( testFuncPtr, paramListPtr ));
  253.  
  254.     while( int(containerIterator) != 0 )
  255.     {
  256.         Object& testResult = 
  257.                     containerIterator++.lastThat( testFuncPtr, paramListPtr );
  258.         if ( testResult != NOOBJECT )
  259.         {
  260.             lastMet = &testResult;
  261.         }
  262.     } // end while //
  263.     delete &containerIterator;
  264.     return *lastMet;
  265. }
  266. // End Member Function Container::lastThat //
  267.  
  268.  
  269. // Member Function //
  270.  
  271. int  Container::isEqual( const Object& testContainer ) const
  272.  
  273. // Summary -----------------------------------------------------------------
  274. //
  275. //      Determines whether the given container is equal to this.
  276. //
  277. // Parameters
  278. //
  279. //      testContainer
  280. //
  281. //      The container which we will be testing for equality with this.
  282. //
  283. // Return Value
  284. //
  285. //      Returns 1 if the two containers have the same objects in them
  286. //      in the same order.
  287. //
  288. // Functional Description
  289. //
  290. //      We initialize an iterator, then iterator through each object,
  291. //      asking the objects to perform an equality test.  As we iterate, 
  292. //      if we find an object which fails the test, we return 0.  If every
  293. //      object is equal and there are the same number of objects in both
  294. //      containers, we return 1.
  295. //
  296. // Remarks
  297. //
  298. //  warnings:
  299. //      We must be sure to delete the container iterator, since it was
  300. //      allocated on the heap.
  301. //
  302. // End ---------------------------------------------------------------------
  303. {
  304.     ContainerIterator& thisIterator = initIterator();
  305.     ContainerIterator& testContainerIterator =
  306.                                 ((Container &)(testContainer)).initIterator();
  307.  
  308.     while( int(thisIterator) != 0 && int(testContainerIterator) != 0 )
  309.     {
  310.         int objectsAreNotEqual = 
  311.                     (thisIterator++ != testContainerIterator++);
  312.         if ( objectsAreNotEqual )
  313.         {
  314.             delete &testContainerIterator;
  315.             delete &thisIterator;
  316.             return 0;
  317.         }
  318.             
  319.     } // end while //
  320.  
  321.     if ( int(thisIterator) !=0 || int(testContainerIterator) != 0 )
  322.     {
  323.         delete &testContainerIterator;
  324.         delete &thisIterator;
  325.         return 0;
  326.     }
  327.     else  // one of the containers has more objects than the other.
  328.     {
  329.         delete &testContainerIterator;
  330.         delete &thisIterator;
  331.         return 1;
  332.     }
  333. }
  334. // End Member Function Container::isEqual //
  335.  
  336.  
  337. // Member Function //
  338.  
  339. void Container::printOn( ostream& outputStream ) const
  340.  
  341. // Summary -----------------------------------------------------------------
  342. //
  343. //      Displays the contents of a container on the given stream.
  344. //
  345. // Parameters
  346. //
  347. //      outputStream
  348. //
  349. //      The stream on which we will be writing the container contents.
  350. //
  351. // Functional Description
  352. //
  353. //      We initialize an iterator, then iterator through each object,
  354. //      asking the objects to print themselves.
  355. //
  356. // Remarks
  357. //
  358. //  warnings:
  359. //      We must be sure to delete the container iterator, since it was
  360. //      allocated on the heap.
  361. //
  362. // End ---------------------------------------------------------------------
  363. {
  364.     ContainerIterator& printIterator = initIterator();
  365.  
  366.     printHeader( outputStream );
  367.  
  368.     while( int(printIterator) != 0 )
  369.     {
  370.         printIterator++.printOn( outputStream );
  371.         if ( int(printIterator) != 0 )
  372.         {
  373.             printSeparator( outputStream );
  374.         }
  375.         else // there are no more objects in the container.
  376.         {
  377.             break;
  378.         }
  379.     } // end while //
  380.  
  381.     printTrailer( outputStream );
  382.     delete &printIterator;
  383. }
  384. // End Member Function Container::printOn //
  385.  
  386.  
  387. // Member Function //
  388.  
  389. void Container::printHeader( ostream& outputStream ) const
  390.  
  391. // Summary -----------------------------------------------------------------
  392. //
  393. //      Displays a standard header for a container on the given stream.
  394. //
  395. // Parameters
  396. //
  397. //      outputStream
  398. //
  399. //      The stream on which we will be writing the header.
  400. //
  401. // Functional Description
  402. //
  403. //      We print the string returned by nameOf() and an opening brace.
  404. //
  405. // End ---------------------------------------------------------------------
  406. {
  407.     outputStream << nameOf() << " { ";
  408. }
  409. // End Member Function Container::printHeader //
  410.  
  411.  
  412. // Member Function //
  413.  
  414. void Container::printSeparator( ostream& outputStream ) const
  415.  
  416. // Summary -----------------------------------------------------------------
  417. //
  418. //      Displays a standard separator for a container on the given stream.
  419. //
  420. // Parameters
  421. //
  422. //      outputStream
  423. //
  424. //      The stream on which we will be writing the separator.
  425. //
  426. // End ---------------------------------------------------------------------
  427. {
  428.     outputStream << ",\n    ";
  429. }
  430. // End Member Function Container::printSeparator //
  431.  
  432.  
  433. // Member Function //
  434.  
  435. void Container::printTrailer( ostream& outputStream ) const
  436.  
  437. // Summary -----------------------------------------------------------------
  438. //
  439. //      Displays a standard trailer for a container on the given stream.
  440. //
  441. // Parameters
  442. //
  443. //      outputStream
  444. //
  445. //      The stream on which we will be writing the trailer.
  446. //
  447. // End ---------------------------------------------------------------------
  448. {
  449.     outputStream << " }\n";
  450. }
  451. // End Member Function Container::printTrailer //
  452.  
  453. // Member Function //
  454.  
  455. ContainerIterator::~ContainerIterator()
  456.  
  457. // Summary -----------------------------------------------------------------
  458. //
  459. //      Destructor for a ContainerIterator object.
  460. //
  461. // End ---------------------------------------------------------------------
  462. {
  463. }
  464. // End Destructor //
  465.  
  466.  
  467.  
  468.