OGLplus  (0.59.0) a C++ wrapper for rendering APIs

any_iterator.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_ANY_ITERATOR_HPP
10 #define EAGINE_ANY_ITERATOR_HPP
11 
12 #include "assert.hpp"
13 #include "deep_copy_ptr.hpp"
14 #include "interface.hpp"
15 #include "selector.hpp"
16 #include <iterator>
17 
18 namespace eagine {
19 
23 template <typename VT, typename RT, typename PT, typename DT>
25 public:
27  using value_type = VT;
28 
30  using reference = RT;
31 
33  using pointer = PT;
34 
36  using difference_type = DT;
37 
39  using iterator_category = std::forward_iterator_tag;
40 
41  template <typename Iter>
43  : _pimpl{make_deep_copy_ptr<_impl<Iter>>(i)} {}
44 
46  friend bool
48  return a._pimpl->_equal(b._pimpl.get());
49  }
50 
52  friend bool
54  return !a._pimpl->_equal(b._pimpl.get());
55  }
56 
58  reference operator*() const {
59  return _pimpl->_deref();
60  }
61 
64  _pimpl->_advance();
65  return *this;
66  }
67 
70  any_forward_iterator i = *this;
71  _pimpl->_advance();
72  return i;
73  }
74 
75 private:
76  struct _intf : interface<_intf> {
77  virtual std::unique_ptr<_intf> copy() = 0;
78  virtual RT _deref() = 0;
79  virtual void _advance() = 0;
80  virtual bool _equal(_intf*) = 0;
81  };
82 
83  template <typename Iter>
84  struct _impl : _intf {
85  Iter _i;
86 
87  _impl(Iter i)
88  : _i(i) {}
89 
90  std::unique_ptr<_intf> copy() override {
91  return std::unique_ptr<_intf>(new _impl(*this));
92  }
93 
94  RT _deref() override {
95  return *_i;
96  }
97 
98  void _advance() override {
99  ++_i;
100  }
101 
102  bool _equal(_intf* intf) override {
103  auto* that = dynamic_cast<_impl*>(intf);
104  EAGINE_ASSERT(that != nullptr);
105  return this->_i == that->_i;
106  }
107  };
108 
109  deep_copy_ptr<_intf> _pimpl;
110 };
111 
114 template <typename T>
117 
118 template <typename T>
121 
122 // any_forward_iterator_range
123 template <typename VT, typename RT, typename PT, typename DT>
124 class any_forward_iterator_range {
125 private:
127 
128 public:
129  using iterator = any_forward_iterator<VT, RT, PT, DT>;
130 
131  any_forward_iterator_range(iterator b, iterator e)
132  : _bgn(std::move(b))
133  , _end(std::move(e)) {}
134 
135  template <typename Range>
136  any_forward_iterator_range(const Range& range)
137  : any_forward_iterator_range(range.begin(), range.end()) {}
138 
139  iterator begin() const {
140  return _bgn;
141  }
142 
143  iterator end() const {
144  return _end;
145  }
146 };
147 
148 template <typename T>
149 using any_std_forward_range =
150  any_forward_iterator_range<T, const T&, const T*, std::ptrdiff_t>;
151 
152 template <typename T>
153 using any_copying_forward_range =
154  any_forward_iterator_range<T, T, const T*, std::ptrdiff_t>;
155 
156 } // namespace eagine
157 
158 #endif // EAGINE_ANY_ITERATOR_HPP
reference operator*() const
Dereference operator.
Definition: any_iterator.hpp:58
VT value_type
Alias for value type.
Definition: any_iterator.hpp:27
friend bool operator!=(const any_forward_iterator &a, const any_forward_iterator &b)
Non-equality comparison.
Definition: any_iterator.hpp:53
Common code is placed in this namespace.
Definition: eagine.hpp:21
any_forward_iterator< T, const T &, const T *, std::ptrdiff_t > any_std_forward_iterator
Alias for type erased STL forward iterators.
Definition: any_iterator.hpp:116
std::forward_iterator_tag iterator_category
Iterator category.
Definition: any_iterator.hpp:39
Template used to construct tag-types used mostly in tag-dispatching.
Definition: selector.hpp:21
any_forward_iterator & operator++()
Pre-increment.
Definition: any_iterator.hpp:63
Base template for abstract interfaces, implements common functionality.
Definition: interface.hpp:18
const any_forward_iterator operator++(int)
Post-increment.
Definition: any_iterator.hpp:69
PT pointer
Alias for pointer type.
Definition: any_iterator.hpp:33
Type erasure for forward iterators.
Definition: any_iterator.hpp:24
friend bool operator==(const any_forward_iterator &a, const any_forward_iterator &b)
Equality comparison.
Definition: any_iterator.hpp:47
DT difference_type
Alias fro difference type.
Definition: any_iterator.hpp:36
RT reference
Alias for reference type.
Definition: any_iterator.hpp:30

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).