Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
stdexcept.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_STDEXCEPT_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_STDEXCEPT_HPP_
7
8// parasoft-begin-suppress CERT_CPP-DCL58-a-2 "Part of a standard library implementation"
9// parasoft-begin-suppress AUTOSAR-A17_6_1-a-2 "Part of a standard library implementation"
10// parasoft-begin-suppress AUTOSAR-A7_1_5-a "False positive: all functions have trailing return specifiers."
11
12// IWYU pragma: private, include <stdexcept>
13// IWYU pragma: friend "stdlib_detail/.*"
14
15// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
16#include "arene/base/detail/raw_c_string.hpp"
17#include "stdlib/include/stdlib_detail/cstddef.hpp"
18// parasoft-end-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
19namespace std {
20
22
23///
24/// @brief A simple string class that manages a static storage size C-style string.
25/// @warning This class is intended only for the narrow usecase of being used inside an exception type, where it is
26/// guaranteed there will always be some message to store. It does not handle construction from nullptr.
27///
29 /// @brief the maximum length of the string
30 static constexpr std::size_t storage_size{256U};
31
32 // parasoft-begin-suppress AUTOSAR-A18_1_1-a "internal stdlib has no std::array backport."
33 /// @brief The internal storage for the string data.
34 // NOLINTNEXTLINE(hicpp-avoid-c-arrays) needs to work without any extra dependencies.
35 arene::base::detail::character data_[storage_size]{};
36 // parasoft-end-suppress AUTOSAR-A18_1_1-a
37
38 public:
39 /// @brief Construct a new simple string object
40 /// @param str The data to initialize the string with. It will be copied into this string.
41 /// @post @c c_str() will return a cstring for which @c strcmp(c_str(),str)==0 .
42 // parasoft-begin-suppress AUTOSAR-A3_1_5-a "This function is intended to be inlined"
43 // parasoft-begin-suppress AUTOSAR-A7_1_3-a "False positive: const is placed on the right hand side"
44 explicit simple_string(arene::base::detail::raw_c_string const str) noexcept {
45 // parasoft-end-suppress AUTOSAR-A7_1_3-a
46 // parasoft-end-suppress AUTOSAR-A3_1_5-a
47 // Note that this stops 1 character before storage_size so that data_ will be null-terminated.
48 for (std::size_t copied_count{}; copied_count + 1U < storage_size; ++copied_count) {
49 // parasoft-begin-suppress AUTOSAR-M5_0_15-a "Either indexing or pointer arithmetic is necessary here"
50 if (str[copied_count] == '\0') {
51 break;
52 }
53 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-constant-array-index) Needs either indexing or pointer arithmetic
54 data_[copied_count] = str[copied_count];
55 // parasoft-end-suppress AUTOSAR-M5_0_15-a
56 }
57
58 // The rest of data_ is already initialized to '\0' so there's no need to fill in the rest of it.
59 }
60 /// @brief dtor
61 ~simple_string() noexcept = default;
62 /// @brief copy ctor
63 /// @param other The other string to copy from
64 /// @post @c c_str() will return a cstring for which @c strcmp(c_str(),other.c_str())==0 .
65 simple_string(simple_string const& other) noexcept = default;
66 /// @brief copy assignment
67 /// @param other The other string to copy from
68 /// @return simple_string& @c *this post assignment
69 /// @post @c c_str() will return a cstring for which @c strcmp(c_str(),other.c_str())==0 .
70 auto operator=(simple_string const& other) noexcept -> simple_string& = default;
71 /// @brief move ctor
72 /// @param other The other string to move from
73 /// @post @c c_str() will return a cstring for which @c strcmp(c_str(),other.c_str())==0 with @c other 's state before
74 /// move. @c other 's state post-move is unspecified.
75 simple_string(simple_string&& other) noexcept = default;
76 /// @brief move assignment
77 /// @param other The other string to move from
78 /// @return simple_string& @c *this post assignment
79 /// @post @c c_str() will return a cstring for which @c strcmp(c_str(),other.c_str())==0 with @c other 's state before
80 /// move. @c other 's state post-move is unspecified.
81 auto operator=(simple_string&& other) noexcept -> simple_string& = default;
82 /// @brief Returns a pointer to the internal C-style string.
83 /// @return arene::base::detail::raw_c_string A pointer to the internal C-style string.
84 auto c_str() const noexcept -> arene::base::detail::raw_c_string {
85 return static_cast<arene::base::detail::raw_c_string>(data_);
86 }
87};
88} // namespace stdexcept_detail
89
90// parasoft-begin-suppress AUTOSAR-A12_8_6-a "Public assignment operators are required by the C++ standard for these
91// types."
92
93/// @brief The base class of all exceptions in the stdlib.
94class exception {
95 // parasoft-begin-suppress AUTOSAR-A27_0_4-d "'const char *' value constructed from string literal always has the
96 // correct buffer size and a static lifetime"
97 /// @brief The default @c what() message.
98 static constexpr arene::base::detail::raw_c_string default_exception_message{"std::exception"};
99 // parasoft-end-suppress AUTOSAR-A27_0_4-d "'const char *' value constructed from string literal always has the
100 // correct buffer size and a static lifetime"
101
102 public:
103 /// @brief default ctor
104 /// @post @c what() returns an implementation defined message.
105 exception() = default;
106 /// @brief default move ctor
107 /// @param move The rvalue reference to move from.
108 exception(exception&& move) = default;
109 /// @brief default move assignment
110 /// @param move The rvalue reference to move from.
111 auto operator=(exception&& move) -> exception& = default;
112 /// @brief default copy ctor
113 /// @param copy The lvalue reference to copy from.
114 exception(exception const& copy) = default;
115 /// @brief default copy assignment
116 /// @param copy The lvalue reference to copy from.
117 auto operator=(exception const& copy) -> exception& = default;
118 /// @brief default dtor
119 virtual ~exception() = default;
120 /// @brief Access the exception's message.
121 /// @return arene::base::detail::raw_c_string A c-string containing the message content.
122 virtual auto what() const noexcept -> arene::base::detail::raw_c_string { return default_exception_message; }
123};
124
125/// @brief An exception base class dealing with logic errors, which typically are programming defects.
126/// @see https://en.cppreference.com/w/cpp/error/logic_error.html
127class logic_error : exception {
128 // parasoft-begin-suppress AUTOSAR-A27_0_4-d "'const char *' value constructed from string literal always has the
129 // correct buffer size and a static lifetime"
130 /// @brief The default @c what() message.
131 static constexpr arene::base::detail::raw_c_string default_logic_error_message{"std::logic_error"};
132 // parasoft-end-suppress AUTOSAR-A27_0_4-d "'const char *' value constructed from string literal always has the
133 // correct buffer size and a static lifetime"
134
135 /// @brief Storage for the message held in the exception.
136 stdexcept_detail::simple_string message_{default_logic_error_message};
137
138 public:
139 /// @brief default ctor
140 /// @post @c what() returns @c "std::logic_error" .
141 logic_error() = default;
142 /// @brief Construct a new logic error object
143 /// @param msg The message to hold in the exception.
144 /// @post @c strcmp(what(),msg)==0
145 explicit logic_error(arene::base::detail::raw_c_string const msg) noexcept
146 : exception(),
147 message_((msg != nullptr) ? msg : default_logic_error_message) {}
148 /// @brief default dtor
149 ~logic_error() override = default;
150 /// @brief default move ctor
151 /// @param move The rvalue reference to move from.
152 logic_error(logic_error&& move) = default;
153 /// @brief default copy ctor
154 /// @param copy The lvalue reference to copy from.
155 logic_error(logic_error const& copy) = default;
156 /// @brief default move assignment
157 /// @param move The rvalue reference to move from.
158 auto operator=(logic_error&& move) -> logic_error& = default;
159 /// @brief default copy assignment
160 /// @param copy The lvalue reference to copy from.
161 auto operator=(logic_error const& copy) -> logic_error& = default;
162 /// @brief Access the exception's message.
163 /// @return arene::base::detail::raw_c_string A c-string containing the message content.
164 auto what() const noexcept -> arene::base::detail::raw_c_string override { return message_.c_str(); }
165};
166
167/// @brief An exception type representing a failure to validate bounds preconditions on a function call.
168/// @see https://en.cppreference.com/w/cpp/error/out_of_range.html
169class out_of_range : logic_error {
170 // parasoft-begin-suppress AUTOSAR-A27_0_4-d "'const char *' value constructed from string literal always has the
171 // correct buffer size and a static lifetime"
172 /// @brief The default @c what() message.
173 static constexpr arene::base::detail::raw_c_string default_out_of_range_message{"std::out_of_range"};
174 // parasoft-end-suppress AUTOSAR-A27_0_4-d "'const char *' value constructed from string literal always has the
175 // correct buffer size and a static lifetime"
176
177 public:
178 /// @brief default ctor
179 /// @post @c what() returns @c "std::out_of_range" .
180 // parasoft-begin-suppress AUTOSAR-A12_1_1-a "False positive: it delegates to another constructor."
181 out_of_range() noexcept
182 : out_of_range(default_out_of_range_message) {}
183 // parasoft-end-suppress AUTOSAR-A12_1_1-a "False positive: it delegates to another constructor."
184 /// @brief Construct a new logic error object
185 /// @param msg The message to hold in the exception.
186 /// @post @c strcmp(what(),msg)==0
187 explicit out_of_range(arene::base::detail::raw_c_string const msg) noexcept
188 : logic_error((msg != nullptr) ? msg : default_out_of_range_message) {}
189 /// @brief default dtor
190 ~out_of_range() override = default;
191 /// @brief default move ctor
192 /// @param move The rvalue reference to move from.
193 out_of_range(out_of_range&& move) = default;
194 /// @brief default copy ctor
195 /// @param copy The lvalue reference to copy from.
196 out_of_range(out_of_range const& copy) = default;
197 /// @brief default move assignment
198 /// @param move The rvalue reference to move from.
199 auto operator=(out_of_range&& move) -> out_of_range& = default;
200 /// @brief default copy assignment
201 /// @param copy The lvalue reference to copy from.
202 auto operator=(out_of_range const& copy) -> out_of_range& = default;
203
204 /// @brief alias in underlying @c what()
205 using logic_error::what;
206};
207
208/// @brief An exception type representing a failure at runtime that is generally not representative of a programming
209/// defect.
210/// @see https://en.cppreference.com/w/cpp/error/runtime_error.html
211class runtime_error : exception {
212 // parasoft-begin-suppress AUTOSAR-A27_0_4-d "'const char *' value constructed from string literal always has the
213 // correct buffer size and a static lifetime"
214 /// @brief The default error message
215 static constexpr arene::base::detail::raw_c_string default_runtime_error_message{"std::runtime_error"};
216 // parasoft-end-suppress AUTOSAR-A27_0_4-d "'const char *' value constructed from string literal always has the
217 // correct buffer size and a static lifetime"
218 /// @brief Storage for the message held in the exception.
219 stdexcept_detail::simple_string message_{default_runtime_error_message};
220
221 public:
222 /// @brief default ctor
223 /// @post @c what() returns @c "std::runtime_error" .
224 runtime_error() = default;
225 /// @brief Construct a new runtime error object
226 /// @param msg The message to hold in the exception.
227 /// @post @c strcmp(what(),msg)==0
228 explicit runtime_error(arene::base::detail::raw_c_string const msg) noexcept
229 : exception(),
230 message_((msg != nullptr) ? msg : default_runtime_error_message) {}
231 /// @brief default dtor
232 ~runtime_error() override = default;
233 /// @brief default move ctor
234 /// @param move The rvalue reference to move from.
235 runtime_error(runtime_error&& move) = default;
236 /// @brief default copy ctor
237 /// @param copy The lvalue reference to copy from.
238 runtime_error(runtime_error const& copy) = default;
239 /// @brief default move assignment
240 /// @param move The rvalue reference to move from.
241 auto operator=(runtime_error&& move) -> runtime_error& = default;
242 /// @brief default copy assignment
243 /// @param copy The lvalue reference to copy from.
244 auto operator=(runtime_error const& copy) -> runtime_error& = default;
245 /// @brief Access the exception's message.
246 /// @return arene::base::detail::raw_c_string A c-string containing the message content.
247 auto what() const noexcept -> arene::base::detail::raw_c_string override { return message_.c_str(); }
248};
249
250// parasoft-end-suppress AUTOSAR-A12_8_6-a "Public assignment operators are required by the C++ standard for these
251// types."
252
253} // namespace std
254
255#endif // INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_STDEXCEPT_HPP_
A simple string class that manages a static storage size C-style string.
Definition stdexcept.hpp:28
simple_string(arene::base::detail::raw_c_string const str) noexcept
Construct a new simple string object.
Definition stdexcept.hpp:44
simple_string(simple_string &&other) noexcept=default
move ctor
auto operator=(simple_string const &other) noexcept -> simple_string &=default
copy assignment
~simple_string() noexcept=default
dtor
auto operator=(simple_string &&other) noexcept -> simple_string &=default
move assignment
auto c_str() const noexcept -> arene::base::detail::raw_c_string
Returns a pointer to the internal C-style string.
Definition stdexcept.hpp:84
simple_string(simple_string const &other) noexcept=default
copy ctor
Definition stdexcept.hpp:21
constexpr auto operator()(::arene::base::result< void, E > const &value) const noexcept(noexcept(hash< E >{}(std::declval< E const & >()))) -> std::size_t
Calculate the hash of a result.
Definition result.hpp:1827