1// <system_error> -*- C++ -*-
3// Copyright (C) 2007-2022 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
46namespace 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;
80 inline namespace _V2 {
82 /** @addtogroup diagnostics
86 /** Abstract base class for types defining a category of error codes.
88 * An error category defines a context that gives meaning to the integer
89 * stored in an `error_code` or `error_condition` object. For example,
90 * the standard `errno` constants such a `EINVAL` and `ENOMEM` are
91 * associated with the "generic" category and other OS-specific error
92 * numbers are associated with the "system" category, but a user-defined
93 * category might give different meanings to the same numerical values.
95 * A user-defined category can override the `equivalent` member functions
96 * to define correspondence between errors in different categories.
97 * For example, a category for errors from disk I/O could consider some
98 * of its error numbers equivalent to ENOSPC and ENOENT in the generic
101 * @headerfile system_error
107 constexpr error_category() noexcept = default;
109 virtual ~error_category();
111 error_category(const error_category&) = delete;
112 error_category& operator=(const error_category&) = delete;
114 /// A string that identifies the error category.
116 name() const noexcept = 0;
118 // We need two different virtual functions here, one returning a
119 // COW string and one returning an SSO string. Their positions in the
120 // vtable must be consistent for dynamic dispatch to work, but which one
121 // the name "message()" finds depends on which ABI the caller is using.
122#if _GLIBCXX_USE_CXX11_ABI
124 _GLIBCXX_DEFAULT_ABI_TAG
126 _M_message(int) const;
129 /// A description of the error condition corresponding to the number.
130 _GLIBCXX_DEFAULT_ABI_TAG
132 message(int) const = 0;
135 message(int) const = 0;
139 _M_message(int) const;
143 /// Return an error_condition corresponding to `i` in this category.
144 virtual error_condition
145 default_error_condition(int __i) const noexcept;
147 /// Test whether `cond` corresponds to `i` for this category.
149 equivalent(int __i, const error_condition& __cond) const noexcept;
151 /// Test whether `code` corresponds to `i` for this category.
153 equivalent(const error_code& __code, int __i) const noexcept;
155 /// An error_category only compares equal to itself.
158 operator==(const error_category& __other) const noexcept
159 { return this == &__other; }
161 /// Ordered comparison that defines a total order for error categories.
162#if __cpp_lib_three_way_comparison
165 operator<=>(const error_category& __rhs) const noexcept
166 { return std::compare_three_way()(this, &__rhs); }
169 operator<(const error_category& __other) const noexcept
170 { return less<const error_category*>()(this, &__other); }
173 operator!=(const error_category& __other) const noexcept
174 { return this != &__other; }
180 /// Error category for `errno` error codes.
181 [[__nodiscard__, __gnu__::__const__]]
182 const error_category&
183 generic_category() noexcept;
185 /// Error category for other error codes defined by the OS.
186 [[__nodiscard__, __gnu__::__const__]]
187 const error_category&
188 system_category() noexcept;
191 } // end inline namespace
193 /** @addtogroup diagnostics
199 void make_error_code() = delete;
200 void make_error_condition() = delete;
205 * This class is a value type storing an integer error number and a
206 * category that gives meaning to the error number. Typically this is done
207 * close the the point where the error happens, to capture the original
210 * An `error_code` object can be used to store the original error value
211 * emitted by some subsystem, with a category relevant to the subsystem.
212 * For example, errors from POSIX library functions can be represented by
213 * an `errno` value and the "generic" category, but errors from an HTTP
214 * library might be represented by an HTTP response status code (e.g. 404)
215 * and a custom category defined by the library.
217 * @headerfile system_error
223 error_code() noexcept
224 : _M_value(0), _M_cat(&system_category()) { }
226 error_code(int __v, const error_category& __cat) noexcept
227 : _M_value(__v), _M_cat(&__cat) { }
229 template<typename _ErrorCodeEnum, typename = typename
230 enable_if<is_error_code_enum<_ErrorCodeEnum>::value>::type>
231 error_code(_ErrorCodeEnum __e) noexcept
233 using __adl_only::make_error_code;
234 *this = make_error_code(__e);
238 assign(int __v, const error_category& __cat) noexcept
246 { assign(0, system_category()); }
249 template<typename _ErrorCodeEnum>
250 typename enable_if<is_error_code_enum<_ErrorCodeEnum>::value,
252 operator=(_ErrorCodeEnum __e) noexcept
253 { return *this = make_error_code(__e); }
258 value() const noexcept { return _M_value; }
260 /// The error category that this error belongs to.
262 const error_category&
263 category() const noexcept { return *_M_cat; }
265 /// An `error_condition` for this error's category and value.
267 default_error_condition() const noexcept;
269 /// The category's description of the value.
270 _GLIBCXX_DEFAULT_ABI_TAG
273 { return category().message(value()); }
275 /// Test whether `value()` is non-zero.
277 explicit operator bool() const noexcept
278 { return _M_value != 0; }
283 const error_category* _M_cat;
286 // C++11 19.5.2.5 non-member functions
288 /** Create an `error_code` representing a standard `errc` condition.
290 * The `std::errc` constants correspond to `errno` macros and so use the
293 * @relates error_code
298 make_error_code(errc __e) noexcept
299 { return error_code(static_cast<int>(__e), generic_category()); }
301 /** Ordered comparison for std::error_code.
303 * This defines a total order by comparing the categories, and then
304 * if they are equal comparing the values.
306 * @relates error_code
309#if __cpp_lib_three_way_comparison
311 inline strong_ordering
312 operator<=>(const error_code& __lhs, const error_code& __rhs) noexcept
314 if (auto __c = __lhs.category() <=> __rhs.category(); __c != 0)
316 return __lhs.value() <=> __rhs.value();
320 operator<(const error_code& __lhs, const error_code& __rhs) noexcept
322 return (__lhs.category() < __rhs.category()
323 || (__lhs.category() == __rhs.category()
324 && __lhs.value() < __rhs.value()));
328 /** Write a std::error_code to an ostream.
330 * @relates error_code
333 template<typename _CharT, typename _Traits>
334 basic_ostream<_CharT, _Traits>&
335 operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
336 { return (__os << __e.category().name() << ':' << __e.value()); }
338 /** Class error_condition
340 * This class represents error conditions that may be visible at an API
341 * boundary. Different `error_code` values that can occur within a library
342 * or module might map to the same `error_condition`.
344 * An `error_condition` represents something that the program can test for,
345 * and subsequently take appropriate action.
347 * @headerfile system_error
350 class error_condition
353 /// Initialize with a zero (no error) value and the generic category.
354 error_condition() noexcept
355 : _M_value(0), _M_cat(&generic_category()) { }
357 /// Initialize with the specified value and category.
358 error_condition(int __v, const error_category& __cat) noexcept
359 : _M_value(__v), _M_cat(&__cat) { }
361 template<typename _ErrorConditionEnum, typename = typename
362 enable_if<is_error_condition_enum<_ErrorConditionEnum>::value>::type>
363 error_condition(_ErrorConditionEnum __e) noexcept
365 using __adl_only::make_error_condition;
366 *this = make_error_condition(__e);
369 /// Set the value and category.
371 assign(int __v, const error_category& __cat) noexcept
378 template<typename _ErrorConditionEnum>
379 typename enable_if<is_error_condition_enum
380 <_ErrorConditionEnum>::value, error_condition&>::type
381 operator=(_ErrorConditionEnum __e) noexcept
382 { return *this = make_error_condition(__e); }
384 /// Reset the value and category to the default-constructed state.
387 { assign(0, generic_category()); }
389 // C++11 19.5.3.4 observers
394 value() const noexcept { return _M_value; }
396 /// The error category that this error belongs to.
398 const error_category&
399 category() const noexcept { return *_M_cat; }
401 /// The category's description of the value.
402 _GLIBCXX_DEFAULT_ABI_TAG
405 { return category().message(value()); }
407 /// Test whether `value()` is non-zero.
409 explicit operator bool() const noexcept
410 { return _M_value != 0; }
415 const error_category* _M_cat;
418 // C++11 19.5.3.5 non-member functions
420 /** Create an `error_condition` representing a standard `errc` condition.
422 * The `std::errc` constants correspond to `errno` macros and so use the
425 * @relates error_condition
429 inline error_condition
430 make_error_condition(errc __e) noexcept
431 { return error_condition(static_cast<int>(__e), generic_category()); }
433 // C++11 19.5.4 Comparison operators
435 /** Equality comparison for std::error_code.
437 * Returns true only if they have the same category and the same value.
439 * @relates error_condition
444 operator==(const error_code& __lhs, const error_code& __rhs) noexcept
446 return __lhs.category() == __rhs.category()
447 && __lhs.value() == __rhs.value();
450 /** Equality comparison for std::error_code and std::error_condition.
452 * Uses each category's `equivalent` member function to check whether
453 * the values correspond to an equivalent error in that category.
455 * @relates error_condition
460 operator==(const error_code& __lhs, const error_condition& __rhs) noexcept
462 return __lhs.category().equivalent(__lhs.value(), __rhs)
463 || __rhs.category().equivalent(__lhs, __rhs.value());
466 /** Equality comparison for std::error_condition.
468 * Returns true only if they have the same category and the same value.
470 * @relates error_condition
475 operator==(const error_condition& __lhs,
476 const error_condition& __rhs) noexcept
478 return __lhs.category() == __rhs.category()
479 && __lhs.value() == __rhs.value();
482 /** Ordered comparison for std::error_condition.
484 * This defines a total order by comparing the categories, and then
485 * if they are equal comparing the values.
487 * @relates error_condition
490#if __cpp_lib_three_way_comparison
492 inline strong_ordering
493 operator<=>(const error_condition& __lhs,
494 const error_condition& __rhs) noexcept
496 if (auto __c = __lhs.category() <=> __rhs.category(); __c != 0)
498 return __lhs.value() <=> __rhs.value();
502 operator<(const error_condition& __lhs,
503 const error_condition& __rhs) noexcept
505 return (__lhs.category() < __rhs.category()
506 || (__lhs.category() == __rhs.category()
507 && __lhs.value() < __rhs.value()));
510 /// @relates error_condition
512 operator==(const error_condition& __lhs, const error_code& __rhs) noexcept
514 return (__rhs.category().equivalent(__rhs.value(), __lhs)
515 || __lhs.category().equivalent(__rhs, __lhs.value()));
518 /// @relates error_code
520 operator!=(const error_code& __lhs, const error_code& __rhs) noexcept
521 { return !(__lhs == __rhs); }
523 /// @relates error_code
525 operator!=(const error_code& __lhs, const error_condition& __rhs) noexcept
526 { return !(__lhs == __rhs); }
528 /// @relates error_condition
530 operator!=(const error_condition& __lhs, const error_code& __rhs) noexcept
531 { return !(__lhs == __rhs); }
533 /// @relates error_condition
535 operator!=(const error_condition& __lhs,
536 const error_condition& __rhs) noexcept
537 { return !(__lhs == __rhs); }
538#endif // three_way_comparison
542 * @brief An exception type that includes an `error_code` value.
544 * Typically used to report errors from the operating system and other
547 * @headerfile system_error
549 * @ingroup exceptions
551 class system_error : public std::runtime_error
557 system_error(error_code __ec = error_code())
558 : runtime_error(__ec.message()), _M_code(__ec) { }
560 system_error(error_code __ec, const string& __what)
561 : runtime_error(__what + ": " + __ec.message()), _M_code(__ec) { }
563 system_error(error_code __ec, const char* __what)
564 : runtime_error(__what + (": " + __ec.message())), _M_code(__ec) { }
566 system_error(int __v, const error_category& __ecat, const char* __what)
567 : system_error(error_code(__v, __ecat), __what) { }
569 system_error(int __v, const error_category& __ecat)
570 : runtime_error(error_code(__v, __ecat).message()),
571 _M_code(__v, __ecat) { }
573 system_error(int __v, const error_category& __ecat, const string& __what)
574 : runtime_error(__what + ": " + error_code(__v, __ecat).message()),
575 _M_code(__v, __ecat) { }
577#if __cplusplus >= 201103L
578 system_error (const system_error &) = default;
579 system_error &operator= (const system_error &) = default;
582 virtual ~system_error() noexcept;
585 code() const noexcept { return _M_code; }
588_GLIBCXX_END_NAMESPACE_VERSION
591#include <bits/functional_hash.h>
593namespace std _GLIBCXX_VISIBILITY(default)
595_GLIBCXX_BEGIN_NAMESPACE_VERSION
597#ifndef _GLIBCXX_COMPATIBILITY_CXX0X
599 /// std::hash specialization for error_code.
600 /// @relates error_code
602 struct hash<error_code>
603 : public __hash_base<size_t, error_code>
606 operator()(const error_code& __e) const noexcept
608 const size_t __tmp = std::_Hash_impl::hash(__e.value());
609 return std::_Hash_impl::__hash_combine(&__e.category(), __tmp);
612#endif // _GLIBCXX_COMPATIBILITY_CXX0X
614#if __cplusplus >= 201703L
616 /// std::hash specialization for error_condition.
617 /// @relates error_condition
619 struct hash<error_condition>
620 : public __hash_base<size_t, error_condition>
623 operator()(const error_condition& __e) const noexcept
625 const size_t __tmp = std::_Hash_impl::hash(__e.value());
626 return std::_Hash_impl::__hash_combine(&__e.category(), __tmp);
631_GLIBCXX_END_NAMESPACE_VERSION
636#endif // _GLIBCXX_SYSTEM_ERROR