5 #ifndef PACKIO_HANDLER_H
6 #define PACKIO_HANDLER_H
13 #include "internal/config.h"
14 #include "internal/rpc.h"
15 #include "internal/utils.h"
26 template <
typename Rpc>
29 using id_type =
typename Rpc::id_type;
30 using response_buffer_type =
31 decltype(Rpc::serialize_response(std::declval<id_type>()));
32 using function_type = std::function<void(response_buffer_type&&)>;
36 : id_(
id), handler_(std::forward<F>(handler))
44 set_error(
"call finished with no result");
53 : id_(other.id_), handler_(std::move(other.handler_))
55 other.handler_ =
nullptr;
62 set_error(
"call finished with no result");
65 handler_ = std::move(other.handler_);
66 other.handler_ =
nullptr;
75 complete(Rpc::serialize_response(id_, std::forward<T>(return_value)));
79 void set_value() { complete(Rpc::serialize_response(id_)); }
86 complete(Rpc::serialize_error_response(id_, std::forward<T>(error_value)));
92 complete(Rpc::serialize_error_response(id_,
"unknown error"));
106 void complete(response_buffer_type&& buffer)
108 handler_(std::move(buffer));
113 function_type handler_;
117 struct is_completion_handler : std::false_type {
120 template <
typename T>
124 template <
typename T>
125 constexpr
auto is_completion_handler_v = is_completion_handler<T>::value;
127 template <
typename...>
128 struct is_async_procedure_args;
131 struct is_async_procedure_args<std::tuple<>> : std::false_type {
134 template <
typename T0,
typename... Ts>
135 struct is_async_procedure_args<std::tuple<T0, Ts...>>
136 : is_completion_handler<T0> {
139 template <
typename F>
140 struct is_async_procedure
141 : is_async_procedure_args<typename internal::func_traits<F>::args_type> {
144 template <
typename T>
145 constexpr
auto is_async_procedure_v = is_async_procedure<T>::value;
The completion_handler class.
Definition: handler.h:27
void set_error()
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: handler.h:90
void set_value(T &&return_value)
Notify successful completion of the procedure and set the return value.
Definition: handler.h:73
completion_handler(completion_handler &&other)
Move constructor.
Definition: handler.h:52
void operator()(T &&return_value)
Same as set_value.
Definition: handler.h:97
void operator()()
Same as set_value.
Definition: handler.h:103
~completion_handler()
The destructor will notify an error if the completion_handler has not been used.
Definition: handler.h:41
completion_handler & operator=(completion_handler &&other)
Move assignment operator.
Definition: handler.h:59
void set_error(T &&error_value)
Notify erroneous completion of the procedure with an associated error.
Definition: handler.h:84
void set_value()
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: handler.h:79
completion_handler< rpc > completion_handler
The completion_handler for JSON-RPC.
Definition: json_rpc.h:23
The packio namespace.
Definition: arg.h:14