1#ifndef ENTT_META_UTILITY_HPP
2#define ENTT_META_UTILITY_HPP
8#include "../core/type_traits.hpp"
9#include "../locator/locator.hpp"
23template<
typename Ret,
typename Args,
bool Static,
bool Const>
37template<
typename,
typename>
47template<
typename Type,
typename Ret,
typename Class,
typename...
Args>
51 std::conditional_t<std::is_base_of_v<Class, Type>, type_list<Args...>, type_list<const Class &, Args...>>,
52 !std::is_base_of_v<Class, Type>,
62template<
typename Type,
typename Ret,
typename Class,
typename...
Args>
66 std::conditional_t<std::is_base_of_v<Class, Type>, type_list<Args...>, type_list<Class &, Args...>>,
67 !std::is_base_of_v<Class, Type>,
76template<
typename Type,
typename Ret,
typename Class>
80 std::conditional_t<std::is_base_of_v<Class, Type>, type_list<>, type_list<Class &>>,
81 !std::is_base_of_v<Class, Type>,
96 std::is_same_v<std::remove_cv_t<std::remove_reference_t<MaybeType>>, Type> || std::is_base_of_v<std::remove_cv_t<std::remove_reference_t<MaybeType>>, Type>,
99 !(std::is_same_v<std::remove_cv_t<std::remove_reference_t<MaybeType>>, Type> || std::is_base_of_v<std::remove_cv_t<std::remove_reference_t<MaybeType>>, Type>),
100 std::is_const_v<std::remove_reference_t<MaybeType>> && (std::is_same_v<std::remove_cv_t<std::remove_reference_t<MaybeType>>, Type> || std::is_base_of_v<std::remove_cv_t<std::remove_reference_t<MaybeType>>, Type>)> {};
107template<
typename Type,
typename Ret>
124template<
typename Type,
typename Cand
idate>
126 template<
typename Ret,
typename...
Args,
typename Class>
129 template<
typename Ret,
typename...
Args,
typename Class>
132 template<
typename Ret,
typename Class,
typename = std::enable_if_t<std::is_member_
object_po
inter_v<Ret Class::*>>>
135 template<
typename Ret,
typename...
Args>
138 template<
typename Class>
143 using type =
decltype(get_rid_of_noexcept(std::declval<Candidate>()));
151template<
typename Type,
typename Cand
idate>
167template<
typename Policy = as_is_t,
typename Type>
169 if constexpr(std::is_same_v<Policy, as_void_t>) {
170 return meta_any{ctx, std::in_place_type<void>};
171 }
else if constexpr(std::is_same_v<Policy, as_ref_t>) {
172 return meta_any{ctx, std::in_place_type<Type>, value};
173 }
else if constexpr(std::is_same_v<Policy, as_cref_t>) {
174 static_assert(std::is_lvalue_reference_v<Type>,
"Invalid type");
175 return meta_any{ctx, std::in_place_type<const std::remove_reference_t<Type> &>, std::as_const(value)};
177 return meta_any{ctx, std::forward<Type>(value)};
188template<
typename Policy = as_is_t,
typename Type>
200template<
typename Type>
201[[
nodiscard]]
static meta_type meta_arg(
const meta_ctx &ctx,
const std::size_t index)
noexcept {
202 auto &&context = internal::meta_context::from(ctx);
203 return {ctx, internal::meta_arg_node(context, Type{}, index)};
212template<
typename Type>
213[[
nodiscard]]
static meta_type meta_arg(
const std::size_t index)
noexcept {
225template<
typename Type, auto Data>
227 if constexpr(!std::is_same_v<
decltype(
Data), Type> && !std::is_same_v<
decltype(
Data), std::nullptr_t>) {
228 if constexpr(std::is_member_function_pointer_v<
decltype(
Data)> || std::is_function_v<std::remove_reference_t<std::remove_pointer_t<
decltype(
Data)>>>) {
232 if(
auto *
const clazz = instance->try_cast<Type>(); clazz && value.allow_cast<
data_type>()) {
236 }
else if constexpr(std::is_member_object_pointer_v<
decltype(
Data)>) {
239 if constexpr(!std::is_array_v<data_type> && !std::is_const_v<data_type>) {
240 if(
auto *
const clazz = instance->try_cast<Type>(); clazz && value.allow_cast<
data_type>()) {
246 using data_type = std::remove_reference_t<
decltype(*Data)>;
248 if constexpr(!std::is_array_v<data_type> && !std::is_const_v<data_type>) {
274template<
typename Type, auto Data,
typename Policy = as_is_t>
276 if constexpr(std::is_member_pointer_v<
decltype(
Data)> || std::is_function_v<std::remove_reference_t<std::remove_pointer_t<
decltype(
Data)>>>) {
277 if constexpr(!std::is_array_v<std::remove_cv_t<std::remove_reference_t<std::invoke_result_t<
decltype(
Data), Type &>>>>) {
278 if constexpr(std::is_invocable_v<
decltype(
Data), Type &>) {
279 if(
auto *clazz = instance->try_cast<Type>(); clazz) {
284 if constexpr(std::is_invocable_v<
decltype(
Data),
const Type &>) {
292 }
else if constexpr(std::is_pointer_v<
decltype(
Data)>) {
293 if constexpr(std::is_array_v<std::remove_pointer_t<
decltype(
Data)>>) {
311template<
typename Type, auto Data,
typename Policy = as_is_t>
319template<
typename Policy,
typename Candidate,
typename... Args>
320[[nodiscard]] meta_any meta_invoke_with_args(
const meta_ctx &ctx, Candidate &&candidate, Args &&...args) {
321 if constexpr(std::is_void_v<decltype(std::invoke(std::forward<Candidate>(candidate), args...))>) {
322 std::invoke(std::forward<Candidate>(candidate), args...);
323 return meta_any{ctx, std::in_place_type<void>};
325 return meta_dispatch<Policy>(ctx, std::invoke(std::forward<Candidate>(candidate), args...));
329template<
typename Type,
typename Policy,
typename Candidate, std::size_t... Index>
330[[nodiscard]] meta_any
meta_invoke(
const meta_ctx &ctx, [[maybe_unused]] meta_handle instance, Candidate &&candidate, [[maybe_unused]] meta_any *
const args, std::index_sequence<Index...>) {
331 using descriptor = meta_function_helper_t<Type, std::remove_reference_t<Candidate>>;
334 if constexpr(std::is_invocable_v<std::remove_reference_t<Candidate>,
const Type &, type_list_element_t<Index, typename descriptor::args_type>...>) {
335 if(
const auto *
const clazz = instance->try_cast<
const Type>(); clazz && ((args + Index)->allow_cast<type_list_element_t<Index, typename descriptor::args_type>>() && ...)) {
336 return meta_invoke_with_args<Policy>(ctx, std::forward<Candidate>(candidate), *clazz, (args + Index)->cast<type_list_element_t<Index, typename descriptor::args_type>>()...);
338 }
else if constexpr(std::is_invocable_v<std::remove_reference_t<Candidate>, Type &, type_list_element_t<Index, typename descriptor::args_type>...>) {
339 if(
auto *
const clazz = instance->try_cast<Type>(); clazz && ((args + Index)->allow_cast<type_list_element_t<Index, typename descriptor::args_type>>() && ...)) {
340 return meta_invoke_with_args<Policy>(ctx, std::forward<Candidate>(candidate), *clazz, (args + Index)->cast<type_list_element_t<Index, typename descriptor::args_type>>()...);
343 if(((args + Index)->allow_cast<type_list_element_t<Index, typename descriptor::args_type>>() && ...)) {
344 return meta_invoke_with_args<Policy>(ctx, std::forward<Candidate>(candidate), (args + Index)->cast<type_list_element_t<Index, typename descriptor::args_type>>()...);
352template<
typename Type,
typename... Args, std::size_t... Index>
353[[nodiscard]] meta_any
meta_construct(
const meta_ctx &ctx, meta_any *
const args, std::index_sequence<Index...>) {
355 if(((args + Index)->allow_cast<Args>() && ...)) {
356 return meta_any{ctx, std::in_place_type<Type>, (args + Index)->cast<Args>()...};
382template<
typename Type,
typename Policy = as_is_t,
typename Cand
idate>
384 return internal::meta_invoke<Type, Policy>(ctx, std::move(instance), std::forward<Candidate>(
candidate),
args, std::make_index_sequence<
meta_function_helper_t<Type, std::remove_reference_t<Candidate>>::args_type::size>{});
397template<
typename Type,
typename Policy = as_is_t,
typename Cand
idate>
417template<
typename Type, auto Cand
idate,
typename Policy = as_is_t>
431template<
typename Type, auto Cand
idate,
typename Policy = as_is_t>
449template<
typename Type,
typename...
Args>
451 return internal::meta_construct<Type,
Args...>(ctx,
args, std::index_sequence_for<
Args...>{});
461template<
typename Type,
typename...
Args>
481template<
typename Type,
typename Policy = as_is_t,
typename Cand
idate>
484 return internal::meta_invoke<Type, Policy>(ctx, {}, std::forward<Candidate>(
candidate),
args, std::make_index_sequence<meta_function_helper_t<Type, std::remove_reference_t<Candidate>>::args_type::size>{});
487 return internal::meta_invoke<Type, Policy>(ctx, *
args, std::forward<Candidate>(
candidate),
args + 1u, std::make_index_sequence<
meta_function_helper_t<Type, std::remove_reference_t<Candidate>>::args_type::size>{});
500template<
typename Type,
typename Policy = as_is_t,
typename Cand
idate>
519template<
typename Type, auto Cand
idate,
typename Policy = as_is_t>
532template<
typename Type, auto Cand
idate,
typename Policy = as_is_t>
Service locator, nothing more.
static Service & value_or(Args &&...args)
Returns a service if available or sets it from a fallback type.
constexpr Type make_obj_using_allocator(const Allocator &allocator, Args &&...args)
Uses-allocator construction utility (waiting for C++20).
std::enable_if_t< is_meta_policy_v< Policy >, meta_any > meta_invoke(const meta_ctx &ctx, meta_handle instance, Candidate &&candidate, meta_any *const args)
Tries to invoke an object given a list of erased parameters.
typename meta_function_helper< Type, Candidate >::type meta_function_helper_t
Helper type.
meta_any meta_construct(const meta_ctx &ctx, meta_any *const args)
Tries to construct an instance given a list of erased parameters.
std::enable_if_t< is_meta_policy_v< Policy >, meta_any > meta_dispatch(const meta_ctx &ctx, Type &&value)
Wraps a value depending on the given policy.
typename type_list_element< Index, List >::type type_list_element_t
Helper type.
constexpr meta_ctx_arg_t meta_ctx_arg
Constant of type meta_context_arg_t used to disambiguate calls.
std::enable_if_t< is_meta_policy_v< Policy >, meta_any > meta_getter(const meta_ctx &ctx, meta_handle instance)
Gets the value of a given variable.
bool meta_setter(meta_handle instance, meta_any value)
Sets the value of a given variable.
A class to use to push around lists of types, nothing more.