Lely core libraries  2.2.5
fd_loop.hpp
Go to the documentation of this file.
1 
24 #ifndef LELY_IO2_POSIX_FD_LOOP_HPP_
25 #define LELY_IO2_POSIX_FD_LOOP_HPP_
26 
27 #include <lely/ev/exec.hpp>
28 #include <lely/ev/poll.hpp>
29 #include <lely/io2/posix/fd_loop.h>
30 
31 #include <utility>
32 
33 namespace lely {
34 namespace io {
35 
37 class FdLoop {
38  public:
40  FdLoop(io_poll_t* poll) : loop_(io_fd_loop_create(poll)) {
41  if (!loop_) util::throw_errc("FdLoop");
42  }
43 
44  FdLoop(const FdLoop&) = delete;
45 
46  FdLoop(FdLoop&& other) noexcept : loop_(other.loop_) {
47  other.loop_ = nullptr;
48  }
49 
50  FdLoop& operator=(const FdLoop&) = delete;
51 
52  FdLoop&
53  operator=(FdLoop&& other) noexcept {
54  using ::std::swap;
55  swap(loop_, other.loop_);
56  return *this;
57  }
58 
61 
62  operator io_fd_loop_t*() const noexcept { return loop_; }
63 
65  ev::Poll
66  get_poll() const noexcept {
67  return ev::Poll(io_fd_loop_get_poll(*this));
68  }
69 
72  get_executor() const noexcept {
73  return ev::Executor(io_fd_loop_get_exec(*this));
74  }
75 
77  int
78  get_fd() const noexcept {
79  return io_fd_loop_get_fd(*this);
80  }
81 
83  void
84  stop() noexcept {
85  io_fd_loop_stop(*this);
86  }
87 
89  bool
90  stopped() const noexcept {
91  return io_fd_loop_stopped(*this) != 0;
92  }
93 
95  void
96  restart() noexcept {
97  io_fd_loop_restart(*this);
98  }
99 
101  ::std::size_t
102  run() {
103  ::std::error_code ec;
104  auto result = run(ec);
105  if (ec) throw ::std::system_error(ec, "run");
106  return result;
107  }
108 
110  ::std::size_t
111  run(::std::error_code& ec) {
112  int errsv = get_errc();
113  set_errc(0);
114  auto result = io_fd_loop_run(*this);
115  ec = util::make_error_code();
116  set_errc(errsv);
117  return result;
118  }
119 
121  ::std::size_t
123  ::std::error_code ec;
124  auto result = run_one(ec);
125  if (ec) throw ::std::system_error(ec, "run_one");
126  return result;
127  }
128 
130  ::std::size_t
131  run_one(::std::error_code& ec) {
132  int errsv = get_errc();
133  set_errc(0);
134  auto result = io_fd_loop_run_one(*this);
135  ec = util::make_error_code();
136  set_errc(errsv);
137  return result;
138  }
139 
140  private:
141  io_fd_loop_t* loop_{nullptr};
142 };
143 
144 } // namespace io
145 } // namespace lely
146 
147 #endif // !LELY_IO2_POSIX_FD_LOOP_HPP_
An abstract task executor. This class is a wrapper around #ev_exec_t*.
Definition: exec.hpp:38
The abstract polling interface.
Definition: poll.hpp:36
A file descriptor event loop.
Definition: fd_loop.hpp:37
::std::size_t run(::std::error_code &ec)
Definition: fd_loop.hpp:111
::std::size_t run_one(::std::error_code &ec)
Definition: fd_loop.hpp:131
void stop() noexcept
Definition: fd_loop.hpp:84
FdLoop(io_poll_t *poll)
Definition: fd_loop.hpp:40
int get_fd() const noexcept
Definition: fd_loop.hpp:78
::std::size_t run_one()
Definition: fd_loop.hpp:122
bool stopped() const noexcept
@ see io_fd_loop_stopped()
Definition: fd_loop.hpp:90
ev::Executor get_executor() const noexcept
Definition: fd_loop.hpp:72
ev::Poll get_poll() const noexcept
Definition: fd_loop.hpp:66
void restart() noexcept
Definition: fd_loop.hpp:96
::std::size_t run()
Definition: fd_loop.hpp:102
int get_errc(void)
Returns the last (thread-specific) native error code set by a system call or library function.
Definition: errnum.c:947
void set_errc(int errc)
Sets the current (thread-specific) native error code to errc.
Definition: errnum.c:957
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 event library; it contains the C++ interface for the abstract task ex...
This header file is part of the event library; it contains the file descriptor event loop declaration...
void io_fd_loop_restart(io_fd_loop_t *loop)
Restarts a file descriptor event loop.
Definition: fd_loop.c:313
void io_fd_loop_destroy(io_fd_loop_t *loop)
Destroys a file descriptor event loop.
Definition: fd_loop.c:251
void io_fd_loop_stop(io_fd_loop_t *loop)
Stops the file descriptor event loop.
Definition: fd_loop.c:284
int io_fd_loop_get_fd(const io_fd_loop_t *loop)
Returns the file descriptor corresponding to the event loop.
Definition: fd_loop.c:276
io_fd_loop_t * io_fd_loop_create(io_poll_t *poll)
Creates a new file descriptor event loop.
Definition: fd_loop.c:224
ev_poll_t * io_fd_loop_get_poll(const io_fd_loop_t *loop)
Returns a pointer to the polling instance used by the event loop.
Definition: fd_loop.c:260
ev_exec_t * io_fd_loop_get_exec(const io_fd_loop_t *loop)
Returns a pointer to the executor corresponding to the event loop.
Definition: fd_loop.c:268
int io_fd_loop_stopped(io_fd_loop_t *loop)
Returns 1 if the file descriptor event loop is stopped, and 0 if not.
Definition: fd_loop.c:299
size_t io_fd_loop_run(io_fd_loop_t *loop)
Equivalent to.
Definition: fd_loop.c:328
::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:48
A file descriptor event loop.
Definition: fd_loop.c:80