object.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // object.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 <stdexcept>
  10. #include <typeindex>
  11. #include <utility>
  12. #include "reflection/forward.h"
  13. #include "reflection/scope.h"
  14. namespace reflection {
  15. class Object {
  16. public:
  17. template <typename T> Object(T const & data, std::string name = "this");
  18. template <typename T> Object(T & data, std::string name = "this");
  19. template <typename T> Object(T && data, std::string name = "this");
  20. template <typename T> Object(Proxy<T> data, std::string name = "this");
  21. template <typename V> Object & operator=(V const & value);
  22. Object own() const { return clone_(*this); }
  23. std::string_view name() const {
  24. return std::string_view(name_).substr(name_.rfind('.') + 1);
  25. }
  26. std::string_view path() const { return name_; }
  27. std::type_index type() const { return type_; }
  28. std::string_view type_name() const {
  29. return type_name_.empty() ? type_.name() : type_name_;
  30. }
  31. template <typename T> bool is_a() const { return type_ == typeid(T); }
  32. template <typename T> explicit operator T &() const & { return cast<T &>(); }
  33. template <typename T> explicit operator T const &() const & {
  34. return cast<T const &>();
  35. }
  36. template <typename T> operator T() const & { return cast<T>(); }
  37. template <typename T> operator T() & { return cast<T>(); }
  38. template <typename T> operator T() && {
  39. return const_ ? cast<T>() : std::move(cast<T &>());
  40. }
  41. Object get(std::string_view id) & { return (this->*get_)(id); }
  42. Object get(std::string_view id) const & { return (this->*get_)(id); }
  43. Object get(std::string_view id) && { return (this->*get_)(id).own(); }
  44. Object get(Scope const & scope) const {
  45. if (type_name() != scope.type_name()) { throw std::bad_cast(); }
  46. return get(*this, scope.begin(), scope.end());
  47. }
  48. template <typename C, typename = std::enable_if_t<IS_SCOPE_CONTAINER(C)>>
  49. Object get(C const & container) const {
  50. return get(*this, std::begin(container), std::end(container));
  51. }
  52. template <typename It> static Object get(Object o, It it, It const end) {
  53. for (; it != end; ++it) {
  54. o = std::move(o).get(*it);
  55. }
  56. return o;
  57. }
  58. private:
  59. template <typename T> T cast() const;
  60. template <typename T> Object getter(std::string_view id) const;
  61. template <typename T> Object accessor(std::string_view id) const;
  62. template <typename T> static Object deep(Object const & obj) {
  63. return Object(obj.cast<T>(), obj.name_);
  64. }
  65. private:
  66. std::type_index type_;
  67. std::string_view type_name_; // A more human-readable version of type_
  68. std::string name_; // The property name as a dot-separated path
  69. // Are we using a proxy class to store non-trivial setter behavior
  70. bool proxy_{false};
  71. bool const_; // Is the object immutable (const-ref or rvalue)
  72. std::shared_ptr<void> data_;
  73. Object (Object::*get_)(std::string_view) const;
  74. Object (*clone_)(Object const &) = [](Object const & obj) { return obj; };
  75. };
  76. }
  77. #include "reflection/reflection.h"
  78. #include "reflection/typecast.h"
  79. namespace reflection {
  80. template <typename T> Object Object::getter(std::string_view id) const {
  81. return Reflection<T>::getter(id)(cast<T const &>(),
  82. name_ + "." + std::string(id));
  83. }
  84. template <typename T> Object Object::accessor(std::string_view id) const {
  85. return Reflection<T>::accessor(id)(cast<T &>(),
  86. name_ + "." + std::string(id));
  87. }
  88. template <typename T>
  89. Object::Object(T const & data, std::string name)
  90. : type_(typeid(T)), type_name_(Name<T>), name_(std::move(name)), const_(true),
  91. data_(const_cast<T *>(&data), [](auto *) {}), get_(&Object::getter<T>) {}
  92. template <typename T>
  93. Object::Object(T & data, std::string name)
  94. : type_(typeid(T)), type_name_(Name<T>), name_(std::move(name)), const_(false),
  95. data_(&data, [](auto *) {}), get_(&Object::accessor<T>) {}
  96. template <typename T>
  97. Object::Object(T && data, std::string name)
  98. : type_(typeid(T)), type_name_(Name<T>), name_(std::move(name)), const_(true),
  99. data_(std::make_shared<T>(std::move(data))), get_(&Object::getter<T>),
  100. clone_(&Object::deep<T>) {}
  101. template <typename T>
  102. Object::Object(Proxy<T> data, std::string name)
  103. : type_(typeid(T)), type_name_(Name<T>), name_(std::move(name)), proxy_(true), const_(false),
  104. data_(std::make_shared<Proxy<T>>(std::move(data))),
  105. get_(&Object::getter<T>) {}
  106. template <typename V> Object & Object::operator=(V const & value) {
  107. if (TypeCast<V>::has_cast(*this)) {
  108. TypeCast<V>::set(*this, value);
  109. } else {
  110. cast<V &>() = value;
  111. }
  112. return *this;
  113. }
  114. template <typename T> T Object::cast() const {
  115. // Why can we decay this away?
  116. using V = std::remove_const_t<std::remove_reference_t<T>>;
  117. // 1) Casting is only allowed on non-reference rvals
  118. if constexpr (std::is_same_v<V, T>) {
  119. if (TypeCast<V>::has_cast(*this)) { return TypeCast<V>::get(*this); }
  120. }
  121. // 2) typeid does not respect const
  122. if (!is_a<V>()) { throw std::bad_cast(); }
  123. // 3) We guard against using mutable-reference on immutable Object here
  124. if (std::is_same_v<T, V &> && const_) { throw std::bad_cast(); }
  125. // 4) Proxy always contains mutable data
  126. if (proxy_) { return T(*std::static_pointer_cast<Proxy<V>>(data_)); }
  127. // 5) The const will be re-added by type coercion
  128. return *std::static_pointer_cast<V>(data_);
  129. }
  130. }