EnTT 3.14.0
Loading...
Searching...
No Matches
utility.hpp
1#ifndef ENTT_META_UTILITY_HPP
2#define ENTT_META_UTILITY_HPP
3
4#include <cstddef>
5#include <functional>
6#include <type_traits>
7#include <utility>
8#include "../core/type_traits.hpp"
9#include "../locator/locator.hpp"
10#include "meta.hpp"
11#include "node.hpp"
12#include "policy.hpp"
13
14namespace entt {
15
23template<typename Ret, typename Args, bool Static, bool Const>
28 using args_type = Args;
29
31 static constexpr bool is_static = Static;
33 static constexpr bool is_const = Const;
34};
35
37template<typename, typename>
39
47template<typename Type, typename Ret, typename Class, typename... Args>
48struct meta_function_descriptor<Type, Ret (Class::*)(Args...) const>
50 Ret,
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>,
53 true> {};
54
62template<typename Type, typename Ret, typename Class, typename... Args>
63struct meta_function_descriptor<Type, Ret (Class::*)(Args...)>
65 Ret,
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>,
68 false> {};
69
76template<typename Type, typename Ret, typename Class>
79 Ret &,
80 std::conditional_t<std::is_base_of_v<Class, Type>, type_list<>, type_list<Class &>>,
81 !std::is_base_of_v<Class, Type>,
82 false> {};
83
91template<typename Type, typename Ret, typename MaybeType, typename... Args>
94 Ret,
95 std::conditional_t<
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>,
97 type_list<Args...>,
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>)> {};
101
107template<typename Type, typename Ret>
108struct meta_function_descriptor<Type, Ret (*)()>
110 Ret,
112 true,
113 false> {};
114
124template<typename Type, typename Candidate>
126 template<typename Ret, typename... Args, typename Class>
127 static constexpr meta_function_descriptor<Type, Ret (Class::*)(Args...) const> get_rid_of_noexcept(Ret (Class::*)(Args...) const);
128
129 template<typename Ret, typename... Args, typename Class>
130 static constexpr meta_function_descriptor<Type, Ret (Class::*)(Args...)> get_rid_of_noexcept(Ret (Class::*)(Args...));
131
132 template<typename Ret, typename Class, typename = std::enable_if_t<std::is_member_object_pointer_v<Ret Class::*>>>
133 static constexpr meta_function_descriptor<Type, Ret Class::*> get_rid_of_noexcept(Ret Class::*);
134
135 template<typename Ret, typename... Args>
136 static constexpr meta_function_descriptor<Type, Ret (*)(Args...)> get_rid_of_noexcept(Ret (*)(Args...));
137
138 template<typename Class>
139 static constexpr meta_function_descriptor<Class, decltype(&Class::operator())> get_rid_of_noexcept(Class);
140
141public:
143 using type = decltype(get_rid_of_noexcept(std::declval<Candidate>()));
144};
145
151template<typename Type, typename Candidate>
153
167template<typename Policy = as_is_t, typename Type>
168[[nodiscard]] std::enable_if_t<is_meta_policy_v<Policy>, meta_any> meta_dispatch(const meta_ctx &ctx, [[maybe_unused]] Type &&value) {
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)};
176 } else {
177 return meta_any{ctx, std::forward<Type>(value)};
178 }
179}
180
188template<typename Policy = as_is_t, typename Type>
189[[nodiscard]] std::enable_if_t<is_meta_policy_v<Policy>, meta_any> meta_dispatch(Type &&value) {
190 return meta_dispatch<Policy, Type>(locator<meta_ctx>::value_or(), std::forward<Type>(value));
191}
192
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)};
204}
205
212template<typename Type>
213[[nodiscard]] static meta_type meta_arg(const std::size_t index) noexcept {
215}
216
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)>>>) {
229 using descriptor = meta_function_helper_t<Type, decltype(Data)>;
231
232 if(auto *const clazz = instance->try_cast<Type>(); clazz && value.allow_cast<data_type>()) {
233 std::invoke(Data, *clazz, value.cast<data_type>());
234 return true;
235 }
236 } else if constexpr(std::is_member_object_pointer_v<decltype(Data)>) {
237 using data_type = std::remove_reference_t<typename meta_function_helper_t<Type, decltype(Data)>::return_type>;
238
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>()) {
241 std::invoke(Data, *clazz) = value.cast<data_type>();
242 return true;
243 }
244 }
245 } else {
246 using data_type = std::remove_reference_t<decltype(*Data)>;
247
248 if constexpr(!std::is_array_v<data_type> && !std::is_const_v<data_type>) {
249 if(value.allow_cast<data_type>()) {
250 *Data = value.cast<data_type>();
251 return true;
252 }
253 }
254 }
255 }
256
257 return false;
258}
259
274template<typename Type, auto Data, typename Policy = as_is_t>
275[[nodiscard]] std::enable_if_t<is_meta_policy_v<Policy>, meta_any> meta_getter(const meta_ctx &ctx, [[maybe_unused]] meta_handle instance) {
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) {
280 return meta_dispatch<Policy>(ctx, std::invoke(Data, *clazz));
281 }
282 }
283
284 if constexpr(std::is_invocable_v<decltype(Data), const Type &>) {
285 if(auto *fallback = instance->try_cast<const Type>(); fallback) {
286 return meta_dispatch<Policy>(ctx, std::invoke(Data, *fallback));
287 }
288 }
289 }
290
291 return meta_any{meta_ctx_arg, ctx};
292 } else if constexpr(std::is_pointer_v<decltype(Data)>) {
293 if constexpr(std::is_array_v<std::remove_pointer_t<decltype(Data)>>) {
294 return meta_any{meta_ctx_arg, ctx};
295 } else {
296 return meta_dispatch<Policy>(ctx, *Data);
297 }
298 } else {
299 return meta_dispatch<Policy>(ctx, Data);
300 }
301}
302
311template<typename Type, auto Data, typename Policy = as_is_t>
312[[nodiscard]] std::enable_if_t<is_meta_policy_v<Policy>, meta_any> meta_getter(meta_handle instance) {
314}
315
317namespace internal {
318
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>};
324 } else {
325 return meta_dispatch<Policy>(ctx, std::invoke(std::forward<Candidate>(candidate), args...));
326 }
327}
328
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>>;
332
333 // NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic) - waiting for C++20 (and std::span)
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>>()...);
337 }
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>>()...);
341 }
342 } else {
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>>()...);
345 }
346 }
347 // NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic)
348
349 return meta_any{meta_ctx_arg, ctx};
350}
351
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...>) {
354 // NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic) - waiting for C++20 (and std::span)
355 if(((args + Index)->allow_cast<Args>() && ...)) {
356 return meta_any{ctx, std::in_place_type<Type>, (args + Index)->cast<Args>()...};
357 }
358 // NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic)
359
360 return meta_any{meta_ctx_arg, ctx};
361}
362
363} // namespace internal
382template<typename Type, typename Policy = as_is_t, typename Candidate>
383[[nodiscard]] 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) {
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>{});
385}
386
397template<typename Type, typename Policy = as_is_t, typename Candidate>
398[[nodiscard]] std::enable_if_t<is_meta_policy_v<Policy>, meta_any> meta_invoke(meta_handle instance, Candidate &&candidate, meta_any *const args) {
399 return meta_invoke<Type, Policy>(locator<meta_ctx>::value_or(), std::move(instance), std::forward<Candidate>(candidate), args);
400}
401
417template<typename Type, auto Candidate, typename Policy = as_is_t>
418[[nodiscard]] std::enable_if_t<is_meta_policy_v<Policy>, meta_any> meta_invoke(const meta_ctx &ctx, meta_handle instance, meta_any *const args) {
419 return internal::meta_invoke<Type, Policy>(ctx, std::move(instance), Candidate, args, std::make_index_sequence<meta_function_helper_t<Type, std::remove_reference_t<decltype(Candidate)>>::args_type::size>{});
420}
421
431template<typename Type, auto Candidate, typename Policy = as_is_t>
432[[nodiscard]] std::enable_if_t<is_meta_policy_v<Policy>, meta_any> meta_invoke(meta_handle instance, meta_any *const args) {
434}
435
449template<typename Type, typename... Args>
451 return internal::meta_construct<Type, Args...>(ctx, args, std::index_sequence_for<Args...>{});
452}
453
461template<typename Type, typename... Args>
465
481template<typename Type, typename Policy = as_is_t, typename Candidate>
483 if constexpr(meta_function_helper_t<Type, Candidate>::is_static || std::is_class_v<std::remove_cv_t<std::remove_reference_t<Candidate>>>) {
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>{});
485 } else {
486 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) - waiting for C++20 (and std::span)
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>{});
488 }
489}
490
500template<typename Type, typename Policy = as_is_t, typename Candidate>
501[[nodiscard]] std::enable_if_t<is_meta_policy_v<Policy>, meta_any> meta_construct(Candidate &&candidate, meta_any *const args) {
502 return meta_construct<Type, Policy>(locator<meta_ctx>::value_or(), std::forward<Candidate>(candidate), args);
503}
504
519template<typename Type, auto Candidate, typename Policy = as_is_t>
520[[nodiscard]] std::enable_if_t<is_meta_policy_v<Policy>, meta_any> meta_construct(const meta_ctx &ctx, meta_any *const args) {
522}
523
532template<typename Type, auto Candidate, typename Policy = as_is_t>
533[[nodiscard]] std::enable_if_t<is_meta_policy_v<Policy>, meta_any> meta_construct(meta_any *const args) {
535}
536
537} // namespace entt
538
539#endif
Service locator, nothing more.
Definition locator.hpp:27
static Service & value_or(Args &&...args)
Returns a service if available or sets it from a fallback type.
Definition locator.hpp:88
Opaque wrapper for values of any type.
Definition meta.hpp:179
Opaque meta context type.
Definition context.hpp:34
Meta function helper.
Definition utility.hpp:125
decltype(get_rid_of_noexcept(std::declval< Candidate >())) type
The meta function descriptor of the given function.
Definition utility.hpp:143
EnTT default namespace.
Definition dense_map.hpp:22
constexpr Type make_obj_using_allocator(const Allocator &allocator, Args &&...args)
Uses-allocator construction utility (waiting for C++20).
Definition memory.hpp:219
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.
Definition utility.hpp:383
typename meta_function_helper< Type, Candidate >::type meta_function_helper_t
Helper type.
Definition utility.hpp:152
meta_any meta_construct(const meta_ctx &ctx, meta_any *const args)
Tries to construct an instance given a list of erased parameters.
Definition utility.hpp:450
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.
Definition utility.hpp:168
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.
Definition context.hpp:31
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.
Definition utility.hpp:275
bool meta_setter(meta_handle instance, meta_any value)
Sets the value of a given variable.
Definition utility.hpp:226
Meta function descriptor traits.
Definition utility.hpp:24
static constexpr bool is_static
True if the meta function is static, false otherwise.
Definition utility.hpp:31
Args args_type
Meta function arguments.
Definition utility.hpp:28
static constexpr bool is_const
True if the meta function is const, false otherwise.
Definition utility.hpp:33
Ret return_type
Meta function return type.
Definition utility.hpp:26
Primary template isn't defined on purpose.
Definition utility.hpp:38
Opaque pointers to instances of any type.
Definition meta.hpp:651
A class to use to push around lists of types, nothing more.