ladybird/AK/Concepts.h

129 lines
2.7 KiB
C
Raw Normal View History

/*
* Copyright (c) 2020, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
2021-11-07 13:40:10 +00:00
#include <AK/Forward.h>
#include <AK/IterationDecision.h>
#include <AK/StdLibExtras.h>
namespace AK::Concepts {
template<typename T>
concept Integral = IsIntegral<T>;
template<typename T>
concept FloatingPoint = IsFloatingPoint<T>;
2022-02-22 21:04:24 +00:00
template<typename T>
concept Fundamental = IsFundamental<T>;
template<typename T>
concept Arithmetic = IsArithmetic<T>;
2021-03-27 21:20:15 +00:00
template<typename T>
concept Signed = IsSigned<T>;
2021-03-27 21:20:15 +00:00
template<typename T>
concept Unsigned = IsUnsigned<T>;
2021-03-27 21:20:15 +00:00
template<typename T>
concept Enum = IsEnum<T>;
template<typename T, typename U>
concept SameAs = IsSame<T, U>;
template<typename U, typename... Ts>
concept OneOf = IsOneOf<U, Ts...>;
template<typename T, template<typename...> typename S>
concept SpecializationOf = IsSpecializationOf<T, S>;
2021-11-07 13:40:10 +00:00
template<typename T>
concept AnyString = Detail::IsConstructible<StringView, T>;
template<typename T, typename U>
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)
{
{
func(args...)
}
-> SameAs<void>;
};
template<typename Func, typename... Args>
concept IteratorFunction = requires(Func func, Args... args)
{
{
func(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
}
using AK::Concepts::Arithmetic;
using AK::Concepts::ArrayLike;
using AK::Concepts::Enum;
using AK::Concepts::FloatingPoint;
2022-02-22 21:04:24 +00:00
using AK::Concepts::Fundamental;
using AK::Concepts::Integral;
using AK::Concepts::IterableContainer;
using AK::Concepts::IteratorFunction;
using AK::Concepts::IteratorPairWith;
using AK::Concepts::OneOf;
2022-01-27 12:44:53 +00:00
using AK::Concepts::SameAs;
2021-03-27 21:20:15 +00:00
using AK::Concepts::Signed;
using AK::Concepts::SpecializationOf;
2021-03-27 21:20:15 +00:00
using AK::Concepts::Unsigned;
using AK::Concepts::VoidFunction;