Sfoglia il codice sorgente

refactor: add generate get/set proxy

Sam Jaffe 2 anni fa
parent
commit
e0f53f8b78

+ 4 - 0
include/reflection/proxy.h

@@ -23,6 +23,10 @@ public:
   Proxy(O & obj, T O::*member, void (O::*set)(T))
       : data_(obj.*member), set_([&obj, set](auto & v) { (obj.*set)(v); }) {}
 
+  template <typename O, typename Get, typename Set>
+  Proxy(O & obj, Get get, Set set)
+      : data_(get(obj)), set_([&obj, set](auto & v) { set(obj, v); }) {}
+
   ~Proxy() { set_(data_); }
 
   operator T &() { return data_; }

+ 13 - 5
include/reflection/reflection.h

@@ -82,13 +82,21 @@ public:
     return *this;
   }
 
-  template <typename F> Reflection & bind(std::string_view id, F func) {
-    if constexpr (!std::is_const_v<decltype(func(std::declval<Obj &>()))>) {
-      members_.emplace(id, REFLECTION_F(func(obj)));
+  template <typename Get, typename Set>
+  Reflection & bind(std::string_view id, Get get, Set set) {
+    using V = std::decay_t<std::invoke_result_t<Get, Obj &>>;
+    members_.emplace(id, REFLECTION_F(Proxy<V>(obj, get, set)));
+    const_members_.emplace(id, CONST_REFLECTION_F(get(obj)));
+    return *this;
+  }
+
+  template <typename Get> Reflection & bind(std::string_view id, Get get) {
+    if constexpr (!std::is_const_v<std::invoke_result_t<Get, Obj &>>) {
+      members_.emplace(id, REFLECTION_F(get(obj)));
     } else {
-      members_.emplace(id, CONST_REFLECTION_F(func(obj)));
+      members_.emplace(id, CONST_REFLECTION_F(get(obj)));
     }
-    const_members_.emplace(id, CONST_REFLECTION_F(func(obj)));
+    const_members_.emplace(id, CONST_REFLECTION_F(get(obj)));
     return *this;
   }
 

+ 2 - 2
include/reflection/typecast.h

@@ -14,8 +14,8 @@
 #include <utility>
 
 #if defined __has_include
-#if __has_include(<magic_enum.hpp>)
-#include <magic_enum.hpp>
+#if __has_include(<magic_enum/magic_enum.hpp>)
+#include <magic_enum/magic_enum.hpp>
 #endif
 #endif