WPILibC++ 2023.4.3-108-ge5452e3
Request.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#ifndef WPINET_UV_REQUEST_H_
6#define WPINET_UV_REQUEST_H_
7
8#include <uv.h>
9
10#include <functional>
11#include <memory>
12
13#include "wpinet/uv/Error.h"
14
15namespace wpi::uv {
16
17/**
18 * Request. Requests are not moveable or copyable.
19 * This class provides shared_ptr ownership and shared_from_this.
20 */
21class Request : public std::enable_shared_from_this<Request> {
22 public:
24
25 Request(const Request&) = delete;
26 Request(Request&&) = delete;
27 Request& operator=(const Request&) = delete;
29 virtual ~Request() noexcept = default;
30
31 /**
32 * Get the type of the request.
33 *
34 * A base request offers no functionality to promote it to the actual request
35 * type. By means of this function, the type of the underlying request as
36 * specified by Type is made available.
37 *
38 * @return The actual type of the request.
39 */
40 Type GetType() const noexcept { return m_uv_req->type; }
41
42 /**
43 * Get the name of the type of the request. E.g. "connect" for connect.
44 */
45 const char* GetTypeName() const noexcept {
46 return uv_req_type_name(m_uv_req->type);
47 }
48
49 /**
50 * Cancel a pending request.
51 *
52 * This method fails if the request is executing or has finished
53 * executing.
54 * It can emit an error signal in case of errors.
55 *
56 * @return True in case of success, false otherwise.
57 */
58 bool Cancel() { return uv_cancel(m_uv_req) == 0; }
59
60 /**
61 * Return the size of the underlying request type.
62 * @return The size of the underlying request type.
63 */
64 size_t RawSize() const noexcept { return uv_req_size(m_uv_req->type); }
65
66 /**
67 * Get the underlying request data structure.
68 *
69 * @return The underlying request data structure.
70 */
71 uv_req_t* GetRawReq() noexcept { return m_uv_req; }
72
73 /**
74 * Get the underlying request data structure.
75 *
76 * @return The underlying request data structure.
77 */
78 const uv_req_t* GetRawReq() const noexcept { return m_uv_req; }
79
80 /**
81 * Keep this request in memory even if no outside shared_ptr references
82 * remain. To release call Release().
83 *
84 * Derived classes can override this method for different memory management
85 * approaches (e.g. pooled storage of requests).
86 */
87 virtual void Keep() noexcept { m_self = shared_from_this(); }
88
89 /**
90 * No longer force holding this request in memory. Does not immediately
91 * destroy the object unless no outside shared_ptr references remain.
92 *
93 * Derived classes can override this method for different memory management
94 * approaches (e.g. pooled storage of requests).
95 */
96 virtual void Release() noexcept { m_self.reset(); }
97
98 /**
99 * Error callback. By default, this is set up to report errors to the handle
100 * that created this request.
101 * @param err error code
102 */
103 std::function<void(Error)> error;
104
105 /**
106 * Report an error.
107 * @param err Error code
108 */
109 void ReportError(int err) { error(Error(err)); }
110
111 protected:
112 /**
113 * Constructor.
114 */
115 explicit Request(uv_req_t* uv_req) : m_uv_req{uv_req} {
116 m_uv_req->data = this;
117 }
118
119 private:
120 std::shared_ptr<Request> m_self;
121 uv_req_t* m_uv_req;
122};
123
124/**
125 * Request. Requests are not moveable or copyable.
126 * @tparam T CRTP derived class
127 * @tparam U underlying libuv request type
128 */
129template <typename T, typename U>
130class RequestImpl : public Request {
131 public:
132 std::shared_ptr<T> shared_from_this() {
133 return std::static_pointer_cast<T>(this->shared_from_this());
134 }
135
136 std::shared_ptr<const T> shared_from_this() const {
137 return std::static_pointer_cast<const T>(this->shared_from_this());
138 }
139
140 /**
141 * Get the underlying request data structure.
142 *
143 * @return The underlying request data structure.
144 */
145 U* GetRaw() noexcept { return &m_uv_req; }
146
147 /**
148 * Get the underlying request data structure.
149 *
150 * @return The underlying request data structure.
151 */
152 const U* GetRaw() const noexcept { return &m_uv_req; }
153
154 protected:
155 /**
156 * Constructor.
157 */
158 RequestImpl() : Request{reinterpret_cast<uv_req_t*>(&m_uv_req)} {}
159
160 private:
161 U m_uv_req;
162};
163
164} // namespace wpi::uv
165
166#endif // WPINET_UV_REQUEST_H_
Error code.
Definition: Error.h:15
Request.
Definition: Request.h:21
Request & operator=(Request &&)=delete
std::function< void(Error)> error
Error callback.
Definition: Request.h:103
Request(uv_req_t *uv_req)
Constructor.
Definition: Request.h:115
const char * GetTypeName() const noexcept
Get the name of the type of the request.
Definition: Request.h:45
Request(const Request &)=delete
virtual void Release() noexcept
No longer force holding this request in memory.
Definition: Request.h:96
bool Cancel()
Cancel a pending request.
Definition: Request.h:58
void ReportError(int err)
Report an error.
Definition: Request.h:109
Request(Request &&)=delete
Type GetType() const noexcept
Get the type of the request.
Definition: Request.h:40
size_t RawSize() const noexcept
Return the size of the underlying request type.
Definition: Request.h:64
virtual void Keep() noexcept
Keep this request in memory even if no outside shared_ptr references remain.
Definition: Request.h:87
uv_req_t * GetRawReq() noexcept
Get the underlying request data structure.
Definition: Request.h:71
Request & operator=(const Request &)=delete
virtual ~Request() noexcept=default
const uv_req_t * GetRawReq() const noexcept
Get the underlying request data structure.
Definition: Request.h:78
Request.
Definition: Request.h:130
const U * GetRaw() const noexcept
Get the underlying request data structure.
Definition: Request.h:152
std::shared_ptr< const T > shared_from_this() const
Definition: Request.h:136
RequestImpl()
Constructor.
Definition: Request.h:158
std::shared_ptr< T > shared_from_this()
Definition: Request.h:132
U * GetRaw() noexcept
Get the underlying request data structure.
Definition: Request.h:145
Definition: Buffer.h:18
Definition: uv.h:404
UV_EXTERN size_t uv_req_size(uv_req_type type)
UV_EXTERN const char * uv_req_type_name(uv_req_type type)
UV_EXTERN int uv_cancel(uv_req_t *req)
uv_req_type
Definition: uv.h:195