Skip to content

Commit

Permalink
feat: add TypeTraits, AnyFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Sep 2, 2024
1 parent f65e656 commit 562145b
Show file tree
Hide file tree
Showing 16 changed files with 279 additions and 135 deletions.
24 changes: 24 additions & 0 deletions src-test/common/AnyFnTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "ll/core/LeviLamina.h"

#include "ll/api/data/AnyFunction.h"

static bool run = [] {
using ll::data::AnyFunction;


auto fn = AnyFunction([](std::string const& par, int i, std::vector<float>&& fs) {
ll::getLogger().warn("{}, {}, {}", par, i, fs);
});

fn(std::string{"emmmmm"}, 10, std::vector<float>{0.2f, 0.2f, 0.2f});

auto vec = std::vector<float>{0.7f, 0.7f, 0.7f};

fn = AnyFunction([](std::string const& par, int i, std::vector<float>&& fs) {
ll::getLogger().info("{} | {} | {}", par, i, fs);
});

fn(std::string{"hmmmmm"}, 17, std::ref(vec));

return true;
}();
88 changes: 12 additions & 76 deletions src/ll/api/base/Concepts.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,28 @@

#include <string>
#include <tuple>
#include <type_traits>
#include <utility>

#include "ll/api/base/Macro.h"
#include "ll/api/base/StdInt.h"
#include "ll/api/base/TypeTraits.h"

