Scopira  20080306
iterator.h
1 
2 /*
3  * Copyright (c) 2002 National Research Council
4  *
5  * All rights reserved.
6  *
7  * This material is confidential and proprietary information of
8  * National Research Council Canada ("Confidential Information").
9  * This Confidential Information may only be used and reproduced
10  * in accordance with the terms of the license agreement.
11  *
12  */
13 
14 #ifndef __INCLUDED__SCOPIRA_TOOL_ITERATOR_H__
15 #define __INCLUDED__SCOPIRA_TOOL_ITERATOR_H__
16 
17 #include <assert.h>
18 
19 #include <scopira/tool/object.h>
20 
21 namespace scopira
22 {
23  namespace tool
24  {
25  // general interface
26  template <class T> class iterator_g;
27  // empty iterator
28  template <class T> class null_iterator_imp_g;
29 
30  // imp
31  template <class T> class iterator_imp_g;
32  }
33 }
34 
40 template <class T> class scopira::tool::iterator_imp_g : public virtual scopira::tool::object
41 {
42  public:
43  typedef T data_type;
44 
46  virtual T current(void) = 0;
48  virtual bool valid(void) const = 0;
50  virtual void next(void) = 0;
51 };
52 
58 template <class T> class scopira::tool::null_iterator_imp_g : public scopira::tool::iterator_imp_g<T>
59 {
60  public:
62  virtual T current(void) { return 0; } //hopefully, a valid-compile time cast (should never be executed at runtime)
64  virtual bool valid(void) const { return false; }
66  virtual void next(void) { }
67 };
68 
78 template <class T> class scopira::tool::iterator_g
79 {
80  public:
81  typedef T data_type;
82  typedef null_iterator_imp_g<T> null_type;
83 
85  iterator_g(void)
86  { }
89  : m_imp(imp) { }
91  iterator_g(const iterator_g &src)
92  : m_imp(src.m_imp) { }
95 
97  T operator *(void) {
98  assert(m_imp.get());
99  return m_imp->current();
100  }
102  bool valid(void) const {
103  if (m_imp.get())
104  return m_imp->valid();
105  else
106  return 0;
107  }
109  bool operator ==(const iterator_g &rhs) const {
110  assert(!rhs.m_imp.get());
111  return !valid();
112  }
114  bool operator ==(int x) const {
115  assert(x == 0);
116  return !valid();
117  }
119  bool operator !=(const iterator_g &rhs) const {
120  assert(!rhs.m_imp.get());
121  return valid();
122  }
124  bool operator !=(int x) const {
125  assert(x == 0);
126  return valid();
127  }
128  iterator_g& operator ++(void) {
129  assert(m_imp.get());
130  assert(m_imp->valid());
131  m_imp->next();
132  return *this;
133  }
134  iterator_g& operator =(const iterator_g &rhs) {
135  m_imp = rhs.m_imp;
136  return *this;
137  }
138 
139  private:
141 };
142 
143 #endif
144 
Definition: archiveflow.h:20
virtual bool valid(void) const =0
is there a next one?
Definition: object.h:71
iterator_g(const iterator_g &src)
copy ctor
Definition: iterator.h:91
Definition: iterator.h:26
Definition: object.h:42
virtual T current(void)
gets the current item
Definition: iterator.h:62
bool valid(void) const
better than comparing to a specific EOF iterator
Definition: iterator.h:102
virtual T current(void)=0
gets the current item
Definition: iterator.h:28
iterator_g(iterator_imp_g< T > *imp)
initing ctor
Definition: iterator.h:88
virtual void next(void)
advance to the next item
Definition: iterator.h:66
virtual bool valid(void) const
is there a next one?
Definition: iterator.h:64
iterator_g(void)
default ctor - can be used as a 0-element iterator or end-iterator comparator
Definition: iterator.h:85
~iterator_g()
dtor
Definition: iterator.h:94
virtual void next(void)=0
advance to the next item
Definition: iterator.h:31