add iterators
New iterators can be used to iterate through Nodes. Squashed commit of the following: commit 602ed679631647dd1c8874b0b0145fcb09458341 Author: GRayHook <s@marinkevich.ru> Date: Fri Aug 1 19:36:46 2025 +0700 fup after CR commit08f7b59aa7Author: GRayHook <s@marinkevich.ru> Date: Fri Aug 1 19:20:48 2025 +0700 normalize members of iterator commit038cbb73f4Author: GRayHook <s@marinkevich.ru> Date: Fri Aug 1 18:57:08 2025 +0700 bump commit0f93988fb6Author: GRayHook <s@marinkevich.ru> Date: Fri Aug 1 11:32:35 2025 +0700 tmp
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
#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;
|
||||
};
|
||||
Reference in New Issue
Block a user