proxy.h 709 B

12345678910111213141516171819202122232425262728293031323334
  1. //
  2. // proxy.h
  3. // reflection
  4. //
  5. // Created by Sam Jaffe on 7/3/22.
  6. // Copyright © 2022 Sam Jaffe. All rights reserved.
  7. //
  8. #pragma once
  9. #include <functional>
  10. #include "reflection/forward.h"
  11. namespace reflection {
  12. template <typename T> class Proxy {
  13. public:
  14. template <typename O>
  15. Proxy(O & obj, T (O::*get)() const, void (O::*set)(T))
  16. : data_((obj.*get)()), set_([&obj, set](auto & v) { (obj.*set)(v); }) {}
  17. template <typename O>
  18. Proxy(O & obj, T O::*member, void (O::*set)(T))
  19. : data_(obj.*member), set_([&obj, set](auto & v) { (obj.*set)(v); }) {}
  20. ~Proxy() { set_(data_); }
  21. operator T &() { return data_; }
  22. private:
  23. T data_;
  24. std::function<void(T const &)> set_;
  25. };
  26. }