AK: Introduce the ArrayLike concept

The ArrayLike type concept focuses on array-like data accesses. This
means the ability to randomly index, obtain size information, as well as
being able to expose the storage pointer. The last two already have the
standard APIs `size` and `data`, respectively.

The ArrayLike concept should always be fulfilled by Vector, FixedArray
and Array as the three main array-like types. C-style arrays themselves
of course can't fulfil ArrayLike (which could be considered ironic), but
as we don't use them much anyways this isn't a problem.
This commit is contained in:
kleines Filmröllchen 2022-01-27 12:53:52 +01:00 committed by Andreas Kling
parent 6003b6f4d3
commit 07977ad94c

View file

@ -41,6 +41,32 @@ concept HashCompatible = IsHashCompatible<Detail::Decay<T>, Detail::Decay<U>>;
// FIXME: remove once Clang formats these properly.
// clang-format off
// Any indexable, sized, contiguous data structure.
template<typename ArrayT, typename ContainedT, typename SizeT = size_t>
concept ArrayLike = requires(ArrayT array, SizeT index)
{
{
array[index]
}
-> SameAs<RemoveReference<ContainedT>&>;
{
array.size()
}
-> SameAs<SizeT>;
{
array.span()
}
-> SameAs<Span<RemoveReference<ContainedT>>>;
{
array.data()
}
-> SameAs<RemoveReference<ContainedT>*>;
};
template<typename Func, typename... Args>
concept VoidFunction = requires(Func func, Args... args)
{
@ -77,6 +103,7 @@ concept IterableContainer = requires
}
using AK::Concepts::Arithmetic;
using AK::Concepts::ArrayLike;
using AK::Concepts::Enum;
using AK::Concepts::FloatingPoint;
using AK::Concepts::Integral;