reflection.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 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> Reflection & bind(std::string_view id, T Obj::*member) {
  51. members_.emplace(id, REFLECTION(obj.*member));
  52. const_members_.emplace(id, CONST_REFLECTION(obj.*member));
  53. return *this;
  54. }
  55. template <typename R, typename T>
  56. Reflection & bind(std::string_view id, T Obj::*member) {
  57. TypeConversion<R, T> convert;
  58. members_.emplace(id, CONST_REFLECTION(convert(obj.*member)));
  59. const_members_.emplace(id, CONST_REFLECTION(convert(obj.*member)));
  60. return *this;
  61. }
  62. private:
  63. static Cache<Accessor<Obj>> members_;
  64. static Cache<Getter<Obj>> const_members_;
  65. };
  66. template <typename T>
  67. class Reflection<T, std::enable_if_t<is_final_reflection<T>>> {
  68. public:
  69. static Getter<T> getter(std::string_view) {
  70. throw std::logic_error("unreflectable");
  71. }
  72. static Accessor<T> accessor(std::string_view) {
  73. throw std::logic_error("unreflectable");
  74. }
  75. };
  76. template <typename T> class Reflection<T *> {
  77. public:
  78. static auto getter(std::string_view id) {
  79. return [f = Reflection<T>::getter(id)](T const * ptr, std::string name) {
  80. return f(*ptr, std::move(name));
  81. };
  82. }
  83. static auto accessor(std::string_view id) {
  84. return [f = Reflection<T>::accessor(id)](T * ptr, std::string name) {
  85. return f(*ptr, std::move(name));
  86. };
  87. }
  88. };
  89. template <typename Obj, typename Void>
  90. Cache<Accessor<Obj>> Reflection<Obj, Void>::members_;
  91. template <typename Obj, typename Void>
  92. Cache<Getter<Obj>> Reflection<Obj, Void>::const_members_;
  93. }