Skip to content

Commit 10bb7e4

Browse files
committed
Merge #94: c++ 20 cleanups
6cbe56e refactor, moveonly: order lambda move captures first (Ryan Ofsky) 1f76880 util: Get rid of unused Discard struct (Ryan Ofsky) c92e90c proxy-types Drop JoinPromises function (Ryan Ofsky) Pull request description: There should be a lot of places where new C++ features can be used to simplify code, particularly concept features in C++20 so I'm starting to go through code and look for places where it can be modernized. Will collect the smaller cleanups in this PR and probably open other PRs with bigger cleanups. Top commit has no ACKs. Tree-SHA512: 33caec0a156f635dfe8f995b0e3f2cc4122a570df990f0c239637465cf600dd591909a4e2887ea14792abd78fd6b5e5f56f47dce8cbc6e09b260e22941fe7111
2 parents 3dc1d42 + 6cbe56e commit 10bb7e4

File tree

3 files changed

+20
-42
lines changed

3 files changed

+20
-42
lines changed

include/mp/proxy-io.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ class Connection
333333
// to the EventLoop TaskSet to avoid "Promise callback destroyed itself"
334334
// error in cases where f deletes this Connection object.
335335
m_on_disconnect.add(m_network.onDisconnect().then(
336-
[this, f = std::move(f)]() mutable { m_loop.m_task_set->add(kj::evalLater(kj::mv(f))); }));
336+
[f = std::move(f), this]() mutable { m_loop.m_task_set->add(kj::evalLater(kj::mv(f))); }));
337337
}
338338

339339
EventLoop& m_loop;

include/mp/proxy-types.h

+19-30
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,6 @@ void CustomBuildField(TypeList<>,
9595
context.setCallbackThread(callback_thread->second.m_client);
9696
}
9797

98-
// Invoke promise1, then promise2, and return result of promise2.
99-
template <typename T, typename U>
100-
kj::Promise<U> JoinPromises(kj::Promise<T>&& prom1, kj::Promise<U>&& prom2)
101-
{
102-
return prom1.then([prom2 = kj::mv(prom2)]() mutable { return kj::mv(prom2); });
103-
}
104-
10598
//! PassField override for mp.Context arguments. Return asynchronously and call
10699
//! function on other thread found in context.
107100
template <typename Accessor, typename ServerContext, typename Fn, typename... Args>
@@ -116,9 +109,8 @@ auto PassField(Priority<1>, TypeList<>, ServerContext& server_context, const Fn&
116109
auto& server = server_context.proxy_server;
117110
int req = server_context.req;
118111
auto invoke = MakeAsyncCallable(
119-
[&server, req, fn, args...,
120-
fulfiller = kj::mv(future.fulfiller),
121-
call_context = kj::mv(server_context.call_context)]() mutable {
112+
[fulfiller = kj::mv(future.fulfiller),
113+
call_context = kj::mv(server_context.call_context), &server, req, fn, args...]() mutable {
122114
const auto& params = call_context.getParams();
123115
Context::Reader context_arg = Accessor::get(params);
124116
ServerContext server_context{server, call_context, req};
@@ -201,27 +193,24 @@ auto PassField(Priority<1>, TypeList<>, ServerContext& server_context, const Fn&
201193
// be a local Thread::Server object, but it needs to be looked up
202194
// asynchronously with getLocalServer().
203195
auto thread_client = context_arg.getThread();
204-
return JoinPromises(server.m_context.connection->m_threads.getLocalServer(thread_client)
205-
.then([&server, invoke, req](const kj::Maybe<Thread::Server&>& perhaps) {
206-
// Assuming the thread object is found, pass it a pointer to the
207-
// `invoke` lambda above which will invoke the function on that
208-
// thread.
209-
KJ_IF_MAYBE(thread_server, perhaps)
210-
{
211-
const auto& thread = static_cast<ProxyServer<Thread>&>(*thread_server);
212-
server.m_context.connection->m_loop.log() << "IPC server post request #" << req << " {"
213-
<< thread.m_thread_context.thread_name << "}";
214-
thread.m_thread_context.waiter->post(std::move(invoke));
215-
}
216-
else
217-
{
218-
server.m_context.connection->m_loop.log() << "IPC server error request #" << req
219-
<< ", missing thread to execute request";
220-
throw std::runtime_error("invalid thread handle");
221-
}
222-
}),
196+
return server.m_context.connection->m_threads.getLocalServer(thread_client)
197+
.then([&server, invoke, req](const kj::Maybe<Thread::Server&>& perhaps) {
198+
// Assuming the thread object is found, pass it a pointer to the
199+
// `invoke` lambda above which will invoke the function on that
200+
// thread.
201+
KJ_IF_MAYBE (thread_server, perhaps) {
202+
const auto& thread = static_cast<ProxyServer<Thread>&>(*thread_server);
203+
server.m_context.connection->m_loop.log()
204+
<< "IPC server post request #" << req << " {" << thread.m_thread_context.thread_name << "}";
205+
thread.m_thread_context.waiter->post(std::move(invoke));
206+
} else {
207+
server.m_context.connection->m_loop.log()
208+
<< "IPC server error request #" << req << ", missing thread to execute request";
209+
throw std::runtime_error("invalid thread handle");
210+
}
211+
})
223212
// Wait for the invocation to finish before returning to the caller.
224-
kj::mv(future.promise));
213+
.then([invoke_wait = kj::mv(future.promise)]() mutable { return kj::mv(invoke_wait); });
225214
}
226215

227216
// Destination parameter type that can be passed to ReadField function as an

include/mp/util.h

-11
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,6 @@ struct Priority<0>
115115
{
116116
};
117117

118-
//! Function parameter type for discarded argument. Useful to be able to write a
119-
//! function overload that discards arguments as a normal function instead of a
120-
//! template function.
121-
struct Discard
122-
{
123-
template <typename... Args>
124-
Discard(Args&&...)
125-
{
126-
}
127-
};
128-
129118
//! Return capnp type name with filename prefix removed.
130119
template <typename T>
131120
const char* TypeName()

0 commit comments

Comments
 (0)