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