dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
PollerFd.hpp
1// SPDX-FileCopyrightText: 2025 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 "BorrowedFd.hpp"
16#include "Callback.hpp"
17#include "Events.hpp"
18#include "OwnedFd.hpp"
20
21namespace dfx::FdWatch
22{
23class Poller;
24
42{
43public:
47 PollerFd() noexcept = default;
48
58
66
71 PollerFd(PollerFd && other) noexcept;
72
75
82
87 PollerFd & operator=(PollerFd && other) noexcept;
88
89 // Comparing 2 PollerFd doesn't make sense since a PollerFd owns its fd int
90 // (through a OwnedFd) so if 2 are equals it means that there is a bug somewhere
91 bool operator==(PollerFd const &) const noexcept = delete;
92 bool operator!=(PollerFd const &) const noexcept = delete;
93
94 bool operator==(int fd) const noexcept { return _fd.get() == fd; }
95 bool operator==(BorrowedFd const & fd) const noexcept { return _fd.get() == fd.get(); }
96
97 void swap(PollerFd & other) noexcept;
98
104 [[nodiscard]] BorrowedFd borrow() const noexcept { return BorrowedFd(*this); }
105
106public:
109 Poller * poller() const noexcept { return _poller; }
110
112 bool isRegistered() const noexcept { return _isRegistered; }
113
116 int get() const noexcept { return _fd.get(); }
117
118public:
129
139
149
153 void deregister() noexcept;
154
159 void reset() noexcept;
160
161private:
162 Poller * _poller = nullptr;
163 OwnedFd _fd;
164 bool _isRegistered = false;
165};
166
168[[nodiscard]] inline PollerFd makePollerFd(Poller & poller, OwnedFd fd, EventInterests events, FdCallback cb) noexcept
169{ return PollerFd(poller, std::move(fd), events, std::move(cb)); }
170
171inline void swap(PollerFd & fd1, PollerFd & fd2) noexcept { fd1.swap(fd2); }
172} // !namespace dfx::FdWatch
173
174inline bool operator==(int fd1, dfx::FdWatch::PollerFd const & fd2) noexcept { return fd2 == fd1; }
175
176template<>
177struct std::formatter<dfx::FdWatch::PollerFd> : public formatter<int>
178{
179 auto format(dfx::FdWatch::PollerFd const & e, format_context & ctx) const
180 { return formatter<int>::format(e.get(), ctx); }
181};
Convenience macros to explicitly control copy and move semantics.
Event interest and trigger flags for file-descriptor watching (Linux/epoll).
Non-owning wrapper around a file descriptor.
Definition BorrowedFd.hpp:37
Bitset wrapper for dfx::FdWatch::EventInterest values.
Owning RAII wrapper around a file descriptor.
Definition OwnedFd.hpp:36
int get() const noexcept
Get the owned file descriptor.
Definition OwnedFd.hpp:75
RAII wrapper for the registration of a FD in a Poller.
Definition PollerFd.hpp:42
void attach(Poller &poller, OwnedFd fd, EventInterests events, FdCallback cb)
Attaches this object to poller and adopts fd, registering it.
BorrowedFd borrow() const noexcept
Returns a non-owning view of the underlying FD.
Definition PollerFd.hpp:104
Poller * poller() const noexcept
Returns the poller this FD is currently associated with (if any).
Definition PollerFd.hpp:109
void registerTo(Poller &poller, EventInterests events, FdCallback cb)
Registers the currently owned FD into poller.
void reset() noexcept
Fully releases resources: deregisters (if needed), drops poller association, and closes/releases the ...
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:116
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:112
void deregister() noexcept
Deregisters this FD from its poller (if registered).
DISABLE_COPY(PollerFd)
Copying is disabled because this type owns the FD.
Abstract interface for FD-based event polling.
Definition Poller.hpp:37
Definition SocketClient.hpp:23
std::function< void(BorrowedFd, EventTriggers)> FdCallback
Invoked when events occur on a watched file descriptor.
Definition Callback.hpp:28
PollerFd makePollerFd(Poller &poller, OwnedFd fd, EventInterests events, FdCallback cb) noexcept
Create a PollerFd and transfer FD ownership to it.
Definition PollerFd.hpp:168
Definition Message.hpp:21