uvw  2.12.1
request.hpp
1 #ifndef UVW_REQUEST_INCLUDE_H
2 #define UVW_REQUEST_INCLUDE_H
3 
4 #include <memory>
5 #include <type_traits>
6 #include <utility>
7 #include <uv.h>
8 #include "resource.hpp"
9 
10 namespace uvw {
11 
17 template<typename T, typename U>
18 class Request: public Resource<T, U> {
19 protected:
20  static auto reserve(U *req) {
21  auto ptr = static_cast<T *>(req->data)->shared_from_this();
22  ptr->reset();
23  return ptr;
24  }
25 
26  template<typename E>
27  static void defaultCallback(U *req, int status) {
28  if(auto ptr = reserve(req); status) {
29  ptr->publish(ErrorEvent{status});
30  } else {
31  ptr->publish(E{});
32  }
33  }
34 
35  template<typename F, typename... Args>
36  auto invoke(F &&f, Args &&...args) {
37  if constexpr(std::is_void_v<std::invoke_result_t<F, Args...>>) {
38  std::forward<F>(f)(std::forward<Args>(args)...);
39  this->leak();
40  } else {
41  if(auto err = std::forward<F>(f)(std::forward<Args>(args)...); err) {
43  } else {
44  this->leak();
45  }
46  }
47  }
48 
49 public:
51 
65  bool cancel() {
66  return (0 == uv_cancel(this->template get<uv_req_t>()));
67  }
68 
73  std::size_t size() const noexcept {
74  return uv_req_size(this->template get<uv_req_t>()->type);
75  }
76 };
77 
78 } // namespace uvw
79 
80 #endif // UVW_REQUEST_INCLUDE_H
Event emitter base class.
Definition: emitter.h:82
Request base class.
Definition: request.hpp:18
bool cancel()
Cancels a pending request.
Definition: request.hpp:65
std::size_t size() const noexcept
Returns the size of the underlying request type.
Definition: request.hpp:73
Common class for almost all the resources available in uvw.
Definition: resource.hpp:17
uvw default namespace.
Definition: async.h:8
static constexpr std::uint32_t type() noexcept
Returns a numerical identifier for a given type.
Definition: type_info.hpp:53
The ErrorEvent event.
Definition: emitter.h:22