object.h 4.9 KB

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