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
85using property_predicate_t = std::function<bool(const meta_any&)>;
86REFLECTION_EXPORT auto property_predicate(property_predicate_t predicate)
88
89
90template<typename Value, typename... Args>
91auto make_custom(Args &&...args) -> entt::meta_custom
92{
93 return {entt::internal::meta_custom_node{type_id<Value>().hash(), std::make_shared<Value>(std::forward<Args>(args)...)}};
94}
95
96} // namespace entt
97
98#define CAT_IMPL_(a, b) a##b
99#define CAT_(a, b) CAT_IMPL_(a, b)
100#ifdef __COUNTER__
101#define ANONYMOUS_VARIABLE(str) CAT_(str, CAT_(__COUNTER__, CAT_(__LINE__, __COUNTER__)))
102#else
103#define ANONYMOUS_VARIABLE(str) CAT_(str, __LINE__)
104#endif
105
106namespace refl_detail
107{
108template<typename T>
109inline int get_reg(void (*f)())
110{
111 static const int s = [&f]()
112 {
113 f();
114 return 0;
115 }();
116 return s;
117}
118} // namespace refl_detail
119
120#define REFLECT_EXTERN(cls) \
121 template<typename T> \
122 extern void reflection_auto_register_reflection_function_t(); \
123 template<> \
124 void reflection_auto_register_reflection_function_t<cls>(); \
125 static const int ANONYMOUS_VARIABLE(auto_register__) = \
126 refl_detail::get_reg<cls>(&reflection_auto_register_reflection_function_t<cls>)
127
128#define REFLECT_INLINE(cls) \
129 REFLECT_EXTERN(cls); \
130 template<> \
131 inline void reflection_auto_register_reflection_function_t<cls>()
132
133#define REFLECT(cls) \
134 template<> \
135 void reflection_auto_register_reflection_function_t<cls>()
136
137#define REFLECTION_REGISTRATION \
138 static void reflection_auto_register_reflection_function_(); \
139 namespace \
140 { \
141 struct reflection__auto__register__ \
142 { \
143 reflection__auto__register__() \
144 { \
145 reflection_auto_register_reflection_function_(); \
146 } \
147 }; \
148 } \
149 static const reflection__auto__register__ ANONYMOUS_VARIABLE(auto_register__); \
150 static void reflection_auto_register_reflection_function_()
151
152#endif // RTTR_REFLECTION_H_
std::string name
Definition hub.cpp:27
auto get_derived_types(const meta_type &t) -> std::vector< meta_type >
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 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
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 property_predicate(property_predicate_t predicate) -> property_predicate_t
std::function< bool(const meta_any &)> property_predicate_t
Definition reflection.h:85
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:91
int get_reg(void(*f)())
Definition reflection.h:109