summaryrefslogtreecommitdiff
path: root/AK/IntrusiveList.h
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-03-11 14:12:33 +0100
committerAndreas Kling <kling@serenityos.org>2021-03-11 14:21:49 +0100
commit1b2ea12062354657e195620ad947520406e98c0c (patch)
tree8163915371a301f6694e8c51c7ac887e8568c6d8 /AK/IntrusiveList.h
parentb7b7a48c66b56850b3ee8b5493f9964ef96859d2 (diff)
downloadserenity-1b2ea12062354657e195620ad947520406e98c0c.zip
AK: Add basic const iteration to IntrusiveList
Diffstat (limited to 'AK/IntrusiveList.h')
-rw-r--r--AK/IntrusiveList.h88
1 files changed, 42 insertions, 46 deletions
diff --git a/AK/IntrusiveList.h b/AK/IntrusiveList.h
index bb27ce70e2..132d45ca51 100644
--- a/AK/IntrusiveList.h
+++ b/AK/IntrusiveList.h
@@ -60,14 +60,21 @@ public:
class Iterator {
public:
- Iterator();
- Iterator(T* value);
-
- T& operator*() const;
- T* operator->() const;
- bool operator==(const Iterator& other) const;
+ Iterator() = default;
+ Iterator(T* value)
+ : m_value(value)
+ {
+ }
+
+ T& operator*() const { return *m_value; }
+ T* operator->() const { return m_value; }
+ bool operator==(const Iterator& other) const { return other.m_value == m_value; }
bool operator!=(const Iterator& other) const { return !(*this == other); }
- Iterator& operator++();
+ Iterator& operator++()
+ {
+ m_value = IntrusiveList<T, member>::next(m_value);
+ return *this;
+ }
Iterator& erase();
private:
@@ -75,7 +82,32 @@ public:
};
Iterator begin();
- Iterator end();
+ Iterator end() { return Iterator {}; }
+
+ class ConstIterator {
+ public:
+ ConstIterator() = default;
+ ConstIterator(const T* value)
+ : m_value(value)
+ {
+ }
+
+ const T& operator*() const { return *m_value; }
+ const T* operator->() const { return m_value; }
+ bool operator==(const ConstIterator& other) const { return other.m_value == m_value; }
+ bool operator!=(const ConstIterator& other) const { return !(*this == other); }
+ ConstIterator& operator++()
+ {
+ m_value = IntrusiveList<T, member>::next(const_cast<T*>(m_value));
+ return *this;
+ }
+
+ private:
+ const T* m_value { nullptr };
+ };
+
+ ConstIterator begin() const;
+ ConstIterator end() const { return ConstIterator {}; }
private:
static T* next(T* current);
@@ -98,42 +130,6 @@ private:
};
template<class T, IntrusiveListNode T::*member>
-inline IntrusiveList<T, member>::Iterator::Iterator()
-{
-}
-
-template<class T, IntrusiveListNode T::*member>
-inline IntrusiveList<T, member>::Iterator::Iterator(T* value)
- : m_value(value)
-{
-}
-
-template<class T, IntrusiveListNode T::*member>
-inline T& IntrusiveList<T, member>::Iterator::operator*() const
-{
- return *m_value;
-}
-
-template<class T, IntrusiveListNode T::*member>
-inline T* IntrusiveList<T, member>::Iterator::operator->() const
-{
- return m_value;
-}
-
-template<class T, IntrusiveListNode T::*member>
-inline bool IntrusiveList<T, member>::Iterator::operator==(const Iterator& other) const
-{
- return other.m_value == m_value;
-}
-
-template<class T, IntrusiveListNode T::*member>
-inline typename IntrusiveList<T, member>::Iterator& IntrusiveList<T, member>::Iterator::operator++()
-{
- m_value = IntrusiveList<T, member>::next(m_value);
- return *this;
-}
-
-template<class T, IntrusiveListNode T::*member>
inline typename IntrusiveList<T, member>::Iterator& IntrusiveList<T, member>::Iterator::erase()
{
T* old = m_value;
@@ -265,9 +261,9 @@ inline typename IntrusiveList<T, member>::Iterator IntrusiveList<T, member>::beg
}
template<class T, IntrusiveListNode T::*member>
-inline typename IntrusiveList<T, member>::Iterator IntrusiveList<T, member>::end()
+inline typename IntrusiveList<T, member>::ConstIterator IntrusiveList<T, member>::begin() const
{
- return Iterator();
+ return m_storage.m_first ? ConstIterator(node_to_value(*m_storage.m_first)) : ConstIterator();
}
template<class T, IntrusiveListNode T::*member>