@@ -76,37 +76,55 @@ using VoidT = std::monostate;
7676
7777template <typename T> using Callback = llvm::unique_function<T>;
7878
79+ // / A handler for the response to an outgoing request.
7980template <typename T>
8081using Reply = typename std::conditional<
81- std::is_same_v<T, VoidT> == true , llvm::unique_function<void (llvm::Error)>,
82+ std::is_same_v<T, VoidT> || std::is_void_v<T>,
83+ llvm::unique_function<void (llvm::Error)>,
8284 llvm::unique_function<void (llvm::Expected<T>)>>::type;
8385
86+ // / A function to send an outgoing request and receive a response.
8487template <typename Result, typename Params>
8588using OutgoingRequest = typename std::conditional<
86- std::is_same_v<Params, VoidT> == true ,
89+ std::is_same_v<Params, VoidT> || std::is_void_v<Params> ,
8790 llvm::unique_function<void (Reply<Result>)>,
8891 llvm::unique_function<void (const Params &, Reply<Result>)>>::type;
8992
93+ // / A function to send an outgoing event.
9094template <typename Params>
9195using OutgoingEvent = typename std::conditional<
92- std::is_same_v<Params, VoidT> == true , llvm::unique_function<void ()>,
96+ std::is_same_v<Params, VoidT> || std::is_void_v<Params>,
97+ llvm::unique_function<void ()>,
9398 llvm::unique_function<void (const Params &)>>::type;
9499
100+ // / Creates a request with the given id, method, and optional params.
95101template <typename Id, typename Req>
96- Req make_request (Id id, llvm::StringRef method,
97- std::optional<llvm::json::Value> params = std::nullopt );
102+ Req MakeRequest (Id, llvm::StringRef, std::optional<llvm::json::Value>);
103+
104+ // / Creates an error response for a given request.
98105template <typename Req, typename Resp>
99- Resp make_response (const Req &req, llvm::Error error);
106+ Resp MakeResponse (const Req &, llvm::Error);
107+
108+ // / Creates a success response for a given request.
100109template <typename Req, typename Resp>
101- Resp make_response (const Req &req, llvm::json::Value result);
110+ Resp MakeResponse (const Req &, llvm::json::Value);
111+
112+ // / Creates an event.
102113template <typename Evt>
103- Evt make_event (llvm::StringRef method,
104- std::optional<llvm::json::Value> params = std::nullopt );
114+ Evt MakeEvent (llvm::StringRef, std::optional<llvm::json::Value>);
115+
116+ // / Extracts the result value from a response.
105117template <typename Resp>
106- llvm::Expected<llvm::json::Value> get_result (const Resp &resp);
107- template <typename Id, typename T> Id get_id (const T &);
108- template <typename T> llvm::StringRef get_method (const T &);
109- template <typename T> llvm::json::Value get_params (const T &);
118+ llvm::Expected<llvm::json::Value> GetResult (const Resp &);
119+
120+ // / Extracts the id from a response.
121+ template <typename Id, typename Resp> Id GetId (const Resp &);
122+
123+ // / Extracts the method from a request or event.
124+ template <typename T> llvm::StringRef GetMethod (const T &);
125+
126+ // / Extracts the parameters from a request or event.
127+ template <typename T> llvm::json::Value GetParams (const T &);
110128
111129// / A transport is responsible for maintaining the connection to a client
112130// / application, and reading/writing structured messages to it.
@@ -193,7 +211,7 @@ class JSONTransport {
193211 ~ReplyOnce () {
194212 if (transport && handler && !replied) {
195213 assert (false && " must reply to all calls!" );
196- (*this )(make_response <Req, Resp>(
214+ (*this )(MakeResponse <Req, Resp>(
197215 req, llvm::createStringError (" failed to reply" )));
198216 }
199217 }
@@ -257,9 +275,9 @@ class JSONTransport {
257275 return std::move (result);
258276 }
259277
260- // / Bind a handler for a request.
261- // / e.g. `bind("peek", &ThisModule::peek, this, std::placeholders::_1 );`.
262- // / Handler should be e.g. `Expected<PeekResult> peek(const PeekParams&);`
278+ // / Bind a handler for an incoming request.
279+ // / e.g. `bind("peek", &ThisModule::peek, this);`.
280+ // / Handler should be e.g. `Expected<PeekResult> peek(const PeekParams&);`
263281 // / PeekParams must be JSON parsable and PeekResult must be serializable.
264282 template <typename Result, typename Params, typename Fn, typename ... Args>
265283 void bind (llvm::StringLiteral method, Fn &&fn, Args &&...args) {
@@ -273,30 +291,30 @@ class JSONTransport {
273291 llvm::Expected<Result> result = std::invoke (
274292 std::forward<Fn>(fn), std::forward<Args>(args)...);
275293 if (!result)
276- return reply (make_response <Req, Resp>(req, result.takeError ()));
277- reply (make_response <Req, Resp>(req, toJSON (*result)));
294+ return reply (MakeResponse <Req, Resp>(req, result.takeError ()));
295+ reply (MakeResponse <Req, Resp>(req, toJSON (*result)));
278296 };
279297 } else {
280298 m_request_handlers[method] =
281299 [method, fn,
282300 args...](const Req &req,
283301 llvm::unique_function<void (const Resp &)> reply) mutable {
284302 llvm::Expected<Params> params =
285- parse<Params>(get_params <Req>(req), method);
303+ parse<Params>(GetParams <Req>(req), method);
286304 if (!params)
287- return reply (make_response <Req, Resp>(req, params.takeError ()));
305+ return reply (MakeResponse <Req, Resp>(req, params.takeError ()));
288306
289307 llvm::Expected<Result> result = std::invoke (
290308 std::forward<Fn>(fn), std::forward<Args>(args)..., *params);
291309 if (!result)
292- return reply (make_response <Req, Resp>(req, result.takeError ()));
310+ return reply (MakeResponse <Req, Resp>(req, result.takeError ()));
293311
294- reply (make_response <Req, Resp>(req, toJSON (*result)));
312+ reply (MakeResponse <Req, Resp>(req, toJSON (*result)));
295313 };
296314 }
297315 }
298316
299- // / Bind a handler for a event.
317+ // / Bind a handler for an incoming event.
300318 // / e.g. `bind("peek", &ThisModule::peek, this);`
301319 // / Handler should be e.g. `void peek(const PeekParams&);`
302320 // / PeekParams must be JSON parsable.
@@ -312,7 +330,7 @@ class JSONTransport {
312330 m_event_handlers[method] = [this , method, fn,
313331 args...](const Evt &evt) mutable {
314332 llvm::Expected<Params> params =
315- parse<Params>(get_params <Evt>(evt), method);
333+ parse<Params>(GetParams <Evt>(evt), method);
316334 if (!params)
317335 return OnError (params.takeError ());
318336 std::invoke (std::forward<Fn>(fn), std::forward<Args>(args)...,
@@ -330,10 +348,10 @@ class JSONTransport {
330348 return [this , method](Reply<Result> fn) {
331349 std::scoped_lock<std::recursive_mutex> guard (m_mutex);
332350 Id id = ++m_seq;
333- Req req = make_request <Req, Resp>(id, method, std::nullopt );
351+ Req req = MakeRequest <Req, Resp>(id, method, std::nullopt );
334352 m_pending_responses[id] = [fn = std::move (fn),
335353 method](const Resp &resp) mutable {
336- llvm::Expected<llvm::json::Value> result = get_result <Resp>(resp);
354+ llvm::Expected<llvm::json::Value> result = GetResult <Resp>(resp);
337355 if (!result)
338356 return fn (result.takeError ());
339357 fn (parse<Result>(*result, method));
@@ -345,11 +363,10 @@ class JSONTransport {
345363 return [this , method](const Params ¶ms, Reply<Result> fn) {
346364 std::scoped_lock<std::recursive_mutex> guard (m_mutex);
347365 Id id = ++m_seq;
348- Req req =
349- make_request<Id, Req>(id, method, llvm::json::Value (params));
366+ Req req = MakeRequest<Id, Req>(id, method, llvm::json::Value (params));
350367 m_pending_responses[id] = [fn = std::move (fn),
351368 method](const Resp &resp) mutable {
352- llvm::Expected<llvm::json::Value> result = get_result <Resp>(resp);
369+ llvm::Expected<llvm::json::Value> result = GetResult <Resp>(resp);
353370 if (llvm::Error err = result.takeError ())
354371 return fn (std::move (err));
355372 fn (parse<Result>(*result, method));
@@ -368,21 +385,21 @@ class JSONTransport {
368385 if constexpr (std::is_void_v<Params> || std::is_same_v<VoidT, Params>) {
369386 return [this , method]() {
370387 if (llvm::Error error =
371- m_transport.Send (make_event <Evt>(method, std::nullopt )))
388+ m_transport.Send (MakeEvent <Evt>(method, std::nullopt )))
372389 OnError (std::move (error));
373390 };
374391 } else {
375392 return [this , method](const Params ¶ms) {
376393 if (llvm::Error error =
377- m_transport.Send (make_event <Evt>(method, toJSON (params))))
394+ m_transport.Send (MakeEvent <Evt>(method, toJSON (params))))
378395 OnError (std::move (error));
379396 };
380397 }
381398 }
382399
383400 void Received (const Evt &evt) override {
384401 std::scoped_lock<std::recursive_mutex> guard (m_mutex);
385- auto it = m_event_handlers.find (get_method <Evt>(evt));
402+ auto it = m_event_handlers.find (GetMethod <Evt>(evt));
386403 if (it == m_event_handlers.end ()) {
387404 OnError (llvm::createStringError (
388405 llvm::formatv (" no handled for event {0}" , toJSON (evt))));
@@ -395,9 +412,9 @@ class JSONTransport {
395412 ReplyOnce reply (req, &m_transport, this );
396413
397414 std::scoped_lock<std::recursive_mutex> guard (m_mutex);
398- auto it = m_request_handlers.find (get_method <Req>(req));
415+ auto it = m_request_handlers.find (GetMethod <Req>(req));
399416 if (it == m_request_handlers.end ()) {
400- reply (make_response <Req, Resp>(
417+ reply (MakeResponse <Req, Resp>(
401418 req, llvm::createStringError (" method not found" )));
402419 return ;
403420 }
@@ -407,7 +424,7 @@ class JSONTransport {
407424
408425 void Received (const Resp &resp) override {
409426 std::scoped_lock<std::recursive_mutex> guard (m_mutex);
410- auto it = m_pending_responses.find (get_id <Id, Resp>(resp));
427+ auto it = m_pending_responses.find (GetId <Id, Resp>(resp));
411428 if (it == m_pending_responses.end ()) {
412429 OnError (llvm::createStringError (
413430 llvm::formatv (" no pending request for {0}" , toJSON (resp))));
0 commit comments