MADARA  3.4.1
DeepIterator.h
Go to the documentation of this file.
1 #ifndef MADARA_DEEPITERATOR_H
2 #define MADARA_DEEPITERATOR_H
3 
7 template<class T>
8 struct TypeHelper
9 {
10  typedef void type;
11 };
12 
25 template<class T, class U = void, class V = void>
26 struct IteratorTraits; /* If error here: invalid type passed to deep_iterate()
27  */
28 
33 template<class T, class V>
34 struct IteratorTraits<T, typename TypeHelper<typename T::value_type>::type, V>
35 {
36  enum
37  {
38  is_pair = 0
39  };
40 
41  typedef typename T::value_type value_type;
42 
43  static value_type get_deep_copy(const T& i)
44  {
45  return value_type(i->deep_copy());
46  }
47 };
48 
53 template<class T>
54 struct IteratorTraits<T, typename TypeHelper<typename T::value_type>::type,
55  typename TypeHelper<typename T::value_type::second_type>::type>
56 {
57  enum
58  {
59  is_pair = 1
60  };
61 
62  typedef std::pair<const typename T::value_type::first_type&,
63  typename T::value_type::second_type>
65 
66  static value_type get_deep_copy(const T& i)
67  {
68  return value_type(i->first, i->second.deep_copy());
69  }
70 };
71 
79 template<class Iterator>
80 class DeepIterator : public std::iterator<std::input_iterator_tag,
81  typename IteratorTraits<Iterator>::value_type>
82 {
83 private:
85 
86 public:
88  typedef typename Iterator::value_type underlying_value_type;
89 
95  {
96  return traits::get_deep_copy(i_);
97  }
98 
107  {
108  return i_.operator->();
109  }
110 
118  {
119  ++i_;
120  return *this;
121  }
122 
134  {
135  DeepIterator<Iterator> ret(*this);
136  ++i_;
137  return *this;
138  }
139 
145  bool operator==(const DeepIterator& o) const
146  {
147  return i_ == o.i_;
148  }
149 
155  bool operator!=(const DeepIterator& o) const
156  {
157  return i_ != o.i_;
158  }
159 
160 private:
161  DeepIterator(const Iterator& i) : i_(i) {}
162 
163  Iterator i_;
164 
165  template<class I>
166  friend DeepIterator<I> deep_iterate(const I& i);
167 };
168 
186 template<class Iterator>
188 {
189  return DeepIterator<Iterator>(i);
190 }
191 
192 #endif
DeepIterator< Iterator > deep_iterate(const Iterator &i)
Returns an input iterator from an iterator.
Definition: DeepIterator.h:187
The iterator type returned by deep_iterate.
Definition: DeepIterator.h:82
underlying_value_type * operator->() const
Pass-through to the underlying iterator's operator->().
Definition: DeepIterator.h:106
value_type operator*() const
Returns the same as the underlying iterator's operator*, except any values are deep copied first.
Definition: DeepIterator.h:94
Iterator::value_type underlying_value_type
Definition: DeepIterator.h:88
Iterator i_
Definition: DeepIterator.h:163
DeepIterator::value_type value_type
Definition: DeepIterator.h:87
friend DeepIterator< I > deep_iterate(const I &i)
DeepIterator operator++(int)
Post-fix increment.
Definition: DeepIterator.h:133
bool operator!=(const DeepIterator &o) const
Equality operator.
Definition: DeepIterator.h:155
bool operator==(const DeepIterator &o) const
Equality operator.
Definition: DeepIterator.h:145
IteratorTraits< Iterator > traits
Definition: DeepIterator.h:84
DeepIterator(const Iterator &i)
Definition: DeepIterator.h:161
DeepIterator & operator++()
Pre-fix increment.
Definition: DeepIterator.h:117
std::pair< const typename T::value_type::first_type &, typename T::value_type::second_type > value_type
Definition: DeepIterator.h:64
Specialize this template to support various kinds of iterators.
Definition: DeepIterator.h:26
Helper class for type inference.
Definition: DeepIterator.h:9