libluna: Add support for range-based iteration to LinkedList

This commit is contained in:
apio 2023-04-28 15:12:21 +02:00
parent 77dcfab5ef
commit f3ffdd32d4
Signed by: apio
GPG Key ID: B8A7D06E42258954

View File

@ -210,6 +210,47 @@ template <typename T> class LinkedList
return m_count; return m_count;
} }
struct LinkedListIterator
{
typedef T* PtrT;
private:
LinkedListIterator(PtrT ptr, LinkedList<T>& list) : m_ptr(ptr), m_list(list)
{
}
PtrT m_ptr;
LinkedList<T>& 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<T>;
};
LinkedListIterator begin()
{
return { (T*)m_start_node, *this };
}
LinkedListIterator end()
{
return { nullptr, *this };
}
private: private:
Node* m_start_node = nullptr; Node* m_start_node = nullptr;
Node* m_end_node = nullptr; Node* m_end_node = nullptr;