WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
Request.hpp
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#pragma once
6
7#include <functional>
8#include <memory>
9#include <utility>
10
11#include <uv.h>
12
13#include "wpi/net/uv/Error.hpp"
14
15namespace wpi::net::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 * @return Previous shared pointer
97 */
98 virtual std::shared_ptr<Request> Release() noexcept {
99 return std::move(m_self);
100 }
101
102 /**
103 * Error callback. By default, this is set up to report errors to the handle
104 * that created this request.
105 * @param err error code
106 */
107 std::function<void(Error)> error;
108
109 /**
110 * Report an error.
111 * @param err Error code
112 */
113 void ReportError(int err) { error(Error(err)); }
114
115 protected:
116 /**
117 * Constructor.
118 */
119 explicit Request(uv_req_t* uv_req) : m_uv_req{uv_req} {
120 m_uv_req->data = this;
121 }
122
123 private:
124 std::shared_ptr<Request> m_self;
125 uv_req_t* m_uv_req;
126};
127
128/**
129 * Request. Requests are not moveable or copyable.
130 * @tparam T CRTP derived class
131 * @tparam U underlying libuv request type
132 */
133template <typename T, typename U>
134class RequestImpl : public Request {
135 public:
136 std::shared_ptr<T> shared_from_this() {
137 return std::static_pointer_cast<T>(Request::shared_from_this());
138 }
139
140 std::shared_ptr<const T> shared_from_this() const {
141 return std::static_pointer_cast<const T>(Request::shared_from_this());
142 }
143
144 /**
145 * Get the underlying request data structure.
146 *
147 * @return The underlying request data structure.
148 */
149 U* GetRaw() noexcept { return &m_uv_req; }
150
151 /**
152 * Get the underlying request data structure.
153 *
154 * @return The underlying request data structure.
155 */
156 const U* GetRaw() const noexcept { return &m_uv_req; }
157
158 protected:
159 /**
160 * Constructor.
161 */
162 RequestImpl() : Request{reinterpret_cast<uv_req_t*>(&m_uv_req)} {}
163
164 private:
165 U m_uv_req;
166};
167
168} // namespace wpi::net::uv
Error code.
Definition Error.hpp:14
uv_req_type Type
Definition Request.hpp:23
Request & operator=(Request &&)=delete
Request & operator=(const Request &)=delete
Request(uv_req_t *uv_req)
Constructor.
Definition Request.hpp:119
Request(const Request &)=delete
virtual ~Request() noexcept=default
size_t RawSize() const noexcept
Return the size of the underlying request type.
Definition Request.hpp:64
void ReportError(int err)
Report an error.
Definition Request.hpp:113
Type GetType() const noexcept
Get the type of the request.
Definition Request.hpp:40
uv_req_t * GetRawReq() noexcept
Get the underlying request data structure.
Definition Request.hpp:71
std::function< void(Error)> error
Error callback.
Definition Request.hpp:107
bool Cancel()
Cancel a pending request.
Definition Request.hpp:58
virtual void Keep() noexcept
Keep this request in memory even if no outside shared_ptr references remain.
Definition Request.hpp:87
Request(Request &&)=delete
const uv_req_t * GetRawReq() const noexcept
Get the underlying request data structure.
Definition Request.hpp:78
const char * GetTypeName() const noexcept
Get the name of the type of the request.
Definition Request.hpp:45
virtual std::shared_ptr< Request > Release() noexcept
No longer force holding this request in memory.
Definition Request.hpp:98
U * GetRaw() noexcept
Get the underlying request data structure.
Definition Request.hpp:149
const U * GetRaw() const noexcept
Get the underlying request data structure.
Definition Request.hpp:156
RequestImpl()
Constructor.
Definition Request.hpp:162
std::shared_ptr< T > shared_from_this()
Definition Request.hpp:136
std::shared_ptr< const T > shared_from_this() const
Definition Request.hpp:140
Error
Definition def.inc:106
Definition Errors.hpp:112
Definition Prepare.hpp:14
UV_EXTERN size_t uv_req_size(uv_req_type type)
UV_EXTERN const char * uv_req_type_name(uv_req_type type)
struct uv_req_s uv_req_t
Definition uv.h:235
UV_EXTERN int uv_cancel(uv_req_t *req)
uv_req_type
Definition uv.h:204