reflection.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #pragma once
  2. #include <functional>
  3. #include <stdexcept>
  4. #include <string>
  5. #include <type_traits>
  6. #include <utility>
  7. #include "reflection/forward.h"
  8. #include "reflection/object.h"
  9. #include "reflection/proxy.h"
  10. #define REFLECTION(expr) \
  11. [=](Obj & obj, std::string name) { return Object(expr, std::move(name)); }
  12. #define CONST_REFLECTION(expr) \
  13. [=](Obj const & obj, std::string name) { \
  14. return Object(expr, std::move(name)); \
  15. }
  16. namespace reflection {
  17. template <typename T>
  18. constexpr auto is_final_reflection =
  19. std::is_same_v<T, std::string> || std::is_fundamental_v<T>;
  20. template <typename Obj, typename> class Reflection {
  21. public:
  22. operator bool() const { return true; }
  23. static Getter<Obj> getter(std::string_view id) {
  24. if (auto it = const_members_.find(id); it != const_members_.end()) {
  25. return it->second;
  26. }
  27. throw std::out_of_range("no id in reflection");
  28. }
  29. static Accessor<Obj> accessor(std::string_view id) {
  30. if (auto it = members_.find(id); it != members_.end()) {
  31. return it->second;
  32. }
  33. throw std::out_of_range("no id in reflection");
  34. }
  35. template <typename T, typename V>
  36. Reflection & bind(std::string_view id, T (Obj::*get)() const,
  37. void (Obj::*set)(V)) {
  38. members_.emplace(id, REFLECTION(Proxy<T>(obj, get, set)));
  39. const_members_.emplace(id, CONST_REFLECTION((obj.*get)()));
  40. return *this;
  41. }
  42. template <typename T> Reflection & bind(std::string_view id, T Obj::*member) {
  43. members_.emplace(id, REFLECTION(obj.*member));
  44. const_members_.emplace(id, CONST_REFLECTION(obj.*member));
  45. return *this;
  46. }
  47. template <typename R, typename T>
  48. Reflection & bind(std::string_view id, T Obj::*member) {
  49. TypeConversion<R, T> convert;
  50. members_.emplace(id, CONST_REFLECTION(convert(obj.*member)));
  51. const_members_.emplace(id, CONST_REFLECTION(convert(obj.*member)));
  52. return *this;
  53. }
  54. private:
  55. static Cache<Accessor<Obj>> members_;
  56. static Cache<Getter<Obj>> const_members_;
  57. };
  58. template <typename T>
  59. class Reflection<T, std::enable_if_t<is_final_reflection<T>>> {
  60. public:
  61. static Getter<T> getter(std::string_view) {
  62. throw std::logic_error("unreflectable");
  63. }
  64. static Accessor<T> accessor(std::string_view) {
  65. throw std::logic_error("unreflectable");
  66. }
  67. };
  68. template <typename T> class Reflection<T *> {
  69. public:
  70. static auto getter(std::string_view id) {
  71. return [f = Reflection<T>::getter(id)](T const * ptr, std::string name) {
  72. return f(*ptr, std::move(name));
  73. };
  74. }
  75. static auto accessor(std::string_view id) {
  76. return [f = Reflection<T>::accessor(id)](T * ptr, std::string name) {
  77. return f(*ptr, std::move(name));
  78. };
  79. }
  80. };
  81. template <typename Obj, typename Void>
  82. Cache<Accessor<Obj>> Reflection<Obj, Void>::members_;
  83. template <typename Obj, typename Void>
  84. Cache<Getter<Obj>> Reflection<Obj, Void>::const_members_;
  85. }