-
-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathexample-binding.cpp
More file actions
250 lines (202 loc) · 6.55 KB
/
example-binding.cpp
File metadata and controls
250 lines (202 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
* ***README***
* This example illustrates an example usage scenario for
* refl-cpp: Performing fast type-erased deserialization (from XML)
* with the minimum possible amount of runtime overhead.
* A custom runtime reflection system is implemented here (see UiElementMetadata).
* It is then used to, at runtime, create instances of the reflected types and
* initialize their properties with values read from XML.
*/
#include "refl.hpp"
#include <iostream>
#include <regex>
#include <any>
#include <functional>
using UiElementProperties = std::unordered_map<std::string, std::string>;
using UiElementCreator = std::any(*)(const UiElementProperties&);
enum class UiPropertyType
{
Default = 0b0,
Required = 0b1,
Content = 0b10,
RequiredContent = Required | Content,
};
template <typename Parser>
struct UiProperty : refl::attr::usage::field
{
const Parser parser;
const UiPropertyType type;
constexpr UiProperty(const Parser& parser)
: parser(parser)
, type(UiPropertyType::Default)
{
}
constexpr UiProperty(UiPropertyType type, const Parser& parser)
: parser(parser)
, type(type)
{
}
};
UiElementProperties parse_properties(std::string str)
{
static const std::basic_regex<char> propRegex(R"raw(\s*(\w+)="([\s\S]*)")raw");
UiElementProperties props;
std::smatch matches;
while (std::regex_search(str, matches, propRegex)) {
auto&& propName = matches[1];
auto&& propValue = matches[2];
props.insert({ propName.str(), propValue.str() });
str = matches.suffix().str();
}
return props;
}
class UiElementMetadata
{
public:
template <typename T>
static UiElementMetadata create_metadata()
{
constexpr auto type = refl::reflect<T>();
UiElementMetadata md;
md.name_ = type.name.c_str();
md.creator_ = &UiElementMetadata::create_untyped<T>;
return md;
}
std::any create_instance(const UiElementProperties& props) const
{
return creator_(props);
}
std::string_view name() const
{
return name_;
}
private:
std::string name_;
UiElementCreator creator_;
template <typename T>
static std::any create_untyped(const UiElementProperties& props)
{
T instance{};
for_each(refl::reflect<T>().members, [&](auto member) {
if constexpr (refl::descriptor::has_attribute<UiProperty>(member)) {
auto&& prop = refl::descriptor::get_attribute<UiProperty>(member);
if (auto propIter = props.find(member.name.str()); propIter != props.end()) {
member(instance) = prop.parser(propIter->second);
}
}
});
/**
* for_each loop above essentially gets compiled to multiples of the following (pseudo-code):
* if (auto propIter = props.find("MemberA"); propIter != props.end()) {
* instance.MemberA = prop.parser(propIter->second);
* }
*/
return instance;
}
};
class UiElementRegistry
{
public:
static UiElementRegistry& get()
{
static UiElementRegistry instance;
return instance;
}
UiElementMetadata find(std::string_view elementName) const
{
auto iter = std::find_if(metadata.begin(), metadata.end(), [&](auto&& x) {
return x.name() == elementName;
});
if (iter == metadata.end()) {
throw std::runtime_error("UiElement not found");
}
return *iter;
}
template <typename T>
void register_type()
{
metadata.push_back(UiElementMetadata::create_metadata<T>());
}
private:
std::vector<UiElementMetadata> metadata;
UiElementRegistry() {};
};
class ParsingError : public std::runtime_error
{
using std::runtime_error::runtime_error;
};
#define MY_CONCAT_(A, B) A##B
#define MY_CONCAT(A, B) MY_CONCAT_(A, B)
#define UI_ELEMENT_REGISTER(TypeName) static int MY_CONCAT(_global_dummy_, __COUNTER__) = (UiElementRegistry::get().register_type<TypeName>(), 0)
enum class Orientation
{
Horizontal,
Vertical,
};
Orientation parse_orientation(std::string_view str)
{
if (str == "horizontal") {
return Orientation::Horizontal;
}
else if (str == "vertical") {
return Orientation::Vertical;
}
else {
throw ParsingError("Cannot parse " + std::string(str) + " as Orientation");
}
}
void debug_orientation(std::ostream& os, Orientation value)
{
os << (value == Orientation::Horizontal ? "Horizontal" : "Vertical");
}
REFL_AUTO(type(Orientation, debug{ debug_orientation }))
struct StackPanel
{
Orientation orientation;
std::string content;
static std::string parse_content(std::string_view content)
{
return content.data();
}
};
REFL_AUTO(
type(StackPanel),
field(orientation, UiProperty(&parse_orientation)),
field(content, UiProperty(UiPropertyType::RequiredContent, &StackPanel::parse_content))
)
UI_ELEMENT_REGISTER(StackPanel);
const char* view_template = R"<?>(
<StackPanel orientation="horizontal"> Hello, World! </StackPanel>
)<?>";
int main()
{
std::basic_regex<char> xmlRegex(R"raw(<(\w+)([^>]*)>([\s\S]*)</\s*\1\s*>)raw");
std::string view(view_template);
std::replace(view.begin(), view.end(), '\n', ' ');
std::smatch matches;
while (std::regex_search(view, matches, xmlRegex)) {
if (matches.size() < 4) {
throw std::runtime_error("Error in XML parser!");
}
auto&& tagName = matches[1];
auto&& attributes = matches[2];
auto&& content = matches[3];
std::cout << "Matches: ";
for (size_t i = 1; i < matches.size(); i++) {
auto&& match = matches[i];
std::cout << "(" << match << ")";
}
std::cout << '\n';
UiElementProperties props = parse_properties(attributes.str());
props["content"] = content;
UiElementMetadata metadata = UiElementRegistry::get().find(tagName.str());
std::any element = metadata.create_instance(props);
if (StackPanel* sp = std::any_cast<StackPanel>(&element)) {
std::cout << "object at " << sp << " = ";
refl::runtime::debug(std::cout, *sp);
}
std::cout << '\n';
view = matches.suffix().str();
}
std::cout << std::endl;
}