Unravel Engine C++ Reference
Loading...
Searching...
No Matches
reflection.h
Go to the documentation of this file.
1#ifndef REFLECTION_H
2#define REFLECTION_H
3
4#include <entt/core/hashed_string.hpp>
5#include <entt/meta/container.hpp>
6#include <entt/meta/factory.hpp>
7#include <entt/meta/meta.hpp>
8#include <reflection/reflection_export.h>
9#include <string>
10#include <vector>
11#include <functional>
12#include <map>
13
14using namespace entt::literals;
15
16namespace entt
17{
18using attributes = std::map<std::string, meta_any>;
19using attribute = attributes::value_type;
20
21REFLECTION_EXPORT auto get_attribute(const meta_custom& custom, const char* name) -> const meta_any&;
22
23template<typename T>
24auto get_attribute_as(const meta_custom& custom, const char* name) -> T
25{
26 auto attr = get_attribute(custom, name);
27 if(attr.allow_cast<T>())
28 {
29 return attr.cast<T>();
30 }
31 return T();
32}
33
34template<>
35inline auto get_attribute_as<std::string>(const meta_custom& custom, const char* name) -> std::string
36{
37 auto attr = get_attribute(custom, name);
38 if(attr.allow_cast<std::string>())
39 {
40 return attr.cast<std::string>();
41 }
42
43 if(attr.allow_cast<const char*>())
44 {
45 return std::string(attr.cast<const char*>());
46 }
47 return std::string();
48}
49
50REFLECTION_EXPORT auto get_attribute(const meta_type& t, const char* name) -> const meta_any&;
51
52template<typename T>
53auto get_attribute_as(const meta_type& t, const char* name) -> T
54{
55 return get_attribute_as<T>(t.custom(), name);
56}
57
58REFLECTION_EXPORT auto get_attribute(const meta_data& prop, const char* name) -> const meta_any&;
59
60template<typename T>
61auto get_attribute_as(const meta_data& prop, const char* name) -> T
62{
63 return get_attribute_as<T>(prop.custom(), name);
64}
65
66
67// Deep-copy src into a brand new, owned meta_any (type-erased).
68
69REFLECTION_EXPORT auto copy_meta_any(const entt::meta_any& src) -> entt::meta_any;
70
71
72REFLECTION_EXPORT auto get_derived_types(const meta_type& t) -> std::vector<meta_type>;
73
74REFLECTION_EXPORT auto as_derived(meta_any& obj) -> bool;
75
76REFLECTION_EXPORT auto get_derived_type(meta_any& obj) -> meta_type;
77
78REFLECTION_EXPORT auto get_pretty_name(const meta_type& t) -> std::string;
79REFLECTION_EXPORT auto get_name(const meta_type& t) -> std::string;
80REFLECTION_EXPORT auto get_pretty_name(const meta_data& prop) -> std::string;
81REFLECTION_EXPORT auto get_name(const meta_data& prop) -> std::string;
82REFLECTION_EXPORT auto get_pretty_name(const meta_custom& prop) -> std::string;
83REFLECTION_EXPORT auto get_name(const meta_custom& prop) -> std::string;
84
85template<typename T>
86using property_predicate_t = std::function<T(const meta_any&)>;
87
88template<typename T>
91{
92 return std::move(predicate);
93
94}
95
96template<typename Value, typename... Args>
97auto make_custom(Args &&...args) -> entt::meta_custom
98{
99 return {entt::internal::meta_custom_node{type_id<Value>().hash(), std::make_shared<Value>(std::forward<Args>(args)...)}};
100}
101
102
103auto is_property_visible(const entt::meta_any& object, const entt::meta_data& prop) -> bool;
104auto is_property_readonly(const entt::meta_any& object, const entt::meta_data& prop) -> bool;
105auto is_property_flattable(const entt::meta_any& object, const entt::meta_data& prop) -> bool;
106auto is_property_visible(const entt::meta_any& object, const entt::meta_custom& custom) -> bool;
107auto is_property_readonly(const entt::meta_any& object, const entt::meta_custom& custom) -> bool;
108auto is_property_flattable(const entt::meta_any& object, const entt::meta_custom& custom) -> bool;
109auto get_property_hint(const entt::meta_any& object, const entt::meta_data& prop) -> std::string;
110
111
112} // namespace entt
113
114#define CAT_IMPL_(a, b) a##b
115#define CAT_(a, b) CAT_IMPL_(a, b)
116#ifdef __COUNTER__
117#define ANONYMOUS_VARIABLE(str) CAT_(str, CAT_(__COUNTER__, CAT_(__LINE__, __COUNTER__)))
118#else
119#define ANONYMOUS_VARIABLE(str) CAT_(str, __LINE__)
120#endif
121
122namespace refl_detail
123{
124template<typename T>
125inline int get_reg(void (*f)())
126{
127 static const int s = [&f]()
128 {
129 f();
130 return 0;
131 }();
132 return s;
133}
134} // namespace refl_detail
135
136#define REFLECT_EXTERN(cls) \
137 template<typename T> \
138 extern void reflection_auto_register_reflection_function_t(); \
139 template<> \
140 void reflection_auto_register_reflection_function_t<cls>(); \
141 static const int ANONYMOUS_VARIABLE(auto_register__) = \
142 refl_detail::get_reg<cls>(&reflection_auto_register_reflection_function_t<cls>)
143
144#define REFLECT_INLINE(cls) \
145 REFLECT_EXTERN(cls); \
146 template<> \
147 inline void reflection_auto_register_reflection_function_t<cls>()
148
149#define REFLECT(cls) \
150 template<> \
151 void reflection_auto_register_reflection_function_t<cls>()
152
153#define REFLECTION_REGISTRATION \
154 static void reflection_auto_register_reflection_function_(); \
155 namespace \
156 { \
157 struct reflection__auto__register__ \
158 { \
159 reflection__auto__register__() \
160 { \
161 reflection_auto_register_reflection_function_(); \
162 } \
163 }; \
164 } \
165 static const reflection__auto__register__ ANONYMOUS_VARIABLE(auto_register__); \
166 static void reflection_auto_register_reflection_function_()
167
168#endif // RTTR_REFLECTION_H_
std::string name
Definition hub.cpp:33
auto property_predicate(property_predicate_t< T > predicate) -> property_predicate_t< T >
Definition reflection.h:89
auto get_derived_types(const meta_type &t) -> std::vector< meta_type >
auto get_property_hint(const entt::meta_any &object, const entt::meta_data &prop) -> std::string
attributes::value_type attribute
Definition reflection.h:19
auto get_pretty_name(const meta_type &t) -> std::string
auto as_derived(meta_any &obj) -> bool
auto get_attribute_as< std::string >(const meta_custom &custom, const char *name) -> std::string
Definition reflection.h:35
auto is_property_visible(const entt::meta_any &object, const entt::meta_data &prop) -> bool
auto get_attribute_as(const meta_custom &custom, const char *name) -> T
Definition reflection.h:24
auto copy_meta_any(const entt::meta_any &src) -> entt::meta_any
std::function< T(const meta_any &)> property_predicate_t
Definition reflection.h:86
auto get_attribute(const meta_custom &custom, const char *name) -> const meta_any &
std::map< std::string, meta_any > attributes
Definition reflection.h:18
auto is_property_readonly(const entt::meta_any &object, const entt::meta_data &prop) -> bool
auto is_property_flattable(const entt::meta_any &object, const entt::meta_data &prop) -> bool
auto get_derived_type(meta_any &obj) -> meta_type
auto get_name(const meta_type &t) -> std::string
auto make_custom(Args &&...args) -> entt::meta_custom
Definition reflection.h:97
int get_reg(void(*f)())
Definition reflection.h:125