Iterator.hpp
1 #ifndef DM_ITERATOR_HPP_INCLUDED
2 #define DM_ITERATOR_HPP_INCLUDED
3 
4 #ifdef _MSC_VER
5  #pragma once
6 #endif
7 
8 #include "DM/config.hpp"
9 #include "DM/Handle.hpp"
10 #include "DM/ObjectBase.hpp"
11 
12 DM_NAMESPACE_BEGIN
13 
15  eORDER_INTERNAL, ///< Traverse elements in internal geometrical order (usually faster)
16  eORDER_CHACHED, ///< Traverse chached elements first (if elements are already in memory this is the fastest method)
17  eORDER_SORTED ///< Traverse elements in sorted order, i.e. insert sequence (usually slower)
18 };
19 
20 template< class T>
21 class DM_API Iterator
22 {
23  typedef Iterator self;
24 
25 protected:
26  struct DM_API imp : public ObjectBase {
27  typedef T value_type;
28  typedef T* pointer;
29  typedef T& reference;
30  typedef Handle<T> handle;
31 
32  imp() : RefCount(1) {}
33  virtual ~imp() {};
34 
35  virtual imp& operator++() = 0;
36  virtual imp& operator--() = 0;
37 
38  virtual reference operator*() const = 0;
39  virtual pointer operator->() const = 0;
40  virtual handle operator()() const = 0;
41 
42  virtual bool operator==(const imp &ref) const=0;
43  virtual bool operator!=(const imp &ref) const { return !operator==(ref); };
44 
45  virtual imp* clone() const=0;
46 
47  int RefCount;
48  };
49 public:
50  //typedef ConstIterator_imp<T> iterator_imp;
51  typedef imp iterator_imp;
52 
53  typedef T* pointer;
54  typedef T& reference;
55  typedef Handle<T> handle;
56 
57  typedef const T* const_pointer;
58  typedef const T& const_reference;
59  typedef const Handle<T> const_handle;
60  typedef T value_type;
61 
62  Iterator() : _Handle() {}
63  Iterator(iterator_imp *h) : _Handle(h) {}
64  Iterator(const self& ref) : _Handle(ref._Handle) { if (_Handle) (_Handle->RefCount)++; }
65  virtual ~Iterator() {
66  if (_Handle && _Handle->RefCount == 1)
67  delete _Handle;
68  };
69 
70  self& operator++() { _Handle->operator++(); return *this; };
71  self& operator--() { _Handle->operator--(); return *this; };
72 
73  reference operator*() const { return _Handle->operator*(); };
74  pointer operator->() const { return _Handle->operator->(); };
75  handle operator()() const { return _Handle->operator()(); };
76 
77  self &operator=(const self &ref) { self tmp = ref; swap(tmp); return *this; }
78 
79  bool operator==(const self &ref) const { return ( _Handle == ref._Handle || *_Handle == *(ref._Handle) ); };
80  bool operator!=(const self &ref) const { return ( _Handle != ref._Handle && *_Handle != *(ref._Handle) ); };
81 
82  operator bool() const { return (_Handle!=0); }
83  void reset() { swap( this->ConstIterator() ); }
84 
85  void swap(self &it) {
86  std::swap(_Handle, it._Handle);
87  }
88 
89  self clone() const { return (_Handle) ? self( (iterator_imp *)_Handle->clone() ) : self(); }
90 
91 protected:
92  iterator_imp* _Handle; ///< Smart pointer to the iterator object
93 
94  template< class TT >
95  friend class ConstIterator;
96  friend struct IteratorAccess;
97 };
98 
99 template< class T>
100 class DM_API ConstIterator
101 {
102  typedef ConstIterator self;
103 
104 protected:
105  struct DM_API imp : public ObjectBase {
106  typedef T value_type;
107  typedef const T* const_pointer;
108  typedef const T& const_reference;
109  typedef const Handle<T> const_handle;
110 
111  imp() : RefCount(1) {}
112  virtual ~imp() {};
113 
114  virtual imp& operator++() = 0;
115  virtual imp& operator--() = 0;
116 
117  virtual const_reference operator*() const = 0;
118  virtual const_pointer operator->() const = 0;
119  virtual const_handle operator()() const = 0;
120 
121  virtual bool operator==(const imp &ref) const=0;
122  virtual bool operator!=(const imp &ref) const { return !operator==(ref); };
123 
124  virtual imp* clone() const=0;
125 
126  int RefCount;
127  };
128  typedef Iterator<T> iterator;
129 public:
130  //typedef ConstIterator_imp<T> iterator_imp;
131  typedef imp iterator_imp;
132 
133  typedef T* pointer;
134  typedef T& reference;
135  typedef const T* const_pointer;
136  typedef const T& const_reference;
137  typedef const Handle<T> const_handle;
138  typedef T value_type;
139 
140  ConstIterator() : _Handle() {}
141  ConstIterator(iterator_imp *h) : _Handle(h) {}
142  ConstIterator(const self& ref) : _Handle(ref._Handle) { if (_Handle) (_Handle->RefCount)++; }
143  //non-const to const conversion
144  ConstIterator(const Iterator<T>& ref) : _Handle(convert(ref._Handle)) { if (_Handle) (_Handle->RefCount)++; }
145  ConstIterator(typename Iterator<T>::iterator_imp *h) : _Handle(convert(h)) { }
146 
147  virtual ~ConstIterator() {
148  if (_Handle)
149  {
150  if (--(_Handle->RefCount) == 0)
151  delete _Handle;
152  }
153  };
154 
155  self& operator++() { _Handle->operator++(); return *this; };
156  self& operator--() { _Handle->operator--(); return *this; };
157 
158  const_reference operator*() const { return _Handle->operator*(); };
159  const_pointer operator->() const { return _Handle->operator->(); };
160  const_handle operator()() const { return _Handle->operator()(); };
161 
162  self &operator=(const self &ref) { self tmp = ref; swap(tmp); return *this; }
163 
164  bool operator==(const self &ref) const { return ( _Handle == ref._Handle || *_Handle == *(ref._Handle) ); };
165  bool operator!=(const self &ref) const { return ( _Handle != ref._Handle && *_Handle != *(ref._Handle) ); };
166 
167  operator bool() const { return (_Handle!=0); }
168  void reset() { swap( ConstIterator() ); }
169 
170  void swap(self &it) {
171  std::swap(_Handle, it._Handle);
172  }
173 
174  self clone() const { return (_Handle) ? self( _Handle->clone( ) ) : self(); }
175 
176 protected:
177  iterator_imp* _Handle; ///< Smart pointer to the iterator object
178 
179  static iterator_imp * convert(typename Iterator<T>::iterator_imp *h) {
180  return (iterator_imp *)((void *)(h));
181  }
182 
183  friend struct IteratorAccess;
184 };
185 
186 template<class T>
187 class DM_API InsertIterator : public ObjectBase {
188  typedef InsertIterator self;
189 
190 public:
191  typedef T* pointer;
192  typedef T& reference;
193  typedef const T* const_pointer;
194  typedef const T& const_reference;
195  typedef T value_type;
196 
197  typedef Handle<T> handle_type;
198  typedef const handle_type& const_handle_reference;
199 
200  InsertIterator() {}
201  virtual ~InsertIterator() {};
202 
203  virtual self& operator=(const_reference value) = 0;
204  virtual self& operator=(const_handle_reference handle) = 0;
205 
206  virtual self& operator*() { return *this; }
207  virtual self& operator++() { return *this; }
208 
209  virtual void finished() {}
210 
211  virtual void Delete() {
212  // this Delete function is only needed if InsertIterator object are used in conjunction with the DM::Handle class (which is generally not the case)
213  // if this exception occurs overload this function in the corresponding child class
214  throw NG_exception("Overload function in the corresponding child class");
215  }
216 };
217 
218 template<class T>
219 class DM_API InsertIteratorProxy : public InsertIterator<T>
220 {
221  typedef InsertIteratorProxy self;
222  typedef InsertIterator<T> base;
223 
224 public:
225  typedef typename base::pointer pointer;
226  typedef typename base::reference reference;
227  typedef typename base::const_pointer const_pointer;
228  typedef typename base::const_reference const_reference;
229  typedef typename base::value_type value_type;
230 
231  typedef typename base::handle_type handle_type;
233 
235  virtual ~InsertIteratorProxy() {}
236 
237  virtual self& operator=(const_reference value) {
238  operator=(handle_type((pointer)(value.clone())));
239  }
240  virtual self& operator=(const_handle_reference handle) {
241  operator=(*handle);
242  }
243 };
244 
245 DM_NAMESPACE_END
246 
247 #endif //DM_ITERATOR_HPP_INCLUDED