home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / watc11up.zip / cpplib_hdr.zip / h / wclibase.h < prev    next >
C/C++ Source or Header  |  2001-08-28  |  9KB  |  322 lines

  1. //
  2. //  wclibase.h    Defines for the WATCOM Container List Iterator Base Classes
  3. //
  4. //  Copyright by WATCOM International Corp. 1988-1996.  All rights reserved.
  5. //  Copyright by Sybase, Inc. 1997-1999.  All rights reserved.
  6. //
  7. #ifndef _WCLIBASE_H_INCLUDED
  8. #define _WCLIBASE_H_INCLUDED
  9. #if !defined(_ENABLE_AUTODEPEND)
  10.   #pragma read_only_file;
  11. #endif
  12.  
  13. #ifndef __cplusplus
  14. #error wclibase.h is for use with C++
  15. #endif
  16.  
  17. #ifndef _WCDEFS_H_INCLUDED
  18.  #include <wcdefs.h>
  19. #endif
  20. #ifndef _WCEXCEPT_H_INCLUDED
  21.  #include <wcexcept.h>
  22. #endif
  23.  
  24.  
  25. //
  26. //  The WCListIterBase class is used as a basis for the iterator
  27. //  classes for the various list containers. 
  28. //
  29. //  It is an abstract base class to prevent objects of this class
  30. //  from being created.
  31. //
  32.  
  33. class WCListIterBase : public WCIterExcept {
  34. protected:
  35.     enum {                      // flags for outside_list
  36.         before_first    = 1,
  37.         after_last
  38.     };
  39.     WCIsvListBase *     curr_list;
  40.     WCSLink *           curr_item;
  41.     // if outside_list is non-zero, curr_item must be 0.
  42.     int                 outside_list;
  43.  
  44.     _WPRTLINK WCSLink *          base_advance( int );
  45.     _WPRTLINK WCDLink *          base_retreat( int );
  46.     inline WCSLink *         base_get_tail() {
  47.         return( curr_list->tail );
  48.     };
  49.  
  50.     // helpers for insert and append
  51.     _WPRTLINK WCSLink *          base_tail_hit( WCSLink * );
  52.     _WPRTLINK void               base_tail_unhit( WCSLink * );
  53.  
  54.     inline void base_reset();
  55.     inline void base_reset_list( WCIsvListBase * list );
  56.  
  57. public:
  58.     inline WCListIterBase() : curr_item( 0 ), curr_list( 0 )
  59.                                 , outside_list( 0 ) {};
  60.     inline WCListIterBase( WCIsvListBase * list )
  61.                          : curr_item( 0 ), curr_list( list )
  62.                          , outside_list( before_first ) {};
  63.     inline virtual ~WCListIterBase() = 0;
  64.  
  65.     inline WCSLink * operator()();
  66.     inline WCSLink * operator++();
  67.     inline WCSLink * operator+=( int );
  68.     inline WCDLink * operator--();
  69.     inline WCDLink * operator-=( int );
  70. };
  71.  
  72. inline WCListIterBase::~WCListIterBase() {};
  73.  
  74. inline void WCListIterBase::base_reset() {
  75.     curr_item = 0;
  76.     outside_list = before_first;
  77. }
  78.  
  79. inline void WCListIterBase::base_reset_list( WCIsvListBase * list ) {
  80.     curr_list = list;
  81.     base_reset();
  82. }
  83.  
  84. inline WCSLink * WCListIterBase::operator()() {
  85.     return( base_advance( 1 ) );
  86. }
  87.  
  88. inline WCSLink * WCListIterBase::operator++() {
  89.     return( base_advance( 1 ) );
  90. }
  91.  
  92. inline WCDLink * WCListIterBase::operator--() {
  93.     return( base_retreat( 1 ) );
  94. }
  95.  
  96. inline WCSLink * WCListIterBase::operator+=( int incr_amount ) {
  97.     return( base_advance( incr_amount ) );
  98. }
  99.  
  100. inline WCDLink * WCListIterBase::operator-=( int incr_amount ) {
  101.     return( base_retreat( incr_amount ) );
  102. }
  103.  
  104.  
  105.  
  106.  
  107. //
  108. //  The WCIsvListIterBase defines the base iterator class for intrusive
  109. //  value type containers.
  110. //
  111.  
  112. template<class Type, class FType>
  113. class WCIsvListIterBase : public WCListIterBase {
  114. public:
  115.     inline WCIsvListIterBase() {};
  116.     inline virtual ~WCIsvListIterBase() = 0;
  117.     inline WCIsvListIterBase( FType * slist ) : WCListIterBase( slist ) {};
  118.  
  119.     inline void reset( FType & slist ) {
  120.         WCListIterBase::base_reset_list( &slist );
  121.     };
  122.  
  123.     inline void reset() {
  124.         WCListIterBase::base_reset();
  125.     };
  126.  
  127.     inline Type * operator()() {
  128.         return( (Type *)WCListIterBase::operator()() );
  129.     };
  130.     
  131.     inline Type * operator++() {
  132.         return( (Type *)WCListIterBase::operator++() );
  133.     };
  134.     
  135.     inline Type * operator--() {
  136.         return( (Type *)WCListIterBase::operator--() );
  137.     };
  138.     
  139.     inline Type * operator+=( int adv_amount ) {
  140.         return( (Type *)WCListIterBase::operator+=( adv_amount ) );
  141.     };
  142.     
  143.     inline Type * operator-=( int adv_amount ) {
  144.         return( (Type *)WCListIterBase::operator-=( adv_amount ) );
  145.     };
  146.     
  147.     inline Type * current() const {
  148.         if( ( curr_list == 0 )||( curr_item == 0 ) ) {
  149.             base_throw_undef_item();
  150.             return( 0 );
  151.         }
  152.         return( (Type *)curr_item );
  153.     };
  154.     
  155.     inline FType * container() const {
  156.         if( curr_list == 0 ) {
  157.             base_throw_undef_iter();
  158.         }
  159.         return( (FType *)curr_list );
  160.     };
  161.  
  162.     WCbool insert( Type * );
  163.  
  164.     WCbool append( Type * );
  165. };
  166.  
  167. template<class Type, class FType>
  168. inline WCIsvListIterBase<Type,FType>::~WCIsvListIterBase() {};
  169.  
  170. template<class Type, class FType>
  171. WCbool WCIsvListIterBase<Type,FType>::insert( Type * datum ) {
  172.     if( ( curr_list == 0 )||( curr_item == 0 ) ) {
  173.         base_throw_undef_iter();
  174.         return( FALSE );
  175.     }
  176.     WCSLink * prev_item = curr_item;
  177.     WCbool ret_val;
  178.     if( prev_item != 0 ) {
  179.         prev_item = ((WCDLink *)prev_item)->prev.link;
  180.     }
  181.     // make the tail the element before the element being inserted
  182.     WCSLink * curr_tail = base_tail_hit( prev_item );
  183.     ret_val = ((FType *)curr_list)->insert( datum );
  184.     // restore the tail
  185.     base_tail_unhit( curr_tail );
  186.     return( ret_val );
  187. };
  188.  
  189. template<class Type, class FType>
  190. WCbool WCIsvListIterBase<Type,FType>::append( Type * datum ) {
  191.     if( ( curr_list == 0 )||( curr_item == 0 ) ) {
  192.         base_throw_undef_iter();
  193.         return( FALSE );
  194.     }
  195.     WCbool ret_val;
  196.     if( curr_item == base_get_tail() ) {
  197.         // append the new element to the end of the list
  198.         ret_val = ((FType *)curr_list)->append( datum );
  199.     } else {
  200.         // the new element is not the last element in the list, so make
  201.         // the tail the element before the new element (curr_item)
  202.         WCSLink * curr_tail = base_tail_hit( curr_item );
  203.         ret_val = ((FType *)curr_list)->insert( datum );
  204.         // restore the tail
  205.         base_tail_unhit( curr_tail );
  206.     }
  207.     return( ret_val );
  208. };
  209.  
  210.  
  211.  
  212.  
  213. //
  214. //  The WCValListIterBase defines the base iterator class for value type
  215. //  containers.
  216. //
  217.  
  218. template<class Type, class FType, class LType>
  219. class WCValListIterBase : public WCListIterBase {
  220. public:
  221.     inline WCValListIterBase() {};
  222.     inline virtual ~WCValListIterBase() = 0;
  223.     inline WCValListIterBase( FType * slist ) : WCListIterBase( slist ) {};
  224.  
  225.     inline void reset( FType & dlist ) {
  226.         WCListIterBase::base_reset_list( &dlist );
  227.     };
  228.  
  229.     inline void reset() {
  230.         WCListIterBase::base_reset();
  231.     };
  232.  
  233.     inline int operator()() {
  234.         return( WCListIterBase::operator()() != 0 );
  235.     };
  236.     
  237.     inline int operator++() {
  238.         return( WCListIterBase::operator++() != 0 );
  239.     };
  240.     
  241.     inline int operator--() {
  242.         return( WCListIterBase::operator--() != 0 );
  243.     };
  244.     
  245.     inline int operator+=( int adv_amount ) {
  246.         return( WCListIterBase::operator+=( adv_amount ) != 0 );
  247.     };
  248.     
  249.     inline int operator-=( int adv_amount ) {
  250.         return( WCListIterBase::operator-=( adv_amount ) != 0 );
  251.     };
  252.  
  253.     inline FType * container() const {
  254.         if( curr_list == 0 ) {
  255.             base_throw_undef_iter();
  256.         }
  257.         return( (FType *)curr_list );
  258.     };
  259.  
  260.     Type current() const;
  261.  
  262.     WCbool insert( Type& );
  263.     
  264.     WCbool append( Type& );
  265. };
  266.  
  267. template<class Type, class FType, class LType>
  268. inline WCValListIterBase<Type,FType,LType>::~WCValListIterBase() {};
  269.  
  270. template<class Type, class FType, class LType>
  271. Type WCValListIterBase<Type,FType,LType>::current() const {
  272.     if( ( curr_list == 0 )||( curr_item == 0 ) ) {
  273.         base_throw_undef_item();
  274.         Type ret_val;
  275.         return( ret_val );
  276.     } else {
  277.         return( ((LType *)curr_item)->data );
  278.     }
  279. };
  280.  
  281. template<class Type, class FType, class LType>
  282. WCbool WCValListIterBase<Type,FType,LType>::insert( Type& datum ) {
  283.     if( ( curr_list == 0 )||( curr_item == 0 ) ) {
  284.         base_throw_undef_iter();
  285.         return( FALSE );
  286.     }
  287.     WCSLink * prev_item = curr_item;
  288.     WCbool ret_val;
  289.     if( prev_item != 0 ) {
  290.         prev_item = ((WCDLink *)prev_item)->prev.link;
  291.     }
  292.     // make the tail the element before the element being inserted
  293.     WCSLink * curr_tail = base_tail_hit( prev_item );
  294.     ret_val = ((FType *)curr_list)->insert( datum );
  295.     // restore the tail
  296.     base_tail_unhit( curr_tail );
  297.     return( ret_val );
  298. };
  299.  
  300. template<class Type, class FType, class LType>
  301. WCbool WCValListIterBase<Type,FType,LType>::append( Type& datum ) {
  302.     if( ( curr_list == 0 )||( curr_item == 0 ) ) {
  303.         base_throw_undef_iter();
  304.         return( FALSE );
  305.     }
  306.     WCbool ret_val;
  307.     if( curr_item == base_get_tail() ) {
  308.         // append the new element to the end of the list
  309.         ret_val = ((FType *)curr_list)->append( datum );
  310.     } else {
  311.         // the new element is not the last element in the list, so make
  312.         // the tail the element before the new element (curr_item)
  313.         WCSLink * curr_tail = base_tail_hit( curr_item );
  314.         ret_val = ((FType *)curr_list)->insert( datum );
  315.         // restore the tail
  316.         base_tail_unhit( curr_tail );
  317.     }
  318.     return( ret_val );
  319. };
  320.  
  321. #endif
  322.