From 34e5a2cf569ba5f1e9c5b7e51cfdcd7588d4e3e9 Mon Sep 17 00:00:00 2001 From: "matthias@arch" Date: Sun, 11 Sep 2022 01:31:36 +0200 Subject: [PATCH] Added generic iterator --- src/container/iterator.hpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/container/iterator.hpp diff --git a/src/container/iterator.hpp b/src/container/iterator.hpp new file mode 100644 index 0000000..4f0ad3a --- /dev/null +++ b/src/container/iterator.hpp @@ -0,0 +1,31 @@ +#pragma once + +namespace gz { + +/** + * @brief A generic iterator that satisfies std::forward_iterator + */ +template +struct Iterator { + public: + using value_type = T; + Iterator() : ptr(nullptr) {}; + Iterator(T* ptr) : ptr(ptr) {}; + T& operator*() const { return *ptr; }; + Iterator& operator=(const Iterator& other) { + ptr = other.ptr; + return *this; + }; + Iterator& operator++() { ptr += sizeof(T); return *this; }; + Iterator operator++(int) { auto copy = *this; ptr += sizeof(T); return copy; }; + friend int operator-(Iterator lhs, Iterator rhs) { + return lhs.ptr - rhs.ptr; + }; + bool operator==(const Iterator& other) const { return ptr == other.ptr; }; + // bool operator!=(const Iterator& other) const { return ptr != other.ptr; }; + private: + T* ptr; +}; // Iterator + +/* static_assert(std::forward_iterator>, "Iterator not a forward range."); */ +} // namespace gz