home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tlx501.zip / TEMPLATE / PTRITER.CPP < prev    next >
C/C++ Source or Header  |  1996-07-02  |  18KB  |  537 lines

  1. /****************************************************************************
  2.     $Id: ptriter.cpp 501.0 1995/03/07 12:26:58 RON Exp $
  3.  
  4.     Copyright (c) 1991-95 Tarma Software Research. All rights reserved.
  5.  
  6.     Project:    Tarma Library for C++ V5.0
  7.     Author:    Ron van der Wal
  8.  
  9.     Implementation of pointer iterator templates:
  10.  
  11.     - TLPtrIter
  12.     - TLPtrIterConst
  13.  
  14.     $Log: ptriter.cpp $
  15.     Revision 501.0  1995/03/07 12:26:58  RON
  16.     Updated for TLX 5.01
  17.     Revision 1.6  1995/01/31 16:30:44  RON
  18.     Update for release 012
  19.     Added partial support for SunPro C++ compiler
  20.     Revision 1.5  1994/11/16  15:33:12  ron
  21.     Chnaged names of UNREACHABLE assertions
  22.  
  23.     Revision 1.4  1994/10/06  17:51:06  ron
  24.     Changed #defined name
  25.  
  26.     Revision 1.3  1994/10/05  18:48:19  ron
  27.     Implemented TLPtrIter, TLPtrIterConst, TLPtrItemIter,
  28.     and TLPtrItemIterConst
  29.  
  30.     Revision 1.2  1994/09/27  20:27:19  ron
  31.     Changed path separator from / to \
  32.  
  33.     Revision 1.1  1994/09/26  15:32:03  ron
  34.     Initial revision
  35.  
  36. ****************************************************************************/
  37.  
  38. #ifndef _TLX_PTRITER_CPP
  39. #define _TLX_PTRITER_CPP
  40.  
  41. #include <iostream.h>
  42.  
  43. #ifndef _TLX_DEBUG_H
  44. #include <tlx\501\debug.h>
  45. #endif
  46. #ifndef _TLX_EXCEPT_H
  47. #include <tlx\501\except.h>
  48. #endif
  49. #ifndef _TLX_ITER_H
  50. #include <tlx\501\iter.h>
  51. #endif
  52.  
  53. /****************************************************************************
  54.  
  55.     Implementation of class TLPtrIter<T>
  56.  
  57. ****************************************************************************/
  58.  
  59. /*-------------------------------------------------------------------------*/
  60.     template<class T> TLPtrIter<T>::TLPtrIter()
  61.  
  62. /*  Default constructor. Does nothing.
  63. ---------------------------------------------------------------------------*/
  64. {
  65. }
  66.  
  67. /*-------------------------------------------------------------------------*/
  68.     template<class T> bool TLPtrIter<T>::Contains(T *&aPtr)
  69.  
  70. /*  Checks of the given item appears in the associated collection. This
  71.     operation will change the state of the iterator.
  72. ---------------------------------------------------------------------------*/
  73. {
  74.     for (Reset(); Next(); )
  75.     if (Peek() == aPtr)
  76.         return true;
  77.     return false;
  78. }
  79.  
  80. /*-------------------------------------------------------------------------*/
  81.     template<class T> size_t TLPtrIter<T>::Count() const
  82.  
  83. /*  Returns the number of items in the underlying collection, which is
  84.     always 0.
  85. ---------------------------------------------------------------------------*/
  86. {
  87.     return 0;
  88. }
  89.  
  90. /*-------------------------------------------------------------------------*/
  91.     template<class T> bool TLPtrIter<T>::FirstPos()
  92.  
  93. /*  Advances the iterator to the first position in the associated
  94.     collection. In this case, the operation always fails.
  95. ---------------------------------------------------------------------------*/
  96. {
  97.     return false;
  98. }
  99.  
  100. /*-------------------------------------------------------------------------*/
  101.     template<class T> bool TLPtrIter<T>::FirstThat
  102.     (
  103.         tTestFunc     aTest,         // Test to apply
  104.     void *        aArg,         // Optional argument
  105.     T **&        aSuccess    // Set to first succeeding element
  106.     )
  107.  
  108. /*  Applies a given test to elements in the associated collection until the
  109.     test returns true. It then sets 'aSuccess' to point to the first element
  110.     that made the test succeed and returns true. If no element made the
  111.     test succeed, false is returned and 'aSuccess' is not changed.
  112. ---------------------------------------------------------------------------*/
  113. {
  114.     TLX_ASSERT_PTR(aTest);
  115.  
  116.     for (Reset(); Next(); )
  117.     if ((*aTest)(Peek(), aArg))
  118.     {
  119.         aSuccess = &Peek();
  120.         return true;
  121.     }
  122.     return false;
  123. }
  124.  
  125. /*-------------------------------------------------------------------------*/
  126.     template<class T> bool TLPtrIter<T>::FirstThatNot
  127.     (
  128.         tTestFunc     aTest,        // Test to apply
  129.     void *        aArg,         // Optional argument
  130.     T **&        aFailure    // Set to first failing element
  131.     )
  132.  
  133. /*  Applies a given test to elements in the associated collection as long as
  134.     the test returns true. It then sets 'aFailure' to point to the first
  135.     element that made the test fail and returns true. If no element made the
  136.     test fail, false is returned and 'aFailure' is not changed.
  137. ---------------------------------------------------------------------------*/
  138. {
  139.     TLX_ASSERT_PTR(aTest);
  140.  
  141.     for (Reset(); Next(); )
  142.     if (!(*aTest)(Peek(), aArg))
  143.     {
  144.         aFailure = &Peek();
  145.         return true;
  146.     }
  147.     return false;
  148. }
  149.  
  150. /*-------------------------------------------------------------------------*/
  151.     template<class T> void TLPtrIter<T>::ForAll
  152.     (
  153.         tApplyFunc     aFunc,
  154.     void *        aArg
  155.     )
  156.  
  157. /*  Applies a given function to all elements in the associated collection.
  158. ---------------------------------------------------------------------------*/
  159. {
  160.     TLX_ASSERT_PTR(aFunc);
  161.  
  162.     for (Reset(); Next(); )
  163.     (*aFunc)(Peek(), aArg);
  164. }
  165.  
  166. /*-------------------------------------------------------------------------*/
  167.     template<class T> bool TLPtrIter<T>::NextPos()
  168.  
  169. /*  Advances the iterator to the next position in the associated collection.
  170.     This version should never be called, since the iterator already fails
  171.     the first position.
  172. ---------------------------------------------------------------------------*/
  173. {
  174.     TLX_ASSERT_UNREACHABLE;
  175.     return false;
  176. }
  177.  
  178. /*-------------------------------------------------------------------------*/
  179.     template<class T> T *&TLPtrIter<T>::Peek() const
  180.  
  181. /*  Returns a reference to the currently addressed item in the associated
  182.     collection. This version should never be called, since the iterator
  183.     fails all iteration attempts.
  184. ---------------------------------------------------------------------------*/
  185. {
  186.     TLX_ASSERT_UNREACHABLE;
  187.     THROW(TLXEmpty(LOCUS));
  188. #if defined(_MSC_VER) || defined(__SC__)
  189.     // Microsoft and Symantec C++ don't know this is never reached
  190.     static T *dummy = 0;
  191.     return dummy;
  192. #endif
  193. }
  194.  
  195. /*-------------------------------------------------------------------------*/
  196.     template<class T> ostream &TLPtrIter<T>::PrintAll(ostream &os)
  197.  
  198. /*  Prints all elements in the associated collection without any formatting.
  199.     This function assumes the existence of operator <<(ostream &, const T &).
  200. ---------------------------------------------------------------------------*/
  201. {
  202.     return PrintFormat(os, TLPrintFormat());
  203. }
  204.  
  205. /*-------------------------------------------------------------------------*/
  206.     template<class T> ostream &TLPtrIter<T>::PrintFormat
  207.     (
  208.         ostream &        os,
  209.     const TLPrintFormat &    aFmt
  210.     )
  211.  
  212. /*  Prints all elements in the associated collection with the given
  213.     formatting around the collection and its elements. This function
  214.     assumes the existence of operator <<(ostream &, const T &).
  215. ---------------------------------------------------------------------------*/
  216. {
  217.     TLX_ASSERT_PTR(aFmt.mLeader);
  218.     TLX_ASSERT_PTR(aFmt.mPresep);
  219.     TLX_ASSERT_PTR(aFmt.mPostsep);
  220.     TLX_ASSERT_PTR(aFmt.mTrailer);
  221.  
  222.     os << aFmt.mLeader;
  223.     for (Reset(); Next(); )
  224.     {
  225.     os << aFmt.mPresep;
  226. /*    if (Peek())
  227.         os << *Peek();
  228.     else
  229.         os << "NULL";
  230. */    os << aFmt.mPostsep;
  231.     }
  232.  
  233.     return os << aFmt.mTrailer;
  234. }
  235.  
  236. /****************************************************************************
  237.  
  238.     Implementation of class TLPtrIterConst<T>
  239.  
  240. ****************************************************************************/
  241.  
  242. /*-------------------------------------------------------------------------*/
  243.     template<class T> TLPtrIterConst<T>::TLPtrIterConst()
  244.  
  245. /*  Default constructor. Does nothing.
  246. ---------------------------------------------------------------------------*/
  247. {
  248. }
  249.  
  250. /*-------------------------------------------------------------------------*/
  251.     template<class T> bool TLPtrIterConst<T>::Contains(T *aPtr)
  252.  
  253. /*  Checks of the given item appears in the associated collection. This
  254.     operation will change the state of the iterator.
  255. ---------------------------------------------------------------------------*/
  256. {
  257.     for (Reset(); Next(); )
  258.     if (Peek() == aPtr)
  259.         return true;
  260.     return false;
  261. }
  262.  
  263. /*-------------------------------------------------------------------------*/
  264.     template<class T> size_t TLPtrIterConst<T>::Count() const
  265.  
  266. /*  Returns the number of items in the underlying collection, which is
  267.     always 0.
  268. ---------------------------------------------------------------------------*/
  269. {
  270.     return 0;
  271. }
  272.  
  273. /*-------------------------------------------------------------------------*/
  274.     template<class T> bool TLPtrIterConst<T>::FirstPos()
  275.  
  276. /*  Advances the iterator to the first position in the associated
  277.     collection. In this case, the operation always fails.
  278. ---------------------------------------------------------------------------*/
  279. {
  280.     return false;
  281. }
  282.  
  283. /*-------------------------------------------------------------------------*/
  284.     template<class T> bool TLPtrIterConst<T>::FirstThat
  285.     (
  286.         tTestFunc     aTest,         // Test to apply
  287.     void *        aArg,         // Optional argument
  288.     T *&        aSuccess    // Set to first succeeding element
  289.     )
  290.  
  291. /*  Applies a given test to elements in the associated collection until the
  292.     test returns true. It then sets 'aSuccess' to point to the first element
  293.     that made the test succeed and returns true. If no element made the
  294.     test succeed, false is returned and 'aSuccess' is not changed.
  295. ---------------------------------------------------------------------------*/
  296. {
  297.     TLX_ASSERT_PTR(aTest);
  298.  
  299.     for (Reset(); Next(); )
  300.     if ((*aTest)(Peek(), aArg))
  301.     {
  302.         aSuccess = Peek();
  303.         return true;
  304.     }
  305.     return false;
  306. }
  307.  
  308. /*-------------------------------------------------------------------------*/
  309.     template<class T> bool TLPtrIterConst<T>::FirstThatNot
  310.     (
  311.         tTestFunc     aTest,        // Test to apply
  312.     void *        aArg,         // Optional argument
  313.     T *&        aFailure    // Set to first failing element
  314.     )
  315.  
  316. /*  Applies a given test to elements in the associated collection as long as
  317.     the test returns true. It then sets 'aFailure' to point to the first
  318.     element that made the test fail and returns true. If no element made the
  319.     test fail, false is returned and 'aFailure' is not changed.
  320. ---------------------------------------------------------------------------*/
  321. {
  322.     TLX_ASSERT_PTR(aTest);
  323.  
  324.     for (Reset(); Next(); )
  325.     if (!(*aTest)(Peek(), aArg))
  326.     {
  327.         aFailure = Peek();
  328.         return true;
  329.     }
  330.     return false;
  331. }
  332.  
  333. /*-------------------------------------------------------------------------*/
  334.     template<class T> void TLPtrIterConst<T>::ForAll
  335.     (
  336.         tApplyFunc     aFunc,
  337.     void *        aArg
  338.     )
  339.  
  340. /*  Applies a given function to all elements in the associated collection.
  341. ---------------------------------------------------------------------------*/
  342. {
  343.     TLX_ASSERT_PTR(aFunc);
  344.  
  345.     for (Reset(); Next(); )
  346.     (*aFunc)(Peek(), aArg);
  347. }
  348.  
  349. /*-------------------------------------------------------------------------*/
  350.     template<class T> bool TLPtrIterConst<T>::NextPos()
  351.  
  352. /*  Advances the iterator to the next position in the associated collection.
  353.     This version should never be called, since the iterator already fails
  354.     the first position.
  355. ---------------------------------------------------------------------------*/
  356. {
  357.     TLX_ASSERT_UNREACHABLE;
  358.     return false;
  359. }
  360.  
  361. /*-------------------------------------------------------------------------*/
  362.     template<class T> T *TLPtrIterConst<T>::Peek() const
  363.  
  364. /*  Returns a pointer to the currently addressed item in the associated
  365.     collection. This version should never be called, since the iterator
  366.     fails all iteration attempts.
  367. ---------------------------------------------------------------------------*/
  368. {
  369.     TLX_ASSERT_UNREACHABLE;
  370.     THROW(TLXEmpty(LOCUS));
  371. #if defined(_MSC_VER) || defined(__SC__)
  372.     // Microsoft and Symantec C++ don't know this is never reached
  373.     return 0;
  374. #endif
  375. }
  376.  
  377. /*-------------------------------------------------------------------------*/
  378.     template<class T> ostream &TLPtrIterConst<T>::PrintAll(ostream &os)
  379.  
  380. /*  Prints all elements in the associated collection without any formatting.
  381.     This function assumes the existence of operator <<(ostream &, const T &).
  382. ---------------------------------------------------------------------------*/
  383. {
  384.     return PrintFormat(os, TLPrintFormat());
  385. }
  386.  
  387. /*-------------------------------------------------------------------------*/
  388.     template<class T> ostream &TLPtrIterConst<T>::PrintFormat
  389.     (
  390.         ostream &        os,
  391.     const TLPrintFormat &    aFmt
  392.     )
  393.  
  394. /*  Prints all elements in the associated collection with the given
  395.     formatting around the collection and its elements. This function
  396.     assumes the existence of operator <<(ostream &, const T &).
  397. ---------------------------------------------------------------------------*/
  398. {
  399.     TLX_ASSERT_PTR(aFmt.mLeader);
  400.     TLX_ASSERT_PTR(aFmt.mPresep);
  401.     TLX_ASSERT_PTR(aFmt.mPostsep);
  402.     TLX_ASSERT_PTR(aFmt.mTrailer);
  403.  
  404.     os << aFmt.mLeader;
  405.     for (Reset(); Next(); )
  406.     {
  407.     os << aFmt.mPresep;
  408.     if (Peek())
  409.         os << *Peek();
  410.     else
  411.         os << "NULL";
  412.     os << aFmt.mPostsep;
  413.     }
  414.  
  415.     return os << aFmt.mTrailer;
  416. }
  417.  
  418. /*-------------------------------------------------------------------------*/
  419.     template<class T> TLPtrItemIter<T>::TLPtrItemIter(T *&aItem)
  420.  
  421. /*  Constructor. Links to the given item.
  422. ---------------------------------------------------------------------------*/
  423. : mItem(aItem)
  424. {
  425. }
  426.  
  427. /*-------------------------------------------------------------------------*/
  428.     template<class T> bool TLPtrItemIter<T>::Contains(T *&aItem)
  429.  
  430. /*  Returns true iff the associated collection (a single element) contains
  431.     the item. In this case, a simple test for equality is performed.
  432. ---------------------------------------------------------------------------*/
  433. {
  434.     return aItem == mItem;
  435. }
  436.  
  437. /*-------------------------------------------------------------------------*/
  438.     template<class T> size_t TLPtrItemIter<T>::Count() const
  439.  
  440. /*  Returns the number of elements in the associated collection, which is
  441.     always 1 in this case.
  442. ---------------------------------------------------------------------------*/
  443. {
  444.     return 1;
  445. }
  446.  
  447. /*-------------------------------------------------------------------------*/
  448.     template<class T> bool TLPtrItemIter<T>::FirstPos()
  449.  
  450. /*  Sets the iterator to the first available position (if any), returning
  451.     true if that move succeeds.
  452. ---------------------------------------------------------------------------*/
  453. {
  454.     return true;
  455. }
  456.  
  457. /*-------------------------------------------------------------------------*/
  458.     template<class T> bool TLPtrItemIter<T>::NextPos()
  459.  
  460. /*  Advances the iterator to the next available position (if any), returning
  461.     true if that move succeeds.
  462. ---------------------------------------------------------------------------*/
  463. {
  464.     return false;
  465. }
  466.  
  467. /*-------------------------------------------------------------------------*/
  468.     template<class T> T *&TLPtrItemIter<T>::Peek() const
  469.  
  470. /*  Returns a reference to the currently iterated element.
  471. ---------------------------------------------------------------------------*/
  472. {
  473.     TLX_ASSERT(IsValid());
  474.     return mItem;
  475. }
  476.  
  477. /*-------------------------------------------------------------------------*/
  478.     template<class T> TLPtrItemIterConst<T>::TLPtrItemIterConst(T *aItem)
  479.  
  480. /*  Constructor. Links to the given item.
  481. ---------------------------------------------------------------------------*/
  482. : mItem(aItem)
  483. {
  484. }
  485.  
  486. /*-------------------------------------------------------------------------*/
  487.     template<class T> bool TLPtrItemIterConst<T>::Contains(T *aItem)
  488.  
  489. /*  Returns true iff the associated collection (a single element) contains
  490.     the item. In this case, a simple test for equality is performed.
  491. ---------------------------------------------------------------------------*/
  492. {
  493.     return aItem == mItem;
  494. }
  495.  
  496. /*-------------------------------------------------------------------------*/
  497.     template<class T> size_t TLPtrItemIterConst<T>::Count() const
  498.  
  499. /*  Returns the number of elements in the associated collection, which is
  500.     always 1 in this case.
  501. ---------------------------------------------------------------------------*/
  502. {
  503.     return 1;
  504. }
  505.  
  506. /*-------------------------------------------------------------------------*/
  507.     template<class T> bool TLPtrItemIterConst<T>::FirstPos()
  508.  
  509. /*  Sets the iterator to the first available position (if any), returning
  510.     true if that move succeeds.
  511. ---------------------------------------------------------------------------*/
  512. {
  513.     return true;
  514. }
  515.  
  516. /*-------------------------------------------------------------------------*/
  517.     template<class T> bool TLPtrItemIterConst<T>::NextPos()
  518.  
  519. /*  Advances the iterator to the next available position (if any), returning
  520.     true if that move succeeds.
  521. ---------------------------------------------------------------------------*/
  522. {
  523.     return false;
  524. }
  525.  
  526. /*-------------------------------------------------------------------------*/
  527.     template<class T> T *TLPtrItemIterConst<T>::Peek() const
  528.  
  529. /*  Returns a reference to the currently iterated element.
  530. ---------------------------------------------------------------------------*/
  531. {
  532.     TLX_ASSERT(IsValid());
  533.     return mItem;
  534. }
  535.  
  536. #endif    // _TLX_PTRITER_CPP
  537.