#pragma once #include "name-of.hpp" #include "accessor.hpp" #include "util.hpp" #include #include #include #include #include #include #include #include namespace floormat::entities { template concept FieldReader_memfn = requires(const T x, F f) { { (x.*f)() } -> std::convertible_to; }; template concept FieldReader_ptr = requires(const T x, F f) { { x.*f } -> std::convertible_to; }; template concept FieldReader_function = requires(const T x, F f) { { f(x) } -> std::convertible_to; }; template concept FieldReader = requires { requires FieldReader_memfn || FieldReader_ptr || FieldReader_function; }; template concept FieldWriter_memfn = requires(T x, move_qualified value, F f) { { (x.*f)(value) } -> std::same_as; }; template concept FieldWriter_ptr = requires(T x, move_qualified value, F f) { { x.*f = value }; }; template concept FieldWriter_function = requires(T x, move_qualified value, F f) { { f(x, value) } -> std::same_as; }; template concept FieldWriter = requires { requires FieldWriter_memfn || FieldWriter_ptr || FieldWriter_function || std::same_as; }; namespace detail { template R> struct read_field { static constexpr Type read(const Obj& x, R r) { return r(x); } }; template struct read_field { static constexpr Type read(const Obj& x, Type (Obj::*r)() const) { return (x.*r)(); } }; template struct read_field { static constexpr Type read(const Obj& x, Type Obj::*r) { return x.*r; } }; template struct read_field) const>> { template static constexpr Type read(const Obj& x, F&& fun) { return fun(x); } }; template W> struct write_field { static constexpr void write(Obj& x, W w, move_qualified value) { w(x, value); } }; template struct write_field)> { static constexpr void write(Obj& x, void(Obj::*w)(move_qualified), move_qualified value) { (x.*w)(value); } }; template struct write_field { static constexpr void write(Obj& x, FieldType Obj::* w, move_qualified value) { x.*w = value; } }; template struct write_field) const>> { template static constexpr void write(Obj& x, F&& fun, move_qualified value) { fun(x, value); } }; template struct write_field { static constexpr void write(Obj&, std::nullptr_t, move_qualified) { fm_abort("no writing for this accessor"); } }; template requires std::invocable(std::declval()))> constexpr CORRADE_ALWAYS_INLINE void visit_tuple(F&& fun, Tuple&& tuple) { using Size = std::tuple_size>; static_assert(N < Size()); fun(std::get(tuple)); if constexpr(N+1 < Size()) visit_tuple(std::forward(fun), std::forward(tuple)); } template requires std::is_invocable_r_v(std::declval()))> constexpr CORRADE_ALWAYS_INLINE bool find_in_tuple(F&& fun, Tuple&& tuple) { using Size = std::tuple_size>; static_assert(N < Size()); if (fun(std::get(tuple))) return true; if constexpr(N+1 < Size()) return find_in_tuple(std::forward(fun), std::forward(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 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); 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); } static constexpr bool can_write = !std::is_same_v::writer)>; constexpr entity_field(StringView name, R r, W w) noexcept : name{name}, reader{r}, writer{w} {} constexpr erased_accessor erased() const; }; template R, FieldWriter W> constexpr void entity_field::write(const W& writer, Obj& x, move_qualified v) { static_assert(can_write); detail::write_field::write(x, writer, v); } template R, FieldWriter W> constexpr erased_accessor entity_field::erased() const { using reader_t = typename erased_accessor::erased_reader_t; using writer_t = typename erased_accessor::erased_writer_t; constexpr auto obj_name = name_of, field_name = name_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_); }; constexpr auto writer_stub_fn = [](void*, const writer_t*, void*) { fm_abort("no writer for this accessor"); }; return erased_accessor { (void*)&reader, writer ? (void*)&writer : nullptr, name, obj_name, field_name, reader_fn, writer ? writer_fn : writer_stub_fn, }; } template struct Entity final { static_assert(std::is_same_v>); template struct type final { template R, FieldWriter W> struct field final : entity_field { constexpr field(StringView field_name, R r, W w) noexcept : entity_field{field_name, r, w} {} }; template R, FieldWriter W> field(StringView name, R r, W w) -> field; }; }; template constexpr void visit_tuple(F&& fun, Tuple&& tuple) { using Size = std::tuple_size>; if constexpr(Size() > 0) detail::visit_tuple(std::forward(fun), std::forward(tuple)); } template constexpr bool find_in_tuple(F&& fun, Tuple&& tuple) { using Size = std::tuple_size>; if constexpr(Size() > 0) return detail::find_in_tuple(std::forward(fun), std::forward(tuple)); else return false; } } // namespace floormat::entities namespace floormat { template requires std::is_same_v> class entity_metadata final { template static consteval auto erased_helper(const std::tuple& tuple) { std::array array { std::get(tuple).erased()..., }; return array; } 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 auto erased_accessors = erased_helper(accessors); }; } // namespace floormat