Lely core libraries  2.2.5
poll.hpp
Go to the documentation of this file.
1 
24 #ifndef LELY_IO2_POSIX_POLL_HPP_
25 #define LELY_IO2_POSIX_POLL_HPP_
26 
27 #include <lely/ev/poll.hpp>
28 #include <lely/io2/ctx.hpp>
29 #include <lely/io2/event.hpp>
30 #include <lely/io2/posix/poll.h>
31 #include <lely/util/error.hpp>
32 
33 #include <utility>
34 
35 namespace lely {
36 namespace io {
37 
42 class Poll {
43  public:
45  Poll(ContextBase& ctx, int signo = 0) : poll_(io_poll_create(ctx, signo)) {
46  if (!poll_) util::throw_errc("Poll");
47  }
48 
49  Poll(const Poll&) = delete;
50 
51  Poll(Poll&& other) noexcept : poll_(other.poll_) { other.poll_ = nullptr; }
52 
53  Poll& operator=(const Poll&) = delete;
54 
55  Poll&
56  operator=(Poll&& other) noexcept {
57  using ::std::swap;
58  swap(poll_, other.poll_);
59  return *this;
60  }
61 
63  ~Poll() { io_poll_destroy(*this); }
64 
65  operator io_poll_t*() const noexcept { return poll_; }
66 
69  get_ctx() const noexcept {
70  return ContextBase(io_poll_get_ctx(*this));
71  }
72 
74  ev::Poll
75  get_poll() const noexcept {
76  return ev::Poll(io_poll_get_poll(*this));
77  }
78 
79 #ifdef __linux__
80  int
82  get_fd() const noexcept {
83  return io_poll_get_fd(*this);
84  }
85 #endif
86 
88  void
89  watch(int fd, Event events, struct io_poll_watch& watch,
90  ::std::error_code& ec) noexcept {
91  int errsv = errno;
92  errno = 0;
93  if (!io_poll_watch(*this, fd, static_cast<int>(events), &watch))
94  ec.clear();
95  else
96  ec = util::make_error_code();
97  errno = errsv;
98  }
99 
101  void
102  watch(int fd, Event events, struct io_poll_watch& watch) {
103  ::std::error_code ec;
104  this->watch(fd, events, watch, ec);
105  if (ec) throw ::std::system_error(ec, "watch");
106  }
107 
108  protected:
109  io_poll_t* poll_{nullptr};
110 };
111 
112 } // namespace io
113 } // namespace lely
114 
115 #endif // !LELY_IO2_POSIX_POLL_HPP_
ev_poll_t * io_poll_get_poll(const io_poll_t *poll)
Returns a pointer to the ev_poll_t instance corresponding to the I/O polling instance.
Definition: poll.c:281
A refence to an I/O context. This class is a wrapper around io_ctx_t*.
Definition: ctx.hpp:49
An I/O polling interface.
Definition: poll.c:48
int io_poll_get_fd(const io_poll_t *poll)
Returns the epoll file descriptor used by the I/O polling instance.
Definition: poll.c:287
Poll(ContextBase &ctx, int signo=0)
Definition: poll.hpp:45
This header file is part of the I/O library; it contains the C++ interface for the I/O context...
The system-dependent I/O polling interface.
Definition: poll.hpp:42
void watch(int fd, Event events, struct io_poll_watch &watch, ::std::error_code &ec) noexcept
Definition: poll.hpp:89
The abstract polling interface.
Definition: poll.hpp:36
void io_poll_destroy(io_poll_t *poll)
Destroys an I/O polling interface.
Definition: poll.c:240
ev::Poll get_poll() const noexcept
Definition: poll.hpp:75
int get_fd() const noexcept
Definition: poll.hpp:82
int io_poll_watch(io_poll_t *poll, io_handle_t handle, struct io_event *event, int keep)
Registers an I/O device with an I/O polling interface and instructs it to watch for certain events...
Definition: poll.c:249
This header file is part of the I/O library; it contains the I/O polling declarations for POSIX platf...
An object representing a file descriptor being monitored for I/O events.
Definition: poll.h:56
This header file is part of the utilities library; it contains C++ convenience functions for creating...
io_ctx_t * io_poll_get_ctx(const io_poll_t *poll)
Returns a pointer to the I/O context with which the I/O polling instance is registered.
Definition: poll.c:275
io_poll_t * io_poll_create(void)
Creates a new I/O polling interface.
Definition: poll.c:215
This header file is part of the I/O library; it contains the C++ interface for the I/O events...
::std::error_code make_error_code(SdoErrc e) noexcept
Creates an error code corresponding to an SDO abort code.
Definition: sdo_error.cpp:170
This header file is part of the event library; it contains the C++ interface for the abstract polling...
Definition: buf.hpp:32
Event
The type of I/O event monitored by lely::io::Poll::watch() and reported to io_poll_watch_func_t callb...
Definition: event.hpp:45
void watch(int fd, Event events, struct io_poll_watch &watch)
Definition: poll.hpp:102
ContextBase get_ctx() const noexcept
Definition: poll.hpp:69