dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
BorrowedFd.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 "CopyMoveControl.hpp"
16
17namespace dfx::Utils
18{
33{
34public:
36 BorrowedFd() noexcept = default;
37
41 explicit BorrowedFd(int fd) noexcept;
42
43 DFX_ENABLE_DEFAULT_COPY_AND_MOVE_NOEXCEPT(BorrowedFd);
44
45 void swap(BorrowedFd & other) noexcept;
46
47 bool operator==(BorrowedFd const & fd) const noexcept { return _fd == fd._fd; }
48 bool operator==(int fd) const noexcept { return _fd == fd; }
49
50public:
53 int get() const noexcept { return _fd; }
54
56 bool isValid() const noexcept { return _fd != -1; }
57
60 void reset() noexcept { _fd = -1; }
61
62private:
63 int _fd = -1;
64};
65
66inline void swap(BorrowedFd & fd1, BorrowedFd & fd2) noexcept { fd1.swap(fd2); }
67} // !namespace dfx::Utils
68
69template<>
70struct std::formatter<dfx::Utils::BorrowedFd> : public formatter<int>
71{
72 auto format(dfx::Utils::BorrowedFd const & e, format_context & ctx) const
73 { return formatter<int>::format(e.get(), ctx); }
74};
75
76template<>
77struct std::hash<dfx::Utils::BorrowedFd>
78{
79 std::size_t operator()(dfx::Utils::BorrowedFd const & fd) const noexcept
80 { return static_cast<std::size_t>(fd.get()); }
81};
Convenience macros to explicitly control copy and move semantics.
Non-owning wrapper around a file descriptor.
Definition BorrowedFd.hpp:33
BorrowedFd() noexcept=default
Create an invalid BorrowedFd.
int get() const noexcept
Get the underlying file descriptor value.
Definition BorrowedFd.hpp:53
bool isValid() const noexcept
Check if the current BorrowedFd contains a valid fd.
Definition BorrowedFd.hpp:56
void reset() noexcept
Invalidate this BorrowedFd.
Definition BorrowedFd.hpp:60
Definition SystemConfigCommandHandler.hpp:15
Definition Message.hpp:21