uvw  1.3.0
poll.hpp
1 #pragma once
2 
3 
4 #include <type_traits>
5 #include <utility>
6 #include <memory>
7 #include <uv.h>
8 #include "handle.hpp"
9 #include "util.hpp"
10 
11 
12 namespace uvw {
13 
14 
15 namespace details {
16 
17 
18 enum class UVPollEvent: std::underlying_type_t<uv_poll_event> {
19  READABLE = UV_READABLE,
20  WRITABLE = UV_WRITABLE,
21  DISCONNECT = UV_DISCONNECT,
22  PRIORITIZED = UV_PRIORITIZED
23 };
24 
25 
26 }
27 
28 
34 struct PollEvent {
35  explicit PollEvent(Flags<details::UVPollEvent> events) noexcept
36  : flags{std::move(events)}
37  {}
38 
50 };
51 
52 
69 class PollHandle final: public Handle<PollHandle, uv_poll_t> {
70  static void startCallback(uv_poll_t *handle, int status, int events) {
71  PollHandle &poll = *(static_cast<PollHandle*>(handle->data));
72  if(status) { poll.publish(ErrorEvent{status}); }
73  else { poll.publish(PollEvent{static_cast<std::underlying_type_t<Event>>(events)}); }
74  }
75 
76 public:
77  using Event = details::UVPollEvent;
78 
79  explicit PollHandle(ConstructorAccess ca, std::shared_ptr<Loop> ref, int desc)
80  : Handle{ca, std::move(ref)}, tag{FD}, fd{desc}
81  {}
82 
83  explicit PollHandle(ConstructorAccess ca, std::shared_ptr<Loop> ref, OSSocketHandle sock)
84  : Handle{ca, std::move(ref)}, tag{SOCKET}, socket{sock}
85  {}
86 
91  bool init() {
92  return (tag == SOCKET)
93  ? initialize(&uv_poll_init_socket, socket)
94  : initialize(&uv_poll_init, fd);
95  }
96 
116  void start(Flags<Event> flags) {
117  invoke(&uv_poll_start, get(), flags, &startCallback);
118  }
119 
139  void start(Event event) {
140  start(Flags<Event>{event});
141  }
142 
146  void stop() {
147  invoke(&uv_poll_stop, get());
148  }
149 
150 private:
151  enum { FD, SOCKET } tag;
152  union {
153  int fd;
154  OSSocketHandle::Type socket;
155  };
156 };
157 
158 
159 }
bool init()
Initializes the handle.
Definition: poll.hpp:91
PollEvent event.
Definition: poll.hpp:34
Handle base class.
Definition: handle.hpp:29
The PollHandle handle.
Definition: poll.hpp:69
void stop()
Stops polling the file descriptor.
Definition: poll.hpp:146
void start(Flags< Event > flags)
Starts polling the file descriptor.
Definition: poll.hpp:116
The ErrorEvent event.
Definition: emitter.hpp:23
details::UVTypeWrapper< uv_os_sock_t > OSSocketHandle
Definition: util.hpp:187
void start(Event event)
Starts polling the file descriptor.
Definition: poll.hpp:139
Flags< details::UVPollEvent > flags
Detected events all in one.
Definition: poll.hpp:49
uvw default namespace.
Definition: async.hpp:11