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
This commit is contained in:
Ali Mohammad Pur 2021-07-22 19:07:17 +04:30 committed by Andreas Kling
parent 2891dca0cd
commit f879e04133

View file

@ -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;