forward.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //
  2. // forward.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 <functional>
  10. #include <map>
  11. #include <string>
  12. #include <type_traits>
  13. #include <typeindex>
  14. #include <unordered_map>
  15. namespace reflection {
  16. class Object;
  17. template <typename Obj, typename = void> class Reflection;
  18. template <typename T> class Proxy;
  19. template <typename T> class TypeCast;
  20. template <typename Func> using Cache = std::map<std::string_view, Func>;
  21. template <typename T> using TypeMap = std::unordered_map<std::type_index, T>;
  22. template <typename Obj>
  23. using Accessor = std::function<Object(Obj &, std::string)>;
  24. template <typename Obj>
  25. using Getter = std::function<Object(Obj const &, std::string)>;
  26. }
  27. #define reflect(object) reflection::Object(object, #object)
  28. #define INSTANTIATE_REFLECTION(T) \
  29. template Cache<Accessor<T>> Reflection<T>::members_; \
  30. template Cache<Getter<T>> Reflection<T>::const_members_
  31. #define INSTANTIATE_REFLECTION_TYPECAST(T) \
  32. template TypeMap<std::function<T(Object const &)>> TypeCast<T>::get_; \
  33. template TypeMap<std::function<void(Object &, T const &)>> TypeCast<T>::set_
  34. #define CONCAT_IMPL(A, B) A##B
  35. #define CONCAT(A, B) CONCAT_IMPL(A, B)
  36. #define REFLECTION(T) \
  37. bool const CONCAT(reflection_, __LINE__) = ::reflection::Reflection<T>()
  38. #define REFLECTION_TYPE_CAST(T) \
  39. bool const CONCAT(type_cast_, __LINE__) = ::reflection::TypeCast<T>()