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