Unravel Engine C++ Reference
Loading...
Searching...
No Matches
reflection.cpp
Go to the documentation of this file.
1#include "reflection.h"
2
3namespace entt
4{
5namespace
6{
7auto get_derived(const entt::meta_type& base) -> std::vector<entt::meta_type>
8{
9 std::vector<entt::meta_type> result;
10
11 auto types = entt::resolve();
12 for (auto mt : types)
13 {
14 for (auto b : mt.second.base())
15 {
16 if (b.second == base)
17 {
18 result.push_back(mt.second);
19 }
20 }
21 }
22 return result;
23}
24
25}
26
27auto copy_meta_any(const entt::meta_any& src) -> entt::meta_any
28{
29 if (!src)
30 {
31 return {};
32 }
33
34 const entt::meta_type mt = src.type();
35
36 // 2) T() + operator=
37 if (auto owned = mt.construct(); owned) // default-construct
38 {
39 if (owned.assign(src)) // in-place copy
40 {
41 return owned; // still owned
42 }
43 }
44
45 return {};
46}
47
48auto get_derived_types(const meta_type& t) -> std::vector<meta_type>
49{
50 return get_derived(t);
51}
52
53auto as_derived(meta_any& obj) -> bool
54{
55 auto type = obj.type();
56 auto as_derived = type.func("as_derived"_hs);
57 if(as_derived)
58 {
59 obj = as_derived.invoke(obj);
60 return true;
61 }
62 return false;
63}
64
65auto get_derived_type(meta_any& obj) -> meta_type
66{
67 auto type = obj.type();
68
69 auto as_derived = type.func("as_derived"_hs);
70 if(as_derived)
71 {
72 return as_derived.invoke(obj).type();
73 }
74
75 auto func = type.func("get_meta_type"_hs);
76 if(func)
77 {
78 auto result = func.invoke(obj);
79 return result.cast<meta_type>();
80 }
81 return type;
82}
83
84auto get_attribute(const meta_custom& custom, const char* name) -> const meta_any&
85{
86 const attributes* attrs = custom;
87
88 if(attrs)
89 {
90 auto it = attrs->find(name);
91 if(it != attrs->end())
92 {
93 return it->second;
94 }
95 }
96
97 static const meta_any any;
98 return any;
99}
100
101auto get_attribute(const meta_type& t, const char* name) -> const meta_any&
102{
103 return get_attribute(t.custom(), name);
104}
105
106auto get_attribute(const meta_data& prop, const char* name) -> const meta_any&
107{
108 return get_attribute(prop.custom(), name);
109}
110
111auto get_pretty_name(const meta_type& t) -> std::string
112{
113 auto name = get_attribute_as<std::string>(t, "pretty_name");
114 if(name.empty())
115 {
116 return get_name(t);
117 }
118 return name;
119}
120
121auto get_name(const meta_type& t) -> std::string
122{
123 auto name = get_attribute_as<std::string>(t, "name");
124 if(name.empty())
125 {
126 return std::string(t.info().name());
127 }
128 return name;
129}
130
131
132auto get_pretty_name(const meta_custom& t) -> std::string
133{
134 auto name = get_attribute_as<std::string>(t, "pretty_name");
135 if(name.empty())
136 {
137 return get_name(t);
138 }
139 return name;
140}
141
142auto get_name(const meta_custom& t) -> std::string
143{
144 auto name = get_attribute_as<std::string>(t, "name");
145 if(name.empty())
146 {
147 return std::string();
148 }
149 return name;
150}
151
152auto get_name(const meta_data& prop) -> std::string
153{
154 return get_attribute_as<std::string>(prop, "name");
155}
156
157auto get_pretty_name(const meta_data& prop) -> std::string
158{
159 auto name = get_attribute_as<std::string>(prop, "pretty_name");
160 if(name.empty())
161 {
162 return get_name(prop);
163 }
164 return name;
165}
166
168{
169 return std::move(predicate);
170}
171
172}
173
174auto register_type_helper(const char* name) -> int
175{
176 return 0;
177}
entt::handle b
manifold_type type
std::string name
Definition hub.cpp:27
auto get_derived_types(const meta_type &t) -> std::vector< meta_type >
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 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 register_type_helper(const char *name) -> int