Skip to content

Commit 5724a2c

Browse files
committed
Remove boost usage from GetAnnotation()
Split GetAnnotation() to 3 functions that emit the result via an output parameter and return true/false to designate whether it was set or not.
1 parent cab9c51 commit 5724a2c

File tree

1 file changed

+46
-38
lines changed

1 file changed

+46
-38
lines changed

src/mp/gen.cpp

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,38 @@ constexpr uint64_t NAME_ANNOTATION_ID = 0xb594888f63f4dbb9ull; // From prox
2525
constexpr uint64_t SKIP_ANNOTATION_ID = 0x824c08b82695d8ddull; // From proxy.capnp
2626

2727
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)
2929
{
3030
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+
}
3258
}
33-
return {};
59+
return false;
3460
}
3561

3662
using CharSlice = kj::ArrayPtr<const char>;
@@ -162,9 +188,7 @@ void Generate(kj::StringPtr src_prefix,
162188
h << "namespace mp {\n";
163189

164190
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);
168192

169193
std::string base_name = include_base;
170194
size_t output_slash = base_name.rfind("/");
@@ -202,9 +226,7 @@ void Generate(kj::StringPtr src_prefix,
202226
kj::StringPtr node_name = node_nested.getName();
203227
const auto& node = file_schema.getNested(node_name);
204228
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);
208230

209231
if (node.getProto().isStruct()) {
210232
const auto& struc = node.asStruct();
@@ -239,7 +261,7 @@ void Generate(kj::StringPtr src_prefix,
239261
dec << " using Accessors = std::tuple<";
240262
size_t i = 0;
241263
for (const auto field : struc.getFields()) {
242-
if (GetAnnotation(field.getProto(), SKIP_ANNOTATION_ID)) {
264+
if (AnnotationExists(field.getProto(), SKIP_ANNOTATION_ID)) {
243265
continue;
244266
}
245267
if (i) dec << ", ";
@@ -258,14 +280,12 @@ void Generate(kj::StringPtr src_prefix,
258280
inl << " using Struct = " << message_namespace << "::" << node_name << ";\n";
259281
size_t i = 0;
260282
for (const auto field : struc.getFields()) {
261-
if (GetAnnotation(field.getProto(), SKIP_ANNOTATION_ID)) {
283+
if (AnnotationExists(field.getProto(), SKIP_ANNOTATION_ID)) {
262284
continue;
263285
}
264286
auto field_name = field.getProto().getName();
265287
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);
269289
inl << " static auto get(std::integral_constant<size_t, " << i << ">) -> AUTO_RETURN("
270290
<< "&" << proxied_class_type << "::" << member_name << ")\n";
271291
++i;
@@ -300,9 +320,7 @@ void Generate(kj::StringPtr src_prefix,
300320
for (const auto method : interface.getMethods()) {
301321
kj::StringPtr method_name = method.getProto().getName();
302322
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);
306324

307325
const std::string method_prefix = Format() << message_namespace << "::" << node_name
308326
<< "::" << Cap(method_name);
@@ -326,7 +344,7 @@ void Generate(kj::StringPtr src_prefix,
326344
bool has_result = false;
327345

328346
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)) {
330348
return;
331349
}
332350

@@ -343,32 +361,22 @@ void Generate(kj::StringPtr src_prefix,
343361
has_result = true;
344362
}
345363

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);
349365

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);
362374
}
363375
}
364376

365377

366378
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;
372380
}
373381
};
374382

0 commit comments

Comments
 (0)