JSON-RPC 2.0
JSON-RPC 2.0 Modern C++ Library
Loading...
Searching...
No Matches
jsonrpc::endpoint::TypedMethodHandler< ParamsType, ResultType, ErrorType > Class Template Reference

#include <typed_handlers.hpp>

Collaboration diagram for jsonrpc::endpoint::TypedMethodHandler< ParamsType, ResultType, ErrorType >:

Public Member Functions

 TypedMethodHandler (SimpleHandler handler)
 
 TypedMethodHandler (ExpectedHandler handler)
 
auto operator() (std::optional< nlohmann::json > params) -> asio::awaitable< nlohmann::json >
 

Detailed Description

template<typename ParamsType, typename ResultType, typename ErrorType = std::monostate>
requires (FromJson<ParamsType> && ToJson<ResultType> && ToJson<ErrorType>)
class jsonrpc::endpoint::TypedMethodHandler< ParamsType, ResultType, ErrorType >

Definition at line 41 of file typed_handlers.hpp.

Constructor & Destructor Documentation

◆ TypedMethodHandler() [1/2]

template<typename ParamsType , typename ResultType , typename ErrorType = std::monostate>
jsonrpc::endpoint::TypedMethodHandler< ParamsType, ResultType, ErrorType >::TypedMethodHandler ( SimpleHandler handler)
inlineexplicit

Definition at line 55 of file typed_handlers.hpp.

57 : handler_(std::move(handler)) {
58 }

◆ TypedMethodHandler() [2/2]

template<typename ParamsType , typename ResultType , typename ErrorType = std::monostate>
jsonrpc::endpoint::TypedMethodHandler< ParamsType, ResultType, ErrorType >::TypedMethodHandler ( ExpectedHandler handler)
inlineexplicit

Definition at line 61 of file typed_handlers.hpp.

63 : handler_(std::move(handler)) {
64 }

Member Function Documentation

◆ operator()()

template<typename ParamsType , typename ResultType , typename ErrorType = std::monostate>
auto jsonrpc::endpoint::TypedMethodHandler< ParamsType, ResultType, ErrorType >::operator() ( std::optional< nlohmann::json > params) -> asio::awaitable<nlohmann::json>
inline

Definition at line 66 of file typed_handlers.hpp.

67 {
68 ParamsType typed_params{};
69 try {
70 if (params.has_value()) {
71 typed_params = params.value().get<ParamsType>();
72 }
73 } catch (const nlohmann::json::exception& ex) {
74 throw std::runtime_error(
75 "Failed to parse parameters: " + std::string(ex.what()));
76 }
77
78 if constexpr (std::is_same_v<ErrorType, std::monostate>) {
79 if constexpr (std::is_void_v<ResultType>) {
80 co_await handler_(typed_params);
81 co_return nlohmann::json();
82 } else {
83 ResultType result = co_await handler_(typed_params);
84 co_return nlohmann::json(result);
85 }
86 } else {
87 auto result = co_await handler_(typed_params);
88 if (result) {
89 if constexpr (std::is_void_v<ResultType>) {
90 co_return nlohmann::json();
91 } else {
92 co_return nlohmann::json(*result);
93 }
94 } else {
95 throw result.error();
96 }
97 }
98 }

The documentation for this class was generated from the following file: