WPILibC++  2019.4.1-3-g655763a
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
Poll.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_POLL_H_
9 #define WPIUTIL_WPI_UV_POLL_H_
10 
11 #include <uv.h>
12 
13 #include <memory>
14 
15 #include "wpi/Signal.h"
16 #include "wpi/uv/Handle.h"
17 
18 namespace wpi {
19 namespace uv {
20 
21 class Loop;
22 
26 class Poll final : public HandleImpl<Poll, uv_poll_t> {
27  struct private_init {};
28 
29  public:
30  explicit Poll(const private_init&) {}
31  ~Poll() noexcept override = default;
32 
39  static std::shared_ptr<Poll> Create(Loop& loop, int fd);
40 
47  static std::shared_ptr<Poll> Create(const std::shared_ptr<Loop>& loop,
48  int fd) {
49  return Create(*loop, fd);
50  }
51 
58  static std::shared_ptr<Poll> CreateSocket(Loop& loop, uv_os_sock_t sock);
59 
66  static std::shared_ptr<Poll> CreateSocket(const std::shared_ptr<Loop>& loop,
67  uv_os_sock_t sock) {
68  return CreateSocket(*loop, sock);
69  }
70 
81  void Reuse(int fd, std::function<void()> callback);
82 
93  void ReuseSocket(uv_os_sock_t sock, std::function<void()> callback);
94 
101  void Start(int events);
102 
106  void Stop() { Invoke(&uv_poll_stop, GetRaw()); }
107 
112 
113  private:
114  struct ReuseData {
115  std::function<void()> callback;
116  bool isSocket;
117  int fd;
118  uv_os_sock_t sock;
119  };
120  std::unique_ptr<ReuseData> m_reuseData;
121 };
122 
123 } // namespace uv
124 } // namespace wpi
125 
126 #endif // WPIUTIL_WPI_UV_POLL_H_
uv_poll_t * GetRaw() const noexcept
Get the underlying handle data structure.
Definition: Handle.h:264
Handle.
Definition: Handle.h:249
static std::shared_ptr< Poll > CreateSocket(Loop &loop, uv_os_sock_t sock)
Create a poll handle using a socket descriptor.
void Stop()
Stop polling the file descriptor.
Definition: Poll.h:106
static std::shared_ptr< Poll > CreateSocket(const std::shared_ptr< Loop > &loop, uv_os_sock_t sock)
Create a poll handle using a socket descriptor.
Definition: Poll.h:66
static std::shared_ptr< Poll > Create(const std::shared_ptr< Loop > &loop, int fd)
Create a poll handle using a file descriptor.
Definition: Poll.h:47
Poll handle.
Definition: Poll.h:26
void ReuseSocket(uv_os_sock_t sock, std::function< void()> callback)
Reuse this handle.
WPILib C++ utilities (wpiutil) namespace.
Definition: SmallString.h:21
void Reuse(int fd, std::function< void()> callback)
Reuse this handle.
void Start(int events)
Start polling the file descriptor.
sig::Signal< int > pollEvent
Signal generated when a poll event occurs.
Definition: Poll.h:111
Event loop.
Definition: Loop.h:39
static std::shared_ptr< Poll > Create(Loop &loop, int fd)
Create a poll handle using a file descriptor.
SignalBase is an implementation of the observer pattern, through the use of an emitting object and sl...
Definition: Signal.h:495