From b147a0daa1be2c5e7faea33f500e7f22541c12a0 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Fri, 25 Aug 2023 19:28:32 +0200 Subject: entities: add intents for metadata It's going to be used for serialization which has different fields. --- editor/inspect-types.cpp | 34 +++++++++++++++++----------------- entity/metadata.hpp | 14 +++++++++----- 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 { +struct entity_accessors { static constexpr auto accessors() { using E = Entity; @@ -67,11 +67,11 @@ struct entity_accessors { }; template<> -struct entity_accessors { +struct entity_accessors { static constexpr auto accessors() { using E = Entity; - auto tuple0 = entity_accessors::accessors(); + auto tuple0 = entity_accessors::accessors(); auto tuple = std::tuple{ E::type::field{"interactive"_s, [](const scenery& x) { return x.interactive; }, @@ -153,15 +153,15 @@ struct enum_values } }; -template -static bool inspect_type(T& x) +template +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::accessors); + }, entity_metadata::accessors); bool ret = false; visit_tuple([&](const auto& field) { @@ -169,16 +169,16 @@ static bool inspect_type(T& x) using enum_type = enum_values; const auto& list = enum_type::get(x); ret |= inspect_field(&x, field.erased(), list, width); - }, entity_metadata::accessors); + }, entity_metadata::accessors); return ret; } template<> -struct entity_accessors { +struct entity_accessors { static constexpr auto accessors() { using E = Entity; - auto tuple0 = entity_accessors::accessors(); + auto tuple0 = entity_accessors::accessors(); auto tuple = std::tuple{ E::type::field{"name"_s, [](const character& x) { return x.name; }, @@ -204,12 +204,12 @@ template struct enum_values }; template<> -struct entity_accessors +struct entity_accessors { static constexpr auto accessors() { using E = Entity; - auto tuple0 = entity_accessors::accessors(); + auto tuple0 = entity_accessors::accessors(); auto tuple = std::tuple{ E::type::field{"color"_s, [](const light& x) { return x.color; }, @@ -234,9 +234,9 @@ struct entity_accessors }; //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(x)); - case entity_type::character: return inspect_type(static_cast(x)); - case entity_type::light: return inspect_type(static_cast(x)); + case entity_type::scenery: return inspect_type(static_cast(x), inspect_intent_t{}); + case entity_type::character: return inspect_type(static_cast(x), inspect_intent_t{}); + case entity_type::light: return inspect_type(static_cast(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 struct entity_accessors; +struct inspect_intent_t {}; +struct serialize_intent_t {}; +struct report_intent_t {}; + +template struct entity_accessors; } // namespace floormat::entities @@ -248,7 +252,7 @@ constexpr bool find_in_tuple(F&& fun, Tuple&& tuple) namespace floormat { -template +template class entity_metadata final { static_assert(std::is_same_v>); @@ -257,14 +261,14 @@ class entity_metadata final { public: static constexpr StringView class_name = name_of; - static constexpr auto accessors = entities::entity_accessors::accessors(); + static constexpr auto accessors = entities::entity_accessors::accessors(); static constexpr size_t size = std::tuple_size_v>; static constexpr auto erased_accessors = erased_helper(accessors, std::make_index_sequence{}); }; -template +template template -consteval auto entity_metadata::erased_helper(const Tuple& tuple, std::index_sequence) +consteval auto entity_metadata::erased_helper(const Tuple& tuple, std::index_sequence) { std::array array { std::get(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 { +template<> struct entity_accessors { static constexpr auto accessors() { using entity = Entity; @@ -126,7 +126,7 @@ void test_erasure() { void test_metadata() { - constexpr auto m = entity_metadata(); + constexpr auto m = entity_metadata(); static_assert(sizeof m == 1); fm_assert(m.class_name == name_of); fm_assert(m.class_name.contains("TestAccessors"_s)); @@ -160,7 +160,7 @@ void test_type_name() constexpr auto foo = entity::type::field{"foo"_s, &TestAccessors::foo, nullptr}; static_assert(foo.writer == nullptr); static_assert(!foo.can_write); - static_assert(std::get<0>(entity_accessors::accessors()).can_write); + static_assert(std::get<0>(entity_accessors::accessors()).can_write); } void test_predicate() @@ -185,7 +185,7 @@ void test_predicate() constexpr bool test_names() { - constexpr auto m = entity_metadata(); + constexpr auto m = entity_metadata(); auto [foo1, bar1, baz1] = m.accessors; auto [foo2, bar2, baz2] = m.erased_accessors; -- cgit v1.2.3