@@ -25,12 +25,38 @@ constexpr uint64_t NAME_ANNOTATION_ID = 0xb594888f63f4dbb9ull; // From prox
25
25
constexpr uint64_t SKIP_ANNOTATION_ID = 0x824c08b82695d8ddull ; // From proxy.capnp
26
26
27
27
template <typename Reader>
28
- boost::optional<capnp::schema::Value::Reader> GetAnnotation (const Reader& reader, uint64_t id)
28
+ static bool AnnotationExists (const Reader& reader, uint64_t id)
29
29
{
30
30
for (const auto annotation : reader.getAnnotations ()) {
31
- if (annotation.getId () == id) return annotation.getValue ();
31
+ if (annotation.getId () == id) {
32
+ return true ;
33
+ }
34
+ }
35
+ return false ;
36
+ }
37
+
38
+ template <typename Reader>
39
+ static bool GetAnnotationText (const Reader& reader, uint64_t id, kj::StringPtr* result)
40
+ {
41
+ for (const auto annotation : reader.getAnnotations ()) {
42
+ if (annotation.getId () == id) {
43
+ *result = annotation.getValue ().getText ();
44
+ return true ;
45
+ }
46
+ }
47
+ return false ;
48
+ }
49
+
50
+ template <typename Reader>
51
+ static bool GetAnnotationInt32 (const Reader& reader, uint64_t id, int32_t * result)
52
+ {
53
+ for (const auto annotation : reader.getAnnotations ()) {
54
+ if (annotation.getId () == id) {
55
+ *result = annotation.getValue ().getInt32 ();
56
+ return true ;
57
+ }
32
58
}
33
- return {} ;
59
+ return false ;
34
60
}
35
61
36
62
using CharSlice = kj::ArrayPtr<const char >;
@@ -162,9 +188,7 @@ void Generate(kj::StringPtr src_prefix,
162
188
h << " namespace mp {\n " ;
163
189
164
190
kj::StringPtr message_namespace;
165
- if (auto value = GetAnnotation (file_schema.getProto (), NAMESPACE_ANNOTATION_ID)) {
166
- message_namespace = value->getText ();
167
- }
191
+ GetAnnotationText (file_schema.getProto (), NAMESPACE_ANNOTATION_ID, &message_namespace);
168
192
169
193
std::string base_name = include_base;
170
194
size_t output_slash = base_name.rfind (" /" );
@@ -202,9 +226,7 @@ void Generate(kj::StringPtr src_prefix,
202
226
kj::StringPtr node_name = node_nested.getName ();
203
227
const auto & node = file_schema.getNested (node_name);
204
228
kj::StringPtr proxied_class_type;
205
- if (auto proxy = GetAnnotation (node.getProto (), WRAP_ANNOTATION_ID)) {
206
- proxied_class_type = proxy->getText ();
207
- }
229
+ GetAnnotationText (node.getProto (), WRAP_ANNOTATION_ID, &proxied_class_type);
208
230
209
231
if (node.getProto ().isStruct ()) {
210
232
const auto & struc = node.asStruct ();
@@ -239,7 +261,7 @@ void Generate(kj::StringPtr src_prefix,
239
261
dec << " using Accessors = std::tuple<" ;
240
262
size_t i = 0 ;
241
263
for (const auto field : struc.getFields ()) {
242
- if (GetAnnotation (field.getProto (), SKIP_ANNOTATION_ID)) {
264
+ if (AnnotationExists (field.getProto (), SKIP_ANNOTATION_ID)) {
243
265
continue ;
244
266
}
245
267
if (i) dec << " , " ;
@@ -258,14 +280,12 @@ void Generate(kj::StringPtr src_prefix,
258
280
inl << " using Struct = " << message_namespace << " ::" << node_name << " ;\n " ;
259
281
size_t i = 0 ;
260
282
for (const auto field : struc.getFields ()) {
261
- if (GetAnnotation (field.getProto (), SKIP_ANNOTATION_ID)) {
283
+ if (AnnotationExists (field.getProto (), SKIP_ANNOTATION_ID)) {
262
284
continue ;
263
285
}
264
286
auto field_name = field.getProto ().getName ();
265
287
auto member_name = field_name;
266
- if (auto name = GetAnnotation (field.getProto (), NAME_ANNOTATION_ID)) {
267
- member_name = name->getText ();
268
- }
288
+ GetAnnotationText (field.getProto (), NAME_ANNOTATION_ID, &member_name);
269
289
inl << " static auto get(std::integral_constant<size_t, " << i << " >) -> AUTO_RETURN("
270
290
<< " &" << proxied_class_type << " ::" << member_name << " )\n " ;
271
291
++i;
@@ -300,9 +320,7 @@ void Generate(kj::StringPtr src_prefix,
300
320
for (const auto method : interface.getMethods ()) {
301
321
kj::StringPtr method_name = method.getProto ().getName ();
302
322
kj::StringPtr proxied_method_name = method_name;
303
- if (auto name = GetAnnotation (method.getProto (), NAME_ANNOTATION_ID)) {
304
- proxied_method_name = name->getText ();
305
- }
323
+ GetAnnotationText (method.getProto (), NAME_ANNOTATION_ID, &proxied_method_name);
306
324
307
325
const std::string method_prefix = Format () << message_namespace << " ::" << node_name
308
326
<< " ::" << Cap (method_name);
@@ -326,7 +344,7 @@ void Generate(kj::StringPtr src_prefix,
326
344
bool has_result = false ;
327
345
328
346
auto add_field = [&](const ::capnp::StructSchema::Field& schema_field, bool param) {
329
- if (GetAnnotation (schema_field.getProto (), SKIP_ANNOTATION_ID)) {
347
+ if (AnnotationExists (schema_field.getProto (), SKIP_ANNOTATION_ID)) {
330
348
return ;
331
349
}
332
350
@@ -343,32 +361,22 @@ void Generate(kj::StringPtr src_prefix,
343
361
has_result = true ;
344
362
}
345
363
346
- if (auto value = GetAnnotation (schema_field.getProto (), EXCEPTION_ANNOTATION_ID)) {
347
- field.exception = value->getText ();
348
- }
364
+ GetAnnotationText (schema_field.getProto (), EXCEPTION_ANNOTATION_ID, &field.exception );
349
365
350
- boost::optional<int > count;
351
- if (auto value = GetAnnotation (schema_field.getProto (), COUNT_ANNOTATION_ID)) {
352
- count = value->getInt32 ();
353
- } else if (schema_field.getType ().isStruct ()) {
354
- if (auto value =
355
- GetAnnotation (schema_field.getType ().asStruct ().getProto (), COUNT_ANNOTATION_ID)) {
356
- count = value->getInt32 ();
357
- }
358
- } else if (schema_field.getType ().isInterface ()) {
359
- if (auto value =
360
- GetAnnotation (schema_field.getType ().asInterface ().getProto (), COUNT_ANNOTATION_ID)) {
361
- count = value->getInt32 ();
366
+ int32_t count = 1 ;
367
+ if (!GetAnnotationInt32 (schema_field.getProto (), COUNT_ANNOTATION_ID, &count)) {
368
+ if (schema_field.getType ().isStruct ()) {
369
+ GetAnnotationInt32 (schema_field.getType ().asStruct ().getProto (),
370
+ COUNT_ANNOTATION_ID, &count);
371
+ } else if (schema_field.getType ().isInterface ()) {
372
+ GetAnnotationInt32 (schema_field.getType ().asInterface ().getProto (),
373
+ COUNT_ANNOTATION_ID, &count);
362
374
}
363
375
}
364
376
365
377
366
378
if (inserted.second && !field.retval && !field.exception .size ()) {
367
- if (count) {
368
- field.args = *count;
369
- } else {
370
- field.args = 1 ;
371
- }
379
+ field.args = count;
372
380
}
373
381
};
374
382
0 commit comments