home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / CLOBSH.PAK / DBLLIST.H < prev    next >
C/C++ Source or Header  |  1995-08-29  |  6KB  |  252 lines

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