ECSTASY
All in the name
Loading...
Searching...
No Matches
PolymorphicIterator.hpp
Go to the documentation of this file.
1
11
12#ifndef UTIL_SERIALIZATION_POLYMORPHICITERATOR_HPP_
13#define UTIL_SERIALIZATION_POLYMORPHICITERATOR_HPP_
14
15#include <memory>
16
17namespace util::serialization
18{
27 template <typename T>
29 public:
30 using value_type = T;
35
36 private:
43 struct Concept {
45 virtual ~Concept() noexcept = default;
46
53 virtual void nextInplace() = 0;
54
63 [[nodiscard]] virtual value_type get() const = 0;
64
77 [[nodiscard]] virtual bool equal(const Concept *other) const = 0;
78
87 [[nodiscard]] virtual std::unique_ptr<Concept> clone() const = 0;
88
97 [[nodiscard]] virtual const std::type_info &type() const noexcept = 0;
98
107 [[nodiscard]] virtual const Concept *address() const noexcept = 0;
108 };
109
118 template <class Iter>
119 class Model : public Concept {
120 public:
129 Model(Iter iter) : _iter(iter)
130 {
131 }
132
134 void nextInplace() override final
135 {
136 ++_iter;
137 }
138
140 [[nodiscard]] value_type get() const override final
141 {
142 return *_iter;
143 }
144
146 [[nodiscard]] bool equal(const Concept *other) const override final
147 {
148 return _iter == dynamic_cast<const Model *>(other)->_iter;
149 }
150
152 [[nodiscard]] std::unique_ptr<Concept> clone() const override final
153 {
154 return std::make_unique<Model>(*this);
155 }
156
158 [[nodiscard]] const std::type_info &type() const noexcept override final
159 {
160 return typeid(_iter);
161 }
162
164 [[nodiscard]] const Concept *address() const noexcept override final
165 {
166 return this;
167 }
168
169 private:
171 Iter _iter;
172 };
173
174 public:
185 template <class Iter>
186 PolymorphicIterator(Iter iter) : _impl(std::make_unique<Model<Iter>>(iter))
187 {
188 }
189
199 {
200 }
201
210 [[nodiscard]] value_type operator*() const
211 {
212 return _impl->get();
213 }
214
224 {
225 _impl->nextInplace();
226 return *this;
227 }
228
240 {
241 PolymorphicIterator res = *this;
242
243 return ++res;
244 }
245
256 [[nodiscard]] bool operator==(const PolymorphicIterator &r) const noexcept
257 {
258 return _impl->type() == r._impl->type() && _impl->equal(r._impl->address());
259 }
260
271 [[nodiscard]] bool operator!=(const PolymorphicIterator &r) const noexcept
272 {
273 return !(*this == r);
274 }
275
276 private:
279 };
280} // namespace util::serialization
281
282#endif /* !UTIL_SERIALIZATION_POLYMORPHICITERATOR_HPP_ */
const std::type_info & type() const noexcept override final
Get the current iterator type info.
const Concept * address() const noexcept override final
Get this address.
bool equal(const Concept *other) const override final
Compare two operators.
void nextInplace() override final
Increment the iterator by one.
std::unique_ptr< Concept > clone() const override final
Clone this.
value_type get() const override final
Retrieve the iterator value.
Polymorphism iterator for value T.
PolymorphicIterator(Iter iter)
Construct a new Polymorphic Iterator.
PolymorphicIterator & operator++()
Increment the internal iteraor inplace.
PolymorphicIterator operator++(int)
Copy this and increment it.
PolymorphicIterator(const PolymorphicIterator &r)
Construct a copy of an existing Polymorphic Iterator.
bool operator==(const PolymorphicIterator &r) const noexcept
Equality operator.
value_type operator*() const
Fetch the iterator value.
bool operator!=(const PolymorphicIterator &r) const noexcept
Inequality operator.
virtual ~Concept() noexcept=default
Default destructor.
virtual value_type get() const =0
Retrieve the iterator value.
virtual const Concept * address() const noexcept=0
Get this address.
virtual const std::type_info & type() const noexcept=0
Get the current iterator type info.
virtual std::unique_ptr< Concept > clone() const =0
Clone this.
virtual void nextInplace()=0
Increment the iterator by one.
virtual bool equal(const Concept *other) const =0
Compare two operators.