From cb665e2f6a829285f37abc72d03e434707f5af0d Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sat, 19 Nov 2022 18:31:08 +0100 Subject: entity: get field data indirectly via a specialization so now classes will be able to #ifdef including `entity/metadata.hpp` and specifying the fields without incurring an ODR violation. --- entity/metadata.hpp | 10 ++++++++-- test/entity.cpp | 42 +++++++++++++++++++++++++----------------- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/entity/metadata.hpp b/entity/metadata.hpp index 935f1792..0014f63c 100644 --- a/entity/metadata.hpp +++ b/entity/metadata.hpp @@ -15,6 +15,12 @@ #include #include +namespace floormat::entities { + +template struct entity_accessors; + +} // namespace floormat::entities + namespace floormat::entities::detail { template @@ -46,7 +52,7 @@ constexpr CORRADE_ALWAYS_INLINE bool find_in_tuple(F&& fun, Tuple&& tuple) template struct decay_tuple_; template struct decay_tuple_> { using type = std::tuple...>; }; template using decay_tuple = typename decay_tuple_::type; -template struct accessors_for_ { using type = decay_tuple>; }; +template struct accessors_for_ { using type = decay_tuple::accessors())>>; }; template using accessors_for = typename accessors_for_::type; template struct find_reader; @@ -269,7 +275,7 @@ class entity_metadata final { public: static constexpr StringView class_name = name_of; static constexpr std::size_t size = std::tuple_size_v>; - static constexpr entities::detail::accessors_for accessors = T::accessors(); + static constexpr entities::detail::accessors_for accessors = entities::entity_accessors::accessors(); static constexpr auto erased_accessors = erased_helper(accessors); }; diff --git a/test/entity.cpp b/test/entity.cpp index a10cbb98..ce7af58b 100644 --- a/test/entity.cpp +++ b/test/entity.cpp @@ -15,23 +15,31 @@ struct TestAccessors { int foo; int _bar; int _baz; +}; + +} // namespace - static constexpr auto accessors() noexcept; +namespace floormat::entities { + +template<> struct entity_accessors { + static constexpr auto accessors() + { + using entity = Entity; + constexpr auto r_baz = [](const TestAccessors& x) { return x._baz; }; + constexpr auto w_baz = [](TestAccessors& x, int v) { x._baz = v; }; + + constexpr auto tuple = std::make_tuple( + entity::type::field{"foo"_s, &TestAccessors::foo, &TestAccessors::foo}, + entity::type::field{"bar"_s, &TestAccessors::bar, &TestAccessors::set_bar}, + entity::type::field("baz"_s, r_baz, w_baz) + ); + return tuple; + } }; -constexpr auto TestAccessors::accessors() noexcept -{ - using entity = Entity; - constexpr auto r_baz = [](const TestAccessors& x) { return x._baz; }; - constexpr auto w_baz = [](TestAccessors& x, int v) { x._baz = v; }; - - constexpr auto tuple = std::make_tuple( - entity::type::field{"foo"_s, &TestAccessors::foo, &TestAccessors::foo}, - entity::type::field{"bar"_s, &TestAccessors::bar, &TestAccessors::set_bar}, - entity::type::field("baz"_s, r_baz, w_baz) - ); - return tuple; -} +} // namespace floormat::entities + +namespace { using entity = Entity; constexpr auto m_foo = entity::type::field{"foo"_s, &TestAccessors::foo, &TestAccessors::foo}; @@ -151,7 +159,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>(TestAccessors::accessors()).can_write); + static_assert(std::get<0>(entity_accessors::accessors()).can_write); } void test_predicate() @@ -189,7 +197,7 @@ constexpr bool test_names() constexpr void test_constraints() { constexpr auto x = TestAccessors{}; - constexpr auto foo = entity::type::field{ + constexpr auto foo = entity::type::field { "foo"_s, &TestAccessors::foo, &TestAccessors::foo, constantly(constraints::max_length{42}), constantly(constraints::range{37, 42}), @@ -221,8 +229,8 @@ void test_erased_constraints() constantly(constraints::range{37, 42}), constantly(constraints::group{"foo"_s}) }; + static constexpr auto erased = foo.erased(); const auto x = TestAccessors{}; - const auto erased = foo.erased(); fm_assert(erased.get_range(x) == constraints::range{37, 42}); fm_assert(erased.get_max_length(x) == 42); -- cgit v1.2.3