From a9fd9c92e2dbf1beb1bf6a27ab5a9532ef7a9764 Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Thu, 10 Jan 2019 11:10:19 +0200 Subject: [PATCH] MXS-2219 Add iterator adapter Iterator adapter for making it possible to iterate over an intrusive singly linked list in an STL fashion. --- maxutils/maxbase/include/maxbase/iterator.hh | 74 ++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 maxutils/maxbase/include/maxbase/iterator.hh diff --git a/maxutils/maxbase/include/maxbase/iterator.hh b/maxutils/maxbase/include/maxbase/iterator.hh new file mode 100644 index 000000000..6c7bdc468 --- /dev/null +++ b/maxutils/maxbase/include/maxbase/iterator.hh @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2019 MariaDB Corporation Ab + * + * Use of this software is governed by the Business Source License included + * in the LICENSE.TXT file and at www.mariadb.com/bsl11. + * + * Change Date: 2022-01-01 + * + * On the date above, in accordance with the Business Source License, use + * of this software will be governed by version 2 or later of the General + * Public License. + */ +#pragma once + +#include +#include + +namespace maxbase +{ + +/** + * This template can be used for providing an STL compatible iterator + * for an intrusive singly linked list, that is, a list where the + * elements themselves contain a @c next pointer. + */ +template +class intrusive_slist_iterator : public std::iterator +{ +public: + explicit intrusive_slist_iterator(T& t) + : m_pT(&t) + { + } + + explicit intrusive_slist_iterator() + : m_pT(nullptr) + { + } + + intrusive_slist_iterator& operator++() + { + mxb_assert(m_pT); + m_pT = m_pT->next; + return *this; + } + + intrusive_slist_iterator& operator++(int) + { + intrusive_slist_iterator prev(*this); + ++(*this); + return prev; + } + + bool operator == (const intrusive_slist_iterator& rhs) const + { + return m_pT == rhs.m_pT; + } + + bool operator != (const intrusive_slist_iterator& rhs) const + { + return !(m_pT == rhs.m_pT); + } + + T& operator * () const + { + mxb_assert(m_pT); + return *m_pT; + } + +private: + T* m_pT; +}; + +}