From f3ffdd32d4aa79646ba40cf2cc80c62be9e3d702 Mon Sep 17 00:00:00 2001 From: apio Date: Fri, 28 Apr 2023 15:12:21 +0200 Subject: [PATCH] libluna: Add support for range-based iteration to LinkedList --- libluna/include/luna/LinkedList.h | 41 +++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/libluna/include/luna/LinkedList.h b/libluna/include/luna/LinkedList.h index f1e6df08..2a2ce167 100644 --- a/libluna/include/luna/LinkedList.h +++ b/libluna/include/luna/LinkedList.h @@ -210,6 +210,47 @@ template class LinkedList return m_count; } + struct LinkedListIterator + { + typedef T* PtrT; + + private: + LinkedListIterator(PtrT ptr, LinkedList& list) : m_ptr(ptr), m_list(list) + { + } + + PtrT m_ptr; + LinkedList& m_list; + + public: + PtrT& operator*() + { + return m_ptr; + } + + void operator++() + { + m_ptr = m_list.next(m_ptr).value_or(nullptr); + } + + bool operator!=(LinkedListIterator& other) + { + return m_ptr != other.m_ptr || &m_list != &other.m_list; + } + + friend class LinkedList; + }; + + LinkedListIterator begin() + { + return { (T*)m_start_node, *this }; + } + + LinkedListIterator end() + { + return { nullptr, *this }; + } + private: Node* m_start_node = nullptr; Node* m_end_node = nullptr;