]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/std/system_error
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / std / system_error
CommitLineData
0646d8a3
BK
1// <system_error> -*- C++ -*-
2
8d9254fc 3// Copyright (C) 2007-2020 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
32#pragma GCC system_header
33
734f5023 34#if __cplusplus < 201103L
ab65a4c7 35# include <bits/c++0x_warning.h>
57317d2a 36#else
0646d8a3
BK
37
38#include <bits/c++config.h>
e4bf5dfc 39#include <bits/error_constants.h>
0646d8a3
BK
40#include <iosfwd>
41#include <stdexcept>
42
12ffa228
BK
43namespace std _GLIBCXX_VISIBILITY(default)
44{
45_GLIBCXX_BEGIN_NAMESPACE_VERSION
0646d8a3 46
2f7f1aca
JW
47 /** @addtogroup diagnostics
48 * @{
49 */
50
0646d8a3 51 class error_code;
70593ad2 52 class error_condition;
70593ad2
BK
53 class system_error;
54
55 /// is_error_code_enum
c9faf465 56 template<typename _Tp>
70593ad2
BK
57 struct is_error_code_enum : public false_type { };
58
70593ad2 59 /// is_error_condition_enum
c9faf465 60 template<typename _Tp>
70593ad2
BK
61 struct is_error_condition_enum : public false_type { };
62
33ac58d5 63 template<>
92010a79 64 struct is_error_condition_enum<errc>
70593ad2 65 : public true_type { };
0646d8a3 66
137422c8
VV
67#if __cplusplus > 201402L
68 template <typename _Tp>
288695f7
DK
69 inline constexpr bool is_error_code_enum_v =
70 is_error_code_enum<_Tp>::value;
137422c8 71 template <typename _Tp>
288695f7 72 inline constexpr bool is_error_condition_enum_v =
137422c8
VV
73 is_error_condition_enum<_Tp>::value;
74#endif // C++17
34a2b755 75 inline namespace _V2 {
0646d8a3 76
2f7f1aca
JW
77 /** Abstract base class for types defining a category of error codes.
78 *
79 * An error category defines a context that give meaning to the integer
5b503252 80 * stored in an `error_code` or `error_condition` object. For example,
2f7f1aca
JW
81 * the standard `errno` constants such a `EINVAL` and `ENOMEM` are
82 * associated with the "generic" category and other OS-specific error
83 * numbers are associated with the "system" category, but a user-defined
84 * category might give different meanings to the same numerical values.
85 */
92010a79 86 class error_category
0646d8a3 87 {
f891e5d1 88 public:
f891e5d1 89 constexpr error_category() noexcept = default;
92010a79 90
f891e5d1 91 virtual ~error_category();
92010a79
CF
92
93 error_category(const error_category&) = delete;
94 error_category& operator=(const error_category&) = delete;
0646d8a3 95
33ac58d5 96 virtual const char*
cd88bb8c 97 name() const noexcept = 0;
70593ad2 98
34a2b755
JW
99 // We need two different virtual functions here, one returning a
100 // COW string and one returning an SSO string. Their positions in the
101 // vtable must be consistent for dynamic dispatch to work, but which one
102 // the name "message()" finds depends on which ABI the caller is using.
103#if _GLIBCXX_USE_CXX11_ABI
104 private:
105 _GLIBCXX_DEFAULT_ABI_TAG
106 virtual __cow_string
107 _M_message(int) const;
108
109 public:
110 _GLIBCXX_DEFAULT_ABI_TAG
111 virtual string
112 message(int) const = 0;
113#else
33ac58d5 114 virtual string
70593ad2
BK
115 message(int) const = 0;
116
34a2b755
JW
117 private:
118 virtual __sso_string
119 _M_message(int) const;
120#endif
121
122 public:
70593ad2 123 virtual error_condition
cd88bb8c 124 default_error_condition(int __i) const noexcept;
70593ad2 125
33ac58d5 126 virtual bool
cd88bb8c 127 equivalent(int __i, const error_condition& __cond) const noexcept;
70593ad2 128
33ac58d5 129 virtual bool
cd88bb8c 130 equivalent(const error_code& __code, int __i) const noexcept;
70593ad2 131
33ac58d5 132 bool
cd88bb8c 133 operator<(const error_category& __other) const noexcept
70593ad2
BK
134 { return less<const error_category*>()(this, &__other); }
135
33ac58d5 136 bool
cd88bb8c 137 operator==(const error_category& __other) const noexcept
0646d8a3
BK
138 { return this == &__other; }
139
33ac58d5 140 bool
cd88bb8c 141 operator!=(const error_category& __other) const noexcept
0646d8a3 142 { return this != &__other; }
0646d8a3
BK
143 };
144
92010a79 145 // DR 890.
2f7f1aca
JW
146
147 /// Error category for `errno` error codes.
cd88bb8c 148 _GLIBCXX_CONST const error_category& generic_category() noexcept;
70593ad2 149
2f7f1aca
JW
150 /// Error category for other error codes defined by the OS.
151 _GLIBCXX_CONST const error_category& system_category() noexcept;
152
34a2b755
JW
153 } // end inline namespace
154
cd88bb8c 155 error_code make_error_code(errc) noexcept;
bb81f9a0 156
2f7f1aca
JW
157 /** Class error_code
158 *
159 * This class is a value type storing an integer error number and a
160 * category that gives meaning to the error number. Typically this is done
161 * close the the point where the error happens, to capture the original
162 * error value.
163 *
164 * An `error_code` object can be used to store the original error value
165 * emitted by some subsystem, with a category relevant to the subsystem.
166 * For example, errors from POSIX library functions can be represented by
167 * an `errno` value and the "generic" category, but errors from an HTTP
168 * library might be represented by an HTTP response status code (e.g. 404)
169 * and a custom category defined by the library.
170 */
0646d8a3
BK
171 struct error_code
172 {
cd88bb8c 173 error_code() noexcept
bb81f9a0 174 : _M_value(0), _M_cat(&system_category()) { }
0646d8a3 175
cd88bb8c 176 error_code(int __v, const error_category& __cat) noexcept
0646d8a3
BK
177 : _M_value(__v), _M_cat(&__cat) { }
178
cd88bb8c
PC
179 template<typename _ErrorCodeEnum, typename = typename
180 enable_if<is_error_code_enum<_ErrorCodeEnum>::value>::type>
181 error_code(_ErrorCodeEnum __e) noexcept
bb81f9a0 182 { *this = make_error_code(__e); }
0646d8a3 183
33ac58d5 184 void
cd88bb8c 185 assign(int __v, const error_category& __cat) noexcept
0646d8a3
BK
186 {
187 _M_value = __v;
33ac58d5 188 _M_cat = &__cat;
0646d8a3
BK
189 }
190
33ac58d5 191 void
cd88bb8c 192 clear() noexcept
9b3003d5 193 { assign(0, system_category()); }
70593ad2 194
4661c8fd 195 // DR 804.
70593ad2 196 template<typename _ErrorCodeEnum>
4661c8fd
PC
197 typename enable_if<is_error_code_enum<_ErrorCodeEnum>::value,
198 error_code&>::type
cd88bb8c 199 operator=(_ErrorCodeEnum __e) noexcept
bb81f9a0 200 { return *this = make_error_code(__e); }
0646d8a3
BK
201
202 int
cd88bb8c 203 value() const noexcept { return _M_value; }
33ac58d5
JW
204
205 const error_category&
cd88bb8c 206 category() const noexcept { return *_M_cat; }
0646d8a3 207
33ac58d5 208 error_condition
cd88bb8c 209 default_error_condition() const noexcept;
70593ad2 210
34a2b755 211 _GLIBCXX_DEFAULT_ABI_TAG
33ac58d5 212 string
70593ad2
BK
213 message() const
214 { return category().message(value()); }
0646d8a3 215
cd88bb8c 216 explicit operator bool() const noexcept
2eb57e54 217 { return _M_value != 0; }
0646d8a3 218
4661c8fd 219 // DR 804.
0646d8a3
BK
220 private:
221 int _M_value;
222 const error_category* _M_cat;
223 };
224
92010a79 225 // 19.4.2.6 non-member functions
2f7f1aca
JW
226
227 /// @relates error_code @{
228
92010a79 229 inline error_code
cd88bb8c 230 make_error_code(errc __e) noexcept
9b3003d5 231 { return error_code(static_cast<int>(__e), generic_category()); }
92010a79 232
4661c8fd 233 inline bool
cd88bb8c 234 operator<(const error_code& __lhs, const error_code& __rhs) noexcept
33ac58d5 235 {
4661c8fd
PC
236 return (__lhs.category() < __rhs.category()
237 || (__lhs.category() == __rhs.category()
238 && __lhs.value() < __rhs.value()));
239 }
70593ad2 240
4661c8fd
PC
241 template<typename _CharT, typename _Traits>
242 basic_ostream<_CharT, _Traits>&
243 operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
244 { return (__os << __e.category().name() << ':' << __e.value()); }
70593ad2 245
2f7f1aca
JW
246 // @}
247
cd88bb8c 248 error_condition make_error_condition(errc) noexcept;
70593ad2 249
2f7f1aca
JW
250 /** Class error_condition
251 *
252 * This class represents error conditions that may be visible at an API
253 * boundary. Different `error_code` values that can occur within a library
254 * or module might map to the same `error_condition`.
255 *
256 * An `error_condition` represents something that the program can test for,
257 * and subsequently take appropriate action.
258 */
33ac58d5 259 struct error_condition
70593ad2 260 {
cd88bb8c 261 error_condition() noexcept
bb81f9a0 262 : _M_value(0), _M_cat(&generic_category()) { }
70593ad2 263
cd88bb8c 264 error_condition(int __v, const error_category& __cat) noexcept
4661c8fd 265 : _M_value(__v), _M_cat(&__cat) { }
70593ad2 266
cd88bb8c
PC
267 template<typename _ErrorConditionEnum, typename = typename
268 enable_if<is_error_condition_enum<_ErrorConditionEnum>::value>::type>
269 error_condition(_ErrorConditionEnum __e) noexcept
bb81f9a0 270 { *this = make_error_condition(__e); }
70593ad2 271
4661c8fd 272 void
cd88bb8c 273 assign(int __v, const error_category& __cat) noexcept
4661c8fd
PC
274 {
275 _M_value = __v;
276 _M_cat = &__cat;
277 }
70593ad2 278
4661c8fd
PC
279 // DR 804.
280 template<typename _ErrorConditionEnum>
281 typename enable_if<is_error_condition_enum
282 <_ErrorConditionEnum>::value, error_condition&>::type
cd88bb8c 283 operator=(_ErrorConditionEnum __e) noexcept
bb81f9a0 284 { return *this = make_error_condition(__e); }
70593ad2 285
33ac58d5 286 void
cd88bb8c 287 clear() noexcept
9b3003d5 288 { assign(0, generic_category()); }
70593ad2
BK
289
290 // 19.4.3.4 observers
cd88bb8c
PC
291 int
292 value() const noexcept { return _M_value; }
70593ad2 293
4661c8fd 294 const error_category&
cd88bb8c 295 category() const noexcept { return *_M_cat; }
70593ad2 296
34a2b755 297 _GLIBCXX_DEFAULT_ABI_TAG
33ac58d5 298 string
70593ad2
BK
299 message() const
300 { return category().message(value()); }
301
cd88bb8c 302 explicit operator bool() const noexcept
2eb57e54 303 { return _M_value != 0; }
70593ad2 304
4661c8fd 305 // DR 804.
70593ad2
BK
306 private:
307 int _M_value;
4661c8fd 308 const error_category* _M_cat;
70593ad2
BK
309 };
310
92010a79 311 // 19.4.3.6 non-member functions
2f7f1aca
JW
312
313 /// Create an `error_condition` representing a standard `errc` condition.
314 /// @relates error_condition
92010a79 315 inline error_condition
cd88bb8c 316 make_error_condition(errc __e) noexcept
9b3003d5 317 { return error_condition(static_cast<int>(__e), generic_category()); }
92010a79 318
2f7f1aca
JW
319 /// Define an ordering for error_condition objects.
320 /// @relates error_condition
33ac58d5 321 inline bool
cd88bb8c
PC
322 operator<(const error_condition& __lhs,
323 const error_condition& __rhs) noexcept
4661c8fd
PC
324 {
325 return (__lhs.category() < __rhs.category()
326 || (__lhs.category() == __rhs.category()
327 && __lhs.value() < __rhs.value()));
328 }
329
70593ad2 330 // 19.4.4 Comparison operators
2f7f1aca
JW
331
332 /// @relates error_code
4661c8fd 333 inline bool
cd88bb8c 334 operator==(const error_code& __lhs, const error_code& __rhs) noexcept
4661c8fd
PC
335 { return (__lhs.category() == __rhs.category()
336 && __lhs.value() == __rhs.value()); }
70593ad2 337
2f7f1aca
JW
338 /// @relates error_code
339 /// @relates error_condition
4661c8fd 340 inline bool
cd88bb8c 341 operator==(const error_code& __lhs, const error_condition& __rhs) noexcept
70593ad2 342 {
4661c8fd
PC
343 return (__lhs.category().equivalent(__lhs.value(), __rhs)
344 || __rhs.category().equivalent(__lhs, __rhs.value()));
70593ad2
BK
345 }
346
2f7f1aca
JW
347 /// @relates error_code
348 /// @relates error_condition
4661c8fd 349 inline bool
cd88bb8c 350 operator==(const error_condition& __lhs, const error_code& __rhs) noexcept
70593ad2 351 {
4661c8fd
PC
352 return (__rhs.category().equivalent(__rhs.value(), __lhs)
353 || __lhs.category().equivalent(__rhs, __lhs.value()));
70593ad2
BK
354 }
355
2f7f1aca 356 /// @relates error_condition
4661c8fd 357 inline bool
cd88bb8c
PC
358 operator==(const error_condition& __lhs,
359 const error_condition& __rhs) noexcept
4661c8fd
PC
360 {
361 return (__lhs.category() == __rhs.category()
362 && __lhs.value() == __rhs.value());
363 }
70593ad2 364
2f7f1aca 365 /// @relates error_code
4661c8fd 366 inline bool
cd88bb8c 367 operator!=(const error_code& __lhs, const error_code& __rhs) noexcept
4661c8fd 368 { return !(__lhs == __rhs); }
70593ad2 369
2f7f1aca
JW
370 /// @relates error_code
371 /// @relates error_condition
4661c8fd 372 inline bool
cd88bb8c 373 operator!=(const error_code& __lhs, const error_condition& __rhs) noexcept
4661c8fd 374 { return !(__lhs == __rhs); }
70593ad2 375
2f7f1aca
JW
376 /// @relates error_code
377 /// @relates error_condition
4661c8fd 378 inline bool
cd88bb8c 379 operator!=(const error_condition& __lhs, const error_code& __rhs) noexcept
4661c8fd
PC
380 { return !(__lhs == __rhs); }
381
2f7f1aca 382 /// @relates error_condition
4661c8fd 383 inline bool
cd88bb8c
PC
384 operator!=(const error_condition& __lhs,
385 const error_condition& __rhs) noexcept
4661c8fd 386 { return !(__lhs == __rhs); }
70593ad2 387
70593ad2 388
33ac58d5 389 /**
2f7f1aca
JW
390 * @brief An exception type that includes an `error_code` value.
391 *
392 * Typically used to report errors from the operating system and other
393 * low-level APIs.
5b9daa7e 394 *
2f7f1aca 395 * @ingroup exceptions
5b9daa7e 396 */
0646d8a3
BK
397 class system_error : public std::runtime_error
398 {
399 private:
400 error_code _M_code;
401
402 public:
4514bed6 403 system_error(error_code __ec = error_code())
b5fbd147 404 : runtime_error(__ec.message()), _M_code(__ec) { }
4514bed6 405
70593ad2 406 system_error(error_code __ec, const string& __what)
b5fbd147
PC
407 : runtime_error(__what + ": " + __ec.message()), _M_code(__ec) { }
408
a5dde6dd
JW
409 system_error(error_code __ec, const char* __what)
410 : runtime_error(__what + (": " + __ec.message())), _M_code(__ec) { }
411
412 system_error(int __v, const error_category& __ecat, const char* __what)
413 : system_error(error_code(__v, __ecat), __what) { }
0646d8a3 414
70593ad2 415 system_error(int __v, const error_category& __ecat)
b5fbd147
PC
416 : runtime_error(error_code(__v, __ecat).message()),
417 _M_code(__v, __ecat) { }
70593ad2
BK
418
419 system_error(int __v, const error_category& __ecat, const string& __what)
b5fbd147
PC
420 : runtime_error(__what + ": " + error_code(__v, __ecat).message()),
421 _M_code(__v, __ecat) { }
0646d8a3 422
f07c2237
JM
423#if __cplusplus >= 201103L
424 system_error (const system_error &) = default;
425 system_error &operator= (const system_error &) = default;
426#endif
427
8535715d 428 virtual ~system_error() noexcept;
0646d8a3 429
33ac58d5 430 const error_code&
cd88bb8c 431 code() const noexcept { return _M_code; }
0646d8a3
BK
432 };
433
12ffa228
BK
434_GLIBCXX_END_NAMESPACE_VERSION
435} // namespace
0646d8a3 436
15d81a3c
PC
437#include <bits/functional_hash.h>
438
12ffa228
BK
439namespace std _GLIBCXX_VISIBILITY(default)
440{
441_GLIBCXX_BEGIN_NAMESPACE_VERSION
15d81a3c 442
7f359d19 443#ifndef _GLIBCXX_COMPATIBILITY_CXX0X
15d81a3c
PC
444 // DR 1182.
445 /// std::hash specialization for error_code.
2f7f1aca 446 /// @relates error_code
15d81a3c
PC
447 template<>
448 struct hash<error_code>
5d64ee19 449 : public __hash_base<size_t, error_code>
15d81a3c
PC
450 {
451 size_t
72f1c34b 452 operator()(const error_code& __e) const noexcept
15d81a3c 453 {
fe6fb0d1
JW
454 const size_t __tmp = std::_Hash_impl::hash(__e.value());
455 return std::_Hash_impl::__hash_combine(&__e.category(), __tmp);
15d81a3c
PC
456 }
457 };
7f359d19
DK
458#endif // _GLIBCXX_COMPATIBILITY_CXX0X
459
fe6fb0d1 460#if __cplusplus >= 201703L
7f359d19
DK
461 // DR 2686.
462 /// std::hash specialization for error_condition.
2f7f1aca 463 /// @relates error_condition
7f359d19
DK
464 template<>
465 struct hash<error_condition>
466 : public __hash_base<size_t, error_condition>
467 {
468 size_t
469 operator()(const error_condition& __e) const noexcept
470 {
471 const size_t __tmp = std::_Hash_impl::hash(__e.value());
fe6fb0d1 472 return std::_Hash_impl::__hash_combine(&__e.category(), __tmp);
7f359d19
DK
473 }
474 };
475#endif
15d81a3c 476
12ffa228
BK
477_GLIBCXX_END_NAMESPACE_VERSION
478} // namespace
15d81a3c 479
734f5023 480#endif // C++11
57317d2a
PC
481
482#endif // _GLIBCXX_SYSTEM_ERROR