From 3bdd6d8e638ed19973b9df6a894e8b7966731b9c Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 13 Nov 2022 20:00:57 +0100 Subject: entity stuff --- src/entity.hpp | 42 +++++++++++++++++++++++++++++------------- test/entity.cpp | 43 ++++++++++++++++++++++--------------------- 2 files changed, 51 insertions(+), 34 deletions(-) diff --git a/src/entity.hpp b/src/entity.hpp index 7d55bfd6..b46fefcc 100644 --- a/src/entity.hpp +++ b/src/entity.hpp @@ -1,8 +1,10 @@ #pragma once #include "compat/integer-types.hpp" -#include #include -#include +#include +#include +#include +#include #include @@ -10,9 +12,13 @@ namespace floormat {} namespace floormat::entities { -template using const_qualified = std::conditional_t, T, const T&>; -template using ref_qualified = std::conditional_t, T, T&>; -template using move_qualified = std::conditional_t, T, T&&>; +template struct pass_by_value : std::bool_constant> {}; +template constexpr inline bool pass_by_value_v = pass_by_value::value; +template<> struct pass_by_value : std::true_type {}; + +template using const_qualified = std::conditional_t, T, const T&>; +template using ref_qualified = std::conditional_t, T, T&>; +template using move_qualified = std::conditional_t, T, T&&>; template concept FieldReader_memfn = requires(const T x, F f) { @@ -95,8 +101,8 @@ struct field { using Writer = W; StringView name; - Reader reader; - Writer writer; + [[no_unique_address]] Reader reader; + [[no_unique_address]] Writer writer; constexpr field(StringView name, Reader r, Writer w) noexcept : name{name}, reader{r}, writer{w} {} decltype(auto) read(const Obj& x) const { return read_field::read(x, reader); } @@ -104,16 +110,26 @@ struct field { }; template -struct entity { +struct entity final { template struct Field { template R, FieldWriter W> - struct make final : field { - consteval make(StringView name_, R r, W w) noexcept : field{name_, r, w} {} - }; - template R, FieldWriter W> - make(StringView name, R r, W w) -> make; + static consteval auto make(StringView name, R r, W w) noexcept { + return field { name, r, w }; + } }; }; +template +struct assoc final { + template using Types = std::tuple; + consteval assoc(Xs&&... xs) { + + } + +private: + template struct cell { Key key; T value; }; + std::tuple...> _tuple; +}; + } // namespace floormat::entities diff --git a/test/entity.cpp b/test/entity.cpp index ae50853c..6789a241 100644 --- a/test/entity.cpp +++ b/test/entity.cpp @@ -2,34 +2,32 @@ #include "compat/assert.hpp" #include "src/entity.hpp" +struct Test { + int foo = 111; + int bar() const { return _bar; } + void set_bar(int value) { _bar = value; } + int _baz = 333; +private: + int _bar = 222; +}; + namespace floormat { using namespace floormat::entities; -void test_app::test_entity() +static void test_accessors() { - struct Test { - int foo = 111; - int bar() const { return _bar; } - void set_bar(int value) { _bar = value; } - int _baz = 333; - private: - int _bar = 222; - }; - using Entity = entity; - constexpr const auto m_foo = Entity::Field::make{ "foo"_s, &Test::foo, &Test::foo, }; - constexpr const auto m_bar = Entity::Field::make{ "bar"_s, &Test::bar, &Test::set_bar, }; - constexpr const auto r_baz = [](const Test& x) { return x._baz; }; - constexpr const auto w_baz = [](Test& x, int v) { x._baz = v; }; - constexpr const auto m_baz = Entity::Field::make{ "baz"_s, r_baz, w_baz }; + constexpr auto m_foo = Entity::Field::make("foo"_s, &Test::foo, &Test::foo); + constexpr auto m_bar = Entity::Field::make("bar"_s, &Test::bar, &Test::set_bar); + constexpr auto r_baz = [](const Test& x) { return x._baz; }; + constexpr auto w_baz = [](Test& x, int v) { x._baz = v; }; + constexpr auto m_baz = Entity::Field::make("baz"_s, r_baz, w_baz); auto x = Test{}; { - auto a = m_foo.read(x); - auto b = m_bar.read(x); - auto c = m_baz.read(x); + auto a = m_foo.read(x), b = m_bar.read(x), c = m_baz.read(x); fm_assert(a == 111 && b == 222 && c == 333); } @@ -37,11 +35,14 @@ void test_app::test_entity() m_foo.write(x, 1111); m_bar.write(x, 2222); m_baz.write(x, 3333); - auto a = m_foo.read(x); - auto b = m_bar.read(x); - auto c = m_baz.read(x); + auto a = m_foo.read(x), b = m_bar.read(x), c = m_baz.read(x); fm_assert(a == 1111 && b == 2222 && c == 3333); } } +void test_app::test_entity() +{ + test_accessors(); +} + } // namespace floormat -- cgit v1.2.3