EnTT 3.14.0
Loading...
Searching...
No Matches
factory.hpp
1#ifndef ENTT_META_FACTORY_HPP
2#define ENTT_META_FACTORY_HPP
3
4#include <cstddef>
5#include <cstdint>
6#include <functional>
7#include <memory>
8#include <tuple>
9#include <type_traits>
10#include <utility>
11#include "../config/config.h"
12#include "../core/bit.hpp"
13#include "../core/fwd.hpp"
14#include "../core/type_info.hpp"
15#include "../core/type_traits.hpp"
16#include "../locator/locator.hpp"
17#include "context.hpp"
18#include "meta.hpp"
19#include "node.hpp"
20#include "policy.hpp"
21#include "range.hpp"
22#include "resolve.hpp"
23#include "utility.hpp"
24
25namespace entt {
26
28namespace internal {
29
30class basic_meta_factory {
31 using invoke_type = std::remove_pointer_t<decltype(meta_func_node::invoke)>;
32
33 auto *find_member_or_assert() {
34 auto *member = find_member<&meta_data_node::id>(details->data, bucket);
35 ENTT_ASSERT(member != nullptr, "Cannot find member");
36 return member;
37 }
38
39 auto *find_overload_or_assert() {
40 auto *overload = find_overload(find_member<&meta_func_node::id>(details->func, bucket), invoke);
41 ENTT_ASSERT(overload != nullptr, "Cannot find overload");
42 return overload;
43 }
44
45 void reset_bucket(const id_type id, invoke_type *const ref = nullptr) {
46 invoke = ref;
47 bucket = id;
48 }
49
50protected:
51 void type(const id_type id) noexcept {
52 reset_bucket(parent);
53 auto &&elem = meta_context::from(*ctx).value[parent];
54 ENTT_ASSERT(elem.id == id || !resolve(*ctx, id), "Duplicate identifier");
55 elem.id = id;
56 }
57
58 template<typename Type>
59 void insert_or_assign(Type node) {
60 reset_bucket(parent);
61
62 if constexpr(std::is_same_v<Type, meta_base_node>) {
63 auto *member = find_member<&meta_base_node::type>(details->base, node.type);
64 member ? (*member = node) : details->base.emplace_back(node);
65 } else if constexpr(std::is_same_v<Type, meta_conv_node>) {
66 auto *member = find_member<&meta_conv_node::type>(details->conv, node.type);
67 member ? (*member = node) : details->conv.emplace_back(node);
68 } else {
69 static_assert(std::is_same_v<Type, meta_ctor_node>, "Unexpected type");
70 auto *member = find_member<&meta_ctor_node::id>(details->ctor, node.id);
71 member ? (*member = node) : details->ctor.emplace_back(node);
72 }
73 }
74
75 void dtor(meta_dtor_node node) {
76 reset_bucket(parent);
77 meta_context::from(*ctx).value[parent].dtor = node;
78 }
79
80 void data(meta_data_node node) {
81 reset_bucket(node.id);
82
83 if(auto *member = find_member<&meta_data_node::id>(details->data, node.id); member == nullptr) {
84 details->data.emplace_back(std::move(node));
85 } else if(member->set != node.set || member->get != node.get) {
86 *member = std::move(node);
87 }
88 }
89
90 void func(meta_func_node node) {
91 reset_bucket(node.id, node.invoke);
92
93 if(auto *member = find_member<&meta_func_node::id>(details->func, node.id); member == nullptr) {
94 details->func.emplace_back(std::move(node));
95 } else if(auto *overload = find_overload(member, node.invoke); overload == nullptr) {
96 while(member->next != nullptr) { member = member->next.get(); }
97 member->next = std::make_shared<meta_func_node>(std::move(node));
98 }
99 }
100
101 void prop(meta_prop_node node) {
102 std::vector<meta_prop_node> *container = nullptr;
103
104 if(bucket == parent) {
105 container = &details->prop;
106 } else if(invoke == nullptr) {
107 container = &find_member_or_assert()->prop;
108 } else {
109 container = &find_overload_or_assert()->prop;
110 }
111
112 auto *member = find_member<&meta_prop_node::id>(*container, node.id);
113 (member != nullptr) ? (*member = std::move(node)) : container->emplace_back(std::move(node));
114 }
115
116 void traits(const meta_traits value) {
117 if(bucket == parent) {
118 meta_context::from(*ctx).value[bucket].traits |= value;
119 } else if(invoke == nullptr) {
120 find_member_or_assert()->traits |= value;
121 } else {
122 find_overload_or_assert()->traits |= value;
123 }
124 }
125
126 void custom(meta_custom_node node) {
127 if(bucket == parent) {
128 meta_context::from(*ctx).value[bucket].custom = std::move(node);
129 } else if(invoke == nullptr) {
130 find_member_or_assert()->custom = std::move(node);
131 } else {
132 find_overload_or_assert()->custom = std::move(node);
133 }
134 }
135
136public:
137 basic_meta_factory(const id_type id, meta_ctx &area)
138 : ctx{&area},
139 parent{id},
140 bucket{id} {
141 auto &&elem = meta_context::from(*ctx).value[parent];
142
143 if(!elem.details) {
144 elem.details = std::make_shared<meta_type_descriptor>();
145 }
146
147 details = elem.details.get();
148 }
149
150private:
151 meta_ctx *ctx{};
152 id_type parent{};
153 id_type bucket{};
154 invoke_type *invoke{};
155 meta_type_descriptor *details{};
156};
157
158} // namespace internal
165template<typename Type>
166class meta_factory: private internal::basic_meta_factory {
167 using base_type = internal::basic_meta_factory;
168
169 template<typename Setter, auto Getter, typename Policy, std::size_t... Index>
170 void data(const id_type id, std::index_sequence<Index...>) noexcept {
171 using data_type = std::invoke_result_t<decltype(Getter), Type &>;
172 using args_type = type_list<typename meta_function_helper_t<Type, decltype(value_list_element_v<Index, Setter>)>::args_type...>;
173 static_assert(Policy::template value<data_type>, "Invalid return type for the given policy");
174
175 base_type::data(
176 internal::meta_data_node{
177 id,
178 /* this is never static */
179 (std::is_member_object_pointer_v<decltype(value_list_element_v<Index, Setter>)> && ... && std::is_const_v<std::remove_reference_t<data_type>>) ? internal::meta_traits::is_const : internal::meta_traits::is_none,
180 Setter::size,
181 &internal::resolve<std::remove_cv_t<std::remove_reference_t<data_type>>>,
183 +[](meta_handle instance, meta_any value) { return (meta_setter<Type, value_list_element_v<Index, Setter>>(*instance.operator->(), value.as_ref()) || ...); },
185 }
186
187public:
190 : internal::basic_meta_factory{type_id<Type>(), locator<meta_ctx>::value_or()} {}
191
197 : internal::basic_meta_factory{type_id<Type>().hash(), area} {}
198
204 meta_factory type(const id_type id) noexcept {
205 base_type::type(id);
206 return *this;
207 }
208
217 template<typename Base>
219 static_assert(!std::is_same_v<Type, Base> && std::is_base_of_v<Base, Type>, "Invalid base type");
220 auto *const op = +[](const void *instance) noexcept { return static_cast<const void *>(static_cast<const Base *>(static_cast<const Type *>(instance))); };
221 base_type::insert_or_assign(internal::meta_base_node{type_id<Base>().hash(), &internal::resolve<Base>, op});
222 return *this;
223 }
224
237 template<auto Candidate>
238 auto conv() noexcept {
239 using conv_type = std::remove_cv_t<std::remove_reference_t<std::invoke_result_t<decltype(Candidate), Type &>>>;
240 auto *const op = +[](const meta_ctx &area, const void *instance) { return forward_as_meta(area, std::invoke(Candidate, *static_cast<const Type *>(instance))); };
241 base_type::insert_or_assign(internal::meta_conv_node{type_id<conv_type>().hash(), op});
242 return *this;
243 }
244
254 template<typename To>
256 using conv_type = std::remove_cv_t<std::remove_reference_t<To>>;
257 auto *const op = +[](const meta_ctx &area, const void *instance) { return forward_as_meta(area, static_cast<To>(*static_cast<const Type *>(instance))); };
258 base_type::insert_or_assign(internal::meta_conv_node{type_id<conv_type>().hash(), op});
259 return *this;
260 }
261
275 template<auto Candidate, typename Policy = as_is_t>
277 using descriptor = meta_function_helper_t<Type, decltype(Candidate)>;
278 static_assert(Policy::template value<typename descriptor::return_type>, "Invalid return type for the given policy");
279 static_assert(std::is_same_v<std::remove_cv_t<std::remove_reference_t<typename descriptor::return_type>>, Type>, "The function doesn't return an object of the required type");
280 base_type::insert_or_assign(internal::meta_ctor_node{type_id<typename descriptor::args_type>().hash(), descriptor::args_type::size, &meta_arg<typename descriptor::args_type>, &meta_construct<Type, Candidate, Policy>});
281 return *this;
282 }
283
294 template<typename... Args>
296 // default constructor is already implicitly generated, no need for redundancy
297 if constexpr(sizeof...(Args) != 0u) {
298 using descriptor = meta_function_helper_t<Type, Type (*)(Args...)>;
299 base_type::insert_or_assign(internal::meta_ctor_node{type_id<typename descriptor::args_type>().hash(), descriptor::args_type::size, &meta_arg<typename descriptor::args_type>, &meta_construct<Type, Args...>});
300 }
301
302 return *this;
303 }
304
323 template<auto Func>
325 static_assert(std::is_invocable_v<decltype(Func), Type &>, "The function doesn't accept an object of the type provided");
326 auto *const op = +[](void *instance) { std::invoke(Func, *static_cast<Type *>(instance)); };
327 base_type::dtor(internal::meta_dtor_node{op});
328 return *this;
329 }
330
344 template<auto Data, typename Policy = as_is_t>
345 meta_factory data(const id_type id) noexcept {
346 if constexpr(std::is_member_object_pointer_v<decltype(Data)>) {
347 using data_type = std::invoke_result_t<decltype(Data), Type &>;
348 static_assert(Policy::template value<data_type>, "Invalid return type for the given policy");
349
350 base_type::data(
351 internal::meta_data_node{
352 id,
353 /* this is never static */
354 std::is_const_v<std::remove_reference_t<data_type>> ? internal::meta_traits::is_const : internal::meta_traits::is_none,
355 1u,
356 &internal::resolve<std::remove_cv_t<std::remove_reference_t<data_type>>>,
360 } else {
361 using data_type = std::remove_pointer_t<decltype(Data)>;
362
363 if constexpr(std::is_pointer_v<decltype(Data)>) {
364 static_assert(Policy::template value<decltype(*Data)>, "Invalid return type for the given policy");
365 } else {
366 static_assert(Policy::template value<data_type>, "Invalid return type for the given policy");
367 }
368
369 base_type::data(
370 internal::meta_data_node{
371 id,
372 ((std::is_same_v<Type, std::remove_cv_t<std::remove_reference_t<data_type>>> || std::is_const_v<std::remove_reference_t<data_type>>) ? internal::meta_traits::is_const : internal::meta_traits::is_none) | internal::meta_traits::is_static,
373 1u,
374 &internal::resolve<std::remove_cv_t<std::remove_reference_t<data_type>>>,
375 &meta_arg<type_list<std::remove_cv_t<std::remove_reference_t<data_type>>>>,
378 }
379
380 return *this;
381 }
382
403 template<auto Setter, auto Getter, typename Policy = as_is_t>
404 meta_factory data(const id_type id) noexcept {
405 using data_type = std::invoke_result_t<decltype(Getter), Type &>;
406 static_assert(Policy::template value<data_type>, "Invalid return type for the given policy");
407
408 if constexpr(std::is_same_v<decltype(Setter), std::nullptr_t>) {
409 base_type::data(
410 internal::meta_data_node{
411 id,
412 /* this is never static */
413 internal::meta_traits::is_const,
414 0u,
415 &internal::resolve<std::remove_cv_t<std::remove_reference_t<data_type>>>,
419 } else {
420 using args_type = typename meta_function_helper_t<Type, decltype(Setter)>::args_type;
421
422 base_type::data(
423 internal::meta_data_node{
424 id,
425 /* this is never static nor const */
426 internal::meta_traits::is_none,
427 1u,
428 &internal::resolve<std::remove_cv_t<std::remove_reference_t<data_type>>>,
432 }
433
434 return *this;
435 }
436
454 template<typename Setter, auto Getter, typename Policy = as_is_t>
455 meta_factory data(const id_type id) noexcept {
456 data<Setter, Getter, Policy>(id, std::make_index_sequence<Setter::size>{});
457 return *this;
458 }
459
473 template<auto Candidate, typename Policy = as_is_t>
474 meta_factory func(const id_type id) noexcept {
475 using descriptor = meta_function_helper_t<Type, decltype(Candidate)>;
476 static_assert(Policy::template value<typename descriptor::return_type>, "Invalid return type for the given policy");
477
478 base_type::func(
479 internal::meta_func_node{
480 id,
481 (descriptor::is_const ? internal::meta_traits::is_const : internal::meta_traits::is_none) | (descriptor::is_static ? internal::meta_traits::is_static : internal::meta_traits::is_none),
482 descriptor::args_type::size,
483 &internal::resolve<std::conditional_t<std::is_same_v<Policy, as_void_t>, void, std::remove_cv_t<std::remove_reference_t<typename descriptor::return_type>>>>,
486
487 return *this;
488 }
489
500 template<typename... Value>
501 [[deprecated("use ::custom() instead")]] meta_factory prop(id_type id, [[maybe_unused]] Value &&...value) {
502 if constexpr(sizeof...(Value) == 0u) {
503 base_type::prop(internal::meta_prop_node{id, &internal::resolve<void>});
504 } else {
505 base_type::prop(internal::meta_prop_node{id, &internal::resolve<std::decay_t<Value>>..., std::make_shared<std::decay_t<Value>>(std::forward<Value>(value))...});
506 }
507
508 return *this;
509 }
510
520 template<typename Value>
521 meta_factory traits(const Value value) {
522 static_assert(std::is_enum_v<Value>, "Invalid enum type");
523 base_type::traits(internal::user_to_meta_traits(value));
524 return *this;
525 }
526
534 template<typename Value, typename... Args>
536 base_type::custom(internal::meta_custom_node{type_id<Value>().hash(), std::make_shared<Value>(std::forward<Args>(args)...)});
537 return *this;
538 }
539};
540
553template<typename Type>
554[[nodiscard]] auto meta(meta_ctx &ctx) noexcept {
555 auto &&context = internal::meta_context::from(ctx);
556 // make sure the type exists in the context before returning a factory
557 context.value.try_emplace(type_id<Type>().hash(), internal::resolve<Type>(context));
558 return meta_factory<Type>{ctx};
559}
560
572template<typename Type>
576
589inline void meta_reset(meta_ctx &ctx, const id_type id) noexcept {
590 auto &&context = internal::meta_context::from(ctx);
591
592 for(auto it = context.value.begin(); it != context.value.end();) {
593 if(it->second.id == id) {
594 it = context.value.erase(it);
595 } else {
596 ++it;
597 }
598 }
599}
600
612inline void meta_reset(const id_type id) noexcept {
614}
615
624template<typename Type>
625void meta_reset(meta_ctx &ctx) noexcept {
626 internal::meta_context::from(ctx).value.erase(type_id<Type>().hash());
627}
628
636template<typename Type>
640
648inline void meta_reset(meta_ctx &ctx) noexcept {
649 internal::meta_context::from(ctx).value.clear();
650}
651
660
661} // namespace entt
662
663#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
meta_any as_ref() noexcept
Aliasing constructor.
Definition meta.hpp:598
Opaque meta context type.
Definition context.hpp:34
Meta factory to be used for reflection purposes.
Definition factory.hpp:166
meta_factory base() noexcept
Assigns a meta base to a meta type.
Definition factory.hpp:218
meta_factory custom(Args &&...args)
Sets user defined data that will never be used by the library.
Definition factory.hpp:535
meta_factory ctor() noexcept
Assigns a meta constructor to a meta type.
Definition factory.hpp:295
auto conv() noexcept
Assigns a meta conversion function to a meta type.
Definition factory.hpp:238
meta_factory prop(id_type id, Value &&...value)
Assigns a property to the last created meta object.
Definition factory.hpp:501
meta_factory ctor() noexcept
Assigns a meta constructor to a meta type.
Definition factory.hpp:276
meta_factory() noexcept
Default constructor.
Definition factory.hpp:189
meta_factory dtor() noexcept
Assigns a meta destructor to a meta type.
Definition factory.hpp:324
meta_factory data(const id_type id) noexcept
Assigns a meta data to a meta type.
Definition factory.hpp:345
meta_factory(meta_ctx &area) noexcept
Context aware constructor.
Definition factory.hpp:196
meta_factory conv() noexcept
Assigns a meta conversion function to a meta type.
Definition factory.hpp:255
meta_factory data(const id_type id) noexcept
Assigns a meta data to a meta type by means of its setter and getter.
Definition factory.hpp:404
meta_factory func(const id_type id) noexcept
Assigns a meta function to a meta type.
Definition factory.hpp:474
meta_factory type(const id_type id) noexcept
Assigns a custom unique identifier to a meta type.
Definition factory.hpp:204
meta_factory traits(const Value value)
Sets traits on the last created meta object.
Definition factory.hpp:521
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
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
auto meta() noexcept
Utility function to use for reflection.
Definition factory.hpp:573
std::uint32_t id_type
Alias declaration for type identifiers.
Definition fwd.hpp:14
typename type_list_element< Index, List >::type type_list_element_t
Helper type.
void meta_reset() noexcept
Resets a type and all its parts.
Definition factory.hpp:637
meta_any forward_as_meta(const meta_ctx &ctx, Type &&value)
Forwards its argument and avoids copies for lvalue references.
Definition meta.hpp:630
bool meta_setter(meta_handle instance, meta_any value)
Sets the value of a given variable.
Definition utility.hpp:226
void invoke(Registry &reg, const typename Registry::entity_type entt)
Helper to create a listener that directly invokes a member function.
Definition helper.hpp:108
constexpr auto overload(Type Class::*member) noexcept
Constant utility to disambiguate overloaded members of a class.
Definition utility.hpp:34
meta_type resolve(const meta_ctx &ctx) noexcept
Returns the meta type associated with a given type.
Definition resolve.hpp:21
@ ref
Aliasing mode, the object points to a non-const element.
Opaque pointers to instances of any type.
Definition meta.hpp:651
A class to use to push around lists of types, nothing more.