@@ -76,37 +76,55 @@ using VoidT = std::monostate;
76
76
77
77
template <typename T> using Callback = llvm::unique_function<T>;
78
78
79
+ // / A handler for the response to an outgoing request.
79
80
template <typename T>
80
81
using 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)>,
82
84
llvm::unique_function<void (llvm::Expected<T>)>>::type;
83
85
86
+ // / A function to send an outgoing request and receive a response.
84
87
template <typename Result, typename Params>
85
88
using OutgoingRequest = typename std::conditional<
86
- std::is_same_v<Params, VoidT> == true ,
89
+ std::is_same_v<Params, VoidT> || std::is_void_v<Params> ,
87
90
llvm::unique_function<void (Reply<Result>)>,
88
91
llvm::unique_function<void (const Params &, Reply<Result>)>>::type;
89
92
93
+ // / A function to send an outgoing event.
90
94
template <typename Params>
91
95
using 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 ()>,
93
98
llvm::unique_function<void (const Params &)>>::type;
94
99
100
+ // / Creates a request with the given id, method, and optional params.
95
101
template <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.
98
105
template <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.
100
109
template <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.
102
113
template <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.
105
117
template <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 &);
110
128
111
129
// / A transport is responsible for maintaining the connection to a client
112
130
// / application, and reading/writing structured messages to it.
@@ -193,7 +211,7 @@ class JSONTransport {
193
211
~ReplyOnce () {
194
212
if (transport && handler && !replied) {
195
213
assert (false && " must reply to all calls!" );
196
- (*this )(make_response <Req, Resp>(
214
+ (*this )(MakeResponse <Req, Resp>(
197
215
req, llvm::createStringError (" failed to reply" )));
198
216
}
199
217
}
@@ -257,9 +275,9 @@ class JSONTransport {
257
275
return std::move (result);
258
276
}
259
277
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&);`
263
281
// / PeekParams must be JSON parsable and PeekResult must be serializable.
264
282
template <typename Result, typename Params, typename Fn, typename ... Args>
265
283
void bind (llvm::StringLiteral method, Fn &&fn, Args &&...args) {
@@ -273,30 +291,30 @@ class JSONTransport {
273
291
llvm::Expected<Result> result = std::invoke (
274
292
std::forward<Fn>(fn), std::forward<Args>(args)...);
275
293
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)));
278
296
};
279
297
} else {
280
298
m_request_handlers[method] =
281
299
[method, fn,
282
300
args...](const Req &req,
283
301
llvm::unique_function<void (const Resp &)> reply) mutable {
284
302
llvm::Expected<Params> params =
285
- parse<Params>(get_params <Req>(req), method);
303
+ parse<Params>(GetParams <Req>(req), method);
286
304
if (!params)
287
- return reply (make_response <Req, Resp>(req, params.takeError ()));
305
+ return reply (MakeResponse <Req, Resp>(req, params.takeError ()));
288
306
289
307
llvm::Expected<Result> result = std::invoke (
290
308
std::forward<Fn>(fn), std::forward<Args>(args)..., *params);
291
309
if (!result)
292
- return reply (make_response <Req, Resp>(req, result.takeError ()));
310
+ return reply (MakeResponse <Req, Resp>(req, result.takeError ()));
293
311
294
- reply (make_response <Req, Resp>(req, toJSON (*result)));
312
+ reply (MakeResponse <Req, Resp>(req, toJSON (*result)));
295
313
};
296
314
}
297
315
}
298
316
299
- // / Bind a handler for a event.
317
+ // / Bind a handler for an incoming event.
300
318
// / e.g. `bind("peek", &ThisModule::peek, this);`
301
319
// / Handler should be e.g. `void peek(const PeekParams&);`
302
320
// / PeekParams must be JSON parsable.
@@ -312,7 +330,7 @@ class JSONTransport {
312
330
m_event_handlers[method] = [this , method, fn,
313
331
args...](const Evt &evt) mutable {
314
332
llvm::Expected<Params> params =
315
- parse<Params>(get_params <Evt>(evt), method);
333
+ parse<Params>(GetParams <Evt>(evt), method);
316
334
if (!params)
317
335
return OnError (params.takeError ());
318
336
std::invoke (std::forward<Fn>(fn), std::forward<Args>(args)...,
@@ -330,10 +348,10 @@ class JSONTransport {
330
348
return [this , method](Reply<Result> fn) {
331
349
std::scoped_lock<std::recursive_mutex> guard (m_mutex);
332
350
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 );
334
352
m_pending_responses[id] = [fn = std::move (fn),
335
353
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);
337
355
if (!result)
338
356
return fn (result.takeError ());
339
357
fn (parse<Result>(*result, method));
@@ -345,11 +363,10 @@ class JSONTransport {
345
363
return [this , method](const Params ¶ms, Reply<Result> fn) {
346
364
std::scoped_lock<std::recursive_mutex> guard (m_mutex);
347
365
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));
350
367
m_pending_responses[id] = [fn = std::move (fn),
351
368
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);
353
370
if (llvm::Error err = result.takeError ())
354
371
return fn (std::move (err));
355
372
fn (parse<Result>(*result, method));
@@ -368,21 +385,21 @@ class JSONTransport {
368
385
if constexpr (std::is_void_v<Params> || std::is_same_v<VoidT, Params>) {
369
386
return [this , method]() {
370
387
if (llvm::Error error =
371
- m_transport.Send (make_event <Evt>(method, std::nullopt )))
388
+ m_transport.Send (MakeEvent <Evt>(method, std::nullopt )))
372
389
OnError (std::move (error));
373
390
};
374
391
} else {
375
392
return [this , method](const Params ¶ms) {
376
393
if (llvm::Error error =
377
- m_transport.Send (make_event <Evt>(method, toJSON (params))))
394
+ m_transport.Send (MakeEvent <Evt>(method, toJSON (params))))
378
395
OnError (std::move (error));
379
396
};
380
397
}
381
398
}
382
399
383
400
void Received (const Evt &evt) override {
384
401
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));
386
403
if (it == m_event_handlers.end ()) {
387
404
OnError (llvm::createStringError (
388
405
llvm::formatv (" no handled for event {0}" , toJSON (evt))));
@@ -395,9 +412,9 @@ class JSONTransport {
395
412
ReplyOnce reply (req, &m_transport, this );
396
413
397
414
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));
399
416
if (it == m_request_handlers.end ()) {
400
- reply (make_response <Req, Resp>(
417
+ reply (MakeResponse <Req, Resp>(
401
418
req, llvm::createStringError (" method not found" )));
402
419
return ;
403
420
}
@@ -407,7 +424,7 @@ class JSONTransport {
407
424
408
425
void Received (const Resp &resp) override {
409
426
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));
411
428
if (it == m_pending_responses.end ()) {
412
429
OnError (llvm::createStringError (
413
430
llvm::formatv (" no pending request for {0}" , toJSON (resp))));
0 commit comments