1#ifndef FAB131EA_3F90_43B6_833D_EB89DA373735
2#define FAB131EA_3F90_43B6_833D_EB89DA373735
5
6
7
8
9
10
11
12
23#include <nlohmann/json.hpp>
30
31
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
82
83
84
85
86
87
88
89
90
91
105
106
107
111
112
113
114
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
192 inline void add(std::string_view method, F&& f)
194 this->add(method, std::forward<F>(f),
nullptr);
198
199
200
201
202
203
204
205
206
207
208
209 template<
typename C,
typename F>
210 void add(std::string_view method, F&& f, C instance)
212 using traits = details::function_traits<std::decay_t<F>>;
213 using ArgsTuple =
typename traits::args_tuple;
215 auto&& closure =
this->create_closure<C, F,
void, ArgsTuple>(instance, std::forward<F>(f));
216 this->add_internal_method(method, std::forward<
decltype(closure)>(closure));
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
254 inline void add_ex(std::string_view method, F&& f)
256 this->add_ex(method, std::forward<F>(f),
nullptr);
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276 template<
typename C,
typename F>
277 inline void add_ex(std::string_view method, F&& f, C instance)
279 using traits = details::function_traits<std::decay_t<F>>;
280 using ArgsTuple =
typename traits::args_tuple;
283 std::tuple_size<std::decay_t<ArgsTuple>>::value > 0,
284 "Handler function must accept the `extra` argument. Use `add()` for handlers without an extra parameter."
287 using ExtraType = std::decay_t<std::tuple_element_t<0, ArgsTuple>>;
288 auto&& closure =
this->create_closure<C, F, ExtraType, ArgsTuple>(instance, std::forward<F>(f));
289 this->add_internal_method(method, std::forward<
decltype(closure)>(closure));
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
363
364
365
366
367
368
369
370
371
372
373
374
375
376 virtual void on_request(
const nlohmann::json& extra);
379
380
381
382
383
384
385
386
387
388 virtual void on_method(
const std::string& method,
const nlohmann::json& extra);
391
392
393
394
395
396
397
398
399
400
401
402
403
404 virtual void on_request_processed(
const std::string& method,
int code,
const nlohmann::json& extra);
408
409
410
411
412
416
417
418
419
420
421
422
423
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454 template<
typename C,
typename F,
typename Extra,
typename Args>
457 static_assert((std::is_pointer_v<C> && std::is_class_v<std::remove_pointer_t<C>>) || std::is_null_pointer_v<C>);
458 return [func = std::forward<F>(f), inst](
const nlohmann::json& extra,
const nlohmann::json& params) {
459 assert(params.is_array());
460 constexpr auto args_size = std::tuple_size<std::decay_t<Args>>::value;
461 constexpr auto arg_pos = std::is_void_v<Extra> ? 0 : 1;
463 if constexpr (args_size == arg_pos + 1) {
464 if constexpr (std::is_same_v<std::decay_t<std::tuple_element_t<arg_pos, Args>>, nlohmann::json>) {
465 auto&& tuple_args = std::tuple_cat(
466 details::make_inst_tuple(inst), details::make_extra_tuple<Extra>(extra), std::make_tuple(params)
469 return details::invoke_function(func, std::forward<
decltype(tuple_args)>(tuple_args));
473 if (params.size() + arg_pos == args_size) {
474 constexpr auto offset = std::is_void_v<Extra> ? 0U : 1U;
475 auto&& tuple_args = std::tuple_cat(
476 details::make_inst_tuple(inst), details::make_extra_tuple<Extra>(extra),
477 details::convert_args<Extra, Args>(
478 params, details::offset_sequence_t<offset, std::make_index_sequence<args_size - offset>>{}
482 return details::invoke_function(func, std::forward<
decltype(tuple_args)>(tuple_args));
485 throw exception(exception::INVALID_PARAMS, err_invalid_params_passed_to_method);
Private implementation of the JSON RPC dispatcher class.
A class that manages JSON RPC method handlers and processes JSON RPC requests.
dispatcher & operator=(dispatcher &&rhs)=default
Move assignment operator.
dispatcher(dispatcher &&rhs)=default
Move constructor.
virtual void on_request(const nlohmann::json &extra)
Invoked when a request is received.
void add_ex(std::string_view method, F &&f, C instance)
Adds a method handler with an extra parameter and a class instance.
std::string parse_and_process_request(const std::string &request, const nlohmann::json &extra=nlohmann::json::object())
Parses and processes a JSON RPC request.
constexpr auto create_closure(C inst, F &&f) const
Creates a closure for invoking a member function with JSON parameters.
std::string process_request(const nlohmann::json &request, const nlohmann::json &extra=nlohmann::json::object())
Processes a JSON RPC request.
virtual void on_request_processed(const std::string &method, int code, const nlohmann::json &extra)
Invoked after the method handler is called.
void add_internal_method(std::string_view method, handler_t &&handler)
Adds a method handler for the specified method.
void add(std::string_view method, F &&f)
Adds a method handler f for the method method.
virtual void on_method(const std::string &method, const nlohmann::json &extra)
Invoked right before the method handler is called.
void add(std::string_view method, F &&f, C instance)
Adds a method to the dispatcher with the specified instance and function.
dispatcher()
Class constructor.
dispatcher(const dispatcher &)=delete
dispatcher & operator=(const dispatcher &)=delete
virtual ~dispatcher()
Class destructor.
void add_ex(std::string_view method, F &&f)
Adds a method handler with an extra parameter.
std::unique_ptr< dispatcher_private > d_ptr
Pointer to the implementation (Pimpl idiom).
#define WWA_JSONRPC_EXPORT
Macro for exporting symbols when building the library dynamically or importing symbols when using the...