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
32namespace lely {
33namespace io {
34
47
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
90 io_ctx_shutdown(*this);
91 }
92
93 protected:
94 io_ctx_t* ctx{nullptr};
95};
96
98class Context : public ContextBase {
99 public:
102 if (!ctx) util::throw_errc("Context");
103 }
104
105 Context(const Context&) = delete;
106
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
122};
123
124} // namespace io
125} // namespace lely
126
127#endif // !LELY_IO2_CTX_HPP_
A CANopen value.
Definition val.hpp:42
A refence to an I/O context. This class is a wrapper around #io_ctx_t*.
Definition ctx.hpp:49
void notify_fork(ForkEvent e, ::std::error_code &ec) noexcept
Definition ctx.hpp:69
void remove(io_svc &svc) noexcept
Definition ctx.hpp:63
void shutdown() noexcept
Definition ctx.hpp:89
void insert(io_svc &svc) noexcept
Definition ctx.hpp:57
void notify_fork(ForkEvent e)
Definition ctx.hpp:81
An I/O context.
Definition ctx.hpp:98
This header file is part of the I/O library; it contains the I/O context and service declarations.
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
void io_ctx_destroy(io_ctx_t *ctx)
Destroys an I/O context.
Definition ctx.c:117
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_ctx_t * io_ctx_create(void)
Creates a new I/O context.
Definition ctx.c:90
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_fork_event
The type of event generated by an I/O context before and after a process fork.
Definition ctx.h:37
@ IO_FORK_CHILD
The event generated after the fork in the child process.
Definition ctx.h:43
@ IO_FORK_PARENT
The event generated after the fork in the parent process.
Definition ctx.h:41
@ IO_FORK_PREPARE
The event generated before the fork.
Definition ctx.h:39
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
ForkEvent
The type of event generated by an I/O context before and after a process fork.
Definition ctx.hpp:39
@ prepare
The event generated before the fork.
@ child
The event generated after the fork in the child process.
@ parent
The event generated after the fork in the parent process.
int get_errc(void)
Returns the last (thread-specific) native error code set by a system call or library function.
Definition errnum.c:932
void set_errc(int errc)
Sets the current (thread-specific) native error code to errc.
Definition errnum.c:944
This header file is part of the utilities library; it contains C++ convenience functions for creating...
Definition ctx.c:38
An I/O service.
Definition ctx.h:49