]>
Commit | Line | Data |
---|---|---|
0646d8a3 BK |
1 | // <system_error> -*- C++ -*- |
2 | ||
6441eb6d | 3 | // Copyright (C) 2007-2025 Free Software Foundation, Inc. |
0646d8a3 BK |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
0646d8a3 BK |
9 | // any later version. |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
748086b7 JJ |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version | |
18 | // 3.1, as published by the Free Software Foundation. | |
19 | ||
20 | // You should have received a copy of the GNU General Public License and | |
21 | // a copy of the GCC Runtime Library Exception along with this program; | |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | // <http://www.gnu.org/licenses/>. | |
0646d8a3 | 24 | |
f910786b | 25 | /** @file include/system_error |
0646d8a3 BK |
26 | * This is a Standard C++ Library header. |
27 | */ | |
28 | ||
29 | #ifndef _GLIBCXX_SYSTEM_ERROR | |
30 | #define _GLIBCXX_SYSTEM_ERROR 1 | |
31 | ||
63a598de | 32 | #ifdef _GLIBCXX_SYSHDR |
0646d8a3 | 33 | #pragma GCC system_header |
63a598de | 34 | #endif |
0646d8a3 | 35 | |
18f176d0 AA |
36 | #include <bits/requires_hosted.h> // OS-dependent |
37 | ||
734f5023 | 38 | #if __cplusplus < 201103L |
ab65a4c7 | 39 | # include <bits/c++0x_warning.h> |
57317d2a | 40 | #else |
0646d8a3 BK |
41 | |
42 | #include <bits/c++config.h> | |
e4bf5dfc | 43 | #include <bits/error_constants.h> |
0646d8a3 BK |
44 | #include <iosfwd> |
45 | #include <stdexcept> | |
4be779f5 JW |
46 | #if __cplusplus > 201703L |
47 | # include <compare> | |
48 | #endif | |
0646d8a3 | 49 | |
12ffa228 BK |
50 | namespace std _GLIBCXX_VISIBILITY(default) |
51 | { | |
52 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
0646d8a3 | 53 | |
2f7f1aca | 54 | /** @addtogroup diagnostics |
f2ce64b5 | 55 | * @{ |
2f7f1aca JW |
56 | */ |
57 | ||
0646d8a3 | 58 | class error_code; |
70593ad2 | 59 | class error_condition; |
70593ad2 BK |
60 | class system_error; |
61 | ||
62 | /// is_error_code_enum | |
c9faf465 | 63 | template<typename _Tp> |
70593ad2 BK |
64 | struct is_error_code_enum : public false_type { }; |
65 | ||
70593ad2 | 66 | /// is_error_condition_enum |
c9faf465 | 67 | template<typename _Tp> |
70593ad2 BK |
68 | struct is_error_condition_enum : public false_type { }; |
69 | ||
33ac58d5 | 70 | template<> |
92010a79 | 71 | struct is_error_condition_enum<errc> |
70593ad2 | 72 | : public true_type { }; |
0646d8a3 | 73 | |
137422c8 VV |
74 | #if __cplusplus > 201402L |
75 | template <typename _Tp> | |
288695f7 DK |
76 | inline constexpr bool is_error_code_enum_v = |
77 | is_error_code_enum<_Tp>::value; | |
137422c8 | 78 | template <typename _Tp> |
288695f7 | 79 | inline constexpr bool is_error_condition_enum_v = |
137422c8 VV |
80 | is_error_condition_enum<_Tp>::value; |
81 | #endif // C++17 | |
f2ce64b5 JW |
82 | /// @} |
83 | ||
e4905f11 | 84 | _GLIBCXX_BEGIN_INLINE_ABI_NAMESPACE(_V2) |
0646d8a3 | 85 | |
f2ce64b5 JW |
86 | /** @addtogroup diagnostics |
87 | * @{ | |
88 | */ | |
89 | ||
2f7f1aca JW |
90 | /** Abstract base class for types defining a category of error codes. |
91 | * | |
20b917e7 | 92 | * An error category defines a context that gives meaning to the integer |
5b503252 | 93 | * stored in an `error_code` or `error_condition` object. For example, |
2f7f1aca JW |
94 | * the standard `errno` constants such a `EINVAL` and `ENOMEM` are |
95 | * associated with the "generic" category and other OS-specific error | |
96 | * numbers are associated with the "system" category, but a user-defined | |
97 | * category might give different meanings to the same numerical values. | |
f2ce64b5 | 98 | * |
20b917e7 JW |
99 | * A user-defined category can override the `equivalent` member functions |
100 | * to define correspondence between errors in different categories. | |
101 | * For example, a category for errors from disk I/O could consider some | |
102 | * of its error numbers equivalent to ENOSPC and ENOENT in the generic | |
103 | * category. | |
104 | * | |
105 | * @headerfile system_error | |
f2ce64b5 | 106 | * @since C++11 |
2f7f1aca | 107 | */ |
92010a79 | 108 | class error_category |
0646d8a3 | 109 | { |
f891e5d1 | 110 | public: |
f891e5d1 | 111 | constexpr error_category() noexcept = default; |
92010a79 | 112 | |
f891e5d1 | 113 | virtual ~error_category(); |
92010a79 CF |
114 | |
115 | error_category(const error_category&) = delete; | |
116 | error_category& operator=(const error_category&) = delete; | |
0646d8a3 | 117 | |
20b917e7 | 118 | /// A string that identifies the error category. |
33ac58d5 | 119 | virtual const char* |
cd88bb8c | 120 | name() const noexcept = 0; |
70593ad2 | 121 | |
34a2b755 JW |
122 | // We need two different virtual functions here, one returning a |
123 | // COW string and one returning an SSO string. Their positions in the | |
124 | // vtable must be consistent for dynamic dispatch to work, but which one | |
125 | // the name "message()" finds depends on which ABI the caller is using. | |
126 | #if _GLIBCXX_USE_CXX11_ABI | |
127 | private: | |
128 | _GLIBCXX_DEFAULT_ABI_TAG | |
129 | virtual __cow_string | |
130 | _M_message(int) const; | |
131 | ||
132 | public: | |
20b917e7 | 133 | /// A description of the error condition corresponding to the number. |
34a2b755 JW |
134 | _GLIBCXX_DEFAULT_ABI_TAG |
135 | virtual string | |
136 | message(int) const = 0; | |
137 | #else | |
33ac58d5 | 138 | virtual string |
70593ad2 BK |
139 | message(int) const = 0; |
140 | ||
34a2b755 JW |
141 | private: |
142 | virtual __sso_string | |
143 | _M_message(int) const; | |
144 | #endif | |
145 | ||
146 | public: | |
20b917e7 | 147 | /// Return an error_condition corresponding to `i` in this category. |
70593ad2 | 148 | virtual error_condition |
cd88bb8c | 149 | default_error_condition(int __i) const noexcept; |
70593ad2 | 150 | |
20b917e7 | 151 | /// Test whether `cond` corresponds to `i` for this category. |
33ac58d5 | 152 | virtual bool |
cd88bb8c | 153 | equivalent(int __i, const error_condition& __cond) const noexcept; |
70593ad2 | 154 | |
20b917e7 | 155 | /// Test whether `code` corresponds to `i` for this category. |
33ac58d5 | 156 | virtual bool |
cd88bb8c | 157 | equivalent(const error_code& __code, int __i) const noexcept; |
70593ad2 | 158 | |
20b917e7 | 159 | /// An error_category only compares equal to itself. |
5f1ce851 | 160 | [[__nodiscard__]] |
33ac58d5 | 161 | bool |
cd88bb8c | 162 | operator==(const error_category& __other) const noexcept |
0646d8a3 BK |
163 | { return this == &__other; } |
164 | ||
20b917e7 | 165 | /// Ordered comparison that defines a total order for error categories. |
4be779f5 | 166 | #if __cpp_lib_three_way_comparison |
5f1ce851 | 167 | [[nodiscard]] |
4be779f5 JW |
168 | strong_ordering |
169 | operator<=>(const error_category& __rhs) const noexcept | |
170 | { return std::compare_three_way()(this, &__rhs); } | |
171 | #else | |
4be779f5 JW |
172 | bool |
173 | operator<(const error_category& __other) const noexcept | |
174 | { return less<const error_category*>()(this, &__other); } | |
20b917e7 JW |
175 | |
176 | bool | |
177 | operator!=(const error_category& __other) const noexcept | |
178 | { return this != &__other; } | |
4be779f5 | 179 | #endif |
0646d8a3 BK |
180 | }; |
181 | ||
92010a79 | 182 | // DR 890. |
2f7f1aca JW |
183 | |
184 | /// Error category for `errno` error codes. | |
5f1ce851 JW |
185 | [[__nodiscard__, __gnu__::__const__]] |
186 | const error_category& | |
187 | generic_category() noexcept; | |
70593ad2 | 188 | |
2f7f1aca | 189 | /// Error category for other error codes defined by the OS. |
5f1ce851 JW |
190 | [[__nodiscard__, __gnu__::__const__]] |
191 | const error_category& | |
192 | system_category() noexcept; | |
2f7f1aca | 193 | |
f2ce64b5 | 194 | /// @} |
e4905f11 JW |
195 | |
196 | _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2) | |
34a2b755 | 197 | |
f2ce64b5 JW |
198 | /** @addtogroup diagnostics |
199 | * @{ | |
200 | */ | |
201 | ||
d3883dc7 JW |
202 | namespace __adl_only |
203 | { | |
204 | void make_error_code() = delete; | |
205 | void make_error_condition() = delete; | |
206 | } | |
bb81f9a0 | 207 | |
2f7f1aca JW |
208 | /** Class error_code |
209 | * | |
210 | * This class is a value type storing an integer error number and a | |
211 | * category that gives meaning to the error number. Typically this is done | |
212 | * close the the point where the error happens, to capture the original | |
213 | * error value. | |
214 | * | |
215 | * An `error_code` object can be used to store the original error value | |
216 | * emitted by some subsystem, with a category relevant to the subsystem. | |
217 | * For example, errors from POSIX library functions can be represented by | |
218 | * an `errno` value and the "generic" category, but errors from an HTTP | |
219 | * library might be represented by an HTTP response status code (e.g. 404) | |
220 | * and a custom category defined by the library. | |
f2ce64b5 | 221 | * |
20b917e7 | 222 | * @headerfile system_error |
f2ce64b5 | 223 | * @since C++11 |
2f7f1aca | 224 | */ |
0e5abeb0 | 225 | class error_code |
0646d8a3 | 226 | { |
1c028806 JW |
227 | template<typename _ErrorCodeEnum> |
228 | using _Check | |
229 | = __enable_if_t<is_error_code_enum<_ErrorCodeEnum>::value>; | |
230 | ||
0e5abeb0 | 231 | public: |
cd88bb8c | 232 | error_code() noexcept |
bb81f9a0 | 233 | : _M_value(0), _M_cat(&system_category()) { } |
0646d8a3 | 234 | |
cd88bb8c | 235 | error_code(int __v, const error_category& __cat) noexcept |
0646d8a3 BK |
236 | : _M_value(__v), _M_cat(&__cat) { } |
237 | ||
1c028806 JW |
238 | /// Initialize with a user-defined type, by calling make_error_code. |
239 | template<typename _ErrorCodeEnum, | |
240 | typename = _Check<_ErrorCodeEnum>> | |
cd88bb8c | 241 | error_code(_ErrorCodeEnum __e) noexcept |
d3883dc7 JW |
242 | { |
243 | using __adl_only::make_error_code; | |
244 | *this = make_error_code(__e); | |
245 | } | |
0646d8a3 | 246 | |
1c028806 JW |
247 | error_code(const error_code&) = default; |
248 | error_code& operator=(const error_code&) = default; | |
249 | ||
33ac58d5 | 250 | void |
cd88bb8c | 251 | assign(int __v, const error_category& __cat) noexcept |
0646d8a3 BK |
252 | { |
253 | _M_value = __v; | |
33ac58d5 | 254 | _M_cat = &__cat; |
0646d8a3 BK |
255 | } |
256 | ||
33ac58d5 | 257 | void |
cd88bb8c | 258 | clear() noexcept |
9b3003d5 | 259 | { assign(0, system_category()); } |
70593ad2 | 260 | |
20b917e7 | 261 | /// The error value. |
5f1ce851 | 262 | [[__nodiscard__]] |
0646d8a3 | 263 | int |
cd88bb8c | 264 | value() const noexcept { return _M_value; } |
33ac58d5 | 265 | |
20b917e7 | 266 | /// The error category that this error belongs to. |
5f1ce851 | 267 | [[__nodiscard__]] |
33ac58d5 | 268 | const error_category& |
cd88bb8c | 269 | category() const noexcept { return *_M_cat; } |
0646d8a3 | 270 | |
20b917e7 | 271 | /// An `error_condition` for this error's category and value. |
33ac58d5 | 272 | error_condition |
cd88bb8c | 273 | default_error_condition() const noexcept; |
70593ad2 | 274 | |
20b917e7 | 275 | /// The category's description of the value. |
34a2b755 | 276 | _GLIBCXX_DEFAULT_ABI_TAG |
33ac58d5 | 277 | string |
70593ad2 BK |
278 | message() const |
279 | { return category().message(value()); } | |
0646d8a3 | 280 | |
20b917e7 | 281 | /// Test whether `value()` is non-zero. |
5f1ce851 | 282 | [[__nodiscard__]] |
cd88bb8c | 283 | explicit operator bool() const noexcept |
2eb57e54 | 284 | { return _M_value != 0; } |
0646d8a3 | 285 | |
4661c8fd | 286 | // DR 804. |
0646d8a3 BK |
287 | private: |
288 | int _M_value; | |
289 | const error_category* _M_cat; | |
290 | }; | |
291 | ||
20b917e7 | 292 | // C++11 19.5.2.5 non-member functions |
2f7f1aca | 293 | |
20b917e7 JW |
294 | /** Create an `error_code` representing a standard `errc` condition. |
295 | * | |
296 | * The `std::errc` constants correspond to `errno` macros and so use the | |
297 | * generic category. | |
298 | * | |
299 | * @relates error_code | |
300 | * @since C++11 | |
301 | */ | |
5f1ce851 | 302 | [[__nodiscard__]] |
92010a79 | 303 | inline error_code |
cd88bb8c | 304 | make_error_code(errc __e) noexcept |
9b3003d5 | 305 | { return error_code(static_cast<int>(__e), generic_category()); } |
92010a79 | 306 | |
20b917e7 JW |
307 | /** Ordered comparison for std::error_code. |
308 | * | |
309 | * This defines a total order by comparing the categories, and then | |
310 | * if they are equal comparing the values. | |
311 | * | |
312 | * @relates error_code | |
313 | * @since C++11 | |
314 | */ | |
4be779f5 | 315 | #if __cpp_lib_three_way_comparison |
5f1ce851 | 316 | [[nodiscard]] |
4be779f5 JW |
317 | inline strong_ordering |
318 | operator<=>(const error_code& __lhs, const error_code& __rhs) noexcept | |
319 | { | |
320 | if (auto __c = __lhs.category() <=> __rhs.category(); __c != 0) | |
321 | return __c; | |
322 | return __lhs.value() <=> __rhs.value(); | |
323 | } | |
324 | #else | |
4661c8fd | 325 | inline bool |
cd88bb8c | 326 | operator<(const error_code& __lhs, const error_code& __rhs) noexcept |
33ac58d5 | 327 | { |
4661c8fd PC |
328 | return (__lhs.category() < __rhs.category() |
329 | || (__lhs.category() == __rhs.category() | |
330 | && __lhs.value() < __rhs.value())); | |
331 | } | |
4be779f5 | 332 | #endif |
70593ad2 | 333 | |
20b917e7 JW |
334 | /** Write a std::error_code to an ostream. |
335 | * | |
336 | * @relates error_code | |
337 | * @since C++11 | |
338 | */ | |
4661c8fd PC |
339 | template<typename _CharT, typename _Traits> |
340 | basic_ostream<_CharT, _Traits>& | |
341 | operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e) | |
342 | { return (__os << __e.category().name() << ':' << __e.value()); } | |
70593ad2 | 343 | |
2f7f1aca JW |
344 | /** Class error_condition |
345 | * | |
346 | * This class represents error conditions that may be visible at an API | |
347 | * boundary. Different `error_code` values that can occur within a library | |
348 | * or module might map to the same `error_condition`. | |
349 | * | |
350 | * An `error_condition` represents something that the program can test for, | |
351 | * and subsequently take appropriate action. | |
f2ce64b5 | 352 | * |
20b917e7 | 353 | * @headerfile system_error |
f2ce64b5 | 354 | * @since C++11 |
2f7f1aca | 355 | */ |
0e5abeb0 | 356 | class error_condition |
70593ad2 | 357 | { |
1c028806 JW |
358 | template<typename _ErrorConditionEnum> |
359 | using _Check | |
360 | = __enable_if_t<is_error_condition_enum<_ErrorConditionEnum>::value>; | |
361 | ||
0e5abeb0 | 362 | public: |
20b917e7 | 363 | /// Initialize with a zero (no error) value and the generic category. |
cd88bb8c | 364 | error_condition() noexcept |
bb81f9a0 | 365 | : _M_value(0), _M_cat(&generic_category()) { } |
70593ad2 | 366 | |
20b917e7 | 367 | /// Initialize with the specified value and category. |
cd88bb8c | 368 | error_condition(int __v, const error_category& __cat) noexcept |
4661c8fd | 369 | : _M_value(__v), _M_cat(&__cat) { } |
70593ad2 | 370 | |
1c028806 JW |
371 | /// Initialize with a user-defined type, by calling make_error_condition. |
372 | template<typename _ErrorConditionEnum, | |
373 | typename = _Check<_ErrorConditionEnum>> | |
cd88bb8c | 374 | error_condition(_ErrorConditionEnum __e) noexcept |
d3883dc7 JW |
375 | { |
376 | using __adl_only::make_error_condition; | |
377 | *this = make_error_condition(__e); | |
378 | } | |
70593ad2 | 379 | |
1c028806 JW |
380 | error_condition(const error_condition&) = default; |
381 | error_condition& operator=(const error_condition&) = default; | |
382 | ||
20b917e7 | 383 | /// Set the value and category. |
4661c8fd | 384 | void |
cd88bb8c | 385 | assign(int __v, const error_category& __cat) noexcept |
4661c8fd PC |
386 | { |
387 | _M_value = __v; | |
388 | _M_cat = &__cat; | |
389 | } | |
70593ad2 | 390 | |
20b917e7 | 391 | /// Reset the value and category to the default-constructed state. |
33ac58d5 | 392 | void |
cd88bb8c | 393 | clear() noexcept |
9b3003d5 | 394 | { assign(0, generic_category()); } |
70593ad2 | 395 | |
20b917e7 JW |
396 | // C++11 19.5.3.4 observers |
397 | ||
398 | /// The error value. | |
5f1ce851 | 399 | [[__nodiscard__]] |
cd88bb8c PC |
400 | int |
401 | value() const noexcept { return _M_value; } | |
70593ad2 | 402 | |
20b917e7 | 403 | /// The error category that this error belongs to. |
5f1ce851 | 404 | [[__nodiscard__]] |
4661c8fd | 405 | const error_category& |
cd88bb8c | 406 | category() const noexcept { return *_M_cat; } |
70593ad2 | 407 | |
20b917e7 | 408 | /// The category's description of the value. |
34a2b755 | 409 | _GLIBCXX_DEFAULT_ABI_TAG |
33ac58d5 | 410 | string |
70593ad2 BK |
411 | message() const |
412 | { return category().message(value()); } | |
413 | ||
20b917e7 | 414 | /// Test whether `value()` is non-zero. |
5f1ce851 | 415 | [[__nodiscard__]] |
cd88bb8c | 416 | explicit operator bool() const noexcept |
2eb57e54 | 417 | { return _M_value != 0; } |
70593ad2 | 418 | |
4661c8fd | 419 | // DR 804. |
70593ad2 BK |
420 | private: |
421 | int _M_value; | |
4661c8fd | 422 | const error_category* _M_cat; |
70593ad2 BK |
423 | }; |
424 | ||
20b917e7 | 425 | // C++11 19.5.3.5 non-member functions |
2f7f1aca | 426 | |
20b917e7 JW |
427 | /** Create an `error_condition` representing a standard `errc` condition. |
428 | * | |
429 | * The `std::errc` constants correspond to `errno` macros and so use the | |
430 | * generic category. | |
431 | * | |
432 | * @relates error_condition | |
433 | * @since C++11 | |
434 | */ | |
5f1ce851 | 435 | [[__nodiscard__]] |
92010a79 | 436 | inline error_condition |
cd88bb8c | 437 | make_error_condition(errc __e) noexcept |
9b3003d5 | 438 | { return error_condition(static_cast<int>(__e), generic_category()); } |
92010a79 | 439 | |
20b917e7 | 440 | // C++11 19.5.4 Comparison operators |
2f7f1aca | 441 | |
20b917e7 JW |
442 | /** Equality comparison for std::error_code. |
443 | * | |
444 | * Returns true only if they have the same category and the same value. | |
445 | * | |
446 | * @relates error_condition | |
447 | * @since C++11 | |
448 | */ | |
5f1ce851 | 449 | [[__nodiscard__]] |
4661c8fd | 450 | inline bool |
cd88bb8c | 451 | operator==(const error_code& __lhs, const error_code& __rhs) noexcept |
20b917e7 JW |
452 | { |
453 | return __lhs.category() == __rhs.category() | |
454 | && __lhs.value() == __rhs.value(); | |
455 | } | |
70593ad2 | 456 | |
20b917e7 JW |
457 | /** Equality comparison for std::error_code and std::error_condition. |
458 | * | |
459 | * Uses each category's `equivalent` member function to check whether | |
460 | * the values correspond to an equivalent error in that category. | |
461 | * | |
462 | * @relates error_condition | |
463 | * @since C++11 | |
464 | */ | |
5f1ce851 | 465 | [[__nodiscard__]] |
4661c8fd | 466 | inline bool |
cd88bb8c | 467 | operator==(const error_code& __lhs, const error_condition& __rhs) noexcept |
70593ad2 | 468 | { |
20b917e7 JW |
469 | return __lhs.category().equivalent(__lhs.value(), __rhs) |
470 | || __rhs.category().equivalent(__lhs, __rhs.value()); | |
70593ad2 BK |
471 | } |
472 | ||
20b917e7 JW |
473 | /** Equality comparison for std::error_condition. |
474 | * | |
475 | * Returns true only if they have the same category and the same value. | |
476 | * | |
477 | * @relates error_condition | |
478 | * @since C++11 | |
479 | */ | |
5f1ce851 | 480 | [[__nodiscard__]] |
4661c8fd | 481 | inline bool |
4be779f5 JW |
482 | operator==(const error_condition& __lhs, |
483 | const error_condition& __rhs) noexcept | |
70593ad2 | 484 | { |
20b917e7 JW |
485 | return __lhs.category() == __rhs.category() |
486 | && __lhs.value() == __rhs.value(); | |
70593ad2 BK |
487 | } |
488 | ||
20b917e7 JW |
489 | /** Ordered comparison for std::error_condition. |
490 | * | |
491 | * This defines a total order by comparing the categories, and then | |
492 | * if they are equal comparing the values. | |
493 | * | |
494 | * @relates error_condition | |
495 | * @since C++11 | |
496 | */ | |
4be779f5 | 497 | #if __cpp_lib_three_way_comparison |
5f1ce851 | 498 | [[nodiscard]] |
4be779f5 JW |
499 | inline strong_ordering |
500 | operator<=>(const error_condition& __lhs, | |
501 | const error_condition& __rhs) noexcept | |
502 | { | |
503 | if (auto __c = __lhs.category() <=> __rhs.category(); __c != 0) | |
504 | return __c; | |
505 | return __lhs.value() <=> __rhs.value(); | |
506 | } | |
507 | #else | |
4661c8fd | 508 | inline bool |
4be779f5 JW |
509 | operator<(const error_condition& __lhs, |
510 | const error_condition& __rhs) noexcept | |
4661c8fd | 511 | { |
4be779f5 JW |
512 | return (__lhs.category() < __rhs.category() |
513 | || (__lhs.category() == __rhs.category() | |
514 | && __lhs.value() < __rhs.value())); | |
515 | } | |
516 | ||
4be779f5 JW |
517 | /// @relates error_condition |
518 | inline bool | |
519 | operator==(const error_condition& __lhs, const error_code& __rhs) noexcept | |
520 | { | |
521 | return (__rhs.category().equivalent(__rhs.value(), __lhs) | |
522 | || __lhs.category().equivalent(__rhs, __lhs.value())); | |
4661c8fd | 523 | } |
70593ad2 | 524 | |
2f7f1aca | 525 | /// @relates error_code |
4661c8fd | 526 | inline bool |
cd88bb8c | 527 | operator!=(const error_code& __lhs, const error_code& __rhs) noexcept |
4661c8fd | 528 | { return !(__lhs == __rhs); } |
70593ad2 | 529 | |
2f7f1aca | 530 | /// @relates error_code |
4661c8fd | 531 | inline bool |
cd88bb8c | 532 | operator!=(const error_code& __lhs, const error_condition& __rhs) noexcept |
4661c8fd | 533 | { return !(__lhs == __rhs); } |
70593ad2 | 534 | |
2f7f1aca | 535 | /// @relates error_condition |
4661c8fd | 536 | inline bool |
cd88bb8c | 537 | operator!=(const error_condition& __lhs, const error_code& __rhs) noexcept |
4661c8fd PC |
538 | { return !(__lhs == __rhs); } |
539 | ||
2f7f1aca | 540 | /// @relates error_condition |
4661c8fd | 541 | inline bool |
cd88bb8c PC |
542 | operator!=(const error_condition& __lhs, |
543 | const error_condition& __rhs) noexcept | |
4661c8fd | 544 | { return !(__lhs == __rhs); } |
4be779f5 | 545 | #endif // three_way_comparison |
f2ce64b5 | 546 | /// @} |
70593ad2 | 547 | |
33ac58d5 | 548 | /** |
2f7f1aca JW |
549 | * @brief An exception type that includes an `error_code` value. |
550 | * | |
551 | * Typically used to report errors from the operating system and other | |
552 | * low-level APIs. | |
5b9daa7e | 553 | * |
20b917e7 | 554 | * @headerfile system_error |
f2ce64b5 | 555 | * @since C++11 |
2f7f1aca | 556 | * @ingroup exceptions |
5b9daa7e | 557 | */ |
0646d8a3 BK |
558 | class system_error : public std::runtime_error |
559 | { | |
560 | private: | |
561 | error_code _M_code; | |
562 | ||
563 | public: | |
4514bed6 | 564 | system_error(error_code __ec = error_code()) |
b5fbd147 | 565 | : runtime_error(__ec.message()), _M_code(__ec) { } |
4514bed6 | 566 | |
70593ad2 | 567 | system_error(error_code __ec, const string& __what) |
51f94778 | 568 | : runtime_error(__what + (": " + __ec.message())), _M_code(__ec) { } |
b5fbd147 | 569 | |
a5dde6dd JW |
570 | system_error(error_code __ec, const char* __what) |
571 | : runtime_error(__what + (": " + __ec.message())), _M_code(__ec) { } | |
572 | ||
573 | system_error(int __v, const error_category& __ecat, const char* __what) | |
574 | : system_error(error_code(__v, __ecat), __what) { } | |
0646d8a3 | 575 | |
70593ad2 | 576 | system_error(int __v, const error_category& __ecat) |
b5fbd147 PC |
577 | : runtime_error(error_code(__v, __ecat).message()), |
578 | _M_code(__v, __ecat) { } | |
70593ad2 BK |
579 | |
580 | system_error(int __v, const error_category& __ecat, const string& __what) | |
51f94778 | 581 | : runtime_error(__what + (": " + error_code(__v, __ecat).message())), |
b5fbd147 | 582 | _M_code(__v, __ecat) { } |
0646d8a3 | 583 | |
f07c2237 JM |
584 | #if __cplusplus >= 201103L |
585 | system_error (const system_error &) = default; | |
586 | system_error &operator= (const system_error &) = default; | |
587 | #endif | |
588 | ||
8535715d | 589 | virtual ~system_error() noexcept; |
0646d8a3 | 590 | |
33ac58d5 | 591 | const error_code& |
cd88bb8c | 592 | code() const noexcept { return _M_code; } |
0646d8a3 BK |
593 | }; |
594 | ||
12ffa228 BK |
595 | _GLIBCXX_END_NAMESPACE_VERSION |
596 | } // namespace | |
0646d8a3 | 597 | |
15d81a3c PC |
598 | #include <bits/functional_hash.h> |
599 | ||
12ffa228 BK |
600 | namespace std _GLIBCXX_VISIBILITY(default) |
601 | { | |
602 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
15d81a3c | 603 | |
7f359d19 | 604 | #ifndef _GLIBCXX_COMPATIBILITY_CXX0X |
15d81a3c PC |
605 | // DR 1182. |
606 | /// std::hash specialization for error_code. | |
2f7f1aca | 607 | /// @relates error_code |
15d81a3c PC |
608 | template<> |
609 | struct hash<error_code> | |
5d64ee19 | 610 | : public __hash_base<size_t, error_code> |
15d81a3c PC |
611 | { |
612 | size_t | |
72f1c34b | 613 | operator()(const error_code& __e) const noexcept |
15d81a3c | 614 | { |
fe6fb0d1 JW |
615 | const size_t __tmp = std::_Hash_impl::hash(__e.value()); |
616 | return std::_Hash_impl::__hash_combine(&__e.category(), __tmp); | |
15d81a3c PC |
617 | } |
618 | }; | |
7f359d19 DK |
619 | #endif // _GLIBCXX_COMPATIBILITY_CXX0X |
620 | ||
fe6fb0d1 | 621 | #if __cplusplus >= 201703L |
7f359d19 DK |
622 | // DR 2686. |
623 | /// std::hash specialization for error_condition. | |
2f7f1aca | 624 | /// @relates error_condition |
7f359d19 DK |
625 | template<> |
626 | struct hash<error_condition> | |
627 | : public __hash_base<size_t, error_condition> | |
628 | { | |
629 | size_t | |
630 | operator()(const error_condition& __e) const noexcept | |
631 | { | |
632 | const size_t __tmp = std::_Hash_impl::hash(__e.value()); | |
fe6fb0d1 | 633 | return std::_Hash_impl::__hash_combine(&__e.category(), __tmp); |
7f359d19 DK |
634 | } |
635 | }; | |
636 | #endif | |
15d81a3c | 637 | |
12ffa228 BK |
638 | _GLIBCXX_END_NAMESPACE_VERSION |
639 | } // namespace | |
15d81a3c | 640 | |
734f5023 | 641 | #endif // C++11 |
57317d2a PC |
642 | |
643 | #endif // _GLIBCXX_SYSTEM_ERROR |