home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 419_01 / odmg10 / lib / odmg / Iterator.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-28  |  2.1 KB  |  122 lines

  1. /*******************************<+>***************************
  2.  **                             DTA
  3.  *************************************************************
  4.  **
  5.  **  $Id: Iterator.h,v 1.7 1994/03/01 23:01:22 dta Exp $
  6.  **
  7.  **  $Source: /cvs/lib/odmg/Iterator.h,v $
  8.  **
  9.  **  What @(#):
  10.  **
  11.  **  Author: Dale T. Anderson
  12.  **
  13.  *******************************<+>***************************/
  14.  
  15. #ifndef Iterator_h
  16. #define Iterator_h
  17.  
  18. #include <Odmg/Ref.h>
  19.  
  20. #define INVALID_POSITION (-1)
  21.  
  22. template <class T> class Collection;
  23.  
  24. template <class T>
  25. class Iterator
  26. {
  27.     protected:
  28.  
  29.     Ref <Collection <T> > m_collection;
  30.     Ref <T> m_ref;
  31.     Position m_position;
  32.  
  33.     public:
  34.  
  35.     virtual void reset (void);
  36.     virtual void close (void);
  37.     virtual boolean next (Ref <T> &);
  38.  
  39.     //
  40.     // Additions
  41.     //
  42.  
  43.     Iterator (const Ref <Collection <T> >);
  44.     virtual ~Iterator (void);
  45.  
  46.     Ref <T> &operator = (Iterator <T> &iter);
  47.     Iterator <T> &operator = (Ref <T> &ref);
  48.  
  49.     Position get_position (void) const;
  50.     void set_position (const Position position);
  51. };
  52.  
  53. //
  54. // Methods
  55. //
  56.  
  57. template <class T>
  58. Iterator<T>::Iterator (const Ref <Collection <T> > collection) :
  59.     m_position (INVALID_POSITION),
  60.     m_collection (collection)
  61. {
  62. }
  63.  
  64. template <class T>
  65. Iterator<T>::~Iterator (void)
  66. {
  67. }
  68.  
  69. template <class T>
  70. void Iterator<T>::reset (void)
  71. {
  72.     m_position = INVALID_POSITION;
  73. }
  74.  
  75. template <class T>
  76. void Iterator<T>::close (void)
  77. {
  78.     cerr << "void Iterator<T>::close(void) - not implemented." << endl;
  79. }
  80.  
  81. template <class T>
  82. boolean Iterator<T>::next (Ref <T> &ref)
  83. {
  84.     if (++m_position < m_collection->cardinality ()) {
  85.     ref = m_collection->retrieve_element_at (m_position);
  86.     m_ref = ref;
  87.     return TRUE;
  88.     }
  89.     return FALSE;
  90. }
  91.  
  92. //
  93. // Additions
  94. //
  95.  
  96. template <class T>
  97. Ref <T> &Iterator<T>::operator = (Iterator <T> &iter)
  98. {
  99.     return iter.m_ref;
  100. }
  101.  
  102. template <class T>
  103. Iterator <T> &Iterator<T>::operator = (Ref <T> &ref)
  104. {
  105.     m_ref = ref;
  106.     return *this;
  107. }
  108.  
  109. template <class T>
  110. Position Iterator<T>::get_position (void) const
  111. {
  112.     return m_position;
  113. }
  114.  
  115. template <class T>
  116. void Iterator<T>::set_position (const Position position)
  117. {
  118.     m_position = position;
  119. }
  120.  
  121. #endif
  122.