-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextended_cpp_generator.cc
More file actions
363 lines (312 loc) · 15.2 KB
/
extended_cpp_generator.cc
File metadata and controls
363 lines (312 loc) · 15.2 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include "extended_cpp_generator.h"
#define TAB_SIZE 2
namespace extended_cpp_generator {
std::string CppNameFromProtoName(const std::string& proto_name) {
std::string cpp_name = proto_name;
size_t start_pos = 0;
while ((start_pos = cpp_name.find(".", start_pos)) != std::string::npos) {
cpp_name.replace(start_pos, 1, "::");
start_pos += 2;
}
return cpp_name;
}
std::string StubTypeFromService(const grpc::protobuf::ServiceDescriptor* service) {
std::string stub_type;
stub_type = CppNameFromProtoName(service->full_name()) + "::Stub";
stub_type = "std::shared_ptr<" + stub_type + ">";
return stub_type;
}
std::string Tabs(int count) {
return std::string(count * TAB_SIZE, ' ');
}
std::string ConvertToCppType(google::protobuf::FieldDescriptor::CppType type) {
std::string cpp_type;
switch (type) {
case google::protobuf::FieldDescriptor::CppType::CPPTYPE_INT32:
cpp_type = "int32_t";
break;
case google::protobuf::FieldDescriptor::CppType::CPPTYPE_INT64:
cpp_type = "int64_t";
break;
case google::protobuf::FieldDescriptor::CppType::CPPTYPE_UINT32:
cpp_type = "uint32_t";
break;
case google::protobuf::FieldDescriptor::CppType::CPPTYPE_UINT64:
cpp_type = "uint64_t";
break;
case google::protobuf::FieldDescriptor::CppType::CPPTYPE_DOUBLE:
cpp_type = "double";
break;
case google::protobuf::FieldDescriptor::CppType::CPPTYPE_FLOAT:
cpp_type = "float";
break;
case google::protobuf::FieldDescriptor::CppType::CPPTYPE_BOOL:
cpp_type = "bool";
break;
case google::protobuf::FieldDescriptor::CppType::CPPTYPE_STRING:
cpp_type = "std::string";
break;
default:
throw std::runtime_error("Unsupported type");
}
return cpp_type;
}
std::string GetHeaderCreateFunctions(const grpc::protobuf::FileDescriptor* file) {
std::stringstream output;
for(int message_index = 0; message_index < file->message_type_count(); message_index++) {
const google::protobuf::Descriptor* message = file->message_type(message_index);
std::string message_name = message->name();
output << message_name << " Create" << message_name << "(";
if (message->field_count() > 0) {
output << "\n";
}
// function paramters
for (int field_index = 0; field_index < message->field_count(); field_index++) {
const google::protobuf::FieldDescriptor* field = message->field(field_index);
std::string field_name = field->name();
google::protobuf::FieldDescriptor::CppType type = field->cpp_type();
std::string field_type;
if (type == google::protobuf::FieldDescriptor::CppType::CPPTYPE_MESSAGE) {
const google::protobuf::Descriptor* nested_message = field->message_type();
field_type = CppNameFromProtoName(nested_message->full_name()) + "*";
} else if (type == google::protobuf::FieldDescriptor::CppType::CPPTYPE_ENUM) {
const google::protobuf::EnumDescriptor* enum_type = field->enum_type();
field_type = CppNameFromProtoName(enum_type->full_name());
} else {
field_type = ConvertToCppType(type);
}
if (field->is_map()) {
auto key_field = field->message_type()->field(0);
std::string key_type;
if (key_field->cpp_type() == google::protobuf::FieldDescriptor::CppType::CPPTYPE_MESSAGE) {
key_type = CppNameFromProtoName(key_field->message_type()->full_name()) + "*";
} else if (key_field->cpp_type() == google::protobuf::FieldDescriptor::CppType::CPPTYPE_ENUM) {
key_type = CppNameFromProtoName(key_field->enum_type()->full_name());
} else {
key_type = ConvertToCppType(key_field->cpp_type());
}
auto value_field = field->message_type()->field(1);
std::string value_type;
if (value_field->cpp_type() == google::protobuf::FieldDescriptor::CppType::CPPTYPE_MESSAGE) {
value_type = CppNameFromProtoName(value_field->message_type()->full_name());
} else if (value_field->cpp_type() == google::protobuf::FieldDescriptor::CppType::CPPTYPE_ENUM) {
value_type = CppNameFromProtoName(value_field->enum_type()->full_name());
} else {
value_type = ConvertToCppType(value_field->cpp_type());
}
field_type = "std::map<" + key_type + ", " + value_type + ">";
} else if (field->is_repeated()) {
field_type = "std::vector<" + field_type + ">";
}
output << Tabs(2) << field_type << " " << field_name;
if (field_index < message->field_count() - 1) {
output << ",\n";
}
}
output << ");\n\n";
}
return output.str();
}
std::string GetSourceCreateFunctions(const grpc::protobuf::FileDescriptor* file) {
std::stringstream output;
for(int message_index = 0; message_index < file->message_type_count(); message_index++) {
const google::protobuf::Descriptor* message = file->message_type(message_index);
std::string message_name = message->name();
output << message_name << " Create" << message_name << "(";
if (message->field_count() > 0) {
output << "\n";
}
// function paramters
for (int field_index = 0; field_index < message->field_count(); field_index++) {
const google::protobuf::FieldDescriptor* field = message->field(field_index);
std::string field_name = field->name();
google::protobuf::FieldDescriptor::CppType type = field->cpp_type();
std::string field_type;
if (type == google::protobuf::FieldDescriptor::CppType::CPPTYPE_MESSAGE) {
const google::protobuf::Descriptor* nested_message = field->message_type();
field_type = CppNameFromProtoName(nested_message->full_name()) + "*";
} else if (type == google::protobuf::FieldDescriptor::CppType::CPPTYPE_ENUM) {
const google::protobuf::EnumDescriptor* enum_type = field->enum_type();
field_type = CppNameFromProtoName(enum_type->full_name());
} else {
field_type = ConvertToCppType(type);
}
if (field->is_map()) {
auto key_field = field->message_type()->field(0);
std::string key_type;
if (key_field->cpp_type() == google::protobuf::FieldDescriptor::CppType::CPPTYPE_MESSAGE) {
key_type = CppNameFromProtoName(key_field->message_type()->full_name()) + "*";
} else if (key_field->cpp_type() == google::protobuf::FieldDescriptor::CppType::CPPTYPE_ENUM) {
key_type = CppNameFromProtoName(key_field->enum_type()->full_name());
} else {
key_type = ConvertToCppType(key_field->cpp_type());
}
auto value_field = field->message_type()->field(1);
std::string value_type;
if (value_field->cpp_type() == google::protobuf::FieldDescriptor::CppType::CPPTYPE_MESSAGE) {
value_type = CppNameFromProtoName(value_field->message_type()->full_name());
} else if (value_field->cpp_type() == google::protobuf::FieldDescriptor::CppType::CPPTYPE_ENUM) {
value_type = CppNameFromProtoName(value_field->enum_type()->full_name());
} else {
value_type = ConvertToCppType(value_field->cpp_type());
}
field_type = "std::map<" + key_type + ", " + value_type + ">";
} else if (field->is_repeated()) {
field_type = "std::vector<" + field_type + ">";
}
output << Tabs(2) << field_type << " " << field_name;
if (field_index < message->field_count() - 1) {
output << ",\n";
}
}
output << ") {\n";
// function body
output << Tabs(1) << message_name << " res;\n";
for (int field_index = 0; field_index < message->field_count(); field_index++) {
const google::protobuf::FieldDescriptor* field = message->field(field_index);
std::string field_name = field->name();
if (field->is_repeated()) {
output << Tabs(1) << "res.mutable_" << field_name << "()->";
if (field->is_map()) {
output << "insert";
} else {
output << "Add";
}
output << "(" << field_name << ".begin(), " << field_name << ".end());\n";
} else if (field->cpp_type() == google::protobuf::FieldDescriptor::CppType::CPPTYPE_MESSAGE) {
output << Tabs(1) << "res.set_allocated_" << field_name << "(" << field_name << ");\n";
} else {
output << Tabs(1) << "res.set_" << field_name << "(" << field_name << ");\n";
}
}
output << Tabs(1) << "return res;\n";
output << "}\n\n";
}
return output.str();
}
std::string GetIncludeGuard(const grpc::protobuf::FileDescriptor* file) {
std::string output;
output = file->name();
size_t start_pos = 0;
while ((start_pos = output.find("/", start_pos)) != std::string::npos) {
output.replace(start_pos, 1, "_");
start_pos += 1;
}
start_pos = 0;
while ((start_pos = output.find(".", start_pos)) != std::string::npos) {
output.replace(start_pos, 1, "_");
start_pos += 1;
}
output = "GENERATED_" + output + "_EXT_H";
return output;
}
// Convert from "a/b/c.proto" to "#include \"a/b/c$message_header_ext$\"\n"
std::string ImportInludeFromProtoName(const std::string& proto_name, const std::string& extension) {
return std::string("#include \"") +
proto_name.substr(0, proto_name.size() - 6) +
extension + "\"\n";
}
std::string GetHeaderCallMethodFunction(const grpc::protobuf::MethodDescriptor* method) {
std::stringstream output;
output << "grpc::Status Call" << method->name() << "(\n";
output << Tabs(2) << "const " << StubTypeFromService(method->service()) << " &stub,\n";
output << Tabs(2) << CppNameFromProtoName(method->input_type()->full_name()) << "& request,\n";
output << Tabs(2) << CppNameFromProtoName(method->output_type()->full_name()) << "* response);\n";
return output.str();
}
std::string GetSourceCallMethodFunction(const grpc::protobuf::MethodDescriptor* method) {
std::stringstream output;
output << "grpc::Status Call" << method->name() << "(\n";
output << Tabs(2) << "const " << StubTypeFromService(method->service()) << " &stub,\n";
output << Tabs(2) << CppNameFromProtoName(method->input_type()->full_name()) << "& request,\n";
output << Tabs(2) << CppNameFromProtoName(method->output_type()->full_name()) << "* response) {\n";
output << Tabs(1) << "grpc::ClientContext context;\n";
output << Tabs(1) << "grpc::Status result;\n";
output << Tabs(1) << "bool done = false;\n";
output << Tabs(1) << "std::mutex mu;\n";
output << Tabs(1) << "std::condition_variable cv;\n";
output << Tabs(1) << "stub->async()->" << method->name() << "(\n";
output << Tabs(2) << "&context, &request, response,\n";
output << Tabs(2) << "[&result, &mu, &cv, &done, response](grpc::Status status) {\n";
output << Tabs(3) << "result = std::move(status);\n";
output << Tabs(3) << "std::lock_guard<std::mutex> lock(mu);\n";
output << Tabs(3) << "done = true;\n";
output << Tabs(3) << "cv.notify_one();\n";
output << Tabs(2) << "});\n";
output << Tabs(1) << "std::unique_lock<std::mutex> lock(mu);\n";
output << Tabs(1) << "cv.wait(lock, [&done] { return done; });\n";
output << Tabs(1) << "return result;\n";
output << "}\n";
return output.str();
}
std::string GetHeaderPrologue(const grpc::protobuf::FileDescriptor* file) {
std::stringstream output;
output << "#ifndef " << GetIncludeGuard(file) << "\n";
output << "#define " << GetIncludeGuard(file) << "\n\n";
return output.str();
}
std::string GetHeaderIncludes(const grpc::protobuf::FileDescriptor* file) {
std::stringstream output;
output << ImportInludeFromProtoName(file->name(), ".grpc.pb.h");
for (int i = 0; i < file->dependency_count(); i++) {
const auto& dep = *file->dependency(i);
output << ImportInludeFromProtoName(dep.name(), ".grpc-ext.pb.h");
}
output << "\n";
return output.str();
}
std::string GetHeaderServices(const grpc::protobuf::FileDescriptor* file) {
std::stringstream output;
output << "namespace " << file->package() << " {\n\n";
for (size_t service_index = 0; service_index < file->service_count(); service_index++) {
const google::protobuf::ServiceDescriptor* service = file->service(service_index);
for (size_t method_index = 0; method_index < service->method_count(); method_index++) {
const google::protobuf::MethodDescriptor* method = service->method(method_index);
if (method->client_streaming() || method->server_streaming()) {
continue;
}
output << GetHeaderCallMethodFunction(method) << "\n";
}
}
output << "} // namespace " << file->package() << "\n\n";
return output.str();
}
std::string GetHeaderEpilogue(const grpc::protobuf::FileDescriptor* file) {
std::stringstream output;
output << "#endif // " << GetIncludeGuard(file) << "\n";
return output.str();
}
std::string GetSourcePrologue(const grpc::protobuf::FileDescriptor* file) {
std::stringstream output;
return output.str();
}
std::string GetSourceIncludes(const grpc::protobuf::FileDescriptor* file) {
std::stringstream output;
output << ImportInludeFromProtoName(file->name(), ".grpc-ext.pb.h");
output << "\n";
output << "#include <condition_variable>\n";
output << "#include <mutex>\n";
output << "\n";
return output.str();
}
std::string GetSourceServices(const grpc::protobuf::FileDescriptor* file) {
std::stringstream output;
output << "namespace " << file->package() << " {\n\n";
for (size_t service_index = 0; service_index < file->service_count(); service_index++) {
const google::protobuf::ServiceDescriptor* service = file->service(service_index);
for (size_t method_index = 0; method_index < service->method_count(); method_index++) {
const google::protobuf::MethodDescriptor* method = service->method(method_index);
if (method->client_streaming() || method->server_streaming()) {
continue;
}
output << GetSourceCallMethodFunction(method) << "\n";
}
}
output << "} // namespace " << file->package() << "\n\n";
return output.str();
}
std::string GetSourceEpilogue(const grpc::protobuf::FileDescriptor* file) {
std::stringstream output;
return output.str();
}
} // namespace extended_cpp_generator