1 // <system_error> -*- C++ -*-
3 // Copyright (C) 2007-2020 Free Software Foundation, Inc.
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
8 // Free Software Foundation; either version 3, or (at your option)
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.
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.
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/>.
25 /** @file include/system_error
26 * This is a Standard C++ Library header.
29 #ifndef _GLIBCXX_SYSTEM_ERROR
30 #define _GLIBCXX_SYSTEM_ERROR 1
32 #pragma GCC system_header
34 #if __cplusplus < 201103L
35 # include <bits/c++0x_warning.h>
38 #include <bits/c++config.h>
39 #include <bits/error_constants.h>
42 #if __cplusplus > 201703L
46 namespace std _GLIBCXX_VISIBILITY(default)
48 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 /** @addtogroup diagnostics
55 class error_condition;
58 /// is_error_code_enum
59 template<typename _Tp>
60 struct is_error_code_enum : public false_type { };
62 /// is_error_condition_enum
63 template<typename _Tp>
64 struct is_error_condition_enum : public false_type { };
67 struct is_error_condition_enum<errc>
68 : public true_type { };
70 #if __cplusplus > 201402L
71 template <typename _Tp>
72 inline constexpr bool is_error_code_enum_v =
73 is_error_code_enum<_Tp>::value;
74 template <typename _Tp>
75 inline constexpr bool is_error_condition_enum_v =
76 is_error_condition_enum<_Tp>::value;
78 inline namespace _V2 {
80 /** Abstract base class for types defining a category of error codes.
82 * An error category defines a context that give meaning to the integer
83 * stored in an `error_code` or `error_condition` object. For example,
84 * the standard `errno` constants such a `EINVAL` and `ENOMEM` are
85 * associated with the "generic" category and other OS-specific error
86 * numbers are associated with the "system" category, but a user-defined
87 * category might give different meanings to the same numerical values.
92 constexpr error_category() noexcept = default;
94 virtual ~error_category();
96 error_category(const error_category&) = delete;
97 error_category& operator=(const error_category&) = delete;
100 name() const noexcept = 0;
102 // We need two different virtual functions here, one returning a
103 // COW string and one returning an SSO string. Their positions in the
104 // vtable must be consistent for dynamic dispatch to work, but which one
105 // the name "message()" finds depends on which ABI the caller is using.
106 #if _GLIBCXX_USE_CXX11_ABI
108 _GLIBCXX_DEFAULT_ABI_TAG
110 _M_message(int) const;
113 _GLIBCXX_DEFAULT_ABI_TAG
115 message(int) const = 0;
118 message(int) const = 0;
122 _M_message(int) const;
126 virtual error_condition
127 default_error_condition(int __i) const noexcept;
130 equivalent(int __i, const error_condition& __cond) const noexcept;
133 equivalent(const error_code& __code, int __i) const noexcept;
136 operator==(const error_category& __other) const noexcept
137 { return this == &__other; }
139 #if __cpp_lib_three_way_comparison
141 operator<=>(const error_category& __rhs) const noexcept
142 { return std::compare_three_way()(this, &__rhs); }
145 operator!=(const error_category& __other) const noexcept
146 { return this != &__other; }
149 operator<(const error_category& __other) const noexcept
150 { return less<const error_category*>()(this, &__other); }
156 /// Error category for `errno` error codes.
157 _GLIBCXX_CONST const error_category& generic_category() noexcept;
159 /// Error category for other error codes defined by the OS.
160 _GLIBCXX_CONST const error_category& system_category() noexcept;
162 } // end inline namespace
164 error_code make_error_code(errc) noexcept;
168 * This class is a value type storing an integer error number and a
169 * category that gives meaning to the error number. Typically this is done
170 * close the the point where the error happens, to capture the original
173 * An `error_code` object can be used to store the original error value
174 * emitted by some subsystem, with a category relevant to the subsystem.
175 * For example, errors from POSIX library functions can be represented by
176 * an `errno` value and the "generic" category, but errors from an HTTP
177 * library might be represented by an HTTP response status code (e.g. 404)
178 * and a custom category defined by the library.
182 error_code() noexcept
183 : _M_value(0), _M_cat(&system_category()) { }
185 error_code(int __v, const error_category& __cat) noexcept
186 : _M_value(__v), _M_cat(&__cat) { }
188 template<typename _ErrorCodeEnum, typename = typename
189 enable_if<is_error_code_enum<_ErrorCodeEnum>::value>::type>
190 error_code(_ErrorCodeEnum __e) noexcept
191 { *this = make_error_code(__e); }
194 assign(int __v, const error_category& __cat) noexcept
202 { assign(0, system_category()); }
205 template<typename _ErrorCodeEnum>
206 typename enable_if<is_error_code_enum<_ErrorCodeEnum>::value,
208 operator=(_ErrorCodeEnum __e) noexcept
209 { return *this = make_error_code(__e); }
212 value() const noexcept { return _M_value; }
214 const error_category&
215 category() const noexcept { return *_M_cat; }
218 default_error_condition() const noexcept;
220 _GLIBCXX_DEFAULT_ABI_TAG
223 { return category().message(value()); }
225 explicit operator bool() const noexcept
226 { return _M_value != 0; }
231 const error_category* _M_cat;
234 // 19.4.2.6 non-member functions
236 /// @relates error_code @{
239 make_error_code(errc __e) noexcept
240 { return error_code(static_cast<int>(__e), generic_category()); }
242 #if __cpp_lib_three_way_comparison
243 inline strong_ordering
244 operator<=>(const error_code& __lhs, const error_code& __rhs) noexcept
246 if (auto __c = __lhs.category() <=> __rhs.category(); __c != 0)
248 return __lhs.value() <=> __rhs.value();
252 operator<(const error_code& __lhs, const error_code& __rhs) noexcept
254 return (__lhs.category() < __rhs.category()
255 || (__lhs.category() == __rhs.category()
256 && __lhs.value() < __rhs.value()));
260 template<typename _CharT, typename _Traits>
261 basic_ostream<_CharT, _Traits>&
262 operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
263 { return (__os << __e.category().name() << ':' << __e.value()); }
267 error_condition make_error_condition(errc) noexcept;
269 /** Class error_condition
271 * This class represents error conditions that may be visible at an API
272 * boundary. Different `error_code` values that can occur within a library
273 * or module might map to the same `error_condition`.
275 * An `error_condition` represents something that the program can test for,
276 * and subsequently take appropriate action.
278 struct error_condition
280 error_condition() noexcept
281 : _M_value(0), _M_cat(&generic_category()) { }
283 error_condition(int __v, const error_category& __cat) noexcept
284 : _M_value(__v), _M_cat(&__cat) { }
286 template<typename _ErrorConditionEnum, typename = typename
287 enable_if<is_error_condition_enum<_ErrorConditionEnum>::value>::type>
288 error_condition(_ErrorConditionEnum __e) noexcept
289 { *this = make_error_condition(__e); }
292 assign(int __v, const error_category& __cat) noexcept
299 template<typename _ErrorConditionEnum>
300 typename enable_if<is_error_condition_enum
301 <_ErrorConditionEnum>::value, error_condition&>::type
302 operator=(_ErrorConditionEnum __e) noexcept
303 { return *this = make_error_condition(__e); }
307 { assign(0, generic_category()); }
309 // 19.4.3.4 observers
311 value() const noexcept { return _M_value; }
313 const error_category&
314 category() const noexcept { return *_M_cat; }
316 _GLIBCXX_DEFAULT_ABI_TAG
319 { return category().message(value()); }
321 explicit operator bool() const noexcept
322 { return _M_value != 0; }
327 const error_category* _M_cat;
330 // 19.4.3.6 non-member functions
332 /// Create an `error_condition` representing a standard `errc` condition.
333 /// @relates error_condition
334 inline error_condition
335 make_error_condition(errc __e) noexcept
336 { return error_condition(static_cast<int>(__e), generic_category()); }
338 // 19.4.4 Comparison operators
340 /// @relates error_code
342 operator==(const error_code& __lhs, const error_code& __rhs) noexcept
343 { return (__lhs.category() == __rhs.category()
344 && __lhs.value() == __rhs.value()); }
346 /// @relates error_code
347 /// @relates error_condition
349 operator==(const error_code& __lhs, const error_condition& __rhs) noexcept
351 return (__lhs.category().equivalent(__lhs.value(), __rhs)
352 || __rhs.category().equivalent(__lhs, __rhs.value()));
355 /// @relates error_condition
357 operator==(const error_condition& __lhs,
358 const error_condition& __rhs) noexcept
360 return (__lhs.category() == __rhs.category()
361 && __lhs.value() == __rhs.value());
364 #if __cpp_lib_three_way_comparison
365 /// Define an ordering for error_condition objects.
366 /// @relates error_condition
367 inline strong_ordering
368 operator<=>(const error_condition& __lhs,
369 const error_condition& __rhs) noexcept
371 if (auto __c = __lhs.category() <=> __rhs.category(); __c != 0)
373 return __lhs.value() <=> __rhs.value();
376 /// Define an ordering for error_condition objects.
377 /// @relates error_condition
379 operator<(const error_condition& __lhs,
380 const error_condition& __rhs) noexcept
382 return (__lhs.category() < __rhs.category()
383 || (__lhs.category() == __rhs.category()
384 && __lhs.value() < __rhs.value()));
387 /// @relates error_code
388 /// @relates error_condition
390 operator==(const error_condition& __lhs, const error_code& __rhs) noexcept
392 return (__rhs.category().equivalent(__rhs.value(), __lhs)
393 || __lhs.category().equivalent(__rhs, __lhs.value()));
396 /// @relates error_code
398 operator!=(const error_code& __lhs, const error_code& __rhs) noexcept
399 { return !(__lhs == __rhs); }
401 /// @relates error_code
402 /// @relates error_condition
404 operator!=(const error_code& __lhs, const error_condition& __rhs) noexcept
405 { return !(__lhs == __rhs); }
407 /// @relates error_code
408 /// @relates error_condition
410 operator!=(const error_condition& __lhs, const error_code& __rhs) noexcept
411 { return !(__lhs == __rhs); }
413 /// @relates error_condition
415 operator!=(const error_condition& __lhs,
416 const error_condition& __rhs) noexcept
417 { return !(__lhs == __rhs); }
418 #endif // three_way_comparison
421 * @brief An exception type that includes an `error_code` value.
423 * Typically used to report errors from the operating system and other
426 * @ingroup exceptions
428 class system_error : public std::runtime_error
434 system_error(error_code __ec = error_code())
435 : runtime_error(__ec.message()), _M_code(__ec) { }
437 system_error(error_code __ec, const string& __what)
438 : runtime_error(__what + ": " + __ec.message()), _M_code(__ec) { }
440 system_error(error_code __ec, const char* __what)
441 : runtime_error(__what + (": " + __ec.message())), _M_code(__ec) { }
443 system_error(int __v, const error_category& __ecat, const char* __what)
444 : system_error(error_code(__v, __ecat), __what) { }
446 system_error(int __v, const error_category& __ecat)
447 : runtime_error(error_code(__v, __ecat).message()),
448 _M_code(__v, __ecat) { }
450 system_error(int __v, const error_category& __ecat, const string& __what)
451 : runtime_error(__what + ": " + error_code(__v, __ecat).message()),
452 _M_code(__v, __ecat) { }
454 #if __cplusplus >= 201103L
455 system_error (const system_error &) = default;
456 system_error &operator= (const system_error &) = default;
459 virtual ~system_error() noexcept;
462 code() const noexcept { return _M_code; }
465 _GLIBCXX_END_NAMESPACE_VERSION
468 #include <bits/functional_hash.h>
470 namespace std _GLIBCXX_VISIBILITY(default)
472 _GLIBCXX_BEGIN_NAMESPACE_VERSION
474 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
476 /// std::hash specialization for error_code.
477 /// @relates error_code
479 struct hash<error_code>
480 : public __hash_base<size_t, error_code>
483 operator()(const error_code& __e) const noexcept
485 const size_t __tmp = std::_Hash_impl::hash(__e.value());
486 return std::_Hash_impl::__hash_combine(&__e.category(), __tmp);
489 #endif // _GLIBCXX_COMPATIBILITY_CXX0X
491 #if __cplusplus >= 201703L
493 /// std::hash specialization for error_condition.
494 /// @relates error_condition
496 struct hash<error_condition>
497 : public __hash_base<size_t, error_condition>
500 operator()(const error_condition& __e) const noexcept
502 const size_t __tmp = std::_Hash_impl::hash(__e.value());
503 return std::_Hash_impl::__hash_combine(&__e.category(), __tmp);
508 _GLIBCXX_END_NAMESPACE_VERSION
513 #endif // _GLIBCXX_SYSTEM_ERROR