-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtemplate.jinja
224 lines (188 loc) · 6.91 KB
/
template.jinja
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
#include "rtti/DB/Log.h"
#include "rtti/Variant.h"
#include "rtti/DB.h"
#ifndef RTTI_DISABLE_BUILTIN_ARRAY_TYPES_DEFAULT_SETTER_GETTER
# include "rtti/DB/ArrayTypesDefaultSetterGetter.h"
#endif
#ifndef RTTI_DISABLE_STL_TYPES_DEFAULT_SETTER_GETTER
# include "rtti/DB/STLTypesDefaultSetterGetter.h"
#endif
/* Forward declaration of explicit specialization for template functions */
## for c in classes
/* Forward declaration of type */ struct {{ c.full_name }}; // TODO(FiTH): forward declare with struct/class
template<> /* static */ rtti::ObjectTypeInfo rtti::DB::make_object_type_info<{{ c.full_name }}>() noexcept;
template<> /* static */ const rtti::ObjectTypeInfo* rtti::DB::get_object_type_info<{{ c.full_name }}>() noexcept;
## endfor
#include "rect.h" // TODO: add file name var
// TODO: add 'const' fields support (fail assert in setter)
// TODO: add enum support
// TODO: add bool
#define RTTI_BUILTIN_TYPE_SETTER(cond, type, name, variant_type_1, variant_type_2, variant_type_3) \
if constexpr (std::cond##_v<type>) { \
if (value.is_number_##variant_type_1()) { \
auto value_data = value.get_ptr<const variant_##variant_type_1##_t*>(); \
ref = *value_data; \
} else if (value.is_number_##variant_type_2()) { \
auto value_data = value.get_ptr<const variant_##variant_type_2##_t*>(); \
ref = *value_data; \
} else if (value.is_number_##variant_type_3()) { \
auto value_data = value.get_ptr<const variant_##variant_type_3##_t*>(); \
ref = *value_data; \
} else if (value.is_null()) { \
ref = {}; \
} else { \
RTTI_ASSERT(false, "Failed to assign variable '" #name "' of type '" #type "' value of type '", \
value.type_name(), "': ", value); \
} \
}
namespace rtti
{
## for c in classes
/* * * * * * * * * * * * * * * * * * * * * * *
{{ c.full_name }}
* * * * * * * * * * * * * * * * * * * * * * * */
namespace
{
static const ObjectTypeInfo info_{{ loop.index }} = DB::make_object_type_info<{{ c.full_name }}>();
} /* namespace <anonymous> */
template<>
/* static */ ObjectTypeInfo DB::make_object_type_info<{{ c.full_name }}>() noexcept
{
const type_info_setter_t {{ c.name }}_setter = [](void* obj, const Variant& object_value) noexcept
{
#ifndef RTTI_DISABLE_OBJECT_DEFAULT_SETTER_GETTER
const Variant::object_t* object_value_ptr = object_value.get_ptr<const Variant::object_t*>();
RTTI_ASSERT(object_value_ptr != nullptr, RTTI_DB_LOG_FAILED_TO_SET_VALUE_FMT(
"{{ c.full_name }}", object_value));
for (const auto& [key, val]: *object_value_ptr) {
auto field_info_iter = info_{{ loop.index }}.fields_info_map.find(key);
RTTI_ASSERT_CONTINUE(field_info_iter != info_{{ loop.index }}.fields_info_map.cend(),
"Object of type '{{ c.full_name }}' does not have field with name '", key, "'. "
"Failed to set value of type '", val.type_name(), "': ", val);
const ObjectFieldInfo* field_info = field_info_iter->second;
void* field_ptr = field_info->get_addr(obj);
field_info->type_info.setter(field_ptr, val);
}
#else
RTTI_OBJECT_SETTER_IMPL(obj, object_value);
#endif // !RTTI_DISABLE_OBJECT_DEFAULT_SETTER_GETTER
};
const type_info_getter_t {{ c.name }}_getter = [](const void* obj, Variant& object_value) noexcept
{
#ifndef RTTI_DISABLE_OBJECT_DEFAULT_SETTER_GETTER
if (!object_value.is_object()) {
object_value = Variant::object_t {};
} else {
object_value.clear();
}
Variant::object_t* object_value_ptr = object_value.get_ptr<Variant::object_t*>();
for (const ObjectFieldInfo& field_info: info_{{ loop.index }}.fields_info) {
Variant field_value;
const void* field_ptr = field_info.get_const_addr(obj);
field_info.type_info.getter(field_ptr, field_value);
if (!field_value.is_null())
object_value_ptr->emplace(field_info.name, std::move(field_value));
}
#else
RTTI_OBJECT_GETTER_IMPL(obj, object_value);
#endif // !RTTI_DISABLE_OBJECT_DEFAULT_SETTER_GETTER
};
## for f in c.fields
/* {{ f.name }} */
const type_info_setter_t {{ f.name }}_setter = [](void* ptr, const Variant& value) noexcept
{
## if f.is_builtin_type
#ifndef RTTI_DISABLE_BUILTIN_TYPES_DEFAULT_SETTER_GETTER
auto& ref = *static_cast<{{ f.type }}*>(ptr);
RTTI_BUILTIN_TYPE_SETTER(is_floating_point, {{ f.type }}, {{ f.full_name }}, float, integer, unsigned)
else RTTI_BUILTIN_TYPE_SETTER(is_signed, {{ f.type }}, {{ f.full_name }}, integer, unsigned, float )
else RTTI_BUILTIN_TYPE_SETTER(is_unsigned, {{ f.type }}, {{ f.full_name }}, unsigned, integer, float )
#endif
## endif
## if f.is_builtin_type
#ifdef RTTI_DISABLE_BUILTIN_TYPES_DEFAULT_SETTER_GETTER
## endif
## if f.is_array_type == false
RTTI_BUILTIN_TYPE_SETTER_IMPL((static_cast<{{ f.type }}*>(ptr)), value);
## else
RTTI_BUILTIN_ARRAY_TYPE_SETTER_IMPL({{ f.array_element_type }}, {{ f.array_elements_count }},
*(static_cast<std::add_pointer_t<{{ f.type }}>>(ptr)), value);
## endif
## if f.is_builtin_type
#endif
## endif
};
const type_info_getter_t {{ f.name }}_getter = [](const void* ptr, Variant& value) noexcept
{
## if f.is_builtin_type
#ifndef RTTI_DISABLE_BUILTIN_TYPES_DEFAULT_SETTER_GETTER
value = *static_cast<const {{ f.type }}*>(ptr);
#endif
## endif
## if f.is_builtin_type
#ifdef RTTI_DISABLE_BUILTIN_TYPES_DEFAULT_SETTER_GETTER
## endif
## if f.is_array_type == false
RTTI_BUILTIN_TYPE_GETTER_IMPL((static_cast<const {{ f.type }}*>(ptr)), value);
## else
RTTI_BUILTIN_ARRAY_TYPE_GETTER_IMPL({{ f.array_element_type }}, {{ f.array_elements_count }},
*(static_cast<std::add_pointer_t<const {{ f.type }}>>(ptr)), value);
## endif
## if f.is_builtin_type
#endif
## endif
};
auto {{ f.name }}_get_addr = [](void* parent_ptr) noexcept -> void*
{
return &(({{ c.full_name }}*)parent_ptr)->{{ f.name }};
};
auto {{ f.name }}_get_const_addr = [](const void* parent_ptr) noexcept -> const void*
{
return &((const {{ c.full_name }}*)parent_ptr)->{{ f.name }};
};
## endfor
ObjectTypeInfo info {
/* TypeInfo */
{
/* name */ "{{ c.name }}",
/* setter */ {{ c.name }}_setter,
/* getter */ {{ c.name }}_getter
},
/* ObjectTypeInfo */
/* fields_info */
{
## for f in c.fields
{
/* type_info */
{
/* name */ "{{ f.type }}",
/* setter */ {{ f.name }}_setter,
/* getter */ {{ f.name }}_getter
},
/* name */ "{{ f.name }}",
/* get_addr */ {{ f.name }}_get_addr,
/* get_const_addr */ {{ f.name }}_get_const_addr,
/* get_object_type_info */ &DB::get_object_type_info<{{ f.type }}>
},
## endfor
},
/* fields_info_map */
{
/*
NOTE(FiTH): Since we are using addresses of elements from std::vector created inside local variable,
compiler must support NRVO or move constructor for std::vector or program will crash.
*/
## for f in c.fields
{ "{{ f.name }}", &info.fields_info[{{ loop.index }}] },
## endfor
}
};
return info;
}
template<>
/* static */ const ObjectTypeInfo* DB::get_object_type_info<{{ c.full_name }}>() noexcept
{
return &info_{{ loop.index }};
}
## endfor
} /* namespace rtti */