home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 2.ddi / CLASSINC.ZIP / LIST.H < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  4.2 KB  |  191 lines

  1. /*------------------------------------------------------------------------*/
  2. /*                                                                        */
  3. /*  LIST.H                                                                */
  4. /*                                                                        */
  5. /*  Copyright Borland International 1991, 1992                            */
  6. /*  All Rights Reserved                                                   */
  7. /*                                                                        */
  8. /*------------------------------------------------------------------------*/
  9.  
  10. #if !defined( __LIST_H )
  11. #define __LIST_H
  12.  
  13. #if !defined( __MEMMGR_H )
  14. #include <MemMgr.h>
  15. #endif  // __MEMMGR_H
  16.  
  17. #if !defined( __COLLECT_H )
  18. #include <Collect.h>
  19. #endif  // __COLLECT_H
  20.  
  21. #pragma option -Vo-
  22. #if defined( __BCOPT__ ) && !defined( _ALLOW_po )
  23. #pragma option -po-
  24. #endif
  25.  
  26. _CLASSDEF(List)
  27. _CLASSDEF(ListIterator)
  28.  
  29. class _CLASSTYPE ListBlockInitializer
  30. {
  31.  
  32. protected:
  33.  
  34.     ListBlockInitializer();
  35.     ~ListBlockInitializer();
  36.  
  37.     static unsigned count;
  38.  
  39. };
  40.  
  41. class _CLASSTYPE List : public Collection, private ListBlockInitializer
  42. {
  43.  
  44. public:
  45.  
  46.     List() :
  47.         headEntry( 0, &tailEntry ),
  48.         tailEntry( 0, &tailEntry ),
  49.         head(&headEntry),
  50.         tail(&tailEntry),
  51.         itemsInContainer(0)
  52.         {
  53.         }
  54.  
  55.     virtual ~List()
  56.         {
  57.         flush();
  58.         }
  59.     
  60.     Object _FAR & peekHead() const
  61.         {
  62.         return ptrToRef(head->next->data);
  63.         }
  64.  
  65.     void add( Object _FAR & );
  66.     virtual void detach( Object _FAR &, DeleteType = NoDelete );
  67.     virtual void flush( DeleteType = DefDelete );
  68.  
  69.     virtual int isEmpty() const
  70.         {
  71.         return itemsInContainer == 0;
  72.         }
  73.  
  74.     virtual countType getItemsInContainer() const
  75.         {
  76.         return itemsInContainer;
  77.         }
  78.  
  79.     virtual ContainerIterator _FAR & initIterator() const;
  80.  
  81.     virtual classType isA() const
  82.         {
  83.         return listClass;
  84.         }
  85.  
  86.     virtual char _FAR *nameOf() const
  87.         {
  88.         return "List";
  89.         }
  90.  
  91. private:
  92.  
  93.     class _CLASSTYPE ListElement
  94.     {
  95.  
  96.     public:
  97.  
  98.         ListElement( Object _FAR *o, ListElement _FAR *n = 0 )
  99.             {
  100.             data = o; next = n;
  101.             }
  102.  
  103.     private:
  104.  
  105.         ListElement _FAR *next;
  106.         Object _FAR *data;
  107.  
  108.         void _FAR *operator new( size_t sz )
  109.             {
  110.             PRECONDITION( mgr != 0 );
  111.             return mgr->allocate( sz );
  112.             }
  113.         void operator delete( void _FAR *b )
  114.             {
  115.             PRECONDITION( mgr != 0 );
  116.             mgr->free( b );
  117.             }
  118.  
  119.         static MemBlocks _FAR *mgr;
  120.  
  121.         friend class List;
  122.         friend class ListIterator;
  123.         friend class ListBlockInitializer;
  124.  
  125.     };
  126.  
  127.     ListElement _FAR *head;
  128.     ListElement _FAR *tail;
  129.  
  130.     ListElement headEntry, tailEntry;
  131.  
  132.     unsigned itemsInContainer;
  133.  
  134.     ListElement _FAR *findPred( const Object _FAR & o );
  135.  
  136.     friend class ListIterator;
  137.     friend class ListBlockInitializer;
  138.  
  139. };
  140.  
  141. inline ListBlockInitializer::ListBlockInitializer()
  142. {
  143.     PRECONDITION( count != UINT_MAX );
  144.     if( count++ == 0 )
  145.         List::ListElement::mgr = 
  146.             new MemBlocks( sizeof(List::ListElement), 20 );
  147. }
  148.  
  149. inline ListBlockInitializer::~ListBlockInitializer()
  150. {
  151.     PRECONDITION( count != 0 );
  152.     if( --count == 0 )
  153.         {
  154.         delete List::ListElement::mgr;
  155.         List::ListElement::mgr = 0;
  156.         }
  157. }
  158.  
  159. class _CLASSTYPE ListIterator : public ContainerIterator
  160. {
  161.  
  162. public:
  163.  
  164.     ListIterator( const List _FAR & );
  165.     virtual ~ListIterator();
  166.  
  167.     virtual operator int();
  168.     virtual Object _FAR & current();
  169.     virtual Object _FAR & operator ++ ( int );
  170.     virtual Object _FAR & operator ++ ();
  171.     virtual void restart();
  172.  
  173. private:
  174.  
  175.     List::ListElement _FAR *currentElement;
  176.     List::ListElement _FAR *startingElement;
  177. };
  178.  
  179. inline ListIterator::ListIterator( const List _FAR & toIterate )
  180. {
  181.     startingElement = currentElement = toIterate.head->next;
  182. }
  183.  
  184. #if defined( __BCOPT__ ) && !defined( _ALLOW_po )
  185. #pragma option -po.
  186. #endif
  187. #pragma option -Vo.
  188.  
  189. #endif  // __LIST_H
  190.  
  191.