meta: fix an issue with meta_factory<T>::prop when using tuple of properties

This commit is contained in:
Michele Caini
2021-11-18 15:44:12 +01:00
parent 97500dff57
commit 0b39ece990
2 changed files with 18 additions and 5 deletions

View File

@@ -46,7 +46,7 @@ class meta_factory<Type, Spec...>: public meta_factory<Type> {
template<std::size_t Step = 0, typename... Property, typename... Other>
void unroll(choice_t<2>, std::tuple<Property...> property, Other &&...other) {
std::apply([this](auto &&...curr) { (unroll<Step>(choice<2>, std::forward<Property>(curr)), ...); }, property);
std::apply([this](auto &&...curr) { (unroll<Step>(choice<2>, std::forward<Property>(curr)...)); }, property);
unroll<Step + sizeof...(Property)>(choice<2>, std::forward<Other>(other)...);
}
@@ -104,7 +104,7 @@ public:
template<typename PropertyOrKey, typename... Value>
meta_factory<Type> prop(PropertyOrKey &&property_or_key, Value &&...value) {
if constexpr(sizeof...(Value) == 0) {
unroll(choice<3>, std::forward<PropertyOrKey>(property_or_key));
unroll(choice<2>, std::forward<PropertyOrKey>(property_or_key));
} else {
assign(std::forward<PropertyOrKey>(property_or_key), std::forward<Value>(value)...);
}
@@ -123,7 +123,7 @@ public:
*/
template<typename... Property>
meta_factory<Type> props(Property... property) {
unroll(choice<3>, std::forward<Property>(property)...);
unroll(choice<2>, std::forward<Property>(property)...);
return {};
}

View File

@@ -1,3 +1,4 @@
#include <tuple>
#include <string>
#include <gtest/gtest.h>
#include <entt/core/hashed_string.hpp>
@@ -7,7 +8,8 @@
struct base_1_t {};
struct base_2_t {};
struct derived_t: base_1_t, base_2_t {};
struct base_3_t {};
struct derived_t: base_1_t, base_2_t, base_3_t {};
struct MetaProp: ::testing::Test {
void SetUp() override {
@@ -21,10 +23,15 @@ struct MetaProp: ::testing::Test {
.type("base_2"_hs)
.props(std::make_pair("bool"_hs, false), std::make_pair("char[]"_hs, "char[]"));
entt::meta<base_3_t>()
.type("base_3"_hs)
.prop(std::make_tuple("key_only"_hs, std::make_pair("key"_hs, 42)));
entt::meta<derived_t>()
.type("derived"_hs)
.base<base_1_t>()
.base<base_2_t>();
.base<base_2_t>()
.base<base_3_t>();
}
void TearDown() override {
@@ -48,12 +55,18 @@ TEST_F(MetaProp, FromBase) {
auto type = entt::resolve<derived_t>();
auto prop_bool = type.prop("bool"_hs);
auto prop_int = type.prop("int"_hs);
auto key_only = type.prop("key_only"_hs);
auto key_value = type.prop("key"_hs);
ASSERT_TRUE(prop_bool);
ASSERT_TRUE(prop_int);
ASSERT_TRUE(key_only);
ASSERT_TRUE(key_value);
ASSERT_FALSE(prop_bool.value().cast<bool>());
ASSERT_EQ(prop_int.value().cast<int>(), 42);
ASSERT_FALSE(key_only.value());
ASSERT_EQ(key_value.value().cast<int>(), 42);
}
TEST_F(MetaProp, DeducedArrayType) {