home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / Dloads / OTHERUTI / TCPP30-1.ZIP / CLASSINC.ZIP / DBLLIST.H < prev    next >
C/C++ Source or Header  |  1992-02-18  |  6KB  |  240 lines

  1. /*------------------------------------------------------------------------*/
  2. /*                                                                        */
  3. /*  DBLLIST.H                                                             */
  4. /*                                                                        */
  5. /*  Copyright Borland International 1991                                  */
  6. /*  All Rights Reserved                                                   */
  7. /*                                                                        */
  8. /*------------------------------------------------------------------------*/
  9.  
  10. #if !defined( __DBLLIST_H )
  11. #define __DBLLIST_H
  12.  
  13. #if !defined( __CLSTYPES_H )
  14. #include <ClsTypes.h>
  15. #endif  // __CLSTYPES_H
  16.  
  17. #if !defined( __OBJECT_H )
  18. #include <Object.h>
  19. #endif  // __OBJECT_H
  20.  
  21. #if !defined( __COLLECT_H )
  22. #include <Collect.h>
  23. #endif  // __COLLECT_H
  24.  
  25. #if !defined( __MEMMGR_H )
  26. #include <MemMgr.h>
  27. #endif  // __MEMMGR_H
  28.  
  29. #if !defined( __SHDDEL_H )
  30. #include <ShdDel.h>
  31. #endif  // __SHDDEL_H
  32.  
  33. _CLASSDEF(DoubleList)
  34. _CLASSDEF(DoubleListIterator)
  35.  
  36. class _CLASSTYPE DoubleListBlockInitializer
  37. {
  38.  
  39. protected:
  40.  
  41.     DoubleListBlockInitializer();
  42.     ~DoubleListBlockInitializer();
  43.  
  44.     static unsigned count;
  45.  
  46. };
  47.  
  48. class _CLASSTYPE DoubleList : 
  49.     public Collection, 
  50.     private DoubleListBlockInitializer
  51. {
  52.  
  53. public:
  54.  
  55.     DoubleList() :
  56.         headEntry( 0, &headEntry, &tailEntry ),
  57.         tailEntry( 0, &headEntry, &tailEntry ),
  58.         head( &headEntry ),
  59.         tail( &tailEntry ),
  60.         itemsInContainer(0)
  61.         {
  62.         }
  63.  
  64.     ~DoubleList()
  65.         {
  66.         flush();
  67.         }
  68.  
  69.     Object _FAR & peekAtHead() const
  70.         {
  71.         return ptrToRef(head->next->data);
  72.         }
  73.  
  74.     Object _FAR & peekAtTail() const
  75.         {
  76.         return ptrToRef(tail->prev->data);
  77.         }
  78.  
  79.     virtual void add( Object _FAR & toAdd )
  80.         {
  81.         addAtHead( toAdd );
  82.         }
  83.  
  84.     virtual void detach( Object _FAR &, DeleteType = NoDelete );
  85.     virtual void flush( DeleteType = DefDelete );
  86.  
  87.     void addAtHead( Object _FAR & );
  88.     void addAtTail( Object _FAR & );
  89.     void destroyFromHead( Object _FAR & );
  90.     void destroyFromTail( Object _FAR & );
  91.     void detachFromHead( Object _FAR &, DeleteType = NoDelete );
  92.     void detachFromTail( Object _FAR &, DeleteType = NoDelete );
  93.  
  94.     int isEmpty() const
  95.         {
  96.         return itemsInContainer == 0;
  97.         }
  98.  
  99.     countType getItemsInContainer() const
  100.         {
  101.         return itemsInContainer;
  102.         }
  103.  
  104.     virtual ContainerIterator _FAR & initIterator() const;
  105.     ContainerIterator _FAR & initReverseIterator() const;
  106.  
  107.     virtual classType isA() const
  108.         {
  109.         return doubleListClass;
  110.         }
  111.  
  112.     virtual char _FAR *nameOf() const
  113.         {
  114.         return "DoubleList";
  115.         }
  116.  
  117. private:
  118.  
  119.     class _CLASSTYPE ListElement
  120.     {
  121.  
  122.     public:
  123.  
  124.         ListElement( Object _FAR *o,
  125.                      ListElement _FAR *p = 0,
  126.                      ListElement _FAR *n = 0
  127.                    )
  128.             {
  129.             data = o;
  130.             prev = p;
  131.             next = n;
  132.             }
  133.  
  134.     private:
  135.  
  136.         ListElement _FAR *next;
  137.         ListElement _FAR *prev;
  138.         Object _FAR *data;
  139.  
  140.         void _FAR *operator new( size_t sz )
  141.             {
  142.             PRECONDITION( mgr != 0 );
  143.             return mgr->allocate( sz );
  144.             }
  145.         void operator delete( void _FAR *b )
  146.             {
  147.             PRECONDITION( mgr != 0 );
  148.             mgr->free( b );
  149.             }
  150.  
  151.         static MemBlocks _FAR *mgr;
  152.  
  153.         friend class DoubleList;
  154.         friend class DoubleListIterator;
  155.         friend class DoubleListBlockInitializer;
  156.  
  157.     };
  158.  
  159.     ListElement _FAR *head;
  160.     ListElement _FAR *tail;
  161.  
  162.     ListElement headEntry, tailEntry;
  163.  
  164.     unsigned itemsInContainer;
  165.  
  166.     friend class DoubleListIterator;
  167.     friend class DoubleListBlockInitializer;
  168.  
  169. };
  170.  
  171. inline DoubleListBlockInitializer::DoubleListBlockInitializer()
  172. {
  173.     PRECONDITION( count != UINT_MAX );
  174.     if( count++ == 0 )
  175.         DoubleList::ListElement::mgr = 
  176.             new MemBlocks( sizeof(DoubleList::ListElement), 20 );
  177. }
  178.  
  179. inline DoubleListBlockInitializer::~DoubleListBlockInitializer()
  180. {
  181.     PRECONDITION( count != 0 );
  182.     if( --count == 0 )
  183.         {
  184.         delete DoubleList::ListElement::mgr;
  185.         DoubleList::ListElement::mgr = 0;
  186.         }
  187. }
  188.  
  189. inline void DoubleList::destroyFromHead( Object _FAR & toDestroy )
  190. {
  191.     detachFromHead( toDestroy, DefDelete );
  192. }
  193.  
  194. inline void DoubleList::destroyFromTail( Object _FAR & toDestroy )
  195. {
  196.     detachFromTail( toDestroy, DefDelete );
  197. }
  198.  
  199. class _CLASSTYPE DoubleListIterator : public ContainerIterator
  200. {
  201.  
  202. public:
  203.  
  204.     DoubleListIterator( const DoubleList _FAR &, int = 1 );
  205.     virtual ~DoubleListIterator();
  206.  
  207.     virtual operator int();
  208.     virtual Object _FAR & current();
  209.     virtual Object _FAR & operator ++ ( int );
  210.     virtual Object _FAR & operator ++ ();
  211.     Object _FAR & operator -- ( int );
  212.     Object _FAR & operator -- ();
  213.  
  214.     virtual void restart();
  215.  
  216. private:
  217.  
  218.     DoubleList::ListElement _FAR *currentElement;
  219.     DoubleList::ListElement _FAR *startingElement;
  220.  
  221. };
  222.  
  223. inline
  224. DoubleListIterator::DoubleListIterator( const DoubleList _FAR & toIterate,
  225.                                         int atHead
  226.                                       )
  227. {
  228.     if ( atHead == 1 )
  229.         {
  230.         startingElement = currentElement = toIterate.head->next;
  231.         }
  232.     else
  233.         {
  234.         startingElement = currentElement = toIterate.tail->prev;
  235.         }
  236. }
  237.  
  238. #endif  // __DBLLIST_H
  239.  
  240.