Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
error_code.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_ERROR_CODE_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_FILESYSTEM_ERROR_CODE_HPP_
7
8#include <cstddef>
9
10// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
11#include "arene/base/compare.hpp"
12#include "arene/base/compiler_support/attributes.hpp"
13#include "arene/base/inline_string.hpp"
14#include "arene/base/string_view.hpp"
15// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
16
17// parasoft-begin-suppress AUTOSAR-A7_1_5-a-2 "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
18
19namespace arene {
20namespace base {
21
22namespace filesystem {
23
24// parasoft-begin-suppress AUTOSAR-A5_1_1-a-2 "False positive: literal is used to initialize a constant"
25/// @brief The maximum length of an error message
26constexpr std::size_t max_error_length{128U};
27// parasoft-end-suppress AUTOSAR-A5_1_1-a-2
28
29/// @brief An inline_string large enough to hold an error message
31
32/// @brief A class representing a filesystem error value. Intended to be a wrapper around errno.
33class error_code {
34 public:
35 // parasoft-begin-suppress AUTOSAR-A3_9_1-b-2 "Using an int as that is the type of errno"
36 /// @brief Raw error code type
37 using raw_error_code = int;
38 // parasoft-end-suppress AUTOSAR-A3_9_1-b-2
39
40 /// @brief Construct with no error
41 /// @post <c> value() == 0 </c>
42 error_code() noexcept
43 : error_code(0) {}
44
45 // parasoft-begin-suppress AUTOSAR-M2_10_1-a-2 "Similar names permitted by M2-10-1 Permit #1"
46 /// @brief Construct with a specified error value
47 /// @param error The error value to construct from
48 /// @post <c> value() == error </c>
49 constexpr explicit error_code(raw_error_code const error) noexcept
50 : error_(error) {}
51 // parasoft-end-suppress AUTOSAR-M2_10_1-a-2
52
53 /// @brief Create an instance from the current state of @c errno.
54 /// @return An @c error_code with @c value() equal to the old value of @c errno
55 /// @post Sets @c errno to 0
56 static auto from_errno() noexcept -> error_code;
57
58 /// @brief Get the stored error value
59 /// @return The stored error value
60 constexpr auto value() const noexcept -> raw_error_code { return error_; }
61
62 /// @brief Check if the error is non-zero, which represents there _is_ an error.
63 /// @return true if the error value is non-zero
64 /// @return false otherwise.
65 explicit operator bool() const noexcept { return value() != 0; }
66
67 /// @brief clear the error
68 /// @post <c> value() == 0 </c>
69 /// @post <c> static_cast<bool>(*this) == false </c>
70 void clear() noexcept { error_ = 0; }
71
72 /// @brief Get a message describing the error
73 /// @return An @c error_string with a description of the error indicated by the stored error value
74 auto message() const noexcept -> error_string;
75
76 /// @brief Raise an exception produced from the error.
77 /// @throws std::system_error containing the error code
78 // clang-format off
79 ARENE_NORETURN void throw_error() const; // parasoft-suppress AUTOSAR-A15_4_5-a-2 "suppress AUTOSAR-A15_4_5-a-2 due to confusing std::__1::system_error and std::system_error"
80 // clang-format on
81
82 /// @brief Raise an exception produced from the error with a descriptive message.
83 /// @param message_prefix A string to prefix the error message with
84 /// @throws std::system_error containing the error code prefixed with the given message.
85 // NOLINTNEXTLINE(readability-avoid-const-params-in-decls)
86 // clang-format off
87 // NOLINTNEXTLINE(readability-avoid-const-params-in-decls)
88 ARENE_NORETURN void throw_error_with_prefix(null_terminated_string_view const message_prefix) const; // parasoft-suppress AUTOSAR-A15_4_5-a-2 "Confusing std::__1::system_error and std::system_error"
89 // clang-format on
90
91 private:
92 /// @brief The stored error value
93 raw_error_code error_;
94};
95
96} // namespace filesystem
97} // namespace base
98} // namespace arene
99#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_FILESYSTEM_ERROR_CODE_HPP_
Definition directory_handle.cpp:39
Definition array_exceptions_disabled.cpp:11
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10