Skip to content

Commit 784235c

Browse files
committed
meta: factory class review
1 parent dc070b0 commit 784235c

File tree

1 file changed

+39
-33
lines changed

1 file changed

+39
-33
lines changed

src/entt/meta/factory.hpp

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,40 +28,52 @@ namespace internal {
2828

2929
class basic_meta_factory {
3030
protected:
31-
void track(const id_type id) noexcept {
31+
void type(const id_type id) noexcept {
3232
auto &&elem = internal::meta_context::from(*ctx).value[parent];
3333
ENTT_ASSERT(elem.id == id || !resolve(*ctx, id), "Duplicate identifier");
3434
bucket = parent;
3535
elem.id = id;
3636
}
3737

38-
void extend(const id_type id, meta_base_node node) {
38+
void base(const id_type id, meta_base_node node) {
3939
details->base.insert_or_assign(id, std::move(node));
4040
bucket = parent;
4141
}
4242

43-
void extend(const id_type id, meta_conv_node node) {
43+
void conv(const id_type id, meta_conv_node node) {
4444
details->conv.insert_or_assign(id, std::move(node));
4545
bucket = parent;
4646
}
4747

48-
void extend(const id_type id, meta_ctor_node node) {
48+
void ctor(const id_type id, meta_ctor_node node) {
4949
details->ctor.insert_or_assign(id, std::move(node));
5050
bucket = parent;
5151
}
5252

53-
void extend(meta_dtor_node node) {
53+
void dtor(meta_dtor_node node) {
5454
internal::meta_context::from(*ctx).value[parent].dtor = std::move(node);
5555
bucket = parent;
5656
}
5757

58-
void extend(const id_type id, meta_data_node node) {
58+
void data(const id_type id) {
59+
ENTT_ASSERT(details->data.find(id) != details->data.cend(), "Invalid id");
60+
is_data = true;
61+
bucket = id;
62+
}
63+
64+
void data(const id_type id, meta_data_node node) {
5965
details->data.insert_or_assign(id, std::move(node));
6066
is_data = true;
6167
bucket = id;
6268
}
6369

64-
void extend(const id_type id, meta_func_node node) {
70+
void func(const id_type id) {
71+
ENTT_ASSERT(details->func.find(id) != details->func.cend(), "Invalid id");
72+
is_data = false;
73+
bucket = id;
74+
}
75+
76+
void func(const id_type id, meta_func_node node) {
6577
is_data = false;
6678
bucket = id;
6779

@@ -81,13 +93,7 @@ class basic_meta_factory {
8193
details->func.insert_or_assign(id, std::move(node));
8294
}
8395

84-
void seek(const id_type id, const bool data) {
85-
ENTT_ASSERT((data && (details->data.find(id) != details->data.cend())) || (!data && (details->func.find(id) != details->func.cend())), "Invalid id");
86-
is_data = data;
87-
bucket = id;
88-
}
89-
90-
void property(const id_type key, internal::meta_prop_node value) {
96+
void prop(const id_type key, internal::meta_prop_node value) {
9197
if(bucket == parent) {
9298
details->prop[key] = std::move(value);
9399
} else if(is_data) {
@@ -130,13 +136,15 @@ class basic_meta_factory {
130136
*/
131137
template<typename Type>
132138
class meta_factory: private internal::basic_meta_factory {
139+
using base_type = internal::basic_meta_factory;
140+
133141
template<typename Setter, auto Getter, typename Policy, std::size_t... Index>
134142
void data(const id_type id, std::index_sequence<Index...>) noexcept {
135143
using data_type = std::invoke_result_t<decltype(Getter), Type &>;
136144
using args_type = type_list<typename meta_function_helper_t<Type, decltype(value_list_element_v<Index, Setter>)>::args_type...>;
137145
static_assert(Policy::template value<data_type>, "Invalid return type for the given policy");
138146

139-
this->extend(
147+
base_type::data(
140148
id,
141149
internal::meta_data_node{
142150
/* this is never static */
@@ -166,7 +174,7 @@ class meta_factory: private internal::basic_meta_factory {
166174
* @return A meta factory for the given type.
167175
*/
168176
meta_factory type(const id_type id) noexcept {
169-
this->track(id);
177+
base_type::type(id);
170178
return *this;
171179
}
172180

@@ -182,7 +190,7 @@ class meta_factory: private internal::basic_meta_factory {
182190
meta_factory base() noexcept {
183191
static_assert(!std::is_same_v<Type, Base> && std::is_base_of_v<Base, Type>, "Invalid base type");
184192
auto *const op = +[](const void *instance) noexcept { return static_cast<const void *>(static_cast<const Base *>(static_cast<const Type *>(instance))); };
185-
this->extend(type_id<Base>().hash(), internal::meta_base_node{&internal::resolve<Base>, op});
193+
base_type::base(type_id<Base>().hash(), internal::meta_base_node{&internal::resolve<Base>, op});
186194
return *this;
187195
}
188196

@@ -202,7 +210,7 @@ class meta_factory: private internal::basic_meta_factory {
202210
auto conv() noexcept {
203211
using conv_type = std::remove_cv_t<std::remove_reference_t<std::invoke_result_t<decltype(Candidate), Type &>>>;
204212
auto *const op = +[](const meta_ctx &area, const void *instance) { return forward_as_meta(area, std::invoke(Candidate, *static_cast<const Type *>(instance))); };
205-
this->extend(type_id<conv_type>().hash(), internal::meta_conv_node{op});
213+
base_type::conv(type_id<conv_type>().hash(), internal::meta_conv_node{op});
206214
return *this;
207215
}
208216

@@ -219,7 +227,7 @@ class meta_factory: private internal::basic_meta_factory {
219227
meta_factory conv() noexcept {
220228
using conv_type = std::remove_cv_t<std::remove_reference_t<To>>;
221229
auto *const op = +[](const meta_ctx &area, const void *instance) { return forward_as_meta(area, static_cast<To>(*static_cast<const Type *>(instance))); };
222-
this->extend(type_id<conv_type>().hash(), internal::meta_conv_node{op});
230+
base_type::conv(type_id<conv_type>().hash(), internal::meta_conv_node{op});
223231
return *this;
224232
}
225233

@@ -241,7 +249,7 @@ class meta_factory: private internal::basic_meta_factory {
241249
using descriptor = meta_function_helper_t<Type, decltype(Candidate)>;
242250
static_assert(Policy::template value<typename descriptor::return_type>, "Invalid return type for the given policy");
243251
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");
244-
this->extend(type_id<typename descriptor::args_type>().hash(), internal::meta_ctor_node{descriptor::args_type::size, &meta_arg<typename descriptor::args_type>, &meta_construct<Type, Candidate, Policy>});
252+
base_type::ctor(type_id<typename descriptor::args_type>().hash(), internal::meta_ctor_node{descriptor::args_type::size, &meta_arg<typename descriptor::args_type>, &meta_construct<Type, Candidate, Policy>});
245253
return *this;
246254
}
247255

@@ -260,7 +268,7 @@ class meta_factory: private internal::basic_meta_factory {
260268
// default constructor is already implicitly generated, no need for redundancy
261269
if constexpr(sizeof...(Args) != 0u) {
262270
using descriptor = meta_function_helper_t<Type, Type (*)(Args...)>;
263-
this->extend(type_id<typename descriptor::args_type>().hash(), internal::meta_ctor_node{descriptor::args_type::size, &meta_arg<typename descriptor::args_type>, &meta_construct<Type, Args...>});
271+
base_type::ctor(type_id<typename descriptor::args_type>().hash(), internal::meta_ctor_node{descriptor::args_type::size, &meta_arg<typename descriptor::args_type>, &meta_construct<Type, Args...>});
264272
}
265273

266274
return *this;
@@ -288,7 +296,7 @@ class meta_factory: private internal::basic_meta_factory {
288296
meta_factory dtor() noexcept {
289297
static_assert(std::is_invocable_v<decltype(Func), Type &>, "The function doesn't accept an object of the type provided");
290298
auto *const op = +[](void *instance) { std::invoke(Func, *static_cast<Type *>(instance)); };
291-
this->extend(internal::meta_dtor_node{op});
299+
base_type::dtor(internal::meta_dtor_node{op});
292300
return *this;
293301
}
294302

@@ -298,8 +306,7 @@ class meta_factory: private internal::basic_meta_factory {
298306
* @return A meta factory for the parent type.
299307
*/
300308
meta_factory data(const id_type id) noexcept {
301-
constexpr auto is_data = true;
302-
this->seek(id, is_data);
309+
base_type::data(id);
303310
return *this;
304311
}
305312

@@ -322,7 +329,7 @@ class meta_factory: private internal::basic_meta_factory {
322329
using data_type = std::invoke_result_t<decltype(Data), Type &>;
323330
static_assert(Policy::template value<data_type>, "Invalid return type for the given policy");
324331

325-
this->extend(
332+
base_type::data(
326333
id,
327334
internal::meta_data_node{
328335
/* this is never static */
@@ -341,7 +348,7 @@ class meta_factory: private internal::basic_meta_factory {
341348
static_assert(Policy::template value<data_type>, "Invalid return type for the given policy");
342349
}
343350

344-
this->extend(
351+
base_type::data(
345352
id,
346353
internal::meta_data_node{
347354
((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,
@@ -381,7 +388,7 @@ class meta_factory: private internal::basic_meta_factory {
381388
static_assert(Policy::template value<data_type>, "Invalid return type for the given policy");
382389

383390
if constexpr(std::is_same_v<decltype(Setter), std::nullptr_t>) {
384-
this->extend(
391+
base_type::data(
385392
id,
386393
internal::meta_data_node{
387394
/* this is never static */
@@ -394,7 +401,7 @@ class meta_factory: private internal::basic_meta_factory {
394401
} else {
395402
using args_type = typename meta_function_helper_t<Type, decltype(Setter)>::args_type;
396403

397-
this->extend(
404+
base_type::data(
398405
id,
399406
internal::meta_data_node{
400407
/* this is never static nor const */
@@ -438,8 +445,7 @@ class meta_factory: private internal::basic_meta_factory {
438445
* @return A meta factory for the parent type.
439446
*/
440447
meta_factory func(const id_type id) noexcept {
441-
constexpr auto is_data = false;
442-
this->seek(id, is_data);
448+
base_type::func(id);
443449
return *this;
444450
}
445451

@@ -461,7 +467,7 @@ class meta_factory: private internal::basic_meta_factory {
461467
using descriptor = meta_function_helper_t<Type, decltype(Candidate)>;
462468
static_assert(Policy::template value<typename descriptor::return_type>, "Invalid return type for the given policy");
463469

464-
this->extend(
470+
base_type::func(
465471
id,
466472
internal::meta_func_node{
467473
(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),
@@ -486,9 +492,9 @@ class meta_factory: private internal::basic_meta_factory {
486492
template<typename... Value>
487493
meta_factory prop(id_type id, [[maybe_unused]] Value &&...value) {
488494
if constexpr(sizeof...(Value) == 0u) {
489-
this->property(id, internal::meta_prop_node{&internal::resolve<void>});
495+
base_type::prop(id, internal::meta_prop_node{&internal::resolve<void>});
490496
} else {
491-
this->property(id, internal::meta_prop_node{&internal::resolve<std::decay_t<Value>>..., std::make_shared<std::decay_t<Value>>(std::forward<Value>(value))...});
497+
base_type::prop(id, internal::meta_prop_node{&internal::resolve<std::decay_t<Value>>..., std::make_shared<std::decay_t<Value>>(std::forward<Value>(value))...});
492498
}
493499

494500
return *this;

0 commit comments

Comments
 (0)