home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / cmdline.lha / cmdline / src / lib / fifolist.h < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-03  |  7.0 KB  |  253 lines

  1. //------------------------------------------------------------------------
  2. // ^FILE: fifolist.h - generic FIFO list classes
  3. //
  4. // ^DESCRIPTION:
  5. //    This file defines a generic FIFO linked list class and two types
  6. //    of iterators for the list.  The first iterator is just your basic
  7. //    run-of-the-mill iterator.  The second iterator treats the list
  8. //    as if it were an array and allows you to index into the list.
  9. //
  10. //    Once these generic classes are declared, macros are defined to allow
  11. //    the programmer to declare lists (and iterators) that contain a
  12. //    particular type of item.  On systems where your C++ compiler supports
  13. //    templates, templates are used, otherwise we "fake it".
  14. //
  15. //    The macro defined is named DECLARE_FIFO_LIST and is used as follows:
  16. //
  17. //       DECLARE_FIFO_LIST(Name, Type);
  18. //
  19. //    This declares a type named "Name" that is a list of pointers to
  20. //    items of type "Type".  Also, the types "NameIter" and "NameArray"
  21. //    are declared as the iterators for this type of list.
  22. //
  23. // ^HISTORY:
  24. //    03/21/92    Brad Appleton    <brad@ssd.csd.harris.com>    Created
  25. //-^^---------------------------------------------------------------------
  26.  
  27. #ifndef _fifolist_h
  28. #define _fifolist_h
  29.  
  30. #ifndef  name2
  31. # if defined(__STDC__) || defined(__ANSI_CPP__)
  32. #  define  name2(x,y) x##y
  33. # else
  34. #  define  name2(x,y) x/**/y
  35. # endif
  36. #endif
  37.  
  38.  
  39.    // GenericFifoList - a FIFO linked list of void * pointers
  40.    //
  41. class  GenericFifoList {
  42. private:
  43.  
  44. protected:
  45.    // Need to define what a "node" in the list looks like
  46.    struct GenericFifoListNode {
  47.       GenericFifoListNode * next;
  48.       void * contents;
  49.  
  50.       GenericFifoListNode(GenericFifoListNode * nd =0, void * val =0)
  51.          : next(nd), contents(val) {}
  52.    } ;
  53.  
  54.    unsigned  mod : 1;
  55.    unsigned  del_items : 1;
  56.    unsigned  num_items ;
  57.    GenericFifoListNode * head;
  58.    GenericFifoListNode * tail;
  59.  
  60. public:
  61.    GenericFifoList(void)
  62.       : head(0), tail(0), num_items(0), del_items(0), mod(0) {}
  63.  
  64.    virtual  ~GenericFifoList(void);
  65.  
  66.    // Remove the first item from the list
  67.    void *
  68.    remove(void);
  69.  
  70.    // Add an item to the end of the list
  71.    void
  72.    add(void * item);
  73.  
  74.    // Was the list modified since the last time we checked?
  75.    int
  76.    modified(void) { return (mod) ? (mod = 0, 1) : 0 ; }
  77.  
  78.    // Is the list empty?
  79.    int
  80.    is_empty(void) const { return  (num_items == 0); }
  81.  
  82.    // How many items are in the list?
  83.    unsigned
  84.    count(void) const { return  num_items; }
  85.  
  86.    // Is the list responsible for deleting the items it contains?
  87.    int
  88.    self_cleaning(void) const { return  int(del_items); }
  89.  
  90.    // Tell the list who is responsible for deleting the items it contains?
  91.    void
  92.    self_cleaning(int  bool_val)  { del_items = (bool_val) ? 1 : 0; }
  93.  
  94.    friend class GenericFifoListIter;
  95.    friend class GenericFifoListArray;
  96. } ;
  97.  
  98.  
  99.    // GenericFifoListIter -- an iterator for a GenericFifoList
  100. class  GenericFifoListIter {
  101. private:
  102.    GenericFifoList::GenericFifoListNode * current;
  103.  
  104. public:
  105.    GenericFifoListIter(GenericFifoList & fifo_list)
  106.       : current(fifo_list.head) {}
  107.  
  108.    GenericFifoListIter(GenericFifoList * fifo_list)
  109.       : current(fifo_list->head) {}
  110.  
  111.    virtual  ~GenericFifoListIter(void);
  112.  
  113.    // Return the current item in the list and advance to the next item.
  114.    // returns NULL if at end-of-list
  115.    //
  116.    void *
  117.    operator()(void);
  118. } ;
  119.  
  120.  
  121.    // GenericFifoListArray -- an array-style iterator for a GenericFifoList
  122. class  GenericFifoListArray {
  123. private:
  124.    GenericFifoList &  list;
  125.    unsigned           index;
  126.    GenericFifoList::GenericFifoListNode * current;
  127.  
  128. public:
  129.    GenericFifoListArray(GenericFifoList & fifo_list)
  130.       : list(fifo_list), index(0), current(fifo_list.head) {}
  131.  
  132.    GenericFifoListArray(GenericFifoList * fifo_list)
  133.       : list(*fifo_list), index(0), current(fifo_list->head) {}
  134.  
  135.    virtual  ~GenericFifoListArray(void);
  136.  
  137.    // How many items are in the array?
  138.    unsigned  count(void) const  { return  list.count(); }
  139.  
  140.    // Return a specified item in the array.
  141.    //   NOTE: the programmer is responsible for making sure the given index
  142.    //         is not out of range. For this base class, NULL is returned
  143.    //         when the index is out of range. Derived classes however
  144.    //         dereference the value returned by this function so using
  145.    //         an out-of-range index in one of the derived classes will
  146.    //         cause a NULL pointer dereferencing error!
  147.    //
  148.    void *
  149.    operator[](unsigned  ndx);
  150. } ;
  151.  
  152. #ifdef TEMPLATES
  153.  
  154. template <class Type>
  155. class FifoList : public GenericFifoList {
  156. public:
  157.    FifoList(void) {}
  158.  
  159.    virtual ~FifoList(void);
  160.  
  161.    void
  162.    add(Type * item)  { GenericFifoList::add((void *)item); }
  163.  
  164.    Type *
  165.    remove(void)  { return  (Type *) GenericFifoList::remove(); }
  166. } ;
  167.  
  168. template <class Type>
  169. class FifoListIter : public GenericFifoListIter {
  170. public:
  171.    FifoListIter(FifoList<Type> & list) : GenericFifoListIter(list) {}
  172.    FifoListIter(FifoList<Type> * list) : GenericFifoListIter(list) {}
  173.  
  174.    virtual  ~FifoListIter(void);
  175.  
  176.    Type *
  177.    operator()(void)  {  return  (Type *) GenericFifoListIter::operator()(); }
  178. } ;
  179.  
  180. template <class Type>
  181. class FifoListArray : public GenericFifoListArray {
  182. public:
  183.    FifoListArray(FifoList<Type> & list) : GenericFifoListArray(list) {}
  184.    FifoListArray(FifoList<Type> * list) : GenericFifoListArray(list) {}
  185.  
  186.    virtual  ~FifoListArray(void);
  187.  
  188.    Type &
  189.    operator[](unsigned  ndx)
  190.       { return  *((Type *) GenericFifoListArray::operator[](ndx)) }
  191. } ;
  192.  
  193. #define  DECLARE_FIFO_LIST(Name,Type) \
  194.    typedef  FifoList<Type> Name; \
  195.    typedef  FifoListIter<Type>  name2(Name,Iter) \
  196.    typedef  FifoListArray<Type> name2(Name,Array)
  197.  
  198. #else  /* dont have templates -- have to fake it */
  199.  
  200. #define  DECLARE_FIFO_LIST(Name,Type) \
  201.    class Name : public GenericFifoList {  \
  202.    public:  \
  203.       Name(void) {}  \
  204. \
  205.       virtual ~Name(void) {  \
  206.          GenericFifoListNode * nd = head;  \
  207.          head = 0; \
  208.          while (nd) {  \
  209.             GenericFifoListNode * to_delete = nd;  \
  210.             nd = nd->next;  \
  211.             if (del_items)  delete (Type *)to_delete->contents;  \
  212.             delete  to_delete;  \
  213.          }  \
  214.       }  \
  215. \
  216.       void  \
  217.       add(Type * item)  { GenericFifoList::add((void *)item); }  \
  218. \
  219.       Type *  \
  220.       remove(void)  { return  (Type *) GenericFifoList::remove(); }  \
  221. \
  222.       friend  class name2(Name,Iter);  \
  223.    } ;  \
  224. \
  225. class name2(Name,Iter) : public GenericFifoListIter {  \
  226. public:  \
  227.    name2(Name,Iter)(Name & list) : GenericFifoListIter(list) {}  \
  228.    name2(Name,Iter)(Name * list) : GenericFifoListIter(list) {}  \
  229. \
  230.    virtual ~ name2(Name,Iter)(void) {} \
  231. \
  232.    Type *  \
  233.    operator()(void)  {  return  (Type *) GenericFifoListIter::operator()(); } \
  234. } ; \
  235. \
  236. class name2(Name,Array) : public GenericFifoListArray {  \
  237. public:  \
  238.    name2(Name,Array)(Name & list) : GenericFifoListArray(list) {}  \
  239.    name2(Name,Array)(Name * list) : GenericFifoListArray(list) {}  \
  240. \
  241.    virtual ~ name2(Name,Array)(void) {} \
  242. \
  243.    Type &  \
  244.    operator[](unsigned  ndx)  \
  245.       { return  *((Type *) GenericFifoListArray::operator[](ndx)); }  \
  246. }
  247.  
  248. #endif  /* TEMPLATES */
  249.  
  250.  
  251. #endif /* _fifolist_h */
  252.  
  253.