dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
PollerFd.hpp
1// SPDX-FileCopyrightText: 2026 Vincent Leroy
2// SPDX-License-Identifier: MIT
3//
4// This file is part of dfx.
5//
6// Licensed under the MIT License. See the LICENSE file in the project root
7// for full license information.
8
9#pragma once
10
11// Standard includes
12#include <format>
13
14// Project includes
15#include "Callback.hpp"
16#include "Events.hpp"
17#include <dfx-utilities/BorrowedFd.hpp>
19#include <dfx-utilities/OwnedFd.hpp>
20
21namespace dfx::FdWatch
22{
23class Poller;
24
42{
43public:
47 PollerFd() noexcept = default;
48
54 explicit PollerFd(Utils::OwnedFd fd) noexcept;
55
65
72 PollerFd(Poller & poller, Utils::OwnedFd fd, EventInterests events, FdCallback cb);
73
78 PollerFd(PollerFd && other) noexcept;
79
82
89
94 PollerFd & operator=(PollerFd && other) noexcept;
95
96 // Comparing 2 PollerFd doesn't make sense since a PollerFd owns its fd int
97 // (through a OwnedFd) so if 2 are equals it means that there is a bug somewhere
98 bool operator==(PollerFd const &) const noexcept = delete;
99 bool operator!=(PollerFd const &) const noexcept = delete;
100
101 bool operator==(int fd) const noexcept { return _fd.get() == fd; }
102 bool operator==(Utils::BorrowedFd const & fd) const noexcept { return _fd.get() == fd.get(); }
103
104 void swap(PollerFd & other) noexcept;
105
111 [[nodiscard]] Utils::BorrowedFd borrow() const noexcept { return Utils::BorrowedFd(get()); }
112
113public:
116 Poller * poller() const noexcept { return _poller; }
117
119 bool isRegistered() const noexcept { return _isRegistered; }
120
123 int get() const noexcept { return _fd.get(); }
124
130 bool hasValidFd() const noexcept { return _fd.isValid(); }
131
132public:
143
153
163
171
175 void deregister() noexcept;
176
181 void reset() noexcept;
182
183private:
184 Poller * _poller = nullptr;
185 Utils::OwnedFd _fd;
186 bool _isRegistered = false;
187};
188
190[[nodiscard]] inline PollerFd makePollerFd(Poller & poller, Utils::OwnedFd fd, EventInterests events, FdCallback cb)
191{ return PollerFd(poller, std::move(fd), events, std::move(cb)); }
192
193inline void swap(PollerFd & fd1, PollerFd & fd2) noexcept { fd1.swap(fd2); }
194} // !namespace dfx::FdWatch
195
196inline bool operator==(int fd1, dfx::FdWatch::PollerFd const & fd2) noexcept { return fd2 == fd1; }
197
198template<>
199struct std::formatter<dfx::FdWatch::PollerFd> : public formatter<int>
200{
201 auto format(dfx::FdWatch::PollerFd const & e, format_context & ctx) const
202 { return formatter<int>::format(e.get(), ctx); }
203};
Convenience macros to explicitly control copy and move semantics.
Event interest and trigger flags for file-descriptor watching (Linux/epoll).
Bitset wrapper for dfx::FdWatch::EventInterest values.
RAII wrapper for the registration of a FD in a Poller.
Definition PollerFd.hpp:42
Poller * poller() const noexcept
Returns the poller this FD is currently associated with (if any).
Definition PollerFd.hpp:116
void updateEvents(EventInterests events)
Update the list of event that this fd is attached to.
void registerTo(Poller &poller, EventInterests events, FdCallback cb)
Registers the currently owned FD into poller.
void attach(Poller &poller, Utils::OwnedFd fd, EventInterests events, FdCallback cb)
Attaches this object to poller and adopts fd, registering it.
void reset() noexcept
Fully releases resources: deregisters (if needed), drops poller association, and closes/releases the ...
Utils::BorrowedFd borrow() const noexcept
Returns a non-owning view of the underlying FD.
Definition PollerFd.hpp:111
bool hasValidFd() const noexcept
Returns true if the internal fd is valid.
Definition PollerFd.hpp:130
PollerFd() noexcept=default
Constructs an empty handle with no poller association and no FD.
int get() const noexcept
Returns the raw integer FD owned by this object.
Definition PollerFd.hpp:123
void migrateTo(Poller &poller, EventInterests events, FdCallback cb)
Moves the registration from the current poller to poller.
bool isRegistered() const noexcept
Returns whether this FD is currently registered in a poller.
Definition PollerFd.hpp:119
void deregister() noexcept
Deregisters this FD from its poller (if registered).
DFX_DISABLE_COPY(PollerFd)
Copying is disabled because this type owns the FD.
Abstract interface for FD-based event polling.
Definition Poller.hpp:37
Non-owning wrapper around a file descriptor.
Definition BorrowedFd.hpp:33
Owning RAII wrapper around a file descriptor.
Definition OwnedFd.hpp:36
int get() const noexcept
Get the owned file descriptor.
Definition OwnedFd.hpp:75
Definition SocketClient.hpp:23
PollerFd makePollerFd(Poller &poller, Utils::OwnedFd fd, EventInterests events, FdCallback cb)
Create a PollerFd and transfer FD ownership to it.
Definition PollerFd.hpp:190
std::function< void(Utils::BorrowedFd, EventTriggers)> FdCallback
Invoked when events occur on a watched file descriptor.
Definition Callback.hpp:28
Definition SystemConfigCommandHandler.hpp:15
Definition Message.hpp:21