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 Extra > |
constexpr auto | wwa::json_rpc::details::make_extra_tuple (const nlohmann::json &extra) |
| Creates a tuple from the provided JSON object based on the type of Extra.
|
|
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 322 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 346 of file details.h.
347{
348 constexpr std::size_t offset = std::is_void_v<Extra> ? 0 : 1;
349 try {
350 return std::make_tuple(params[Indices - offset].template get<tuple_element<Indices, Args>>()...);
351 }
352 catch (const nlohmann::json::exception& e) {
354 }
355}
JSON RPC Exception class.
static constexpr int INVALID_PARAMS
Invalid method parameter(s).
◆ 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 299 of file details.h.
300{
301 using ReturnType = typename details::function_traits<std::decay_t<F>>::return_type;
302
303 if constexpr (std::is_void_v<ReturnType>) {
304 std::apply(std::forward<F>(f), std::forward<Tuple>(tuple));
305 return nullptr;
306 }
307 else {
308 return std::apply(std::forward<F>(f), std::forward<Tuple>(tuple));
309 }
310}
◆ make_extra_tuple()
template<typename Extra >
auto wwa::json_rpc::details::make_extra_tuple |
( |
const nlohmann::json & | extra | ) |
|
|
constexpr |
Creates a tuple from the provided JSON object based on the type of Extra.
- Template Parameters
-
Extra | The type to be extracted from the JSON object. |
- Parameters
-
extra | The JSON object from which the tuple is created. |
- Returns
- A tuple containing the extracted value(s) from the JSON object.
- Return values
-
std::tuple<> | If Extra is void . |
std::tuple<nlohmann::json> | If Extra is nlohmann::json . |
std::tuple<Extra> | If Extra is a different type. |
- Exceptions
-
Definition at line 263 of file details.h.
264{
265 if constexpr (std::is_void_v<Extra>) {
266 return std::make_tuple();
267 }
268 else if constexpr (std::is_same_v<std::decay_t<Extra>, nlohmann::json>) {
269 return std::make_tuple(extra);
270 }
271 else {
272 try {
273 return std::make_tuple(extra.get<Extra>());
274 }
275 catch (const nlohmann::json::exception& e) {
277 }
278 }
279}
static constexpr int INVALID_REQUEST
The JSON sent is not a valid Request object.
◆ 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 239 of file details.h.
241{
242 if constexpr (std::is_null_pointer_v<C>) {
243 return std::make_tuple();
244 }
245 else {
246 return std::make_tuple(inst);
247 }
248}