You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.4 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#pragma once
#include <tuple>
#include "ifaces/IIterator.h"
/// \brief Базовый итератор.
/// \tparam T Тип итерируемого элемента.
/// Предоставляет базовую реализацию методов для работы с итерируемыми объектами.
/// Возвращает объект-прокси, мимикрирующий под итерируемый элемент. Позволяет использовать
/// распаковку структуры для получения уровня элемента в поддереве.
template <typename T>
class BaseIterator : public IIterator<T> {
public:
explicit BaseIterator(T* ptr) : proxy{ptr, 0} {}
struct Proxy {
T* e;
size_t level;
T& operator*() const { return *e; }
T* operator->() const { return e; }
};
Proxy& operator*() { return proxy; }
T* operator->() const override { return current(); }
operator const T*() const { return current(); }
operator T&() const { return *current(); }
operator T*() const { return current(); }
bool operator!=(const IIterator<T>& other) const override {
const T* other_current = other;
return current() != other_current;
}
protected:
T* current() const { return proxy.e; }
void set_current(T* e) { proxy.e = e; }
size_t level() const { return proxy.level; }
void set_level(size_t level) { proxy.level = level; }
private:
Proxy proxy;
};