Lely core libraries  2.3.4
ctx.hpp
Go to the documentation of this file.
1 
24 #ifndef LELY_IO2_CTX_HPP_
25 #define LELY_IO2_CTX_HPP_
26 
27 #include <lely/io2/ctx.h>
28 #include <lely/util/error.hpp>
29 
30 #include <utility>
31 
32 namespace lely {
33 namespace io {
34 
39 enum class ForkEvent {
41  prepare = IO_FORK_PREPARE,
43  parent = IO_FORK_PARENT,
45  child = IO_FORK_CHILD
46 };
47 
49 class ContextBase {
50  public:
51  explicit ContextBase(io_ctx_t* ctx_) noexcept : ctx(ctx_) {}
52 
53  operator io_ctx_t*() const noexcept { return ctx; }
54 
56  void
57  insert(io_svc& svc) noexcept {
58  io_ctx_insert(*this, &svc);
59  }
60 
62  void
63  remove(io_svc& svc) noexcept {
64  io_ctx_remove(*this, &svc);
65  }
66 
68  void
69  notify_fork(ForkEvent e, ::std::error_code& ec) noexcept {
70  int errsv = get_errc();
71  set_errc(0);
72  if (!io_ctx_notify_fork(*this, static_cast<io_fork_event>(e)))
73  ec.clear();
74  else
75  ec = util::make_error_code();
76  set_errc(errsv);
77  }
78 
80  void
82  ::std::error_code ec;
83  notify_fork(e, ec);
84  if (ec) throw ::std::system_error(ec, "notify_fork");
85  }
86 
88  void
89  shutdown() noexcept {
90  io_ctx_shutdown(*this);
91  }
92 
93  protected:
94  io_ctx_t* ctx{nullptr};
95 };
96 
98 class Context : public ContextBase {
99  public:
102  if (!ctx) util::throw_errc("Context");
103  }
104 
105  Context(const Context&) = delete;
106 
107  Context(Context&& other) noexcept : ContextBase(other.ctx) {
108  other.ctx = nullptr;
109  }
110 
111  Context& operator=(const Context&) = delete;
112 
113  Context&
114  operator=(Context&& other) noexcept {
115  using ::std::swap;
116  swap(ctx, other.ctx);
117  return *this;
118  }
119 
121  ~Context() { io_ctx_destroy(*this); }
122 };
123 
124 } // namespace io
125 } // namespace lely
126 
127 #endif // !LELY_IO2_CTX_HPP_
ctx.h
lely::io::Context::Context
Context()
Definition: ctx.hpp:101
lely::io::ContextBase::insert
void insert(io_svc &svc) noexcept
Definition: ctx.hpp:57
lely::io::ContextBase::notify_fork
void notify_fork(ForkEvent e)
Definition: ctx.hpp:81
IO_FORK_PARENT
@ IO_FORK_PARENT
The event generated after the fork in the parent process.
Definition: ctx.h:41
lely::io::ContextBase
A refence to an I/O context. This class is a wrapper around #io_ctx_t*.
Definition: ctx.hpp:49
lely::io::ForkEvent
ForkEvent
The type of event generated by an I/O context before and after a process fork.
Definition: ctx.hpp:39
get_errc
int get_errc(void)
Returns the last (thread-specific) native error code set by a system call or library function.
Definition: errnum.c:932
io_ctx_insert
void io_ctx_insert(io_ctx_t *ctx, struct io_svc *svc)
Registers an I/O service with an I/O context.
Definition: ctx.c:126
IO_FORK_PREPARE
@ IO_FORK_PREPARE
The event generated before the fork.
Definition: ctx.h:39
lely::io::Context
An I/O context.
Definition: ctx.hpp:98
lely::io::ContextBase::remove
void remove(io_svc &svc) noexcept
Definition: ctx.hpp:63
set_errc
void set_errc(int errc)
Sets the current (thread-specific) native error code to errc.
Definition: errnum.c:944
io_ctx_create
io_ctx_t * io_ctx_create(void)
Creates a new I/O context.
Definition: ctx.c:90
lely::io::ContextBase::notify_fork
void notify_fork(ForkEvent e, ::std::error_code &ec) noexcept
Definition: ctx.hpp:69
io_ctx_shutdown
void io_ctx_shutdown(io_ctx_t *ctx)
Shuts down all registered I/O services in reverse order of registration.
Definition: ctx.c:200
io_ctx_destroy
void io_ctx_destroy(io_ctx_t *ctx)
Destroys an I/O context.
Definition: ctx.c:117
io_svc
An I/O service.
Definition: ctx.h:49
io_ctx_notify_fork
int io_ctx_notify_fork(io_ctx_t *ctx, enum io_fork_event e)
Notifies all registered I/O services of the specified fork event.
Definition: ctx.c:156
io_ctx_remove
void io_ctx_remove(io_ctx_t *ctx, struct io_svc *svc)
Unregisters an I/O service with an I/O context.
Definition: ctx.c:141
lely::io::ContextBase::shutdown
void shutdown() noexcept
Definition: ctx.hpp:89
io_fork_event
io_fork_event
The type of event generated by an I/O context before and after a process fork.
Definition: ctx.h:37
io_ctx
Definition: ctx.c:38
lely::canopen::make_error_code
::std::error_code make_error_code(SdoErrc e) noexcept
Creates an error code corresponding to an SDO abort code.
Definition: sdo_error.cpp:170
IO_FORK_CHILD
@ IO_FORK_CHILD
The event generated after the fork in the child process.
Definition: ctx.h:43
error.hpp
lely::io::Context::~Context
~Context()
Definition: ctx.hpp:121