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

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