Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
VirtualIterator.h
Go to the documentation of this file.
1 #ifndef __AUDIO_VIRTUALITERATOR_H__INCLUDED__
2 #define __AUDIO_VIRTUALITERATOR_H__INCLUDED__
3 
4 #include "Types.h"
5 #include <iterator>
6 
7 namespace Audio {
8 
17  template<class _T, class _Rt = _T&, class _Pt = _T*> class VirtualIterator : public std::iterator< std::bidirectional_iterator_tag , _T >
18  {
19  public:
20  typedef _T value_type;
21  typedef _Rt reference_type;
22  typedef _Pt pointer_type;
24 
26  virtual ~VirtualIterator() {}
27 
28  virtual reference_type operator*() = 0;
29  virtual pointer_type operator->() = 0;
30 
31  virtual iterator_type& operator++() = 0;
32  virtual iterator_type& operator--() = 0;
33 
34  virtual SharedPtr<iterator_type> clone() const = 0;
35 
37  virtual bool eos() const = 0;
38 
40  virtual bool sos() const = 0;
41 
42  /* Aliases
43  *
44  * since virtual iterators must be handled through pointers, the standard
45  * operator interface is highly inconvenient
46  */
47 
48  reference_type get() { return operator*(); }
49  pointer_type getPtr() { return operator->(); }
50 
51  iterator_type& next() { return operator++(); }
52  iterator_type& prev() { return operator--(); }
53  };
54 
55  template <typename _It> class VirtualStandardIterator :
56  public VirtualIterator<
57  typename std::iterator_traits<_It>::value_type,
58  typename std::iterator_traits<_It>::reference,
59  typename std::iterator_traits<_It>::pointer
60  >
61  {
62  _It begin;
63  _It end;
64  _It cur;
65 
66  public:
67  // They should be inherited, but somehow gcc 4.1.0 doesn't understand that >:9
68  typedef typename std::iterator_traits<_It>::value_type value_type;
69  typedef typename std::iterator_traits<_It>::reference reference_type;
70  typedef typename std::iterator_traits<_It>::pointer pointer_type;
72 
73  VirtualStandardIterator(const _It &_begin, const _It &_end) : begin(_begin), end(_end), cur(_begin) {}
74  VirtualStandardIterator(const VirtualStandardIterator<_It> &o) : begin(o.begin), end(o.end), cur(o.cur) {}
75 
76  virtual reference_type operator*() { return *cur; };
77  virtual pointer_type operator->() { return cur.operator->(); };
78 
79  virtual iterator_type& operator++() { ++cur; return *this; };
80  virtual iterator_type& operator--() { --cur; return *this; };
81 
82  virtual SharedPtr<iterator_type> clone() const
83  { return SharedPtr<iterator_type>(new VirtualStandardIterator<_It>(*this)); };
84 
85  virtual bool eos() const { return cur == end; }
86  virtual bool sos() const { return cur == begin; };
87  };
88 
89  template <typename _It, typename _T, typename _Rt=_T&, typename _Pt=_T*> class VirtualMappingIterator :
90  public VirtualIterator<_T,_Rt,_Pt>
91  {
92  protected:
94 
95  public:
96  // They should be inherited, but somehow gcc 4.1.0 doesn't understand that >:9
97  typedef _T value_type;
98  typedef _Rt reference_type;
99  typedef _Pt pointer_type;
101 
102  VirtualMappingIterator(const _It &_begin, const _It &_end) :
103  it(_begin,_end) {}
105  it( o.it ) {}
107  it( o ) {}
108 
109  virtual iterator_type& operator++() { ++it; return *this; };
110  virtual iterator_type& operator--() { --it; return *this; };
111 
112  virtual bool eos() const { return it.eos(); }
113  virtual bool sos() const { return it.sos(); };
114  };
115 
116  template <typename _It> class VirtualValuesIterator :
117  public VirtualMappingIterator<_It, typename _It::value_type::second_type>
118  {
119  public:
120  // They should be inherited, but somehow gcc 4.1.0 doesn't understand that >:9
121  typedef typename _It::value_type::second_type value_type;
125 
126  VirtualValuesIterator(const _It &_begin, const _It &_end) :
127  VirtualMappingIterator<_It, value_type>(_begin,_end) {}
129  VirtualMappingIterator<_It, value_type>( o.it ) {}
131  VirtualMappingIterator<_It, value_type>( o ) {}
132 
135 
136  virtual SharedPtr<iterator_type> clone() const
137  { return SharedPtr<iterator_type>(new VirtualValuesIterator(*this)); }
138  };
139 
140  template <typename _It> class VirtualKeysIterator :
141  public VirtualMappingIterator<_It, typename _It::value_type::first_type>
142  {
143  public:
144  // They should be inherited, but somehow gcc 4.1.0 doesn't understand that >:9
145  typedef typename _It::value_type::first_type value_type;
149 
150  VirtualKeysIterator(const _It &_begin, const _It &_end) :
151  VirtualMappingIterator<_It, value_type>(_begin,_end) {}
153  VirtualMappingIterator<_It, value_type>( o.it ) {}
155  VirtualMappingIterator<_It, value_type>( o ) {}
156 
159 
160  virtual SharedPtr<iterator_type> clone() const
161  { return SharedPtr<iterator_type>(new VirtualKeysIterator(*this)); }
162  };
163 
164  template <typename _It1, typename _It2=_It1> class ChainingIterator :
165  public VirtualIterator<
166  typename _It1::value_type,
167  typename _It1::reference_type,
168  typename _It1::pointer_type
169  >
170  {
171  _It1 it1;
172  _It2 it2;
173 
174  public:
175  // They should be inherited, but somehow gcc 4.1.0 doesn't understand that >:9
176  typedef typename _It1::value_type value_type;
177  typedef typename _It1::reference_type reference_type;
178  typedef typename _It1::pointer_type pointer_type;
180 
182  ChainingIterator(const ChainingIterator<_It1,_It2> &o) : it1(o.it1), it2(o.it2) {}
183  ChainingIterator(_It1 _it1, _It2 _it2) : it1(_it1), it2(_it2) {}
184  virtual ~ChainingIterator() {}
185 
187  { return (it1.eos() ? *it2 : *it1); }
188 
190  { return (it1.eos() ? it2.operator->() : it1.operator->()); }
191 
193  {
194  if (it1.eos())
195  ++it2; else
196  ++it1;
197  return *this;
198  }
199 
201  {
202  if (it1.eos() && !it2.sos())
203  --it2; else
204  --it1;
205  return *this;
206  }
207 
208  virtual SharedPtr<iterator_type> clone() const
209  { return SharedPtr<iterator_type>(new ChainingIterator<_It1,_It2>(it1, it2)); }
210 
211  virtual bool eos() const
212  { return (it1.eos() && it2.eos()); }
213 
214  virtual bool sos() const
215  { return (it1.sos() && it2.sos()); }
216  };
217 
218 }
219 
220 
221 #endif//__AUDIO_VIRTUALITERATOR_H__INCLUDED__