Skip to content

Commit

Permalink
fixed issues pointed by clang and added related tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Klaim committed Apr 7, 2024
1 parent 2efac9c commit e3e53bb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
28 changes: 14 additions & 14 deletions include/sparrow/mp_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ namespace sparrow::mpl
//// Type-predicates /////////////////////////////

/// Matches template types which can be used as type-wrappers for evaluation in type-predicates.
template< template< class > class W, class T >
template< template< class... > class W, class T >
concept type_wrapper = std::same_as<W<T>, typelist<T>>
or std::same_as<W<T>, std::type_identity_t<T>>
;
Expand All @@ -79,14 +79,14 @@ namespace sparrow::mpl
/// `std::type_identity_t<T>`. Basically a value wrapper for representing a type.
/// This is useful to detect type predicates which allow being called like normal functions.
template< class P, class T >
concept callable_type_predicate = std::semiregular<P>
concept callable_type_predicate = std::semiregular<std::decay_t<P>>
and (
requires(P predicate)
requires(std::decay_t<P> predicate)
{
{ predicate(typelist<T>{}) } -> std::convertible_to<bool>;
}
or
requires(P predicate)
requires(std::decay_t<P> predicate)
{
{ predicate(std::type_identity_t<T>{}) } -> std::convertible_to<bool>;
}
Expand All @@ -109,7 +109,7 @@ namespace sparrow::mpl
consteval
bool evaluate(P predicate)
{
if constexpr (requires (P p){ { p(typelist<T>{}) } -> std::same_as<bool>; })
if constexpr (requires (std::decay_t<P> p){ { p(typelist<T>{}) } -> std::same_as<bool>; })
{
return predicate(typelist<T>{});
}
Expand All @@ -131,11 +131,11 @@ namespace sparrow::mpl
template<class T>
struct same_as
{
template<class X>
consteval
bool operator()(mpl::typelist<X> list) const
template<template<class...> class W, class X>
requires type_wrapper<W, X>
consteval bool operator()(W<X>) const
{
return std::same_as< mpl::typelist<T>, decltype(list) >;
return std::same_as< T, X >;
}

};
Expand All @@ -144,9 +144,9 @@ namespace sparrow::mpl
template< template<class> class P >
struct ct_type_predicate_to_callable
{
template<template<class> class W, class T>
template<template<class...> class W, class T>
requires ct_type_predicate<P, T> and type_wrapper<W,T>
consteval bool operator()(W<T>)
consteval bool operator()(W<T>) const
{
return P<T>::value;
}
Expand All @@ -169,7 +169,7 @@ namespace sparrow::mpl
/// @returns 'true' if for at least one type T in the type list L,, `Predicate{}(typelist<T>) == true`.
/// `false` otherwise or if the list is empty.
template< class Predicate, template<class...> class L, class... T>
requires any_typelist<L<T...>>
requires any_typelist<L<T...>> and (callable_type_predicate<Predicate, T> && ...)
consteval
bool any_of(L<T...> list, Predicate predicate = {})
{
Expand All @@ -191,7 +191,7 @@ namespace sparrow::mpl
/// @returns `true` if for every type T in the type list L, `Predicate{}(typelist<T>) == true`
/// or if the list is empty; `false` otherwise.
template< class Predicate, template<class...> class L, class... T>
requires any_typelist<L<T...>>
requires any_typelist<L<T...>> and (callable_type_predicate<Predicate, T> && ...)
consteval
bool all_of(L<T...> list, Predicate predicate)
{
Expand Down Expand Up @@ -222,7 +222,7 @@ namespace sparrow::mpl
/// @returns The index position in the first type in the provided type list `L` that matches the provided predicate,
/// or the size of the list if the matching type was not found.
template< class Predicate, template<class...> class L, class... T>
requires any_typelist<L<T...>>
requires any_typelist<L<T...>> and (callable_type_predicate<Predicate, T> && ...)
consteval
std::size_t find_if(L<T...> list, Predicate predicate)
{
Expand Down
25 changes: 17 additions & 8 deletions test/test_mpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,32 @@

namespace sparrow
{
static_assert(mpl::ct_type_predicate<std::is_integral, float>);
static_assert(mpl::callable_type_predicate< mpl::predicate::same_as<int>, float >);

static constexpr auto some_types = mpl::typelist<int, float>{};
static constexpr auto same_as_int = mpl::predicate::same_as<int>{};
static_assert(same_as_int(mpl::typelist<int>{}));
static_assert( mpl::any_of(some_types, same_as_int) == true);
static_assert(mpl::all_of(some_types, same_as_int) == false);


static_assert(mpl::ct_type_predicate<std::is_integral, float>);
static_assert(mpl::callable_type_predicate< mpl::predicate::same_as<int>, float >);

using test_list = mpl::typelist< int, char >;
struct not_a_list { };
static_assert(mpl::any_typelist<test_list>);
static_assert(not mpl::any_typelist<not_a_list>);
static_assert(mpl::size(test_list{}) == 2);

static_assert(mpl::type_wrapper<std::type_identity_t, int>);
static_assert(mpl::type_wrapper<mpl::typelist, int>);

static constexpr mpl::ct_type_predicate_to_callable<std::is_integral> wtf;
static_assert(wtf(mpl::typelist<int>{}));

static_assert(mpl::callable_type_predicate< mpl::ct_type_predicate_to_callable<std::is_integral>, int >);
static constexpr auto is_integral = mpl::as_predicate<std::is_integral>();
static_assert(mpl::callable_type_predicate<decltype(is_integral), int>);

static_assert(mpl::any_of(test_list{}, mpl::predicate::same_as<int>{}));
static_assert(mpl::any_of(test_list{}, mpl::predicate::same_as<char>{}));
Expand All @@ -45,12 +60,6 @@ namespace sparrow
static_assert(mpl::all_of<std::is_integral>(test_list{}));
static_assert(not mpl::all_of<std::is_floating_point>(test_list{}));

static constexpr auto some_types = mpl::typelist<int, float>{};
static constexpr auto same_as_int = mpl::predicate::same_as<int>{};
static_assert( mpl::any_of(some_types, same_as_int) == true);
static_assert(mpl::all_of(some_types, same_as_int) == false);


static_assert(mpl::find_if(test_list{}, mpl::predicate::same_as<int>{}) == 0);
static_assert(mpl::find_if(test_list{}, mpl::predicate::same_as<char>{}) == 1);
static_assert(mpl::find_if(test_list{}, mpl::predicate::same_as<float>{}) == size(test_list{}));
Expand Down

0 comments on commit e3e53bb

Please sign in to comment.