Utilities for handling handler parameters.
More...
|
template<typename Extra, typename Args, std::size_t... Indices> |
constexpr auto | wwa::json_rpc::details::convert_args (const nlohmann::json ¶ms, std::index_sequence< Indices... >) |
| Converts JSON parameters to a tuple of arguments based on the specified types.
|
template<typename F, typename Tuple> |
nlohmann::json | wwa::json_rpc::details::invoke_function (F &&f, Tuple &&tuple) |
| Invokes a function with the provided arguments handling void return type.
|
template<typename Context> |
constexpr auto | wwa::json_rpc::details::make_context_tuple (const std::pair< std::any, nlohmann::json > &ctx) |
| Creates a tuple from the provided context object based on the type of Context.
|
template<typename C>
requires (std::is_class_v<std::remove_pointer_t<C>> || std::is_null_pointer_v<C>) |
constexpr auto | wwa::json_rpc::details::make_inst_tuple (C inst) |
| Creates a tuple containing the instance if it is a class pointer, or an empty tuple if it is a null pointer.
|
Utilities for handling handler parameters.
◆ tuple_element
template<std::size_t I, typename A>
Type alias for a tuple element with decay applied.
This template alias retrieves the type of the I-th element in the tuple type A, and then applies std::decay_t to remove any references and cv-qualifiers.
- Template Parameters
-
I | The index of the element in the tuple. |
A | The tuple type. |
Definition at line 457 of file details.h.
◆ convert_args()
template<typename Extra, typename Args, std::size_t... Indices>
auto wwa::json_rpc::details::convert_args |
( |
const nlohmann::json & | params, |
|
|
std::index_sequence< Indices... > | ) |
|
constexpr |
Converts JSON parameters to a tuple of arguments based on the specified types.
- Template Parameters
-
Extra | An additional type that may be included in the conversion. If Extra is void, it is ignored. |
Args | A tuple of argument types to which the JSON parameters will be converted. |
Indices | A parameter pack representing the indices of the arguments. |
- Parameters
-
params | The JSON object containing the parameters to be converted. |
- Returns
- A tuple containing the converted arguments.
- Exceptions
-
The function attempts to convert each parameter in the JSON object params to the corresponding type in Args and returns them as a tuple. It uses std::index_sequence to unpack the indices and access the corresponding parameters in params.
We support two types of handler functions:
- ReturnType handler(const Extra& extra, Arguments... args); (Extra is not void, Indices are [1..sizeof...(Args)-1])
- ReturnType handler(Arguments... args); (Extra is void, Indices are [0..sizeof...(Args)-1])
Because Args correspond to the function arguments, Args contains both Extra and Arguments for the first type of handler; therefore, Args[i] will be the type of the params[i-1]. For the second type of handler, Args contains only Arguments, and Args[i] will be the type of the params[i].
Definition at line 481 of file details.h.
482{
483 constexpr std::size_t offset = std::is_void_v<Extra> ? 0 : 1;
484 try {
486 }
487 catch (const nlohmann::json::exception& e) {
489 }
490}
JSON RPC Exception class.
static constexpr int INVALID_PARAMS
Invalid method parameter(s).
std::decay_t< std::tuple_element_t< I, A > > tuple_element
Type alias for a tuple element with decay applied.
◆ invoke_function()
template<typename F, typename Tuple>
nlohmann::json wwa::json_rpc::details::invoke_function |
( |
F && | f, |
|
|
Tuple && | tuple ) |
Invokes a function with the provided arguments handling void return type.
- Template Parameters
-
F | The type of the function. |
Tuple | The type of the arguments tuple. |
- Parameters
-
f | The function. |
tuple | The arguments as a tuple. |
- Returns
- The result of the function converted to a JSON value.
- Return values
-
nlohmann::json::null_t | If the function returns void. |
This helper invokes the function with the arguments passed as a tuple. It uses the if constexpr construct to handle the case when the handler function returns void.
The if constexpr construct allows for determinining at compile time whether f returns void. If the return type is void, invoke_function calls f and returns a JSON null value. If the return type is not void, invoke_function calls f and returns the result converted to a JSON value.
Definition at line 434 of file details.h.
435{
437
438 if constexpr (std::is_void_v<ReturnType>) {
439 std::apply(std::forward<F>(f), std::forward<Tuple>(tuple));
440 return nullptr;
441 }
442 else {
443 return std::apply(std::forward<F>(f), std::forward<Tuple>(tuple));
444 }
445}
Primary template for function traits.
◆ make_context_tuple()
template<typename Context>
auto wwa::json_rpc::details::make_context_tuple |
( |
const std::pair< std::any, nlohmann::json > & | ctx | ) |
|
|
constexpr |
Creates a tuple from the provided context object based on the type of Context.
- Template Parameters
-
Context | The type of the context. |
- Parameters
-
ctx | The context object from which the tuple is created. |
- Returns
- A tuple containing the context object.
- Exceptions
-
Definition at line 406 of file details.h.
407{
408 if constexpr (std::is_void_v<Context>) {
409 return std::make_tuple();
410 }
411 else {
412 return std::make_tuple(ctx);
413 }
414}
◆ make_inst_tuple()
template<typename C>
requires (std::is_class_v<std::remove_pointer_t<C>> || std::is_null_pointer_v<C>)
auto wwa::json_rpc::details::make_inst_tuple |
( |
C | inst | ) |
|
|
constexpr |
Creates a tuple containing the instance if it is a class pointer, or an empty tuple if it is a null pointer.
- Template Parameters
-
C | The type of the instance, which should be a class pointer or a null pointer. |
- Parameters
-
inst | The instance to be included in the tuple. |
- Returns
- A tuple containing the instance if it is a class pointer, or an empty tuple if it is a null pointer.
- Note
- This function uses constexpr and if constexpr to ensure that the tuple is created at compile time if possible.
-
The function requires that the type C is either a class pointer or a null pointer.
Definition at line 385 of file details.h.
387{
388 if constexpr (std::is_null_pointer_v<C>) {
389 return std::make_tuple();
390 }
391 else {
392 return std::make_tuple(inst);
393 }
394}