From ba000273212cbfc26fb7fe9459d6c3814ec88a2d Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Fri, 18 Nov 2022 08:15:48 +0100 Subject: entity: accessors now have a static lifetime --- src/entity.hpp | 182 ++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 129 insertions(+), 53 deletions(-) (limited to 'src') diff --git a/src/entity.hpp b/src/entity.hpp index f1c38397..573529b1 100644 --- a/src/entity.hpp +++ b/src/entity.hpp @@ -6,10 +6,21 @@ #include #include #include +#include #include #include -namespace floormat {} +#if defined _MSC_VER +#define FM_PRETTY_FUNCTION __FUNCSIG__ +#else +#define FM_PRETTY_FUNCTION __PRETTY_FUNCTION__ +#endif + +namespace floormat::entities::detail { template static constexpr StringView typename_of_(); } + +namespace floormat { +template constexpr inline StringView typename_of = entities::detail::typename_of_(); +} // namespace floormat namespace floormat::entities { @@ -68,6 +79,14 @@ concept FieldWriter = requires { namespace detail { +template +static constexpr StringView typename_of_() { + using namespace Corrade::Containers; + using SVF = StringViewFlag; + constexpr const char* str = FM_PRETTY_FUNCTION; + return StringView { str, Implementation::strlen_(str), SVF::Global|SVF::NullTerminated }; +} + template R> struct read_field { static constexpr Type read(const Obj& x, R r) { return r(x); } @@ -137,72 +156,109 @@ constexpr CORRADE_ALWAYS_INLINE bool find_in_tuple(F&& fun, Tuple&& tuple) return false; } +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 +using accessors_for = typename accessors_for_::type; + } // namespace detail -struct erased_accessors final { - struct erased_reader_t; - struct erased_writer_t; +struct erased_accessor final { + using erased_reader_t = void; + using erased_writer_t = void; + using Object = void; + using Value = void; + const erased_reader_t* reader; const erased_writer_t* writer; - const char *object_type, *field_type; - void(*read_fun)(const void*, const erased_reader_t*, void*); - void(*write_fun)(void*, const erased_writer_t*, void*); + StringView object_name, type_name; + void(*read_fun)(const Object*, const erased_reader_t*, Value*); + void(*write_fun)(Object*, const erased_writer_t*, Value*); + + constexpr erased_accessor(const erased_accessor&) = default; + constexpr erased_accessor(erased_reader_t* reader, erased_writer_t * writer, + StringView object_name, StringView type_name, + void(*read_fun)(const Object*, const erased_reader_t*, Value*), + void(*write_fun)(Object*, const erased_writer_t*, Value*)) : + reader{reader}, writer{writer}, + object_name{object_name}, type_name{type_name}, + read_fun{read_fun}, write_fun{write_fun} + {} }; -struct EntityBase {}; +template struct entity_field_base {}; + +template R, FieldWriter W> +struct entity_field : entity_field_base { + using ObjectType = Obj; + using FieldType = Type; + using Reader = R; + using Writer = W; + + StringView name; + [[no_unique_address]] R reader; + [[no_unique_address]] W writer; + + constexpr entity_field(const entity_field&) = default; + constexpr entity_field& operator=(const entity_field&) = default; + static constexpr decltype(auto) read(const R& reader, const Obj& x) { return detail::read_field::read(x, reader); } + static constexpr void write(const W& writer, Obj& x, move_qualified v) { detail::write_field::write(x, writer, v); } + constexpr decltype(auto) read(const Obj& x) const { return read(reader, x); } + constexpr void write(Obj& x, move_qualified value) const { write(writer, x, value); } + constexpr entity_field(StringView name, R r, W w) noexcept : name{name}, reader{r}, writer{w} {} + + constexpr erased_accessor erased() const { + using reader_t = typename erased_accessor::erased_reader_t; + using writer_t = typename erased_accessor::erased_writer_t; + constexpr auto obj_name = typename_of, field_name = typename_of; + + constexpr auto reader_fn = [](const void* obj, const reader_t* reader, void* value) + { + const auto& obj_ = *reinterpret_cast(obj); + const auto& reader_ = *reinterpret_cast(reader); + auto& value_ = *reinterpret_cast(value); + value_ = read(reader_, obj_); + }; + constexpr auto writer_fn = [](void* obj, const writer_t* writer, void* value) + { + auto& obj_ = *reinterpret_cast(obj); + const auto& writer_ = *reinterpret_cast(writer); + move_qualified value_ = std::move(*reinterpret_cast(value)); + write(writer_, obj_, value_); + }; + return erased_accessor{ + (void*)&reader, (void*)&writer, + obj_name, field_name, + reader_fn, writer_fn, + }; + } +}; template -struct Entity final : EntityBase { +struct Entity final { static_assert(std::is_same_v>); - struct type_base {}; - template - struct type final : type_base + struct type final { - static_assert(std::is_same_v>); - struct field_base {}; - template R, FieldWriter W> - struct field final : field_base + struct field final : entity_field { - using ObjectType = Obj; - using FieldType = Type; - using Reader = R; - using Writer = W; - - StringView name; - [[no_unique_address]] R reader; - [[no_unique_address]] W writer; - - constexpr field(const field&) = default; - constexpr field& operator=(const field&) = default; - static constexpr decltype(auto) read(const R& reader, const Obj& x) { return detail::read_field::read(x, reader); } - static constexpr void write(const W& writer, Obj& x, move_qualified v) { detail::write_field::write(x, writer, v); } - constexpr decltype(auto) read(const Obj& x) const { return read(reader, x); } - constexpr void write(Obj& x, move_qualified value) const { write(writer, x, value); } - consteval field(StringView name, R r, W w) noexcept : name{name}, reader{r}, writer{w} {} - - erased_accessors accessors() const { - using reader_t = typename erased_accessors::erased_reader_t; - using writer_t = typename erased_accessors::erased_writer_t; - return erased_accessors { - reinterpret_cast(&reader), reinterpret_cast(&writer), - typeid(Obj).name(), typeid(Type).name(), - [](const void* obj, const reader_t* reader, void* value) { - const auto& obj_ = *reinterpret_cast(obj); - const auto& reader_ = *reinterpret_cast(reader); - auto& value_ = *reinterpret_cast(value); - value_ = read(reader_, obj_); - }, - [](void* obj, const writer_t* writer, void* value) { - auto& obj_ = *reinterpret_cast(obj); - const auto& writer_ = *reinterpret_cast(writer); - auto&& value_ = std::move(*reinterpret_cast(value)); - write(writer_, obj_, value_); - }, - }; - } + constexpr field(StringView field_name, R r, W w) noexcept : + entity_field{field_name, r, w} + {} }; template R, FieldWriter W> @@ -255,3 +311,23 @@ FM_ERASED_FIELD_TYPE(StringView, string); #undef FM_ERASED_FIELD_TYPE } // namespace floormat::entities + +namespace floormat { + +template +requires std::is_same_v> +class entity_metadata final { + template + static constexpr auto erased_helper(const std::tuple& tuple) + { + std::array array { std::get(tuple).erased()..., }; + return array; + } +public: + static constexpr StringView class_name = typename_of; + static constexpr std::size_t size = std::tuple_size_v>; + static constexpr entities::detail::accessors_for accessors = T::accessors(); + static constexpr auto erased_accessors = erased_helper(accessors); +}; + +} // namespace floormat -- cgit v1.2.3