Browse Source

refactor: improve type intuitions for getter/setter, add non-const getter option for binding

Sam Jaffe 3 years ago
parent
commit
ba471b83ea
2 changed files with 20 additions and 5 deletions
  1. 13 4
      include/reflection/reflection.h
  2. 7 1
      test/reflection_test.cxx

+ 13 - 4
include/reflection/reflection.h

@@ -41,10 +41,19 @@ public:
     throw std::out_of_range("no id in reflection");
   }
 
-  template <typename T, typename V>
-  Reflection & bind(std::string_view id, T (Obj::*get)() const,
-                    void (Obj::*set)(V)) {
-    members_.emplace(id, REFLECTION(Proxy<T>(obj, get, set)));
+  template <typename R, typename I>
+  Reflection & bind(std::string_view id, R (Obj::*get)() const,
+                    void (Obj::*set)(I)) {
+    using V = std::decay_t<R>;
+    members_.emplace(id, REFLECTION(Proxy<V>(obj, get, set)));
+    const_members_.emplace(id, CONST_REFLECTION((obj.*get)()));
+    return *this;
+  }
+  
+  template <typename R>
+  Reflection & bind(std::string_view id, R (Obj::*get)() const,
+                    std::decay_t<R> &(Obj::*acc)()) {
+    members_.emplace(id, REFLECTION((obj.*acc)()));
     const_members_.emplace(id, CONST_REFLECTION((obj.*get)()));
     return *this;
   }

+ 7 - 1
test/reflection_test.cxx

@@ -12,9 +12,15 @@ namespace {
 struct Example {
   int a;
   int b;
+  int d;
+
+  int get() const { return d; }
+  int &get() { return d; }
 };
 
-bool _ = Reflection<Example>().bind("a", &Example::a).bind("c", &Example::b);
+bool _ = Reflection<Example>().bind("a", &Example::a)
+    .bind("c", &Example::b)
+    .bind("d", &Example::get, &Example::get);
 }
 
 TEST(ReflectionTest, AquiresGetter) {