8 #ifndef WPIUTIL_WPI_UV_HANDLE_H_
9 #define WPIUTIL_WPI_UV_HANDLE_H_
17 #include "wpi/Signal.h"
18 #include "wpi/StringRef.h"
19 #include "wpi/uv/Buffer.h"
20 #include "wpi/uv/Error.h"
21 #include "wpi/uv/Loop.h"
32 class Handle :
public std::enable_shared_from_this<Handle> {
34 using Type = uv_handle_type;
40 virtual ~
Handle() noexcept;
51 Type
GetType() const noexcept {
return m_uv_handle->type; }
57 return uv_handle_type_name(m_uv_handle->type);
65 std::shared_ptr<Loop>
GetLoop() const noexcept {
75 return *static_cast<Loop*>(m_uv_handle->loop->data);
97 bool IsActive() const noexcept {
return uv_is_active(m_uv_handle) != 0; }
108 return m_closed || uv_is_closing(m_uv_handle) != 0;
120 void Close() noexcept;
132 m_loopClosing = loopClosing;
165 bool HasReference() const noexcept {
return uv_has_ref(m_uv_handle) != 0; }
171 size_t RawSize() const noexcept {
return uv_handle_size(m_uv_handle->type); }
196 std::function<
void(
Buffer&)> dealloc) {
213 template <
typename T =
void>
215 return std::static_pointer_cast<T>(m_data);
222 void SetData(std::shared_ptr<void> data) { m_data = std::move(data); }
241 explicit Handle(uv_handle_t* uv_handle) : m_uv_handle{uv_handle} {
242 m_uv_handle->data =
this;
245 void Keep() noexcept { m_self = shared_from_this(); }
246 void Release() noexcept { m_self.reset(); }
247 void ForceClosed() noexcept { m_closed =
true; }
249 static void AllocBuf(uv_handle_t* handle,
size_t size, uv_buf_t* buf);
250 static void DefaultFreeBuf(Buffer& buf);
252 template <
typename F,
typename... Args>
253 bool Invoke(F&& f, Args&&... args)
const {
254 auto err = std::forward<F>(f)(std::forward<Args>(args)...);
260 std::shared_ptr<Handle> m_self;
261 uv_handle_t* m_uv_handle;
262 bool m_closed =
false;
263 bool m_loopClosing =
false;
264 std::function<Buffer(
size_t)> m_allocBuf{&Buffer::Allocate};
265 std::function<void(Buffer&)> m_freeBuf{&DefaultFreeBuf};
266 std::shared_ptr<void> m_data;
272 template <
typename T,
typename U>
275 std::shared_ptr<T> shared_from_this() {
276 return std::static_pointer_cast<T>(Handle::shared_from_this());
279 std::shared_ptr<const T> shared_from_this()
const {
280 return std::static_pointer_cast<const T>(Handle::shared_from_this());
299 #endif // WPIUTIL_WPI_UV_HANDLE_H_
std::shared_ptr< T > GetData() const
Gets user-defined data.
Definition: Handle.h:214
U * GetRaw() const noexcept
Get the underlying handle data structure.
Definition: Handle.h:288
void Unreference() noexcept
Unreference the given handle.
Definition: Handle.h:159
void FreeBuf(Buffer &buf) const noexcept
Free a buffer.
Definition: Handle.h:207
void SetBufferAllocator(std::function< Buffer(size_t)> alloc, std::function< void(Buffer &)> dealloc)
Set the functions used for allocating and releasing buffers.
Definition: Handle.h:195
bool HasReference() const noexcept
Check if the given handle is referenced.
Definition: Handle.h:165
bool IsLoopClosing() const noexcept
Get the loop closing status.
Definition: Handle.h:143
Type GetType() const noexcept
Get the type of the handle.
Definition: Handle.h:51
uv_handle_t * GetRawHandle() const noexcept
Get the underlying handle data structure.
Definition: Handle.h:178
std::shared_ptr< Loop > GetLoop() const noexcept
Get the loop where this handle runs.
Definition: Handle.h:65
Data buffer.
Definition: Buffer.h:27
Handle.
Definition: Handle.h:32
size_t RawSize() const noexcept
Return the size of the underlying handle type.
Definition: Handle.h:171
WPILib C++ utilities (wpiutil) namespace.
Definition: EventLoopRunner.h:17
sig::Signal closed
Closed signal.
Definition: Handle.h:232
void ReportError(int err) const
Report an error.
Definition: Handle.h:238
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
Handle.
Definition: Handle.h:273
sig::Signal< Error > error
Error signal.
Definition: Handle.h:227
void Close() noexcept
Request handle to be closed.
void SetLoopClosing(bool loopClosing) noexcept
Set if the loop is closing.
Definition: Handle.h:131
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
Definition: STLExtras.h:1007
Error code.
Definition: Error.h:19
void Reference() noexcept
Reference the given handle.
Definition: Handle.h:151
Event loop.
Definition: Loop.h:39
void SetData(std::shared_ptr< void > data)
Sets user-defined data.
Definition: Handle.h:222
StringRef GetTypeName() const noexcept
Get the name of the type of the handle.
Definition: Handle.h:56
bool IsActive() const noexcept
Check if the handle is active.
Definition: Handle.h:97
bool IsClosing() const noexcept
Check if a handle is closing or closed.
Definition: Handle.h:107
Loop & GetLoopRef() const noexcept
Get the loop where this handle runs.
Definition: Handle.h:74
SignalBase is an implementation of the observer pattern, through the use of an emitting object and sl...
Definition: Signal.h:495