itch-mold-replay 1.0
Time-accurate NASDAQ TotalView-ITCH 5.0 replay over MoldUDP64
Loading...
Searching...
No Matches
file_descriptor.h
Go to the documentation of this file.
1#pragma once
2
3#include <filesystem>
4#include <fcntl.h>
5#include <source_location>
6#include <system_error>
7#include <type_traits>
8#include <utility>
9
10namespace imr::util
11{
12
13 /// Concept representing any callable taking no arguments and returning an int (wrapping open/dup/socket/etc).
14 template <typename Fn>
15 concept SyscallInvocable = std::is_invocable_r_v<int, Fn>;
16
17 /// RAII file descriptor wrapper.
19 {
20 public:
21 FileDescriptor() = default;
22 /** Wrap existing descriptor
23 *
24 * @throws std::invalid_argument if negative
25 */
26 explicit FileDescriptor(int fd);
27 /** Attempt to get descriptor via open()
28 *
29 * @throws std::invalid_argument if path or directory
30 * @throws std::system_error if open() fails
31 */
32 explicit FileDescriptor(const std::filesystem::path& path, int flags = O_RDONLY);
33
34 /** Constructor for passing any callable that returns a file descriptor.
35
36 @throws std::system_error if the callable returns a negative value.
37
38 @see `SyscallConcept`
39 */
41 {
42 fd_ = std::forward<decltype(syscall)>(syscall)();
43 if (fd_ < 0)
44 {
46 }
47 }
48
51
52 FileDescriptor(FileDescriptor&& other) noexcept;
54
56
57 [[nodiscard]]
58 int get() const noexcept;
59
60 private:
61 int fd_{-1};
62
63 void cleanup() const noexcept;
64 };
65}
RAII file descriptor wrapper.
Definition file_descriptor.h:19
FileDescriptor(int fd)
Wrap existing descriptor.
FileDescriptor(const FileDescriptor &)=delete
FileDescriptor & operator=(FileDescriptor &&other) noexcept
FileDescriptor(FileDescriptor &&other) noexcept
FileDescriptor & operator=(const FileDescriptor &)=delete
FileDescriptor(const std::filesystem::path &path, int flags=O_RDONLY)
Attempt to get descriptor via open()
int get() const noexcept
Definition file_descriptor.h:11