namespace ll::concepts {

template <class T, class U>
struct is_in_types;

template <class T, template <class...> class U, class... Ts>
struct is_in_types<T, U<Ts...>> : std::bool_constant<(std::is_same_v<T, Ts> || ...)> {};

template <class T, class U>
inline constexpr bool is_in_types_v = is_in_types<T, U>::value;

template <class T, class U>
concept IsInTypes = is_in_types_v<T, U>;

template <class T, class... Ts>
inline constexpr bool is_one_of_v = (std::is_same_v<T, Ts> || ...);

template <class T, class... Ts>
struct is_one_of : std::bool_constant<is_one_of_v<T, Ts...>> {};
concept IsInTypes = traits::is_in_types_v<T, U>;

template <class T, class... Ts>
concept IsOneOf = is_one_of_v<T, Ts...>;
concept IsOneOf = traits::is_one_of_v<T, Ts...>;

template <class T, class... Ts>
inline constexpr bool is_all_same_v = (std::is_same_v<T, Ts> && ...);

template <class T, class... Ts>
struct is_all_same : std::bool_constant<is_all_same_v<T, Ts...>> {};

template <class T, class... Ts>
concept IsAllSame = is_all_same_v<T, Ts...>;

template <class T>
inline constexpr bool is_string_v = std::is_constructible_v<std::string, T>;

template <class T>
inline constexpr bool is_non_char_integral_v =
is_one_of_v<std::remove_cv_t<T>, bool, schar, uchar, short, ushort, int, uint, long, ulong, int64, uint64>;
concept IsAllSame = traits::is_all_same_v<T, Ts...>;

template <class T>
concept IsNonCharIntegral = is_non_char_integral_v<T>;
concept IsNonCharIntegral = traits::is_non_char_integral_v<T>;

template <class T>
concept IsString = is_string_v<T>;
concept IsString = traits::is_string_v<T>;

template <class T, template <class> class Z>
concept Require = Z<T>::value;
Expand Down Expand Up @@ -100,32 +72,12 @@ template <typename T>
concept IsVectorBase = std::is_base_of_v<VectorBaseTag, T>;

template <class T, template <class...> class Z>
inline constexpr bool is_specialization_of_v = false;

template <template <class...> class Z, class... Args>
inline constexpr bool is_specialization_of_v<Z<Args...>, Z> = true;

template <class T, template <class...> class Z>
struct is_specialization_of : std::bool_constant<is_specialization_of_v<T, Z>> {};

template <class T, template <class...> class Z>
concept Specializes = is_specialization_of_v<T, Z>;

template <class>
inline constexpr bool is_std_array_v = false;

template <class T, size_t N>
inline constexpr bool is_std_array_v<::std::array<T, N>> = true;

template <class>
inline constexpr bool is_subrange_v = false;

template <class I, class S, ::std::ranges::subrange_kind K>
constexpr bool is_subrange_v<::std::ranges::subrange<I, S, K>> = true;
concept Specializes = traits::is_specialization_of_v<T, Z>;

template <class T>
inline constexpr bool tuple_like_impl = is_specialization_of_v<T, ::std::tuple>
|| is_specialization_of_v<T, ::std::pair> || is_std_array_v<T> || is_subrange_v<T>;
inline constexpr bool tuple_like_impl =
traits::is_specialization_of_v<T, ::std::tuple> || traits::is_specialization_of_v<T, ::std::pair>
|| traits::is_std_array_v<T> || traits::is_subrange_v<T>;

template <class T>
concept TupleLike = tuple_like_impl<std::remove_cvref_t<T>> || (!Rangeable<T> && requires(T t) {
Expand All @@ -136,27 +88,11 @@ concept TupleLike = tuple_like_impl<std::remove_cvref_t<T>> || (!Rangeable<T> &&
template <class T>
concept ArrayLike = Rangeable<T> && !requires { typename std::remove_cvref_t<T>::mapped_type; };

template <template <class...> class T, class... Ts>
void DerivedFromSpecializationImpl(T<Ts...> const&);

template <class T, template <class...> class Z>
concept DerivedFromSpecializes = requires(T const& t) { DerivedFromSpecializationImpl<Z>(t); };

template <class T, template <class...> class Z>
inline constexpr bool is_derived_from_specialization_of_v = DerivedFromSpecializes<T, Z>;

template <class T, template <class...> class Z>
struct is_derived_from_specialization_of : std::bool_constant<is_derived_from_specialization_of_v<T, Z>> {};

template <class...>
inline constexpr bool always_false = false;

template <class T>
inline constexpr bool is_virtual_cloneable_v =
std::is_polymorphic_v<T> && requires(T const& t) { static_cast<T*>(t.clone().release()); };
concept DerivedFromSpecializes = traits::is_derived_from_specialization_of_v<T, Z>;

template <class T>
concept VirtualCloneable = is_virtual_cloneable_v<T>;
concept VirtualCloneable = traits::is_virtual_cloneable_v<T>;

template <class T>
concept Stringable = requires(T t) {
Expand Down
2 changes: 1 addition & 1 deletion src/ll/api/base/Containers.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ using Indirect = data::IndirectValue<T, data::defaultCopy<T>, D>;
template <
class T,
class C =
std::conditional_t<concepts::is_virtual_cloneable_v<T>, data::virtualCloneCopy<T>, data::polymorphicCopy<T>>,
std::conditional_t<traits::is_virtual_cloneable_v<T>, data::virtualCloneCopy<T>, data::polymorphicCopy<T>>,
class D = std::default_delete<T>>
using Polymorphic = data::IndirectValue<T, C, D>;

Expand Down
40 changes: 4 additions & 36 deletions src/ll/api/base/Meta.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#pragma once

#include <cstddef>
#include <type_traits>
#include <utility>

#include "ll/api/base/CompilerPredefine.h"
#include "ll/api/base/Concepts.h"
#include "ll/api/base/Macro.h"
#include "ll/api/base/StdInt.h"
#include "ll/api/base/TypeTraits.h"

namespace ll::meta {
namespace detail {
Expand All @@ -22,7 +22,7 @@ struct VisitStrategy;
template <size_t N>
struct VisitStrategy<N, -1> {
// Fallback strategy for visitations with too many total states for the following "switch" strategies.
template <typename T, typename... Ts>
template <class T, class... Ts>
static constexpr std::array<std::decay_t<T>, sizeof...(Ts) + 1> makeVisitorArray(T&& t, Ts&&... ts) {
return {
{std::forward<T>(t), std::forward<Ts>(ts)...}
Expand Down Expand Up @@ -198,38 +198,6 @@ constexpr Ret visitIndex(size_t index, Fn&& fn, Args&&... args) {
return Strategy::template impl<Ret>(index, std::forward<Fn>(fn), std::forward<Args>(args)...);
}

template <size_t N, class T, class... Types>
struct get_type {
using type = typename get_type<N - 1, Types...>::type;
};

template <class T, class... Types>
struct get_type<0, T, Types...> {
using type = T;
};

template <size_t N, class... Types>
using get_type_t = typename get_type<N, Types...>::type;

template <class T, class U, class... Args>
struct max_type {
using type = typename max_type<T, typename max_type<U, Args...>::type>::type;
};

template <class T, class U>
struct max_type<T, U> {
using type = typename std::conditional<(sizeof(T) < sizeof(U)), U, T>::type;
};

template <class T, class... Ts>
struct index_of;

template <class T, class... Ts>
struct index_of<T, T, Ts...> : std::integral_constant<size_t, 0> {};

template <class T, class U, class... Ts>
struct index_of<T, U, Ts...> : std::integral_constant<size_t, 1 + index_of<T, Ts...>::value> {};

template <class... TL>
class TypeList {
public:
Expand Down Expand Up @@ -257,7 +225,7 @@ class TypeList {
using push_front = TypeList<T, TL...>;

template <size_t N>
using get = get_type_t<N + 1, void, TL...>;
using get = traits::get_type_t<N + 1, void, TL...>;

template <template <class...> class U>
using to = U<TL...>;
Expand All @@ -273,7 +241,7 @@ class TypeList {
}

template <class T>
static constexpr size_t index = index_of<T, TL...>::value;
static constexpr size_t index = traits::index_of<T, TL...>::value;
};

template <class Group, class T, auto Id = int64{}>
Expand Down
2 changes: 1 addition & 1 deletion src/ll/api/base/ToString.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ std::string to_string(T const& t) {
} else if constexpr (requires { t.to_string(); }) {
return t.to_string();
} else {
static_assert(ll::concepts::always_false<T>, "T must be a stringable type");
static_assert(ll::traits::always_false<T>, "T must be a stringable type");
}
}

Expand Down
Loading

0 comments on commit 562145b

Please sign in to comment.