summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-11-16 15:25:48 +0100
committerStanislaw Halik <sthalik@misaki.pl>2022-11-16 15:25:48 +0100
commitc82c854297d216bbc1901ffa9dd18f87a1d514e4 (patch)
treeef915c14e2fc204db61e90f2ac11ca60ffc20b56
parent577445e28b83f5b7aa7ae8fe4853b1e6a5b0e4d4 (diff)
entity: add function2 specializations
-rw-r--r--src/entity.hpp15
-rw-r--r--test/entity.cpp14
2 files changed, 29 insertions, 0 deletions
diff --git a/src/entity.hpp b/src/entity.hpp
index 50b8a4d0..10df2366 100644
--- a/src/entity.hpp
+++ b/src/entity.hpp
@@ -5,6 +5,7 @@
#include <type_traits>
#include <utility>
#include <tuple>
+#include <compat/function2.hpp>
#include <Corrade/Containers/StringView.h>
namespace floormat {}
@@ -80,6 +81,13 @@ struct read_field<Obj, Type, Type Obj::*> {
static constexpr Type read(const Obj& x, Type Obj::*r) { return x.*r; }
};
+template<typename Obj, typename Type, bool IsOwning, bool IsCopyable, typename Capacity, bool IsThrowing, bool HasStrongExceptionGuarantee>
+struct read_field<Obj, Type, fu2::function_base<IsOwning, IsCopyable, Capacity, IsThrowing, HasStrongExceptionGuarantee, void(const Obj&, move_qualified<Type>) const>> {
+ template<typename F> static constexpr Type read(const Obj& x, F&& fun) {
+ return fun(x);
+ }
+};
+
template<typename Obj, typename FieldType, FieldWriter<Obj, FieldType> W> struct write_field {
static constexpr void write(Obj& x, W w, move_qualified<FieldType> value) { w(x, value); }
};
@@ -94,6 +102,13 @@ struct write_field<Obj, FieldType, FieldType Obj::*> {
static constexpr void write(Obj& x, FieldType Obj::* w, move_qualified<FieldType> value) { x.*w = value; }
};
+template<typename Obj, typename Type, bool IsOwning, bool IsCopyable, typename Capacity, bool IsThrowing, bool HasStrongExceptionGuarantee>
+struct write_field<Obj, Type, fu2::function_base<IsOwning, IsCopyable, Capacity, IsThrowing, HasStrongExceptionGuarantee, void(Obj&, move_qualified<Type>) const>> {
+ template<typename F> static constexpr void write(Obj& x, F&& fun, move_qualified<Type> value) {
+ fun(x, value);
+ }
+};
+
constexpr inline int memcmp_(const char* s1, const char* s2, std::size_t n)
{
#ifdef __GNUC__
diff --git a/test/entity.cpp b/test/entity.cpp
index 8266ec47..8f762d73 100644
--- a/test/entity.cpp
+++ b/test/entity.cpp
@@ -64,10 +64,24 @@ static constexpr bool test_visitor()
return true;
}
+static void test_fun2() {
+ auto x = TestAccessors{1, 2, 3};
+ static constexpr auto read_fn = [](const TestAccessors& x) { return x.bar(); };
+ static constexpr auto write_fn = [](TestAccessors& x, int value) { x.set_bar(value); };
+ static constexpr auto read_bar = fu2::function_view<int(const TestAccessors&) const>{read_fn};
+ static constexpr auto write_bar = fu2::function_view<void(TestAccessors&, int) const>{write_fn};
+ static constexpr auto m_bar2 = entity::type<int>::field{"bar"_s, read_bar, write_bar};
+
+ fm_assert(m_bar2.read(x) == 2);
+ m_bar2.write(x, 22);
+ fm_assert(m_bar2.read(x) == 22);
+}
+
void test_app::test_entity()
{
static_assert(test_accessors());
static_assert(test_visitor());
+ test_fun2();
}
} // namespace floormat