diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2022-11-18 08:15:48 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-11-18 08:27:27 +0100 |
commit | ba000273212cbfc26fb7fe9459d6c3814ec88a2d (patch) | |
tree | 20f8a15e94f1dec9d2369e5b3a5a66700a9eb402 /test | |
parent | 9010c9defb81e2659af53decd811376aafd00d77 (diff) |
entity: accessors now have a static lifetime
Diffstat (limited to 'test')
-rw-r--r-- | test/entity.cpp | 49 |
1 files changed, 45 insertions, 4 deletions
diff --git a/test/entity.cpp b/test/entity.cpp index 1bfed35b..46c71c56 100644 --- a/test/entity.cpp +++ b/test/entity.cpp @@ -12,8 +12,24 @@ struct TestAccessors { int foo; int _bar; int _baz; + + static constexpr auto accessors() noexcept; }; +constexpr auto TestAccessors::accessors() noexcept +{ + using entity = Entity<TestAccessors>; + 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<int>::field{"foo"_s, &TestAccessors::foo, &TestAccessors::foo}, + entity::type<int>::field{"bar"_s, &TestAccessors::bar, &TestAccessors::set_bar}, + entity::type<int>::field("baz"_s, r_baz, w_baz) + ); + return tuple; +} + using entity = Entity<TestAccessors>; static constexpr auto m_foo = entity::type<int>::field{"foo"_s, &TestAccessors::foo, &TestAccessors::foo}; static constexpr auto m_bar = entity::type<int>::field{"bar"_s, &TestAccessors::bar, &TestAccessors::set_bar}; @@ -78,10 +94,10 @@ static void test_fun2() { } static void test_erasure() { - erased_accessors accessors[] = { - m_foo.accessors(), - m_bar.accessors(), - m_baz.accessors(), + erased_accessor accessors[] = { + m_foo.erased(), + m_bar.erased(), + m_baz.erased(), }; auto obj = TestAccessors{1, 2, 3}; int value = 0; @@ -93,12 +109,37 @@ static void test_erasure() { fm_assert(value2 == value2_); } +static void test_metadata() +{ + constexpr auto m = entity_metadata<TestAccessors>(); + fm_assert(m.class_name == typename_of<TestAccessors>); + fm_assert(m.class_name.contains("TestAccessors"_s)); + const auto [foo, bar, baz] = m.accessors; + const auto [foo2, bar2, baz2] = m.erased_accessors; + TestAccessors x{0, 0, 0}; + foo.write(x, 1); + fm_assert(x.foo == 1); + int bar_ = 2; + bar2.write_fun(&x, bar2.writer, &bar_); + fm_assert(x.bar() == 2); +} + +static void test_type_name() +{ + using namespace entities; + struct foobar; + constexpr StringView name = typename_of<foobar>; + fm_assert(name.contains("foobar"_s)); +} + void test_app::test_entity() { static_assert(test_accessors()); static_assert(test_visitor()); test_fun2(); test_erasure(); + test_type_name(); + test_metadata(); } } // namespace floormat |