Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
file_handle.hpp
Go to the documentation of this file.
1// Copyright 2024, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_FILESYSTEM_FILE_HANDLE_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_FILESYSTEM_FILE_HANDLE_HPP_
7
8#include <cstdint>
9#include <utility>
10
11// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
12#include "arene/base/byte.hpp"
14#include "arene/base/result.hpp"
15#include "arene/base/span.hpp"
16// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
17
18// parasoft-begin-suppress AUTOSAR-A7_1_5-a-2 "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
19
20namespace arene {
21namespace base {
22namespace filesystem {
23
24// parasoft-begin-suppress AUTOSAR-A12_1_5-a-2 "Delegating constructors would not reduce duplication"
25/// @brief An RAII wrapper around a POSIX file handle with unique-ownership semantics.
27 // parasoft-begin-suppress AUTOSAR-A3_9_1-b-2 "Using an int for compatibility with OS functions"
28 /// @brief The OS file handle type
29 using os_file_handle = int;
30 // parasoft-end-suppress AUTOSAR-A3_9_1-b-2
31
32 public:
33 // parasoft-begin-suppress AUTOSAR-M11_0_1-a-2 "False positive: this is not 'member data', it is a public property"
34 /// @brief An invalid handle value
35 static constexpr os_file_handle invalid_handle_value{-1};
36 // parasoft-end-suppress AUTOSAR-M11_0_1-a-2
37
38 /// @brief Default construct with an invalid value
39 /// @post <c> get_fd() == invalid_handle_value </c>
40 file_handle() noexcept
41 : fd_(invalid_handle_value) {}
42
43 /// @brief Take ownership of the specified handle
44 /// @param file_descriptor The file handle to own
45 /// @post <c> get_fd() == file_description </c>
46 explicit file_handle(os_file_handle const file_descriptor) noexcept
47 : fd_(file_descriptor) {}
48
49 /// @brief Close the handle if there is one
50 /// @post If <c> valid() == true </c>, the file descriptor will be closed using underlying OS mechanisms.
52
53 /// @brief Transfer ownership of the handle from @c other to @c *this.
54 /// @param other the other @c file_handle to take ownership from
55 file_handle(file_handle&& other) noexcept
56 : fd_(std::exchange(other.fd_, invalid_handle_value)) {}
57
58 /// @brief Take ownership of the file handle from @c other.
59 /// @param other the other @c file_handle to take ownership from
60 /// @return @c *this
61 /// @post If @c *this held a file handle before assignment, it is closed.
62 /// @post @c get_fd() will return the value previously held by @c other
63 auto operator=(file_handle&& other) noexcept -> file_handle& {
64 file_handle temp{std::move(other)};
65 std::swap(fd_, temp.fd_);
66 return *this;
67 }
68
69 // parasoft-begin-suppress CERT_C-EXP37-a-3 "False positive: The rule does not mention naming all parameters"
70 /// @brief Not copyable
71 file_handle(file_handle const&) = delete;
72 /// @brief Not copyable
73 auto operator=(file_handle const&) -> file_handle& = delete;
74 // parasoft-end-suppress CERT_C-EXP37-a-3
75
76 /// @brief Get the wrapped handle.
77 /// @return the handle value.
78 auto get_fd() const noexcept -> os_file_handle { return fd_; }
79
80 /// @brief Release ownership of the handle, if held.
81 /// @post <c> get_fd() == invalid_handle_value </c>.
82 void release() noexcept { fd_ = invalid_handle_value; }
83
84 /// @brief Check if the handle currently holds a file descriptor
85 /// @return true if <c> get_fd() != invalid_handle_value </c>.
86 /// @return false otherwise.
87 auto valid() const noexcept -> bool { return fd_ != invalid_handle_value; }
88
89 /// @brief Read from the file handle, starting at the specified position, into the provided buffer
90 /// @param buffer A view onto a buffer to write the read bytes into into. The read will never be larger then
91 /// the provided span.
92 /// @param position The position in the file to read from
93 /// @return result<span<const byte>, error_code> A @c result holding either:
94 /// - A sub-span of @c buffer that is a view onto to the actual data read.
95 /// - An @c error_code indicating the reason for failure to read.
96 // NOLINTNEXTLINE(readability-avoid-const-params-in-decls)
97 auto read_at(span<byte> buffer, std::uint64_t const position) const noexcept -> result<span<byte>, error_code>;
98
99 /// @brief Write data from the provided buffer to the file referenced by the handle, starting at the specified
100 /// position in the file.
101 /// @param data The data to write
102 /// @param position The position in the file to write at
103 /// @return result<span<const byte>, error_code> A @c result holding either:
104 /// - A sub-span of @c data that refers to the remaining unwritten data for a successful write, if any.
105 /// If all data was written, this span will be empty.
106 /// - An @c error_code indicating the reason for failure to write.
107 // NOLINTNEXTLINE(readability-avoid-const-params-in-decls)
108 auto write_at(span<byte const> data, std::uint64_t const position) const noexcept
110
111 /// @brief Read from the file handle, starting at the current sequential access position, into the provided buffer.
112 /// Updates the sequential access position to the end of the read data.
113 /// @param buffer The buffer to read into. The read will never be larger then the provided buffer.
114 /// @return result<span<byte>, error_code> A @c result holding either:
115 /// - A sub-span of @c buffer that is a view onto to the actual data read.
116 /// - An @c error_code indicating the reason for failure to read.
117 auto sequential_read(span<byte> buffer) const noexcept -> result<span<byte>, error_code>;
118
119 /// @brief Write data from the provided buffer to the file referenced by the handle, starting at the current
120 /// sequential access position. Updates the sequential access position to the end of the written data.
121 /// @param data The data to write
122 /// @return result<span<const byte>, error_code> A @c result holding either:
123 /// - A sub-span of @c data that refers to the remaining unwritten data for a successful write, if any.
124 /// If all data was written, this span will be empty.
125 /// - An @c error_code indicating the reason for failure to write.
127
128 /// @brief Move the sequential access position to the specified offset from the start of the file.
129 /// @param position The position in the file to use for subsequent sequential access
130 /// @return result<void, error_code> A @c result that is either:
131 /// - An empty value for success
132 /// - An @c error_code indicating the reason for failure if the seek failed.
133 // NOLINTNEXTLINE(readability-avoid-const-params-in-decls)
134 auto sequential_seek(std::uint64_t const position) const noexcept -> result<void, error_code>;
135
136 /// @brief Flush the file handle
137 /// @return result<void, error_code> A @c result that is either:
138 /// - An empty value for success
139 /// - An @c error_code indicating the reason for failure if the flush failed.
140 auto flush() const noexcept -> result<void, error_code>;
141
142 /// @brief Close the file handle, if there is one.
143 /// @return result<void, error_code> A @c result that is either:
144 /// - An empty value for success.
145 /// - An @c error_code indicating the reason for failure if the handle could not be closed.
146 auto close() noexcept -> result<void, error_code>;
147
148 /// @brief Get the size of the open file
149 /// @return A @c result holding the file size on success, or an @c error_code
150 /// indicating the reason for failure.
151 auto size() const noexcept -> result<std::uint64_t, error_code>;
152
153 private:
154 /// @brief The stored handle
155 os_file_handle fd_;
156};
157// parasoft-end-suppress AUTOSAR-A12_1_5-a-2
158
159} // namespace filesystem
160} // namespace base
161} // namespace arene
162
163#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_FILESYSTEM_FILE_HANDLE_HPP_
Public export header for the byte subpackage.
A class representing a filesystem error value. Intended to be a wrapper around errno.
Definition error_code.hpp:33
auto operator=(file_handle const &) -> file_handle &=delete
Not copyable.
static constexpr os_file_handle invalid_handle_value
An invalid handle value.
Definition file_handle.hpp:35
auto write_at(span< byte const > data, std::uint64_t const position) const noexcept -> result< span< byte const >, error_code >
Write data from the provided buffer to the file referenced by the handle, starting at the specified p...
Definition file_handle.cpp:64
auto get_fd() const noexcept -> os_file_handle
Get the wrapped handle.
Definition file_handle.hpp:78
auto valid() const noexcept -> bool
Check if the handle currently holds a file descriptor.
Definition file_handle.hpp:87
~file_handle()
Close the handle if there is one.
Definition file_handle.cpp:41
auto sequential_write(span< byte const > data) const noexcept -> result< span< byte const >, error_code >
Write data from the provided buffer to the file referenced by the handle, starting at the current seq...
Definition file_handle.cpp:120
file_handle(os_file_handle const file_descriptor) noexcept
Take ownership of the specified handle.
Definition file_handle.hpp:46
void release() noexcept
Release ownership of the handle, if held.
Definition file_handle.hpp:82
auto sequential_read(span< byte > buffer) const noexcept -> result< span< byte >, error_code >
Read from the file handle, starting at the current sequential access position, into the provided buff...
Definition file_handle.cpp:134
auto operator=(file_handle &&other) noexcept -> file_handle &
Take ownership of the file handle from other.
Definition file_handle.hpp:63
auto sequential_seek(std::uint64_t const position) const noexcept -> result< void, error_code >
Move the sequential access position to the specified offset from the start of the file.
Definition file_handle.cpp:148
auto read_at(span< byte > buffer, std::uint64_t const position) const noexcept -> result< span< byte >, error_code >
Read from the file handle, starting at the specified position, into the provided buffer.
Definition file_handle.cpp:46
file_handle(file_handle &&other) noexcept
Transfer ownership of the handle from other to *this.
Definition file_handle.hpp:55
file_handle(file_handle const &)=delete
Not copyable.
auto close() noexcept -> result< void, error_code >
Close the file handle, if there is one.
Definition file_handle.cpp:94
auto flush() const noexcept -> result< void, error_code >
Flush the file handle.
Definition file_handle.cpp:82
file_handle() noexcept
Default construct with an invalid value.
Definition file_handle.hpp:40
A sum type which can hold either a value or an error. Similar to std::expected.
Definition result.hpp:104
A backport of std::span for C++14. Provides a view over a contiguous range of elements of type T.
Definition span.hpp:315
Definition directory_handle.cpp:39
Definition accumulate.hpp:37
constexpr std::integral_constant< std::size_t, N > array< T, N >::size
Definition array.hpp:534
::std::uint64_t uint64_t
A 64-bit unsigned integer type.
Definition integer_types.hpp:46
Definition accumulate.hpp:36
STL namespace.
void swap(pair< T1, T2 > &lhs, pair< T1, T2 > &rhs) noexcept(arene::base::is_nothrow_swappable_v< T1 > &&arene::base::is_nothrow_swappable_v< T2 >)
Swap a pair with another instance of the same type. Swaps lhs.first with rhs.first and swaps lhs....
Definition pair.hpp:448
constexpr auto move(T &&value) noexcept -> remove_reference_t< T > &&
Cast a value to an rvalue.
Definition move.hpp:30
constexpr auto exchange(T &target, U &&source) noexcept(is_nothrow_move_constructible_v< T > &&is_nothrow_assignable_v< T &, U && >) -> T
Assign a new value to an object and return the old value. Implements std::exchange.
Definition exchange.hpp:33
::uint64_t uint64_t
A 64-bit unsigned integer type.
Definition cstdint.hpp:47
Public export header for the result subpackage.
Public export header for span.hpp.