WPILibC++  2020.3.2-60-g3011ebe
Request.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2018 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #ifndef WPIUTIL_WPI_UV_REQUEST_H_
9 #define WPIUTIL_WPI_UV_REQUEST_H_
10 
11 #include <uv.h>
12 
13 #include <functional>
14 #include <memory>
15 
16 #include "wpi/uv/Error.h"
17 
18 namespace wpi {
19 namespace uv {
20 
25 class Request : public std::enable_shared_from_this<Request> {
26  public:
27  using Type = uv_req_type;
28 
29  Request(const Request&) = delete;
30  Request(Request&&) = delete;
31  Request& operator=(const Request&) = delete;
32  Request& operator=(Request&&) = delete;
33  virtual ~Request() noexcept = default;
34 
44  Type GetType() const noexcept { return m_uv_req->type; }
45 
49  const char* GetTypeName() const noexcept {
50  return uv_req_type_name(m_uv_req->type);
51  }
52 
62  bool Cancel() { return uv_cancel(m_uv_req) == 0; }
63 
68  size_t RawSize() const noexcept { return uv_req_size(m_uv_req->type); }
69 
75  uv_req_t* GetRawReq() noexcept { return m_uv_req; }
76 
82  const uv_req_t* GetRawReq() const noexcept { return m_uv_req; }
83 
91  virtual void Keep() noexcept { m_self = shared_from_this(); }
92 
100  virtual void Release() noexcept { m_self.reset(); }
101 
107  std::function<void(Error)> error;
108 
113  void ReportError(int err) { error(Error(err)); }
114 
115  protected:
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 
133 template <typename T, typename U>
134 class RequestImpl : public Request {
135  public:
136  std::shared_ptr<T> shared_from_this() {
137  return std::static_pointer_cast<T>(this->shared_from_this());
138  }
139 
140  std::shared_ptr<const T> shared_from_this() const {
141  return std::static_pointer_cast<const T>(this->shared_from_this());
142  }
143 
149  U* GetRaw() noexcept { return &m_uv_req; }
150 
156  const U* GetRaw() const noexcept { return &m_uv_req; }
157 
158  protected:
162  RequestImpl() : Request{reinterpret_cast<uv_req_t*>(&m_uv_req)} {}
163 
164  private:
165  U m_uv_req;
166 };
167 
168 } // namespace uv
169 } // namespace wpi
170 
171 #endif // WPIUTIL_WPI_UV_REQUEST_H_
wpi::uv::Request::RawSize
size_t RawSize() const noexcept
Return the size of the underlying request type.
Definition: Request.h:68
wpi::uv::Request::GetRawReq
const uv_req_t * GetRawReq() const noexcept
Get the underlying request data structure.
Definition: Request.h:82
wpi::uv::Request::Request
Request(uv_req_t *uv_req)
Constructor.
Definition: Request.h:119
wpi::uv::RequestImpl::GetRaw
const U * GetRaw() const noexcept
Get the underlying request data structure.
Definition: Request.h:156
wpi::uv::Request::Keep
virtual void Keep() noexcept
Keep this request in memory even if no outside shared_ptr references remain.
Definition: Request.h:91
wpi::uv::RequestImpl::GetRaw
U * GetRaw() noexcept
Get the underlying request data structure.
Definition: Request.h:149
wpi
WPILib C++ utilities (wpiutil) namespace.
Definition: Endian.h:31
wpi::uv::Request::Cancel
bool Cancel()
Cancel a pending request.
Definition: Request.h:62
wpi::uv::Request::Release
virtual void Release() noexcept
No longer force holding this request in memory.
Definition: Request.h:100
wpi::uv::Request::error
std::function< void(Error)> error
Error callback.
Definition: Request.h:107
wpi::uv::Request::GetRawReq
uv_req_t * GetRawReq() noexcept
Get the underlying request data structure.
Definition: Request.h:75
wpi::uv::RequestImpl
Request.
Definition: Request.h:134
wpi::uv::Request::GetTypeName
const char * GetTypeName() const noexcept
Get the name of the type of the request.
Definition: Request.h:49
wpi::uv::Request::GetType
Type GetType() const noexcept
Get the type of the request.
Definition: Request.h:44
wpi::uv::RequestImpl::RequestImpl
RequestImpl()
Constructor.
Definition: Request.h:162
wpi::uv::Error
Error code.
Definition: Error.h:19
wpi::uv::Request
Request.
Definition: Request.h:25
wpi::uv::Request::ReportError
void ReportError(int err)
Report an error.
Definition: Request.h:113