Commit | Line | Data |
---|---|---|
57b90af7 JG |
1 | /* |
2 | * Copyright (C) 2023 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
3 | * | |
4 | * SPDX-License-Identifier: LGPL-2.1-only | |
5 | * | |
6 | */ | |
7 | ||
238fab71 JG |
8 | #ifndef LTTNG_FILE_DESCRIPTOR_HPP |
9 | #define LTTNG_FILE_DESCRIPTOR_HPP | |
10 | ||
20c734f5 | 11 | #include <cstddef> |
57b90af7 JG |
12 | |
13 | namespace lttng { | |
14 | ||
20c734f5 | 15 | /* RAII wrapper around a UNIX file descriptor. */ |
57b90af7 JG |
16 | class file_descriptor { |
17 | public: | |
b4853cf4 | 18 | file_descriptor() noexcept = default; |
57b90af7 | 19 | |
20c734f5 | 20 | explicit file_descriptor(int raw_fd) noexcept; |
57b90af7 | 21 | file_descriptor(const file_descriptor&) = delete; |
9d89db29 | 22 | file_descriptor& operator=(const file_descriptor&) = delete; |
57b90af7 | 23 | |
20c734f5 | 24 | file_descriptor(file_descriptor&& other) noexcept; |
57b90af7 | 25 | |
20c734f5 | 26 | file_descriptor& operator=(file_descriptor&& other) noexcept; |
57b90af7 | 27 | |
20c734f5 | 28 | ~file_descriptor() noexcept; |
57b90af7 | 29 | |
20c734f5 JG |
30 | /* |
31 | * Read `size` bytes from the underlying file descriptor, assuming | |
32 | * raw_fd behaves as a blocking device. | |
33 | * | |
34 | * Throws an exception if the requested amount of bytes couldn't be read. | |
35 | */ | |
36 | void read(void *buffer, std::size_t size); | |
37 | /* | |
38 | * Write `size` bytes to the underlying file descriptor, assuming | |
39 | * raw_fd behaves as a blocking device. | |
40 | * | |
41 | * Throws an exception if the requested amount of bytes couldn't be written. | |
42 | */ | |
43 | void write(const void *buffer, std::size_t size); | |
b37c4851 | 44 | |
0038180d | 45 | int fd() const noexcept; |
28f23191 | 46 | |
20c734f5 | 47 | protected: |
20c734f5 | 48 | void _cleanup() noexcept; |
b37c4851 | 49 | |
20c734f5 | 50 | private: |
cd9adb8b | 51 | int _raw_fd = -1; |
57b90af7 JG |
52 | }; |
53 | ||
88277a52 | 54 | } /* namespace lttng */ |
238fab71 JG |
55 | |
56 | #endif /* LTTNG_FILE_DESCRIPTOR_HPP */ |