DiaDes  0.1
DIAgnosis of Discrete-Event System
Iterator.hh
Go to the documentation of this file.
1 
9 #ifndef __DIADES__UTILS__ITERATORS__HH
10 #define __DIADES__UTILS__ITERATORS__HH
11 
14 
15 #include "Pointer.hh"
16 
17 
18 namespace Diades
19 {
20  namespace Utils
21  {
22 
42  template<typename Mapper>
44  {
45  public:
46  using MapStructure = typename Mapper::MapStructure;
47  using Iterator = typename Mapper::Iterator;
48  using Data = typename Mapper::Data;
49 
50  private:
53  public:
54  typedef const Data & reference;
55  using iterator_category = std::forward_iterator_tag;
56 
57  explicit
58  RangeMapperIterator(const MapStructure & m, Iterator it) : _mapStructure(&m), _it(it)
59  {
60  }
61 
62  RangeMapperIterator(const RangeMapperIterator & it) : _mapStructure(it._mapStructure), _it(it._it)
63  {
64  }
65 
67  {
68  if(this != &it)
69  {
70  _mapStructure = it._mapStructure;
71  _it = it._it;
72  }
73  return *this;
74  }
75 
77  {
78  ++_it;
79  return *this;
80  }
81 
83  {
84  auto tmp = *this;
85  ++ * this;
86  return tmp;
87  }
88 
89  reference operator*() const
90  {
91  return Mapper()(*_mapStructure, _it);
92  }
93 
94  bool operator==(const RangeMapperIterator & it) const
95  {
96  return(_mapStructure == it._mapStructure) && (_it == it._it);
97  }
98 
99  bool operator!=(const RangeMapperIterator & it) const
100  {
101  return !(*this == it);
102  }
103  };
104 
105 
106 
112  template<typename T, template <typename ...> class Container>
113  class SharedPointerIterator : public Container<std::shared_ptr<T>>::iterator
114  {
115  public:
116  typedef T& reference;
117  typedef typename Ptr<T>::P pointer;
118 
119  static string typeName()
120  {
121  return "Automata::SharedPointerIterator";
122  }
124 
125  public:
126  using Iterator = typename Container<std::shared_ptr < T>>::iterator;
127 
132  {
133  };
134 
139  {
140  };
141 
146  pointer operator->()
147  {
148  always_require(Exception, Iterator::operator*(),
149  "operator->(): attempt to access the content of a null shared pointer.");
150  return(Iterator::operator*()).get();
151  }
152 
157  reference operator*()
158  {
159  always_require(Exception, Iterator::operator*(),
160  "operator*(): attempt to access the content of a null shared pointer.");
161  return *(Iterator::operator*());
162  }
163  };
164 
165 
166 
172  template<typename T, template <typename ...> class Container>
173  class SharedPointerConstIterator : public Container<std::shared_ptr<T>>::const_iterator
174  {
175  public:
176  typedef const T& reference;
177  typedef typename Ptr<T>::ConstP pointer;
178 
179  static string typeName()
180  {
181  return "Automata::SharedPointerIterator";
182  }
184 
185  public:
186  using Iterator = typename Container<std::shared_ptr < T>>::const_iterator;
187 
192  {
193  };
194 
199  {
200  };
201 
206  pointer operator->() const
207  {
208  always_require(Exception, Iterator::operator*(),
209  "operator->(): attempt to access the content of a null shared pointer.");
210  return(Iterator::operator*()).get();
211  }
212 
217  reference operator*() const
218  {
219  always_require(Exception, Iterator::operator*(),
220  "operator*(): attempt to access the content of a null shared pointer.");
221  return *(Iterator::operator*());
222  }
223  };
224 
230  template<typename T, template <typename ...> class Container>
231  class RawPointerIterator : public Container<typename Ptr<T>::P>::iterator
232  {
233  public:
234  typedef T& reference;
235  typedef typename Ptr<T>::P pointer;
236 
237  static string
239  {
240  return "Automata::SharedPointerIterator";
241  }
243 
244  public:
245  using Iterator = typename Container<pointer>::iterator;
246 
251  {
252  };
253 
258  {
259  };
260 
265  pointer operator->()
266  {
267  always_require(Exception, Iterator::operator*(),
268  "operator->(): attempt to access the content of a null shared pointer.");
269  return(Iterator::operator*());
270  }
271 
276  reference operator*()
277  {
278  always_require(Exception, Iterator::operator*(),
279  "operator*(): attempt to access the content of a null shared pointer.");
280  return *(Iterator::operator*());
281  }
282  };
283 
289  template<typename T, template <typename ...> class Container>
290  class ConstRawPointerConstIterator : public Container<typename Ptr<T>::ConstP>::const_iterator
291  {
292  public:
293  typedef const T& reference;
294  typedef typename Ptr<T>::ConstP pointer;
295 
296  static string
298  {
299  return "Diades::Utils::ConstRawPointerConstIterator";
300  }
302 
303  public:
304  using Iterator = typename Container<pointer>::const_iterator;
305 
310  {
311  };
312 
317  {
318  };
319 
324  pointer operator->()
325  {
326  always_require(Exception, Iterator::operator*(),
327  "operator->(): attempt to access the content of a null shared pointer.");
328  return(Iterator::operator*());
329  }
330 
335  reference operator*()
336  {
337  always_require(Exception, Iterator::operator*(),
338  "operator*(): attempt to access the content of a null shared pointer.");
339  return *(Iterator::operator*());
340  }
341  };
342 
343 
344 
345 
346  }
347 }
348 
349 
350 #endif /* __DIADES__UTILS__ITERATORS__HH */
351 
#define always_require(Exception, expr, message)
Definition: Assertion.hh:122
bool operator==(const RangeMapperIterator &it) const
Definition: Iterator.hh:94
const T * ConstP
Definition: Pointer.hh:31
bool operator!=(const RangeMapperIterator &it) const
Definition: Iterator.hh:99
RangeMapperIterator & operator=(const RangeMapperIterator &it)
Definition: Iterator.hh:66
RangeMapperIterator & operator++()
Definition: Iterator.hh:76
std::forward_iterator_tag iterator_category
Definition: Iterator.hh:55
RangeMapperIterator(const RangeMapperIterator &it)
Definition: Iterator.hh:62
Namespace of the Diades project.
ConstIterator on the Net.
typename Mapper::Data Data
Definition: Iterator.hh:48
RangeMapperIterator(const MapStructure &m, Iterator it)
Definition: Iterator.hh:58
RangeMapperIterator operator++(int)
Definition: Iterator.hh:82
typename Mapper::MapStructure MapStructure
Definition: Iterator.hh:46
const MapStructure * _mapStructure
Definition: Iterator.hh:51