From 50d4d02508767ed3cb67bf1ed424c61c23e44117 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Fri, 18 Nov 2022 12:04:26 +0100 Subject: entity: add read-only property support --- entity/accessor.hpp | 1 + entity/entity.hpp | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) (limited to 'entity') diff --git a/entity/accessor.hpp b/entity/accessor.hpp index 9b836420..a097afd4 100644 --- a/entity/accessor.hpp +++ b/entity/accessor.hpp @@ -40,6 +40,7 @@ struct erased_accessor final { template requires std::is_default_constructible_v FieldType read(const Obj& x) const noexcept; template void read(const Obj& x, FieldType& value) const noexcept; template void write(Obj& x, move_qualified value) const noexcept; + constexpr bool can_write() const noexcept { return writer != nullptr; } }; template diff --git a/entity/entity.hpp b/entity/entity.hpp index 4eca858e..30c75c15 100644 --- a/entity/entity.hpp +++ b/entity/entity.hpp @@ -56,7 +56,8 @@ template concept FieldWriter = requires { requires FieldWriter_memfn || FieldWriter_ptr || - FieldWriter_function; + FieldWriter_function || + std::same_as; }; namespace detail { @@ -100,6 +101,11 @@ struct write_field 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) @@ -152,6 +158,7 @@ struct entity_field : entity_field_base { 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); } + 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; }; -- cgit v1.2.3