-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathTypeInfoGenerator.cpp
More file actions
255 lines (238 loc) · 7.76 KB
/
TypeInfoGenerator.cpp
File metadata and controls
255 lines (238 loc) · 7.76 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
251
252
253
254
255
#include "TypeInfoGenerator.h"
#include <sstream>
using namespace Ubpa::USRefl;
using namespace std;
string TypeInfoGenerator::Generate(const vector<TypeMeta>& typeMetas) {
stringstream ss;
constexpr auto indent = " ";
ss
<< "// This file is generated by Ubpa::USRefl::AutoRefl" << endl
<< endl
<< "#pragma once" << endl
<< endl
<< "#include <USRefl/USRefl.h>" << endl
<< endl;
for (const auto& typeMeta : typeMetas) {
const auto fullname = typeMeta.GenerateFullName();
const std::string nsname = typeMeta.GenerateNsName();
const std::string tname = typeMeta.IsTemplateType() ? fullname : "Type";
ss
<< "template<" << typeMeta.GenerateTemplateList() << ">" << endl
<< "struct Ubpa::USRefl::TypeInfo<" << fullname << "> :" << endl;
ss << indent << "TypeInfoBase<" << fullname;
auto publicBaseIndice = typeMeta.GetPublicBaseIndices();
if(!publicBaseIndice.empty()) {
if (publicBaseIndice.size() > 1) {
ss << "," << endl;
for (std::size_t i = 0; i < publicBaseIndice.size(); i++) {
const auto& base = typeMeta.bases[publicBaseIndice[i]];
ss << indent << indent << base.GenerateText();
if (i != publicBaseIndice.size() - 1)
ss << ",";
ss << endl;
}
ss << indent;
}
else
ss << ", " << typeMeta.bases.front().GenerateText();
}
ss
<< ">" << endl
<< "{" << endl;
// attributes
switch (config.attrListConstMode) {
case ConstMode::Constepxr:
ss << indent << "static constexpr AttrList attrs = {";
break;
case ConstMode::Const:
ss << indent << "inline static const AttrList attrs = {";
break;
case ConstMode::NonConst:
ss << indent << "inline static AttrList attrs = {";
break;
}
if (!typeMeta.attrs.empty()) {
ss << endl;
for (const auto& attr : typeMeta.attrs) {
auto name = attr.GenerateName(
attr.ns.empty() ?
config.nonNamespaceNameWithoutQuotation
: !config.namespaceNameWithQuotation
);
ss << indent << indent << "Attr {" << name;
if (!attr.value.empty())
ss << ", " << attr.GenerateValue(config.isAttrValueToFunction);
ss << "}," << endl;
}
ss << indent;
}
ss << "};" << endl;
// fields
switch (config.attrListConstMode) {
case ConstMode::Constepxr:
ss << indent << "static constexpr FieldList fields = {";
break;
case ConstMode::Const:
ss << indent << "inline static const FieldList fields = {";
break;
case ConstMode::NonConst:
ss << indent << "inline static FieldList fields = {";
break;
}
if(typeMeta.HaveAnyOutputField()) {
ss << endl;
for (const auto& field : typeMeta.fields) {
if (field.isTemplate
|| field.accessSpecifier != AccessSpecifier::PUBLIC
|| field.IsFriendFunction()
|| field.IsDeletedFunction()
)
continue;
ss << indent << indent << "Field {";
// [name]
Attr attr;
if (field.name == typeMeta.name) {
// constructor
attr.ns = config.nameof_namespace;
attr.name = config.nameof_constructor;
}
else if (field.name == ("~" + typeMeta.name)) {
// destructor
attr.ns = config.nameof_namespace;
attr.name = config.nameof_destructor;
}
else
attr.name = field.name;
ss
<< attr.GenerateName(
attr.ns.empty() ?
config.nonNamespaceNameWithoutQuotation
: !config.namespaceNameWithQuotation
) << ", ";
// [value]
switch (field.mode) {
case Field::Mode::Variable: {
ss << "&" << tname << "::" << field.name;
break;
}
case Field::Mode::Function: {
if (field.name == typeMeta.name) {
// constructor
ss << "WrapConstructor<" << tname << "(" << field.GenerateParamTypeList() << ")>()";
}
else if(field.name == ("~" + typeMeta.name)) {
// destructor
ss << "WrapDestructor<" << tname << ">()";
}
else if (typeMeta.IsOverloaded(field.name))
ss << "static_cast<" << field.GenerateFunctionType(tname) << ">(&" << tname << "::" << field.name << ")";
else
ss << "&" << tname << "::" << field.name;
break;
}
case Field::Mode::Value: {
ss << tname << "::" << field.name;
break;
}
}
// attributes
const bool hasInitializerAttr = config.isInitializerAsAttr
&& field.mode != Field::Mode::Value && !field.initializer.empty();
const bool hasDefaultFunctionsAttr = config.generateDefaultFunctions
&& field.mode == Field::Mode::Function && field.GetDefaultParameterNum() > 0;
if (!field.attrs.empty() || hasInitializerAttr || hasDefaultFunctionsAttr) {
ss
<< ", AttrList {" << endl;
// [initializer]
if (hasInitializerAttr) {
Attr attr;
attr.ns = config.nameof_namespace;
attr.name = config.nameof_initializer;
attr.value = field.initializer;
auto name = attr.GenerateName(
attr.ns.empty() ?
config.nonNamespaceNameWithoutQuotation
: !config.namespaceNameWithQuotation
);
ss
<< indent << indent << indent
<< "Attr {" << name << ", "
<< attr.GenerateValue(field.GenerateSimpleFieldType()) << "}," << endl;
}
// [default functions]
if (hasDefaultFunctionsAttr) {
Attr attr;
attr.ns = config.nameof_namespace;
attr.name = config.nameof_default_functions;
auto name = attr.GenerateName(
attr.ns.empty() ?
config.nonNamespaceNameWithoutQuotation
: !config.namespaceNameWithQuotation
);
ss
<< indent << indent << indent
<< "Attr {" << name << ", std::tuple {" << endl;
const std::size_t defaultParameterNum = field.GetDefaultParameterNum();
const bool isConstructor = field.name == typeMeta.name;
for (std::size_t i = 1; i <= defaultParameterNum; i++) {
std::size_t num = field.parameters.size() - i;
if(isConstructor) {
ss
<< indent << indent << indent << indent
<< "WrapConstructor<" << tname << "(" << field.GenerateParamTypeList(num) << ")>()";
}
else if (field.IsMemberFunction()) {
auto qualifiers = field.GenerateQualifiers();
bool isPointer = qualifiers.find('&') == std::string::npos;
if (isPointer)
qualifiers += "*";
auto ftname = tname + " " + qualifiers;
ss
<< indent << indent << indent << indent
<< "[](" << ftname << " this_" << (num > 0 ? ", " : "")
<< field.GenerateNamedParameterList(num) << "){ return "
<< (isPointer? "this_->" : ("std::forward<" + ftname + ">(this_)."))
<< field.name << "(" << field.GenerateForwardArgumentList(num) << "); }";
}
else { // static
auto qualifiers = field.GenerateQualifiers();
bool isPointer = qualifiers.find('&') == std::string::npos;
if (isPointer)
qualifiers += "*";
auto ftname = tname + " " + qualifiers;
ss
<< indent << indent << indent << indent
<< "[](" << field.GenerateNamedParameterList(num) << "){ return "
<< tname << "::" << field.name << "(" << field.GenerateForwardArgumentList(num) << "); }";
}
if (i != defaultParameterNum)
ss << ",";
ss << endl;
}
ss << indent << indent << indent << "}}," << endl; // attr
}
// [normal attrs]
for (const auto& attr : field.attrs) {
auto name = attr.GenerateName(
attr.ns.empty() ?
config.nonNamespaceNameWithoutQuotation
: !config.namespaceNameWithQuotation
);
ss << indent << indent << indent
<< "Attr {" << name;
if (!attr.value.empty())
ss << ", " << attr.GenerateValue(config.isAttrValueToFunction);
ss << "}," << endl;
}
ss
<< indent << indent << "}";
}
ss << "}," << endl;
}
ss << indent;
}
ss << "};" << endl; // end of fields
ss << "};" << endl << endl; // end of TypeInfo
}
return ss.str();
}