reflection.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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_F(expr) \
  11. [=](Obj & obj, std::string name) { return Object(expr, std::move(name)); }
  12. #define CONST_REFLECTION_F(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. std::is_enum_v<T>;
  21. template <typename Obj, typename> class Reflection {
  22. public:
  23. operator bool() const { return true; }
  24. static Getter<Obj> getter(std::string_view id) {
  25. if (auto it = const_members_.find(id); it != const_members_.end()) {
  26. return it->second;
  27. }
  28. throw std::out_of_range("no id in reflection");
  29. }
  30. static Accessor<Obj> accessor(std::string_view id) {
  31. if (auto it = members_.find(id); it != members_.end()) {
  32. return it->second;
  33. }
  34. throw std::out_of_range("no id in reflection");
  35. }
  36. template <typename R, typename I>
  37. Reflection & bind(std::string_view id, R (Obj::*get)() const,
  38. void (Obj::*set)(I)) {
  39. using V = std::decay_t<R>;
  40. members_.emplace(id, REFLECTION_F(Proxy<V>(obj, get, set)));
  41. const_members_.emplace(id, CONST_REFLECTION_F((obj.*get)()));
  42. return *this;
  43. }
  44. template <typename R>
  45. Reflection & bind(std::string_view id, R (Obj::*get)() const,
  46. std::decay_t<R> & (Obj::*acc)()) {
  47. members_.emplace(id, REFLECTION_F((obj.*acc)()));
  48. const_members_.emplace(id, CONST_REFLECTION_F((obj.*get)()));
  49. return *this;
  50. }
  51. template <typename T>
  52. Reflection & bind(std::string_view id, T (Obj::*get)() const) {
  53. const_members_.emplace(id, CONST_REFLECTION_F((obj.*get)()));
  54. return *this;
  55. }
  56. template <typename T> Reflection & bind(std::string_view id, T Obj::*member) {
  57. members_.emplace(id, REFLECTION_F(obj.*member));
  58. const_members_.emplace(id, CONST_REFLECTION_F(obj.*member));
  59. return *this;
  60. }
  61. template <typename F> Reflection & bind(std::string_view id, F func) {
  62. if constexpr (!std::is_const_v<decltype(func(std::declval<Obj &>()))>) {
  63. members_.emplace(id, REFLECTION_F(func(obj)));
  64. } else {
  65. members_.emplace(id, CONST_REFLECTION_F(func(obj)));
  66. }
  67. const_members_.emplace(id, CONST_REFLECTION_F(func(obj)));
  68. return *this;
  69. }
  70. private:
  71. static Cache<Accessor<Obj>> members_;
  72. static Cache<Getter<Obj>> const_members_;
  73. };
  74. template <typename T>
  75. class Reflection<T, std::enable_if_t<is_final_reflection<T>>> {
  76. public:
  77. static Getter<T> getter(std::string_view) {
  78. throw std::logic_error("unreflectable");
  79. }
  80. static Accessor<T> accessor(std::string_view) {
  81. throw std::logic_error("unreflectable");
  82. }
  83. };
  84. template <typename T> class Reflection<T *> {
  85. public:
  86. static auto getter(std::string_view id) {
  87. return [f = Reflection<T>::getter(id)](T const * ptr, std::string name) {
  88. return f(*ptr, std::move(name));
  89. };
  90. }
  91. static auto accessor(std::string_view id) {
  92. return [f = Reflection<T>::accessor(id)](T * ptr, std::string name) {
  93. return f(*ptr, std::move(name));
  94. };
  95. }
  96. };
  97. template <typename Obj, typename Void>
  98. Cache<Accessor<Obj>> Reflection<Obj, Void>::members_;
  99. template <typename Obj, typename Void>
  100. Cache<Getter<Obj>> Reflection<Obj, Void>::const_members_;
  101. }