diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2023-08-25 19:28:32 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2023-08-25 19:28:32 +0200 |
commit | b147a0daa1be2c5e7faea33f500e7f22541c12a0 (patch) | |
tree | e24f5b4a7d3810b4d306d2727e621bd2d9848358 | |
parent | 1ef440478f7861d7d399fa570df950b02626c46e (diff) |
entities: add intents for metadata
It's going to be used for serialization which has different fields.
-rw-r--r-- | editor/inspect-types.cpp | 34 | ||||
-rw-r--r-- | entity/metadata.hpp | 14 | ||||
-rw-r--r-- | test/entity.cpp | 8 |
3 files changed, 30 insertions, 26 deletions
diff --git a/editor/inspect-types.cpp b/editor/inspect-types.cpp index fc60992e..e29d5df9 100644 --- a/editor/inspect-types.cpp +++ b/editor/inspect-types.cpp @@ -16,7 +16,7 @@ namespace floormat::entities { using st = field_status; template<> -struct entity_accessors<entity> { +struct entity_accessors<entity, inspect_intent_t> { static constexpr auto accessors() { using E = Entity<entity>; @@ -67,11 +67,11 @@ struct entity_accessors<entity> { }; template<> -struct entity_accessors<scenery> { +struct entity_accessors<scenery, inspect_intent_t> { static constexpr auto accessors() { using E = Entity<scenery>; - auto tuple0 = entity_accessors<entity>::accessors(); + auto tuple0 = entity_accessors<entity, inspect_intent_t>::accessors(); auto tuple = std::tuple{ E::type<bool>::field{"interactive"_s, [](const scenery& x) { return x.interactive; }, @@ -153,15 +153,15 @@ struct enum_values<rotation, U> } }; -template<typename T> -static bool inspect_type(T& x) +template<typename T, typename Intent> +static bool inspect_type(T& x, Intent) { size_t width = 0; visit_tuple([&](const auto& field) { const auto& name = field.name; auto width_ = (size_t)ImGui::CalcTextSize(name.cbegin(), name.cend()).x; width = std::max(width, width_); - }, entity_metadata<T>::accessors); + }, entity_metadata<T, Intent>::accessors); bool ret = false; visit_tuple([&](const auto& field) { @@ -169,16 +169,16 @@ static bool inspect_type(T& x) using enum_type = enum_values<type, T>; const auto& list = enum_type::get(x); ret |= inspect_field<type>(&x, field.erased(), list, width); - }, entity_metadata<T>::accessors); + }, entity_metadata<T, Intent>::accessors); return ret; } template<> -struct entity_accessors<character> { +struct entity_accessors<character, inspect_intent_t> { static constexpr auto accessors() { using E = Entity<character>; - auto tuple0 = entity_accessors<entity>::accessors(); + auto tuple0 = entity_accessors<entity, inspect_intent_t>::accessors(); auto tuple = std::tuple{ E::type<String>::field{"name"_s, [](const character& x) { return x.name; }, @@ -204,12 +204,12 @@ template<typename U> struct enum_values<light_falloff, U> }; template<> -struct entity_accessors<light> +struct entity_accessors<light, inspect_intent_t> { static constexpr auto accessors() { using E = Entity<light>; - auto tuple0 = entity_accessors<entity>::accessors(); + auto tuple0 = entity_accessors<entity, inspect_intent_t>::accessors(); auto tuple = std::tuple{ E::type<Color4ub>::field{"color"_s, [](const light& x) { return x.color; }, @@ -234,9 +234,9 @@ struct entity_accessors<light> }; //template bool inspect_type(entity&); -template bool inspect_type(scenery&); -template bool inspect_type(character&); -template bool inspect_type(light&); +template bool inspect_type(scenery&, inspect_intent_t); +template bool inspect_type(character&, inspect_intent_t); +template bool inspect_type(light&, inspect_intent_t); bool inspect_entity_subtype(entity& x) { @@ -244,9 +244,9 @@ bool inspect_entity_subtype(entity& x) { default: fm_warn_once("unknown entity subtype '%d'", (int)type); return false; //case entity_type::none: return inspect_type(x); - case entity_type::scenery: return inspect_type(static_cast<scenery&>(x)); - case entity_type::character: return inspect_type(static_cast<character&>(x)); - case entity_type::light: return inspect_type(static_cast<light&>(x)); + case entity_type::scenery: return inspect_type(static_cast<scenery&>(x), inspect_intent_t{}); + case entity_type::character: return inspect_type(static_cast<character&>(x), inspect_intent_t{}); + case entity_type::light: return inspect_type(static_cast<light&>(x), inspect_intent_t{}); } } diff --git a/entity/metadata.hpp b/entity/metadata.hpp index 1902091f..dd82ce8a 100644 --- a/entity/metadata.hpp +++ b/entity/metadata.hpp @@ -17,7 +17,11 @@ namespace floormat::entities { -template<typename T> struct entity_accessors; +struct inspect_intent_t {}; +struct serialize_intent_t {}; +struct report_intent_t {}; + +template<typename T, typename Intent> struct entity_accessors; } // namespace floormat::entities @@ -248,7 +252,7 @@ constexpr bool find_in_tuple(F&& fun, Tuple&& tuple) namespace floormat { -template<typename T> +template<typename T, typename Intent> class entity_metadata final { static_assert(std::is_same_v<T, std::decay_t<T>>); @@ -257,14 +261,14 @@ class entity_metadata final { public: static constexpr StringView class_name = name_of<T>; - static constexpr auto accessors = entities::entity_accessors<T>::accessors(); + static constexpr auto accessors = entities::entity_accessors<T, Intent>::accessors(); static constexpr size_t size = std::tuple_size_v<std::decay_t<decltype(accessors)>>; static constexpr auto erased_accessors = erased_helper(accessors, std::make_index_sequence<size>{}); }; -template<typename T> +template<typename T, typename Intent> template<typename Tuple, std::size_t... Ns> -consteval auto entity_metadata<T>::erased_helper(const Tuple& tuple, std::index_sequence<Ns...>) +consteval auto entity_metadata<T, Intent>::erased_helper(const Tuple& tuple, std::index_sequence<Ns...>) { std::array<entities::erased_accessor, sizeof...(Ns)> array { std::get<Ns>(tuple).erased()..., }; return array; diff --git a/test/entity.cpp b/test/entity.cpp index 1ae77129..3c099a37 100644 --- a/test/entity.cpp +++ b/test/entity.cpp @@ -20,7 +20,7 @@ struct TestAccessors { namespace floormat::entities { -template<> struct entity_accessors<TestAccessors> { +template<> struct entity_accessors<TestAccessors, inspect_intent_t> { static constexpr auto accessors() { using entity = Entity<TestAccessors>; @@ -126,7 +126,7 @@ void test_erasure() { void test_metadata() { - constexpr auto m = entity_metadata<TestAccessors>(); + constexpr auto m = entity_metadata<TestAccessors, inspect_intent_t>(); static_assert(sizeof m == 1); fm_assert(m.class_name == name_of<TestAccessors>); fm_assert(m.class_name.contains("TestAccessors"_s)); @@ -160,7 +160,7 @@ void test_type_name() constexpr auto foo = entity::type<int>::field{"foo"_s, &TestAccessors::foo, nullptr}; static_assert(foo.writer == nullptr); static_assert(!foo.can_write); - static_assert(std::get<0>(entity_accessors<TestAccessors>::accessors()).can_write); + static_assert(std::get<0>(entity_accessors<TestAccessors, inspect_intent_t>::accessors()).can_write); } void test_predicate() @@ -185,7 +185,7 @@ void test_predicate() constexpr bool test_names() { - constexpr auto m = entity_metadata<TestAccessors>(); + constexpr auto m = entity_metadata<TestAccessors, inspect_intent_t>(); auto [foo1, bar1, baz1] = m.accessors; auto [foo2, bar2, baz2] = m.erased_accessors; |