diff options
author | Ali Mohammad Pur <ali.mpfard@gmail.com> | 2021-07-22 19:07:17 +0430 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-07-22 22:56:20 +0200 |
commit | f879e041339ee414ddb9514a0883baa66ce34f6d (patch) | |
tree | ceee2a1750ed10ea3a664516c4af8246bbef4e98 /AK/Concepts.h | |
parent | 2891dca0cd3a6cf960458bc4df55a599d1846425 (diff) | |
download | serenity-f879e041339ee414ddb9514a0883baa66ce34f6d.zip |
AK: Add a concept for iterable containers
This concept describes a type with a begin()/end() pair that can
function as an iterator, given the following criteria:
- The return type of begin() is comparable with the return type of
end(), and the comparison (with operator!=) yields a bool
- The object returned from begin() can be pre-incremented
- The iterator has an operator*() implementation
Diffstat (limited to 'AK/Concepts.h')
-rw-r--r-- | AK/Concepts.h | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/AK/Concepts.h b/AK/Concepts.h index ced4b29142..697a8f1ec0 100644 --- a/AK/Concepts.h +++ b/AK/Concepts.h @@ -51,6 +51,21 @@ concept IteratorFunction = requires(Func func, Args... args) } -> SameAs<IterationDecision>; }; + +template<typename T, typename EndT> +concept IteratorPairWith = requires(T it, EndT end) +{ + *it; + { it != end } -> SameAs<bool>; + ++it; +}; + +template<typename T> +concept IterableContainer = requires +{ + { declval<T>().begin() } -> IteratorPairWith<decltype(declval<T>().end())>; +}; + // clang-format on } @@ -58,7 +73,9 @@ using AK::Concepts::Arithmetic; using AK::Concepts::Enum; using AK::Concepts::FloatingPoint; using AK::Concepts::Integral; +using AK::Concepts::IterableContainer; using AK::Concepts::IteratorFunction; +using AK::Concepts::IteratorPairWith; using AK::Concepts::Signed; using AK::Concepts::Unsigned; using AK::Concepts::VoidFunction; |