Lely core libraries  2.3.4
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 
68  ContextBase
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 
80  void
81  watch(int fd, Event events, struct io_poll_watch& watch,
82  ::std::error_code& ec) noexcept {
83  int errsv = errno;
84  errno = 0;
85  if (!io_poll_watch(*this, fd, static_cast<int>(events), &watch))
86  ec.clear();
87  else
88  ec = util::make_error_code();
89  errno = errsv;
90  }
91 
93  void
94  watch(int fd, Event events, struct io_poll_watch& watch) {
95  ::std::error_code ec;
96  this->watch(fd, events, watch, ec);
97  if (ec) throw ::std::system_error(ec, "watch");
98  }
99 
100  protected:
101  io_poll_t* poll_{nullptr};
102 };
103 
104 } // namespace io
105 } // namespace lely
106 
107 #endif // !LELY_IO2_POSIX_POLL_HPP_
The abstract polling interface.
Definition: poll.hpp:36
A refence to an I/O context. This class is a wrapper around #io_ctx_t*.
Definition: ctx.hpp:49
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:81
ContextBase get_ctx() const noexcept
Definition: poll.hpp:69
Poll(ContextBase &ctx, int signo=0)
Definition: poll.hpp:45
void watch(int fd, Event events, struct io_poll_watch &watch)
Definition: poll.hpp:94
ev::Poll get_poll() const noexcept
Definition: poll.hpp:75
This header file is part of the I/O library; it contains the C++ interface for the I/O context.
This header file is part of the utilities library; it contains C++ convenience functions for creating...
This header file is part of the event library; it contains the C++ interface for the abstract polling...
This header file is part of the I/O library; it contains the C++ interface for the I/O events.
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
This header file is part of the I/O library; it contains the I/O polling declarations for POSIX platf...
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
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
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:252
io_poll_t * io_poll_create(void)
Creates a new I/O polling interface.
Definition: poll.c:218
void io_poll_destroy(io_poll_t *poll)
Destroys an I/O polling interface.
Definition: poll.c:243
::std::error_code make_error_code(SdoErrc e) noexcept
Creates an error code corresponding to an SDO abort code.
Definition: sdo_error.cpp:170
An I/O polling interface.
Definition: poll.c:51
An object representing a file descriptor being monitored for I/O events.
Definition: poll.h:56