From 3d995bba5fb54fe0ebc43d981ab060bab6aa1963 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 10 Jul 2024 20:15:00 +0200 Subject: [PATCH] util/DereferenceIterator: add class DereferenceContainerAdapter --- src/util/DereferenceIterator.hxx | 50 ++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/util/DereferenceIterator.hxx b/src/util/DereferenceIterator.hxx index 709df6ac4..4d21cd998 100644 --- a/src/util/DereferenceIterator.hxx +++ b/src/util/DereferenceIterator.hxx @@ -89,3 +89,53 @@ public: return original == other.original; } }; + +/** + * A container wrapper that wraps the iterators in a + * DereferenceIterator. + */ +template::value_type>> +class DereferenceContainerAdapter { + CT original; + + /* these aliases allow the underlying container to return a + different type for begin() and end() */ + using const_end_iterator = DereferenceIterator().cend()), const VT>; + using end_iterator = DereferenceIterator().end()), VT>; + +public: + using value_type = VT; + using pointer = VT *; + using reference = VT &; + + using const_iterator = DereferenceIterator().cbegin()), const VT>; + using iterator = DereferenceIterator().begin()), VT>; + + explicit constexpr DereferenceContainerAdapter(CT &&_original) noexcept + :original(std::move(_original)) {} + + constexpr iterator begin() noexcept { + return original.begin(); + } + + constexpr const_iterator begin() const noexcept { + return original.cbegin(); + } + + constexpr const_iterator cbegin() const noexcept { + return original.cbegin(); + } + + constexpr end_iterator end() noexcept { + return original.end(); + } + + constexpr const_end_iterator end() const noexcept { + return original.cend(); + } + + constexpr const_end_iterator cend() const noexcept { + return original.cend(); + } +};