From 786036820b46a92b454e987c09dc279368f5dc02 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Boric Date: Tue, 20 Jul 2021 19:41:38 +0200 Subject: [PATCH] AK: Introduce IntrusiveListRelaxedConst This container is the same as IntrusiveList, except that it allows modifications to the elements even if the reference to the IntrusiveList itself is const, by returning mutable iterators. This represents a use-case where we want to allow modifications to the elements while keeping the list itself immutable. This behavior is explicitely opt-in by using IntrusiveListRelaxedConst instead of IntrusiveList. It will be useful later on when we model shared/exclusive locks with the help of const and mutable references. --- AK/IntrusiveListRelaxedConst.h | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 AK/IntrusiveListRelaxedConst.h diff --git a/AK/IntrusiveListRelaxedConst.h b/AK/IntrusiveListRelaxedConst.h new file mode 100644 index 00000000000..a305c08a8cb --- /dev/null +++ b/AK/IntrusiveListRelaxedConst.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2021, the SerenityOS developers. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include + +namespace AK { + +template T::*member> +class IntrusiveListRelaxedConst : public IntrusiveList { + AK_MAKE_NONCOPYABLE(IntrusiveListRelaxedConst); + AK_MAKE_NONMOVABLE(IntrusiveListRelaxedConst); + +public: + using IntrusiveList::IntrusiveList; + + using Iterator = IntrusiveList::Iterator; + + Iterator begin() const { return const_cast(this)->IntrusiveList::begin(); } + Iterator end() const { return Iterator {}; } +}; + +} + +using AK::IntrusiveListRelaxedConst;