MADARA  3.2.3
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  typedef void type;
10 };
11 
24 template<class T, class U = void, class V = void>
25 struct IteratorTraits;/* If error here: invalid type passed to deep_iterate() */
26 
31 template<class T, class V>
32 struct IteratorTraits<T,
33  typename TypeHelper<typename T::value_type>::type, V > {
34  enum { is_pair = 0 };
35 
36  typedef typename T::value_type value_type;
37 
38  static value_type get_deep_copy(const T& i) {
39  return value_type(i->deep_copy());
40  }
41 };
42 
47 template<class T>
48 struct IteratorTraits<T,
49  typename TypeHelper<typename T::value_type>::type,
50  typename TypeHelper<typename T::value_type::second_type>::type
51  > {
52  enum { is_pair = 1 };
53 
54  typedef std::pair<const typename T::value_type::first_type &,
55  typename T::value_type::second_type>
57 
58  static value_type get_deep_copy(const T& i) {
59  return value_type(i->first, i->second.deep_copy());
60  }
61 };
62 
70 template<class Iterator>
71 class DeepIterator :
72  public std::iterator<std::input_iterator_tag,
73  typename IteratorTraits<Iterator>::value_type>
74 {
75 private:
77 
78 public:
80  typedef typename Iterator::value_type underlying_value_type;
81 
86  value_type operator*() const
87  {
88  return traits::get_deep_copy(i_);
89  }
90 
98  underlying_value_type *operator->() const
99  {
100  return i_.operator->();
101  }
102 
110  {
111  ++i_;
112  return *this;
113  }
114 
126  {
127  DeepIterator<Iterator> ret(*this);
128  ++i_;
129  return *this;
130  }
131 
137  bool operator==(const DeepIterator &o) const
138  {
139  return i_ == o.i_;
140  }
141 
147  bool operator!=(const DeepIterator &o) const
148  {
149  return i_ != o.i_;
150  }
151 
152 private:
153  DeepIterator(const Iterator &i) : i_(i) {}
154 
155  Iterator i_;
156 
157  template<class I>
158  friend DeepIterator<I> deep_iterate(const I &i);
159 };
160 
178 template<class Iterator>
180 {
181  return DeepIterator<Iterator>(i);
182 }
183 
184 #endif
DeepIterator(const Iterator &i)
Definition: DeepIterator.h:153
bool operator==(const DeepIterator &o) const
Equality operator.
Definition: DeepIterator.h:137
DeepIterator::value_type value_type
Definition: DeepIterator.h:79
IteratorTraits< Iterator > traits
Definition: DeepIterator.h:76
std::pair< const typename T::value_type::first_type &, typename T::value_type::second_type > value_type
Definition: DeepIterator.h:56
underlying_value_type * operator->() const
Pass-through to the underlying iterator&#39;s operator->().
Definition: DeepIterator.h:98
DeepIterator operator++(int)
Post-fix increment.
Definition: DeepIterator.h:125
DeepIterator< Iterator > deep_iterate(const Iterator &i)
Returns an input iterator from an iterator.
Definition: DeepIterator.h:179
Iterator i_
Definition: DeepIterator.h:155
Specialize this template to support various kinds of iterators.
Definition: DeepIterator.h:25
Iterator::value_type underlying_value_type
Definition: DeepIterator.h:80
The iterator type returned by deep_iterate.
Definition: DeepIterator.h:71
DeepIterator & operator++()
Pre-fix increment.
Definition: DeepIterator.h:109
value_type operator*() const
Returns the same as the underlying iterator&#39;s operator*, except any values are deep copied first...
Definition: DeepIterator.h:86
Helper class for type inference.
Definition: DeepIterator.h:8
bool operator!=(const DeepIterator &o) const
Equality operator.
Definition: DeepIterator.h:147