object.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. Object own() const { return clone_(*this); }
  21. std::string_view name() const {
  22. return std::string_view(name_).substr(name_.rfind('.') + 1);
  23. }
  24. std::string_view path() const { return name_; }
  25. char const * type() const { return type_.name(); }
  26. template <typename T> bool is_a() const { return type_ == typeid(T); }
  27. template <typename T> operator T &() const & { return cast<T &>(); }
  28. template <typename T> operator T const &() const & {
  29. return cast<T const &>();
  30. }
  31. template <typename T> operator T() && {
  32. return is_a<T>() ? std::move(cast<T &>()) : cast<T const &>();
  33. }
  34. Object get(std::string_view id) & { return (this->*get_)(id); }
  35. Object get(std::string_view id) const & { return (this->*get_)(id); }
  36. Object get(std::string_view id) && { return (this->*get_)(id).own(); }
  37. template <typename C,
  38. typename = std::enable_if_t<std::is_constructible_v<
  39. std::string_view, decltype(*std::begin(std::declval<C>()))>>>
  40. Object get(C const & container) const {
  41. return get(*this, std::begin(container), std::end(container));
  42. }
  43. private:
  44. template <typename It> static Object get(Object o, It it, It const end) {
  45. for (; it != end; ++it) {
  46. o = std::move(o).get(*it);
  47. }
  48. return o;
  49. }
  50. template <typename T> T cast() const {
  51. // Why can we decay this away?
  52. using V = std::remove_const_t<std::remove_reference_t<T>>;
  53. // 1) typeid does not respect const
  54. if (!is_a<V>()) { throw std::bad_cast(); }
  55. // 2) We guard against using mutable-reference on immutable Object here
  56. if (std::is_same_v<T, V &> && const_) { throw std::bad_cast(); }
  57. // 3) Proxy always contains mutable data
  58. if (proxy_) { return T(*std::static_pointer_cast<Proxy<V>>(data_)); }
  59. // 4) The const will be re-added by type coercion
  60. return *std::static_pointer_cast<V>(data_);
  61. }
  62. template <typename T> Object getter(std::string_view id) const;
  63. template <typename T> Object accessor(std::string_view id) const;
  64. template <typename T> static Object deep(Object const & obj) {
  65. return Object(obj.cast<T>(), obj.name_);
  66. }
  67. private:
  68. std::type_index type_; //
  69. std::string name_; // The property name as a dot-separated path
  70. bool proxy_{
  71. false}; // Are we using a proxy class to store non-trivial setter behavior
  72. bool const_; // Is the object immutable (const-ref or rvalue)
  73. std::shared_ptr<void> data_;
  74. Object (Object::*get_)(std::string_view) const;
  75. Object (*clone_)(Object const &) = [](Object const & obj) { return obj; };
  76. };
  77. }
  78. #include "reflection/reflection.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)), 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)), 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)), 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)), name_(std::move(name)), proxy_(true), const_(false),
  104. data_(std::make_shared<Proxy<T>>(std::move(data))),
  105. get_(&Object::getter<T>) {}
  106. }