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. --- test/entity.cpp | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) (limited to 'test') 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