JSON RPC
JSON-RPC 2.0 library for C++
Parameter Helpers

Utilities for handling handler parameters. More...

Typedefs

template<std::size_t I, typename A >
using wwa::json_rpc::details::tuple_element = std::decay_t<std::tuple_element_t<I, A>>
 Type alias for a tuple element with decay applied.
 

Functions

template<typename Extra , typename Args , std::size_t... Indices>
constexpr auto wwa::json_rpc::details::convert_args (const nlohmann::json &params, 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.
 

Detailed Description

Utilities for handling handler parameters.

Typedef Documentation

◆ tuple_element

template<std::size_t I, typename A >
using wwa::json_rpc::details::tuple_element = std::decay_t<std::tuple_element_t<I, 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
IThe index of the element in the tuple.
AThe tuple type.

Definition at line 322 of file details.h.

Function Documentation

◆ 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
ExtraAn additional type that may be included in the conversion. If Extra is void, it is ignored.
ArgsA tuple of argument types to which the JSON parameters will be converted.
IndicesA parameter pack representing the indices of the arguments.
Parameters
paramsThe JSON object containing the parameters to be converted.
Returns
A tuple containing the converted arguments.
Exceptions
wwa::json_rpc::exceptionIf the conversion of any parameter from the JSON object fails.

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:

  1. ReturnType handler(const Extra& extra, Arguments... args); (Extra is not void, Indices are [1..sizeof...(Args)-1])
  2. 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.
Definition exception.h:79
static constexpr int INVALID_PARAMS
Invalid method parameter(s).
Definition exception.h:108

◆ 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
FThe type of the function.
TupleThe type of the arguments tuple.
Parameters
fThe function.
tupleThe arguments as a tuple.
Returns
The result of the function converted to a JSON value.
Return values
nlohmann::json::null_tIf 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
ExtraThe type to be extracted from the JSON object.
Parameters
extraThe 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
wwa::json_rpc::exceptionIf the extraction of Extra from the JSON object extra fails.

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) {
276 throw exception(wwa::json_rpc::exception::INVALID_REQUEST, e.what());
277 }
278 }
279}
static constexpr int INVALID_REQUEST
The JSON sent is not a valid Request object.
Definition exception.h:98

◆ 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
CThe type of the instance, which should be a class pointer or a null pointer.
Parameters
instThe 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}