claw  1.9.0
iterator.hpp
Go to the documentation of this file.
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2011 Julien Jorge
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 
23  contact: julien.jorge@stuff-o-matic.com
24 */
30 #ifndef __CLAW_ITERATOR_HPP__
31 #define __CLAW_ITERATOR_HPP__
32 
33 #include <iterator>
34 
35 namespace claw
36 {
41  template <typename Category, typename Value, typename Iterator,
42  typename Function>
44  {}; // class wrapped_iterator_by_category
45 
50  template <typename Value, typename Iterator, typename Function>
51  class wrapped_iterator_by_category<std::forward_iterator_tag, Value,
52  Iterator, Function>
53  {
54  public:
55  typedef typename std::iterator_traits<Iterator>::difference_type
56  difference_type;
57  typedef Value value_type;
58  typedef value_type* pointer;
59  typedef value_type& reference;
60  typedef typename std::iterator_traits<Iterator>::iterator_category
61  iterator_category;
62 
63  typedef wrapped_iterator_by_category<std::forward_iterator_tag, Value,
64  Iterator, Function>
65  self_type;
66 
67  public:
69  {}
70  wrapped_iterator_by_category(const Iterator& it)
71  : m_it(it)
72  {}
73  wrapped_iterator_by_category(const Iterator& it, const Function& f)
74  : m_it(it)
75  , m_fun(f)
76  {}
77  template <typename C, typename V, typename I, typename F>
80  : m_it(that.get_iterator())
81  , m_fun(that.get_function())
82  {}
83 
84  const Iterator& get_iterator() const
85  {
86  return m_it;
87  }
88  const Function& get_function() const
89  {
90  return m_fun;
91  }
92 
93  self_type& operator++()
94  {
95  ++m_it;
96  return *this;
97  }
98 
99  self_type operator++(int)
100  {
101  self_type tmp(*this);
102  ++m_it;
103  return tmp;
104  }
105 
106  reference operator*() const
107  {
108  return m_fun(*m_it);
109  }
110  pointer operator->() const
111  {
112  return &m_fun(*m_it);
113  }
114 
115  bool operator==(const self_type& that) const
116  {
117  return m_it == that.m_it;
118  }
119  bool operator!=(const self_type& that) const
120  {
121  return m_it != that.m_it;
122  }
123  bool operator==(const Iterator& it) const
124  {
125  return m_it == it;
126  }
127  bool operator!=(const Iterator& it) const
128  {
129  return m_it != it;
130  }
131 
132  private:
134  Iterator m_it;
135 
137  Function m_fun;
138 
139  }; // class wrapped_iterator_by_category [forward_iterator_tag]
140 
146  template <typename Value, typename Iterator, typename Function>
147  class wrapped_iterator_by_category<std::bidirectional_iterator_tag, Value,
148  Iterator, Function>
149  {
150  public:
151  typedef typename std::iterator_traits<Iterator>::difference_type
152  difference_type;
153  typedef Value value_type;
154  typedef value_type* pointer;
155  typedef value_type& reference;
156  typedef typename std::iterator_traits<Iterator>::iterator_category
157  iterator_category;
158 
159  typedef wrapped_iterator_by_category<std::bidirectional_iterator_tag,
160  Value, Iterator, Function>
161  self_type;
162 
163  public:
165  {}
166  wrapped_iterator_by_category(const Iterator& it)
167  : m_it(it)
168  {}
169  wrapped_iterator_by_category(const Iterator& it, const Function& f)
170  : m_it(it)
171  , m_fun(f)
172  {}
173  template <typename C, typename V, typename I, typename F>
176  : m_it(that.get_iterator())
177  , m_fun(that.get_function())
178  {}
179 
180  const Iterator& get_iterator() const
181  {
182  return m_it;
183  }
184  const Function& get_function() const
185  {
186  return m_fun;
187  }
188 
189  self_type& operator++()
190  {
191  ++m_it;
192  return *this;
193  }
194 
195  self_type operator++(int)
196  {
197  self_type tmp(*this);
198  ++m_it;
199  return tmp;
200  }
201 
202  self_type& operator--()
203  {
204  --m_it;
205  return *this;
206  }
207 
208  self_type operator--(int)
209  {
210  self_type tmp(*this);
211  --m_it;
212  return tmp;
213  }
214 
215  reference operator*() const
216  {
217  return m_fun(*m_it);
218  }
219  pointer operator->() const
220  {
221  return &m_fun(*m_it);
222  }
223 
224  bool operator==(const self_type& that) const
225  {
226  return m_it == that.m_it;
227  }
228  bool operator!=(const self_type& that) const
229  {
230  return m_it != that.m_it;
231  }
232  bool operator==(const Iterator& it) const
233  {
234  return m_it == it;
235  }
236  bool operator!=(const Iterator& it) const
237  {
238  return m_it != it;
239  }
240 
241  private:
243  Iterator m_it;
244 
246  Function m_fun;
247 
248  }; // class wrapped_iterator_by_category [bidirectional_iterator_tag]
249 
254  template <typename Value, typename Iterator, typename Function>
255  class wrapped_iterator_by_category<std::random_access_iterator_tag, Value,
256  Iterator, Function>
257  {
258  public:
259  typedef typename std::iterator_traits<Iterator>::difference_type
260  difference_type;
261  typedef Value value_type;
262  typedef value_type* pointer;
263  typedef value_type& reference;
264  typedef typename std::iterator_traits<Iterator>::iterator_category
265  iterator_category;
266 
267  typedef wrapped_iterator_by_category<std::random_access_iterator_tag,
268  Value, Iterator, Function>
269  self_type;
270 
271  public:
273  {}
274  wrapped_iterator_by_category(const Iterator& it)
275  : m_it(it)
276  {}
277  wrapped_iterator_by_category(const Iterator& it, const Function& f)
278  : m_it(it)
279  , m_fun(f)
280  {}
281  template <typename V, typename I>
283  const wrapped_iterator_by_category<std::random_access_iterator_tag, V,
284  I, Function>& that)
285  : m_it(that.m_it)
286  , m_fun(that.m_fun)
287  {}
288  template <typename C, typename V, typename I, typename F>
291  : m_it(that.get_iterator())
292  , m_fun(that.get_function())
293  {}
294 
295  const Iterator& get_iterator() const
296  {
297  return m_it;
298  }
299  const Function& get_function() const
300  {
301  return m_fun;
302  }
303 
304  self_type& operator++()
305  {
306  ++m_it;
307  return *this;
308  }
309 
310  self_type operator++(int)
311  {
312  self_type tmp(*this);
313  ++m_it;
314  return tmp;
315  }
316 
317  self_type& operator--()
318  {
319  --m_it;
320  return *this;
321  }
322 
323  self_type operator--(int)
324  {
325  self_type tmp(*this);
326  --m_it;
327  return tmp;
328  }
329 
330  reference operator*() const
331  {
332  return m_fun(*m_it);
333  }
334  pointer operator->() const
335  {
336  return &m_fun(*m_it);
337  }
338 
339  bool operator==(const self_type& that) const
340  {
341  return m_it == that.m_it;
342  }
343  bool operator!=(const self_type& that) const
344  {
345  return m_it != that.m_it;
346  }
347  bool operator==(const Iterator& it) const
348  {
349  return m_it == it;
350  }
351  bool operator!=(const Iterator& it) const
352  {
353  return m_it != it;
354  }
355  bool operator<(const self_type& that) const
356  {
357  return m_it < that.m_it;
358  }
359  bool operator<=(const self_type& that) const
360  {
361  return m_it <= that.m_it;
362  }
363  bool operator>(const self_type& that) const
364  {
365  return m_it > that.m_it;
366  }
367  bool operator>=(const self_type& that) const
368  {
369  return m_it >= that.m_it;
370  }
371 
372  difference_type operator-(const self_type& that) const
373  {
374  return m_it - that.m_it;
375  }
376 
377  self_type& operator+=(int n)
378  {
379  m_it += n;
380  return *this;
381  }
382 
383  self_type operator+(int n) const
384  {
385  self_type result(*this);
386  result += n;
387  return result;
388  }
389 
390  self_type& operator-=(int n)
391  {
392  return *this += -n;
393  }
394 
395  self_type operator-(int n) const
396  {
397  self_type result(*this);
398  result -= n;
399  return result;
400  }
401 
402  reference operator[](int n)
403  {
404  return m_fun(m_it[n]);
405  }
406 
407  private:
409  Iterator m_it;
410 
412  Function m_fun;
413 
414  }; // class wrapped_iterator_by_category [random_access_iterator_tag]
415 
416  template <typename Value, typename Iterator, typename Function>
417  wrapped_iterator_by_category<std::random_access_iterator_tag, Value,
418  Iterator, Function>
419  operator+(int n,
420  const wrapped_iterator_by_category<std::random_access_iterator_tag,
421  Value, Iterator, Function>& it)
422  {
423  return it + n;
424  }
425 
426  template <typename Value, typename Iterator, typename Function>
427  wrapped_iterator_by_category<std::random_access_iterator_tag, Value,
428  Iterator, Function>
429  operator-(int n,
430  const wrapped_iterator_by_category<std::random_access_iterator_tag,
431  Value, Iterator, Function>& it)
432  {
433  return it - n;
434  }
435 
448  template <typename Value, typename Iterator, typename Function>
450  {
451  public:
454  typename std::iterator_traits<Iterator>::iterator_category, Value,
455  Iterator, Function>
457 
458  }; // class wrapped_iterator
459 }
460 
461 #endif // __CLAW_ITERATOR_HPP__
wrapped_iterator_by_category< typename std::iterator_traits< Iterator >::iterator_category, Value, Iterator, Function > iterator_type
This is the type of the iterator that you want.
Definition: iterator.hpp:456
Base class for wrapped iterators, specialized for forward iterators.
Definition: iterator.hpp:51
Base class for wrapped iterators, specialized for random iterators.
Definition: iterator.hpp:255
This class defines an iterator resulting of the appliance of a function to an effective iterator...
Definition: iterator.hpp:449
Base class for wrapped iterators.
Definition: iterator.hpp:43
This is the main namespace.
Definition: application.hpp:49
Base class for wrapped iterators, specialized for bidirectional iterators.
Definition: iterator.hpp:147