reflection.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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> || std::is_enum_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 R, typename I>
  36. Reflection & bind(std::string_view id, R (Obj::*get)() const,
  37. void (Obj::*set)(I)) {
  38. using V = std::decay_t<R>;
  39. members_.emplace(id, REFLECTION(Proxy<V>(obj, get, set)));
  40. const_members_.emplace(id, CONST_REFLECTION((obj.*get)()));
  41. return *this;
  42. }
  43. template <typename R>
  44. Reflection & bind(std::string_view id, R (Obj::*get)() const,
  45. std::decay_t<R> &(Obj::*acc)()) {
  46. members_.emplace(id, REFLECTION((obj.*acc)()));
  47. const_members_.emplace(id, CONST_REFLECTION((obj.*get)()));
  48. return *this;
  49. }
  50. template <typename T>
  51. Reflection & bind(std::string_view id, T (Obj::*get)() const) {
  52. const_members_.emplace(id, CONST_REFLECTION((obj.*get)()));
  53. return *this;
  54. }
  55. template <typename T> Reflection & bind(std::string_view id, T Obj::*member) {
  56. members_.emplace(id, REFLECTION(obj.*member));
  57. const_members_.emplace(id, CONST_REFLECTION(obj.*member));
  58. return *this;
  59. }
  60. template <typename F> Reflection & bind(std::string_view id, F func) {
  61. if constexpr (!std::is_const_v<decltype(func(std::declval<Obj &>()))>) {
  62. members_.emplace(id, REFLECTION(func(obj)));
  63. } else {
  64. members_.emplace(id, CONST_REFLECTION(func(obj)));
  65. }
  66. const_members_.emplace(id, CONST_REFLECTION(func(obj)));
  67. return *this;
  68. }
  69. template <typename R, typename T>
  70. Reflection & bind(std::string_view id, T Obj::*member) {
  71. if constexpr (std::is_convertible_v<T, R>) {
  72. return bind(id, member, [](T const &v) { return static_cast<R>(v); });
  73. } else {
  74. return bind(id, member, TypeConversion<R, T>());
  75. }
  76. }
  77. template <typename R, typename T>
  78. Reflection & bind(std::string_view id, T (Obj::*get)() const) {
  79. if constexpr (std::is_convertible_v<T, R>) {
  80. return bind(id, get, [](T const &v) { return static_cast<R>(v); });
  81. } else {
  82. return bind(id, get, TypeConversion<R, T>());
  83. }
  84. }
  85. template <typename T, typename F>
  86. Reflection & bind(std::string_view id, T Obj::*member, F convert) {
  87. members_.emplace(id, CONST_REFLECTION(convert(obj.*member)));
  88. const_members_.emplace(id, CONST_REFLECTION(convert(obj.*member)));
  89. return *this;
  90. }
  91. template <typename T, typename F>
  92. Reflection & bind(std::string_view id, T (Obj::*get)() const, F convert) {
  93. members_.emplace(id, CONST_REFLECTION(convert((obj.*get)())));
  94. const_members_.emplace(id, CONST_REFLECTION(convert((obj.*get)())));
  95. return *this;
  96. }
  97. private:
  98. static Cache<Accessor<Obj>> members_;
  99. static Cache<Getter<Obj>> const_members_;
  100. };
  101. template <typename T>
  102. class Reflection<T, std::enable_if_t<is_final_reflection<T>>> {
  103. public:
  104. static Getter<T> getter(std::string_view) {
  105. throw std::logic_error("unreflectable");
  106. }
  107. static Accessor<T> accessor(std::string_view) {
  108. throw std::logic_error("unreflectable");
  109. }
  110. };
  111. template <typename T> class Reflection<T *> {
  112. public:
  113. static auto getter(std::string_view id) {
  114. return [f = Reflection<T>::getter(id)](T const * ptr, std::string name) {
  115. return f(*ptr, std::move(name));
  116. };
  117. }
  118. static auto accessor(std::string_view id) {
  119. return [f = Reflection<T>::accessor(id)](T * ptr, std::string name) {
  120. return f(*ptr, std::move(name));
  121. };
  122. }
  123. };
  124. template <typename Obj, typename Void>
  125. Cache<Accessor<Obj>> Reflection<Obj, Void>::members_;
  126. template <typename Obj, typename Void>
  127. Cache<Getter<Obj>> Reflection<Obj, Void>::const_members_;
  128. }