| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- //
- // forward.h
- // reflection
- //
- // Created by Sam Jaffe on 7/3/22.
- // Copyright © 2022 Sam Jaffe. All rights reserved.
- //
- #pragma once
- #include <functional>
- #include <map>
- #include <string>
- #include <type_traits>
- #include <typeindex>
- #include <unordered_map>
- namespace reflection {
- class Context;
- class ContextScope;
- class Object;
- class Scope;
- template <typename Obj, typename = void> class Reflection;
- template <typename T> class Proxy;
- template <typename T> class TypeCast;
- template <typename Func> using Cache = std::map<std::string_view, Func>;
- template <typename T> using TypeMap = std::unordered_map<std::type_index, T>;
- template <typename Obj>
- using Accessor = std::function<Object(Obj &, std::string)>;
- template <typename Obj>
- using Getter = std::function<Object(Obj const &, std::string)>;
- template <typename T> constexpr std::string_view Name = "";
- }
- #define reflect(object) reflection::Object(object, #object)
- #define INSTANTIATE_REFLECTION_IMPL(T, name, ...) \
- template <> constexpr std::string_view Name<T> = name; \
- template Cache<Accessor<T>> Reflection<T>::members_; \
- template Cache<Getter<T>> Reflection<T>::const_members_
- #define INSTANTIATE_REFLECTION(T, ...) \
- INSTANTIATE_REFLECTION_IMPL(T, ##__VA_ARGS__, #T)
- #define INSTANTIATE_REFLECTION_TYPECAST_IMPL(T, name, ...) \
- template <> constexpr std::string_view Name<T> = name; \
- template TypeMap<std::function<T(Object const &)>> TypeCast<T>::get_; \
- template TypeMap<std::function<void(Object &, T const &)>> TypeCast<T>::set_
- #define INSTANTIATE_REFLECTION_TYPECAST(T, ...) \
- INSTANTIATE_REFLECTION_TYPECAST_IMPL(T, ##__VA_ARGS__, #T)
- #if !defined(CONCAT)
- #define CONCAT_IMPL(A, B) A##B
- #define CONCAT(A, B) CONCAT_IMPL(A, B)
- #endif
- #define REFLECTION(T) \
- bool const CONCAT(reflection_, __LINE__) = ::reflection::Reflection<T>()
- #define REFLECTION_TYPE_CAST(T) \
- bool const CONCAT(type_cast_, __LINE__) = ::reflection::TypeCast<T>()
- #define IS_SCOPE_CONTAINER(C) \
- std::is_constructible_v<std::string_view, \
- decltype(*std::begin(std::declval<C>()))>
|