dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
BorrowedFd.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
16
17namespace dfx::FdWatch
18{
19class PollerFd;
20class OwnedFd;
21
22
37{
38public:
40 BorrowedFd() noexcept = default;
41
45 explicit BorrowedFd(PollerFd const & fd) noexcept;
46
50 explicit BorrowedFd(OwnedFd const & fd) noexcept;
51
55 explicit BorrowedFd(int fd) noexcept;
56
57 ENABLE_DEFAULT_COPY_AND_MOVE_NOEXCEPT(BorrowedFd);
58
59 void swap(BorrowedFd & other) noexcept;
60
61 bool operator==(BorrowedFd const & fd) const noexcept { return _fd == fd._fd; }
62 bool operator==(int fd) const noexcept { return _fd == fd; }
63
64public:
67 int get() const noexcept { return _fd; }
68
70 bool isValid() const noexcept { return _fd != -1; }
71
74 void reset() noexcept { _fd = -1; }
75
76private:
77 int _fd = -1;
78};
79
80inline void swap(BorrowedFd & fd1, BorrowedFd & fd2) noexcept { fd1.swap(fd2); }
81} // !namespace dfx::FdWatch
82
83template<>
84struct std::formatter<dfx::FdWatch::BorrowedFd> : public formatter<int>
85{
86 auto format(dfx::FdWatch::BorrowedFd const & e, format_context & ctx) const
87 { return formatter<int>::format(e.get(), ctx); }
88};
89
90template<>
91struct std::hash<dfx::FdWatch::BorrowedFd>
92{
93 std::size_t operator()(dfx::FdWatch::BorrowedFd const & fd) const noexcept
94 { return static_cast<std::size_t>(fd.get()); }
95};
Convenience macros to explicitly control copy and move semantics.
Non-owning wrapper around a file descriptor.
Definition BorrowedFd.hpp:37
BorrowedFd() noexcept=default
Create an invalid BorrowedFd.
bool isValid() const noexcept
Check if the current BorrowedFd contains a valid fd.
Definition BorrowedFd.hpp:70
void reset() noexcept
Invalidate this BorrowedFd.
Definition BorrowedFd.hpp:74
int get() const noexcept
Get the underlying file descriptor value.
Definition BorrowedFd.hpp:67
Owning RAII wrapper around a file descriptor.
Definition OwnedFd.hpp:36
RAII wrapper for the registration of a FD in a Poller.
Definition PollerFd.hpp:42
Definition SocketClient.hpp:23
Definition Message.hpp:21