From 449f206e8fa33f4235997651748b6b2b56619a88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klaim=20=28Jo=C3=ABl=20Lamotte=29?= <142265+Klaim@users.noreply.github.com> Date: Tue, 2 Apr 2024 18:15:03 +0200 Subject: [PATCH] type predicate support (wip) --- include/sparrow/mp_utils.hpp | 59 ++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git a/include/sparrow/mp_utils.hpp b/include/sparrow/mp_utils.hpp index e5b63dc7..a10d257b 100644 --- a/include/sparrow/mp_utils.hpp +++ b/include/sparrow/mp_utils.hpp @@ -56,6 +56,59 @@ namespace sparrow::mpl template using constify_t = typename constify::type; + + + + template< class P > + concept ct_type_predicate = requires + { + { P::value } -> std::same_as; + }; + + template< class P, class T > + concept callable_type_predicate = std::semiregular

+ and ( + requires(P predicate) + { + { predicate(typelist{}) } -> std::same_as; + } + or + requires(P predicate) + { + { predicate(std::type_identity_t{}) } -> std::same_as; + } + ) + ; + + template< class P, class T > + concept type_predicate = ct_type_predicate

or callable_type_predicate; + + + template class P> + requires ct_type_predicate> + consteval + bool evaluate(P) + { + return P::value; + } + + template P> + consteval + bool evaluate(P predicate) + { + if constexpr (requires (P p){ { p(typelist{}) } -> std::same_as; }) + { + return predicate(typelist{}); + } + else + { + return predicate(std::type_identity_t{}); + } + } + + + + /// Checks that at least one type in the provided list of is making the provide predicate return `true`. /// @returns 'true' if for at least one type T in the type list L,, `Predicate{}(typelist) == true`. /// `false` otherwise or if the list is empty. @@ -64,7 +117,7 @@ namespace sparrow::mpl consteval bool any_of(L list, Predicate predicate) { - return (predicate(typelist{}) || ... || false); + return (evaluate(predicate) || ... || false); } /// Checks that every type in the provided list of is making the provide predicate return `true`. @@ -75,7 +128,7 @@ namespace sparrow::mpl consteval bool all_of(L list, Predicate predicate) { - return (predicate(typelist{}) && ... && true); + return (evaluate(predicate) && ... && true); } /// Compile-time type predicate: `true` if the evaluated type is the same as `T`. @@ -126,7 +179,7 @@ namespace sparrow::mpl } }; - (check(predicate(typelist{})) || ...); + (check(evaluate(predicate)) || ...); return idx; }