Skip to content

Commit abb3ae9

Browse files
committed
Merge #16: Reduce boost usage
fb73b81 Change EventLoop::m_task_set to not use boost (Vasil Dimov) 138ad67 Change Field::(param and result) to not use boost (Vasil Dimov) 5724a2c Remove boost usage from GetAnnotation() (Vasil Dimov) cab9c51 Remove unnecessary boost include (Vasil Dimov) Pull request description: A few changes that remove the usage of boost from some places in the code. Top commit has no ACKs. Tree-SHA512: 5dc9698affd5b226ed227fe8ca3ce69bb2d21b46df7e760cbd8f82d026cad51ebf9787c9a40ad61244995eff1e25c81d523a051b75c4bad057c192736c449a9a
2 parents e0319f4 + fb73b81 commit abb3ae9

File tree

3 files changed

+71
-51
lines changed

3 files changed

+71
-51
lines changed

include/mp/proxy-io.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <capnp/rpc-twoparty.h>
1616

1717
#include <functional>
18+
#include <memory>
1819
#include <string>
1920

2021
namespace mp {
@@ -199,7 +200,7 @@ class EventLoop
199200
LoggingErrorHandler m_error_handler{*this};
200201

201202
//! Capnp list of pending promises.
202-
boost::optional<kj::TaskSet> m_task_set;
203+
std::unique_ptr<kj::TaskSet> m_task_set;
203204

204205
//! List of connections.
205206
std::list<Connection> m_incoming_connections;

src/mp/gen.cpp

Lines changed: 64 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
#include <mp/config.h>
66
#include <mp/util.h>
77

8-
#include <boost/optional.hpp>
8+
#include <algorithm>
99
#include <capnp/schema-parser.h>
1010
#include <fstream>
1111
#include <map>
1212
#include <set>
1313
#include <sstream>
14+
#include <unistd.h>
1415
#include <vector>
1516

1617
#define PROXY_BIN "mpgen"
@@ -25,12 +26,38 @@ constexpr uint64_t NAME_ANNOTATION_ID = 0xb594888f63f4dbb9ull; // From prox
2526
constexpr uint64_t SKIP_ANNOTATION_ID = 0x824c08b82695d8ddull; // From proxy.capnp
2627

2728
template <typename Reader>
28-
boost::optional<capnp::schema::Value::Reader> GetAnnotation(const Reader& reader, uint64_t id)
29+
static bool AnnotationExists(const Reader& reader, uint64_t id)
2930
{
3031
for (const auto annotation : reader.getAnnotations()) {
31-
if (annotation.getId() == id) return annotation.getValue();
32+
if (annotation.getId() == id) {
33+
return true;
34+
}
35+
}
36+
return false;
37+
}
38+
39+
template <typename Reader>
40+
static bool GetAnnotationText(const Reader& reader, uint64_t id, kj::StringPtr* result)
41+
{
42+
for (const auto annotation : reader.getAnnotations()) {
43+
if (annotation.getId() == id) {
44+
*result = annotation.getValue().getText();
45+
return true;
46+
}
47+
}
48+
return false;
49+
}
50+
51+
template <typename Reader>
52+
static bool GetAnnotationInt32(const Reader& reader, uint64_t id, int32_t* result)
53+
{
54+
for (const auto annotation : reader.getAnnotations()) {
55+
if (annotation.getId() == id) {
56+
*result = annotation.getValue().getInt32();
57+
return true;
58+
}
3259
}
33-
return {};
60+
return false;
3461
}
3562

3663
using CharSlice = kj::ArrayPtr<const char>;
@@ -162,9 +189,7 @@ void Generate(kj::StringPtr src_prefix,
162189
h << "namespace mp {\n";
163190

164191
kj::StringPtr message_namespace;
165-
if (auto value = GetAnnotation(file_schema.getProto(), NAMESPACE_ANNOTATION_ID)) {
166-
message_namespace = value->getText();
167-
}
192+
GetAnnotationText(file_schema.getProto(), NAMESPACE_ANNOTATION_ID, &message_namespace);
168193

169194
std::string base_name = include_base;
170195
size_t output_slash = base_name.rfind("/");
@@ -202,9 +227,7 @@ void Generate(kj::StringPtr src_prefix,
202227
kj::StringPtr node_name = node_nested.getName();
203228
const auto& node = file_schema.getNested(node_name);
204229
kj::StringPtr proxied_class_type;
205-
if (auto proxy = GetAnnotation(node.getProto(), WRAP_ANNOTATION_ID)) {
206-
proxied_class_type = proxy->getText();
207-
}
230+
GetAnnotationText(node.getProto(), WRAP_ANNOTATION_ID, &proxied_class_type);
208231

209232
if (node.getProto().isStruct()) {
210233
const auto& struc = node.asStruct();
@@ -239,7 +262,7 @@ void Generate(kj::StringPtr src_prefix,
239262
dec << " using Accessors = std::tuple<";
240263
size_t i = 0;
241264
for (const auto field : struc.getFields()) {
242-
if (GetAnnotation(field.getProto(), SKIP_ANNOTATION_ID)) {
265+
if (AnnotationExists(field.getProto(), SKIP_ANNOTATION_ID)) {
243266
continue;
244267
}
245268
if (i) dec << ", ";
@@ -258,14 +281,12 @@ void Generate(kj::StringPtr src_prefix,
258281
inl << " using Struct = " << message_namespace << "::" << node_name << ";\n";
259282
size_t i = 0;
260283
for (const auto field : struc.getFields()) {
261-
if (GetAnnotation(field.getProto(), SKIP_ANNOTATION_ID)) {
284+
if (AnnotationExists(field.getProto(), SKIP_ANNOTATION_ID)) {
262285
continue;
263286
}
264287
auto field_name = field.getProto().getName();
265288
auto member_name = field_name;
266-
if (auto name = GetAnnotation(field.getProto(), NAME_ANNOTATION_ID)) {
267-
member_name = name->getText();
268-
}
289+
GetAnnotationText(field.getProto(), NAME_ANNOTATION_ID, &member_name);
269290
inl << " static auto get(std::integral_constant<size_t, " << i << ">) -> AUTO_RETURN("
270291
<< "&" << proxied_class_type << "::" << member_name << ")\n";
271292
++i;
@@ -300,9 +321,7 @@ void Generate(kj::StringPtr src_prefix,
300321
for (const auto method : interface.getMethods()) {
301322
kj::StringPtr method_name = method.getProto().getName();
302323
kj::StringPtr proxied_method_name = method_name;
303-
if (auto name = GetAnnotation(method.getProto(), NAME_ANNOTATION_ID)) {
304-
proxied_method_name = name->getText();
305-
}
324+
GetAnnotationText(method.getProto(), NAME_ANNOTATION_ID, &proxied_method_name);
306325

307326
const std::string method_prefix = Format() << message_namespace << "::" << node_name
308327
<< "::" << Cap(method_name);
@@ -311,8 +330,10 @@ void Generate(kj::StringPtr src_prefix,
311330

312331
struct Field
313332
{
314-
boost::optional<::capnp::StructSchema::Field> param;
315-
boost::optional<::capnp::StructSchema::Field> result;
333+
::capnp::StructSchema::Field param;
334+
bool param_is_set = false;
335+
::capnp::StructSchema::Field result;
336+
bool result_is_set = false;
316337
int args = 0;
317338
bool retval = false;
318339
bool optional = false;
@@ -326,7 +347,7 @@ void Generate(kj::StringPtr src_prefix,
326347
bool has_result = false;
327348

328349
auto add_field = [&](const ::capnp::StructSchema::Field& schema_field, bool param) {
329-
if (GetAnnotation(schema_field.getProto(), SKIP_ANNOTATION_ID)) {
350+
if (AnnotationExists(schema_field.getProto(), SKIP_ANNOTATION_ID)) {
330351
return;
331352
}
332353

@@ -336,39 +357,35 @@ void Generate(kj::StringPtr src_prefix,
336357
fields.emplace_back();
337358
}
338359
auto& field = fields[inserted.first->second];
339-
(param ? field.param : field.result) = schema_field;
360+
if (param) {
361+
field.param = schema_field;
362+
field.param_is_set = true;
363+
} else {
364+
field.result = schema_field;
365+
field.result_is_set = true;
366+
}
340367

341368
if (!param && field_name == "result") {
342369
field.retval = true;
343370
has_result = true;
344371
}
345372

346-
if (auto value = GetAnnotation(schema_field.getProto(), EXCEPTION_ANNOTATION_ID)) {
347-
field.exception = value->getText();
348-
}
373+
GetAnnotationText(schema_field.getProto(), EXCEPTION_ANNOTATION_ID, &field.exception);
349374

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();
375+
int32_t count = 1;
376+
if (!GetAnnotationInt32(schema_field.getProto(), COUNT_ANNOTATION_ID, &count)) {
377+
if (schema_field.getType().isStruct()) {
378+
GetAnnotationInt32(schema_field.getType().asStruct().getProto(),
379+
COUNT_ANNOTATION_ID, &count);
380+
} else if (schema_field.getType().isInterface()) {
381+
GetAnnotationInt32(schema_field.getType().asInterface().getProto(),
382+
COUNT_ANNOTATION_ID, &count);
362383
}
363384
}
364385

365386

366387
if (inserted.second && !field.retval && !field.exception.size()) {
367-
if (count) {
368-
field.args = *count;
369-
} else {
370-
field.args = 1;
371-
}
388+
field.args = count;
372389
}
373390
};
374391

@@ -385,7 +402,7 @@ void Generate(kj::StringPtr src_prefix,
385402
fields[field.second].optional = true;
386403
}
387404
auto want_field = field_idx.find("want" + Cap(field.first));
388-
if (want_field != field_idx.end() && fields[want_field->second].param) {
405+
if (want_field != field_idx.end() && fields[want_field->second].param_is_set) {
389406
fields[want_field->second].skip = true;
390407
fields[field.second].requested = true;
391408
}
@@ -408,12 +425,12 @@ void Generate(kj::StringPtr src_prefix,
408425
for (const auto& field : fields) {
409426
if (field.skip) continue;
410427

411-
auto field_name = field.param ? field.param->getProto().getName() :
412-
field.result ? field.result->getProto().getName() : "";
413-
auto field_type = field.param ? field.param->getType() : field.result->getType();
428+
const auto& f = field.param_is_set ? field.param : field.result;
429+
auto field_name = f.getProto().getName();
430+
auto field_type = f.getType();
414431

415432
std::ostringstream field_flags;
416-
field_flags << (!field.param ? "FIELD_OUT" : field.result ? "FIELD_IN | FIELD_OUT" : "FIELD_IN");
433+
field_flags << (!field.param_is_set ? "FIELD_OUT" : field.result_is_set ? "FIELD_IN | FIELD_OUT" : "FIELD_IN");
417434
if (field.optional) field_flags << " | FIELD_OPTIONAL";
418435
if (field.requested) field_flags << " | FIELD_REQUESTED";
419436
if (BoxedType(field_type)) field_flags << " | FIELD_BOXED";

src/mp/proxy.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
#include <assert.h>
1313
#include <atomic>
14-
#include <boost/optional.hpp>
1514
#include <capnp/blob.h>
1615
#include <capnp/capability.h>
1716
#include <condition_variable>
@@ -124,13 +123,16 @@ void Connection::addAsyncCleanup(std::function<void()> fn)
124123
}
125124

126125
EventLoop::EventLoop(const char* exe_name, LogFn log_fn, void* context)
127-
: m_exe_name(exe_name), m_io_context(kj::setupAsyncIo()), m_log_fn(std::move(log_fn)), m_context(context)
126+
: m_exe_name(exe_name),
127+
m_io_context(kj::setupAsyncIo()),
128+
m_log_fn(std::move(log_fn)),
129+
m_context(context),
130+
m_task_set(new kj::TaskSet(m_error_handler))
128131
{
129132
int fds[2];
130133
KJ_SYSCALL(socketpair(AF_UNIX, SOCK_STREAM, 0, fds));
131134
m_wait_fd = fds[0];
132135
m_post_fd = fds[1];
133-
m_task_set.emplace(m_error_handler);
134136
}
135137

136138
EventLoop::~EventLoop()

0 commit comments

Comments
 (0)