packio
handler.h
Go to the documentation of this file.
1 // This Source Code Form is subject to the terms of the Mozilla Public
2 // License, v. 2.0. If a copy of the MPL was not distributed with this
3 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
4 
5 #ifndef PACKIO_HANDLER_H
6 #define PACKIO_HANDLER_H
7 
10 
11 #include <functional>
12 
13 #include "internal/config.h"
14 #include "internal/rpc.h"
15 #include "internal/utils.h"
16 
17 namespace packio {
18 
26 template <typename Rpc>
28 public:
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&&)>;
33 
34  template <typename F>
35  completion_handler(const id_type& id, F&& handler)
36  : id_(id), handler_(std::forward<F>(handler))
37  {
38  }
39 
42  {
43  if (handler_) {
44  set_error("call finished with no result");
45  }
46  }
47 
48  completion_handler(const completion_handler&) = delete;
49  completion_handler& operator=(const completion_handler&) = delete;
50 
53  : id_(other.id_), handler_(std::move(other.handler_))
54  {
55  other.handler_ = nullptr;
56  }
57 
60  {
61  if (handler_) {
62  set_error("call finished with no result");
63  }
64  id_ = other.id_;
65  handler_ = std::move(other.handler_);
66  other.handler_ = nullptr;
67  return *this;
68  }
69 
72  template <typename T>
73  void set_value(T&& return_value)
74  {
75  complete(Rpc::serialize_response(id_, std::forward<T>(return_value)));
76  }
77 
79  void set_value() { complete(Rpc::serialize_response(id_)); }
80 
83  template <typename T>
84  void set_error(T&& error_value)
85  {
86  complete(Rpc::serialize_error_response(id_, std::forward<T>(error_value)));
87  }
88 
90  void set_error()
91  {
92  complete(Rpc::serialize_error_response(id_, "unknown error"));
93  }
94 
96  template <typename T>
97  void operator()(T&& return_value)
98  {
99  set_value(std::forward<T>(return_value));
100  }
101 
103  void operator()() { set_value(); }
104 
105 private:
106  void complete(response_buffer_type&& buffer)
107  {
108  handler_(std::move(buffer));
109  handler_ = nullptr;
110  }
111 
112  id_type id_;
113  function_type handler_;
114 };
115 
116 template <typename>
117 struct is_completion_handler : std::false_type {
118 };
119 
120 template <typename T>
121 struct is_completion_handler<completion_handler<T>> : std::true_type {
122 };
123 
124 template <typename T>
125 constexpr auto is_completion_handler_v = is_completion_handler<T>::value;
126 
127 template <typename...>
128 struct is_async_procedure_args;
129 
130 template <>
131 struct is_async_procedure_args<std::tuple<>> : std::false_type {
132 };
133 
134 template <typename T0, typename... Ts>
135 struct is_async_procedure_args<std::tuple<T0, Ts...>>
136  : is_completion_handler<T0> {
137 };
138 
139 template <typename F>
140 struct is_async_procedure
141  : is_async_procedure_args<typename internal::func_traits<F>::args_type> {
142 };
143 
144 template <typename T>
145 constexpr auto is_async_procedure_v = is_async_procedure<T>::value;
146 
147 } // packio
148 
149 #endif // PACKIO_HANDLER_H
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