proxy.h 554 B

123456789101112131415161718192021222324252627282930
  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. ~Proxy() { set_(data_); }
  18. operator T &() { return data_; }
  19. private:
  20. T data_;
  21. std::function<void(T const &)> set_;
  22. };
  23. }