EnTT: src/entt/meta/node.hpp Source File

EnTT 3.15.0

Loading...
Searching...
No Matches
node.hpp
1#ifndef ENTT_META_NODE_HPP
2#define ENTT_META_NODE_HPP
3
4#include <cstddef>
5#include <memory>
6#include <type_traits>
7#include <utility>
8#include <vector>
9#include "../config/config.h"
10#include "../core/attribute.h"
11#include "../core/bit.hpp"
12#include "../core/enum.hpp"
13#include "../core/fwd.hpp"
14#include "../core/type_info.hpp"
15#include "../core/type_traits.hpp"
16#include "../core/utility.hpp"
17#include "context.hpp"
18#include "type_traits.hpp"
19
20namespace entt {
21
22class meta_any;
23class meta_type;
24struct meta_handle;
25
27namespace internal {
28
29enum class meta_traits : std::uint32_t {
30 is_none = 0x0000,
31 is_const = 0x0001,
32 is_static = 0x0002,
33 is_arithmetic = 0x0004,
34 is_integral = 0x0008,
35 is_signed = 0x0010,
36 is_array = 0x0020,
37 is_enum = 0x0040,
38 is_class = 0x0080,
39 is_pointer = 0x0100,
40 is_pointer_like = 0x0200,
41 is_sequence_container = 0x0400,
42 is_associative_container = 0x0800,
43 _user_defined_traits = 0xFFFF,
44 _entt_enum_as_bitmask = 0xFFFF
45};
46
47template<typename Type>
48[[nodiscard]] auto meta_to_user_traits(const meta_traits traits) noexcept {
49 static_assert(std::is_enum_v<Type>, "Invalid enum type");
50 constexpr auto shift = popcount(static_cast<std::underlying_type_t<meta_traits>>(meta_traits::_user_defined_traits));
51 return Type{static_cast<std::underlying_type_t<Type>>(static_cast<std::underlying_type_t<meta_traits>>(traits) >> shift)};
52}
53
54template<typename Type>
55[[nodiscard]] auto user_to_meta_traits(const Type value) noexcept {
56 static_assert(std::is_enum_v<Type>, "Invalid enum type");
57 constexpr auto shift = popcount(static_cast<std::underlying_type_t<meta_traits>>(meta_traits::_user_defined_traits));
58 const auto traits = static_cast<std::underlying_type_t<internal::meta_traits>>(static_cast<std::underlying_type_t<Type>>(value));
59 ENTT_ASSERT(traits < ((~static_cast<std::underlying_type_t<meta_traits>>(meta_traits::_user_defined_traits)) >> shift), "Invalid traits");
60 return meta_traits{traits << shift};
61}
62
63struct meta_type_node;
64
65struct meta_custom_node {
66 id_type type{};
67 std::shared_ptr<void> value;
68};
69
70struct meta_base_node {
71 id_type type{};
72 meta_type_node (*resolve)(const meta_context &) noexcept {};
73 const void *(*cast)(const void *) noexcept {};
74};
75
76struct meta_conv_node {
77 id_type type{};
78 meta_any (*conv)(const meta_ctx &, const void *){};
79};
80
81struct meta_ctor_node {
82 using size_type = std::size_t;
83
84 id_type id{};
85 size_type arity{0u};
86 meta_type (*arg)(const meta_ctx &, const size_type) noexcept {};
87 meta_any (*invoke)(const meta_ctx &, meta_any *const){};
88};
89
90struct meta_dtor_node {
91 void (*dtor)(void *){};
92};
93
94struct meta_data_node {
95 using size_type = std::size_t;
96
97 id_type id{};
98 meta_traits traits{meta_traits::is_none};
99 size_type arity{0u};
100 meta_type_node (*type)(const meta_context &) noexcept {};
101 meta_type (*arg)(const meta_ctx &, const size_type) noexcept {};
102 bool (*set)(meta_handle, meta_any){};
103 meta_any (*get)(meta_handle){};
104 meta_custom_node custom{};
105};
106
107struct meta_func_node {
108 using size_type = std::size_t;
109
110 id_type id{};
111 meta_traits traits{meta_traits::is_none};
112 size_type arity{0u};
113 meta_type_node (*ret)(const meta_context &) noexcept {};
114 meta_type (*arg)(const meta_ctx &, const size_type) noexcept {};
115 meta_any (*invoke)(meta_handle, meta_any *const){};
116 std::shared_ptr<meta_func_node> next;
117 meta_custom_node custom{};
118};
119
120struct meta_template_node {
121 using size_type = std::size_t;
122
123 size_type arity{0u};
124 meta_type_node (*resolve)(const meta_context &) noexcept {};
125 meta_type_node (*arg)(const meta_context &, const size_type) noexcept {};
126};
127
128struct meta_type_descriptor {
129 std::vector<meta_ctor_node> ctor;
130 std::vector<meta_base_node> base;
131 std::vector<meta_conv_node> conv;
132 std::vector<meta_data_node> data;
133 std::vector<meta_func_node> func;
134};
135
136struct meta_type_node {
137 using size_type = std::size_t;
138
139 const type_info *info{};
140 id_type id{};
141 meta_traits traits{meta_traits::is_none};
142 size_type size_of{0u};
143 meta_type_node (*resolve)(const meta_context &) noexcept {};
144 meta_type_node (*remove_pointer)(const meta_context &) noexcept {};
145 meta_any (*default_constructor)(const meta_ctx &){};
146 double (*conversion_helper)(void *, const void *){};
147 meta_any (*from_void)(const meta_ctx &, void *, const void *){};
148 meta_template_node templ{};
149 meta_dtor_node dtor{};
150 meta_custom_node custom{};
151 std::shared_ptr<meta_type_descriptor> details;
152};
153
154template<auto Member, typename Type, typename Value>
155[[nodiscard]] auto *find_member(Type &from, const Value value) {
156 for(auto &&elem: from) {
157 if((elem.*Member) == value) {
158 return &elem;
159 }
160 }
161
162 return static_cast<typename Type::value_type *>(nullptr);
163}
164
165[[nodiscard]] inline auto *find_overload(meta_func_node *curr, std::remove_pointer_t<decltype(meta_func_node::invoke)> *const ref) {
166 while((curr != nullptr) && (curr->invoke != ref)) { curr = curr->next.get(); }
167 return curr;
168}
169
170template<auto Member>
171[[nodiscard]] auto *look_for(const meta_context &context, const meta_type_node &node, const id_type id) {
172 using value_type = typename std::remove_reference_t<decltype((node.details.get()->*Member))>::value_type;
173
174 if(node.details) {
175 if(auto *member = find_member<&value_type::id>((node.details.get()->*Member), id); member != nullptr) {
176 return member;
177 }
178
179 for(auto &&curr: node.details->base) {
180 if(auto *elem = look_for<Member>(context, curr.resolve(context), id); elem) {
181 return elem;
182 }
183 }
184 }
185
186 return static_cast<value_type *>(nullptr);
187}
188
189template<typename Type>
190meta_type_node resolve(const meta_context &) noexcept;
191
192template<typename... Args>
193[[nodiscard]] auto meta_arg_node(const meta_context &context, type_list<Args...>, [[maybe_unused]] const std::size_t index) noexcept {
194 meta_type_node (*value)(const meta_context &) noexcept = nullptr;
195
196 if constexpr(sizeof...(Args) != 0u) {
197 std::size_t pos{};
198 ((value = (pos++ == index ? &resolve<std::remove_cv_t<std::remove_reference_t<Args>>> : value)), ...);
199 }
200
201 ENTT_ASSERT(value != nullptr, "Out of bounds");
202 return value(context);
203}
204
205[[nodiscard]] inline const void *try_cast(const meta_context &context, const meta_type_node &from, const type_info &to, const void *instance) noexcept {
206 if((from.info != nullptr) && *from.info == to) {
207 return instance;
208 }
209
210 if(from.details) {
211 for(auto &&curr: from.details->base) {
212 if(const void *elem = try_cast(context, curr.resolve(context), to, curr.cast(instance)); elem) {
213 return elem;
214 }
215 }
216 }
217
218 return nullptr;
219}
220
221template<typename Func>
222[[nodiscard]] inline auto try_convert(const meta_context &context, const meta_type_node &from, const type_info &to, const bool arithmetic_or_enum, const void *instance, Func func) {
223 if(from.info && *from.info == to) {
224 return func(instance, from);
225 }
226
227 if(from.details) {
228 for(auto &&elem: from.details->conv) {
229 if(elem.type == to.hash()) {
230 return func(instance, elem);
231 }
232 }
233
234 for(auto &&curr: from.details->base) {
235 if(auto other = try_convert(context, curr.resolve(context), to, arithmetic_or_enum, curr.cast(instance), func); other) {
236 return other;
237 }
238 }
239 }
240
241 if(from.conversion_helper && arithmetic_or_enum) {
242 return func(instance, from.conversion_helper);
243 }
244
245 return func(instance);
246}
247
248[[nodiscard]] inline const meta_type_node *try_resolve(const meta_context &context, const type_info &info) noexcept {
249 const auto it = context.value.find(info.hash());
250 return it != context.value.end() ? &it->second : nullptr;
251}
252
253template<typename Type>
254[[nodiscard]] meta_type_node resolve(const meta_context &context) noexcept {
255 static_assert(std::is_same_v<Type, std::remove_const_t<std::remove_reference_t<Type>>>, "Invalid type");
256
257 if(auto *elem = try_resolve(context, type_id<Type>()); elem) {
258 return *elem;
259 }
260
261 meta_type_node node{
262 &type_id<Type>(),
263 type_id<Type>().hash(),
264 (std::is_arithmetic_v<Type> ? meta_traits::is_arithmetic : meta_traits::is_none)
265 | (std::is_integral_v<Type> ? meta_traits::is_integral : meta_traits::is_none)
266 | (std::is_signed_v<Type> ? meta_traits::is_signed : meta_traits::is_none)
267 | (std::is_array_v<Type> ? meta_traits::is_array : meta_traits::is_none)
268 | (std::is_enum_v<Type> ? meta_traits::is_enum : meta_traits::is_none)
269 | (std::is_class_v<Type> ? meta_traits::is_class : meta_traits::is_none)
270 | (std::is_pointer_v<Type> ? meta_traits::is_pointer : meta_traits::is_none)
271 | (is_meta_pointer_like_v<Type> ? meta_traits::is_pointer_like : meta_traits::is_none)
272 | (is_complete_v<meta_sequence_container_traits<Type>> ? meta_traits::is_sequence_container : meta_traits::is_none)
273 | (is_complete_v<meta_associative_container_traits<Type>> ? meta_traits::is_associative_container : meta_traits::is_none),
274 size_of_v<Type>,
275 &resolve<Type>,
276 &resolve<std::remove_cv_t<std::remove_pointer_t<Type>>>};
277
278 if constexpr(std::is_default_constructible_v<Type>) {
279 node.default_constructor = +[](const meta_ctx &ctx) {
280 return meta_any{ctx, std::in_place_type<Type>};
281 };
282 }
283
284 if constexpr(std::is_arithmetic_v<Type>) {
285 node.conversion_helper = +[](void *lhs, const void *rhs) {
286 return lhs ? static_cast<double>(*static_cast<Type *>(lhs) = static_cast<Type>(*static_cast<const double *>(rhs))) : static_cast<double>(*static_cast<const Type *>(rhs));
287 };
288 } else if constexpr(std::is_enum_v<Type>) {
289 node.conversion_helper = +[](void *lhs, const void *rhs) {
290 return lhs ? static_cast<double>(*static_cast<Type *>(lhs) = static_cast<Type>(static_cast<std::underlying_type_t<Type>>(*static_cast<const double *>(rhs)))) : static_cast<double>(*static_cast<const Type *>(rhs));
291 };
292 }
293
294 if constexpr(!std::is_void_v<Type> && !std::is_function_v<Type>) {
295 node.from_void = +[](const meta_ctx &ctx, void *elem, const void *celem) {
296 if(elem && celem) { // ownership construction request
297 return meta_any{ctx, std::in_place, static_cast<std::decay_t<Type> *>(elem)};
298 }
299
300 if(elem) { // non-const reference construction request
301 return meta_any{ctx, std::in_place_type<std::decay_t<Type> &>, *static_cast<std::decay_t<Type> *>(elem)};
302 }
303
304 // const reference construction request
305 return meta_any{ctx, std::in_place_type<const std::decay_t<Type> &>, *static_cast<const std::decay_t<Type> *>(celem)};
306 };
307 }
308
309 if constexpr(is_complete_v<meta_template_traits<Type>>) {
310 node.templ = meta_template_node{
311 meta_template_traits<Type>::args_type::size,
312 &resolve<typename meta_template_traits<Type>::class_type>,
313 +[](const meta_context &area, const std::size_t index) noexcept { return meta_arg_node(area, typename meta_template_traits<Type>::args_type{}, index); }};
314 }
315
316 return node;
317}
318
319} // namespace internal
321
322} // namespace entt
323
324#endif
entt::meta_any
Opaque wrapper for values of any type.
Definition meta.hpp:166
entt::meta_type
Opaque wrapper for types.
Definition meta.hpp:1088
entt
EnTT default namespace.
Definition dense_map.hpp:22
entt::id_type
std::uint32_t id_type
Alias declaration for type identifiers.
Definition fwd.hpp:29
entt::resolve
meta_type resolve(const meta_ctx &ctx) noexcept
Returns the meta type associated with a given type.
Definition resolve.hpp:21
entt::popcount
constexpr std::enable_if_t< std::is_unsigned_v< Type >, int > popcount(const Type value) noexcept
Returns the number of set bits in a value (waiting for C++20 and std::popcount).
Definition bit.hpp:19
entt::meta_handle
Opaque pointers to instances of any type.
Definition meta.hpp:688