home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / Dloads / OTHERUTI / TCPP10-8.ZIP / CLASSSRC.ZIP / CONTAIN.CPP < prev    next >
C/C++ Source or Header  |  1990-09-26  |  13KB  |  477 lines

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