home *** CD-ROM | disk | FTP | other *** search
/ PC Media 7 / PC MEDIA CD07.iso / share / prog / cm / cmtcont.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-06  |  3.3 KB  |  75 lines

  1. // CmTCont.h
  2. // -----------------------------------------------------------------
  3. // Compendium - C++ Container Class Library
  4. // Copyright (C) 1992-1994, Glenn M. Poorman, All rights reserved
  5. // -----------------------------------------------------------------
  6. // Abstract container template definition.
  7. // -----------------------------------------------------------------
  8.  
  9. #ifndef _CMTCONT_H
  10. #define _CMTCONT_H
  11.  
  12. #include <cm/include/cmtiter.h>
  13.  
  14. template <class T> class CmTContainer {             // Container definition.
  15. public:
  16.   CmTContainer() : _error(* new T), _size(0) {}     // Default constructor.
  17.   CmTContainer(const CmTContainer<T>&);             // Copy constructor.
  18.  
  19.   virtual ~CmTContainer();                          // Virtual destructor.
  20.  
  21.   virtual const T& operator[] (int) const;          // Get object by index.
  22.   virtual int      size       () const;             // Return container size.
  23.   virtual Bool     add        (const T&) = 0;       // Add object to container.
  24.   virtual Bool     remove     (const T&) = 0;       // Remove object.
  25.   virtual const T& lookup     (const T&) const = 0; // Find equal object.
  26.   virtual Bool     contains   (const T&) const = 0; // Container has object?
  27.   virtual unsigned occurrences(const T&) const = 0; // Count occurrences.
  28.   virtual void     removeAll  ()         = 0;       // Remove all objects.
  29.   virtual Bool     isEmpty    () const;             // Is container empty?
  30.  
  31.   virtual CmTIterator<T>* newIterator() const = 0;  // Get container iterator.
  32.  
  33. protected:
  34.   void copy(const CmTContainer<T>&);                // Internal copy method.
  35.  
  36.   T&  _error;                                       // Error object.
  37.   int _size;                                        // Container size.
  38. };
  39.  
  40. #if defined(__TURBOC__) || defined(__xlC__)
  41. #include <cm/include/cmtcont.cc>
  42. #endif
  43.  
  44. // "CmTForEach" and "CmTEndFor" define a foreach loop where the item,
  45. // type and collection are passed in and the item will be set equal to
  46. // the next item in the collection for each loop iteration.
  47. //
  48. #define CmTForEach(obj, type, cltn) {                           \
  49.         CmTIterator<type> *___cm_iterator = cltn.newIterator(); \
  50.         while (!___cm_iterator->done()) {                       \
  51.           obj = ___cm_iterator->next();
  52. #define CmTEndFor } delete ___cm_iterator; }
  53.  
  54. // "CmTRepeat" and "CmTUntil" define a pascal style repeat/until loop
  55. // where the item, type, and collection are passed in and the item will
  56. // be set equal to next item in the collection for each loop iteration.
  57. //
  58. #define CmTRepeat(obj, type, cltn) {                            \
  59.         CmTIterator<type> *___cm_iterator = cltn.newIterator(); \
  60.         do {                                                    \
  61.           obj = ___cm_iterator->next();
  62. #define CmTUntil(condition) } while (!(condition)); }
  63.  
  64. // "CmTDo" and "CmTWhile" define a typical do/while loop where the item,
  65. // type, and collection are passed in and the item will be set equal to
  66. // the next item in the collection for each loop iteration.
  67. //
  68. #define CmTDo(obj, type, cltn) {                                \
  69.         CmTIterator<type> *___cm_iterator = cltn.newIterator(); \
  70.         do {                                                    \
  71.           obj = ___cm_iterator->next();
  72. #define CmTWhile(condition) } while (condition); }
  73.  
  74. #endif
  75.