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>
43 namespace std _GLIBCXX_VISIBILITY(default)
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
47 /** @addtogroup diagnostics
52 class error_condition;
55 /// is_error_code_enum
56 template<typename _Tp>
57 struct is_error_code_enum : public false_type { };
59 /// is_error_condition_enum
60 template<typename _Tp>
61 struct is_error_condition_enum : public false_type { };
64 struct is_error_condition_enum<errc>
65 : public true_type { };
67 #if __cplusplus > 201402L
68 template <typename _Tp>
69 inline constexpr bool is_error_code_enum_v =
70 is_error_code_enum<_Tp>::value;
71 template <typename _Tp>
72 inline constexpr bool is_error_condition_enum_v =
73 is_error_condition_enum<_Tp>::value;
75 inline namespace _V2 {
77 /** Abstract base class for types defining a category of error codes.
79 * An error category defines a context that give meaning to the integer
80 * stored in an `error_code` or `error_condition` object. For example,
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.
89 constexpr error_category() noexcept = default;
91 virtual ~error_category();
93 error_category(const error_category&) = delete;
94 error_category& operator=(const error_category&) = delete;
97 name() const noexcept = 0;
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
105 _GLIBCXX_DEFAULT_ABI_TAG
107 _M_message(int) const;
110 _GLIBCXX_DEFAULT_ABI_TAG
112 message(int) const = 0;
115 message(int) const = 0;
119 _M_message(int) const;
123 virtual error_condition
124 default_error_condition(int __i) const noexcept;
127 equivalent(int __i, const error_condition& __cond) const noexcept;
130 equivalent(const error_code& __code, int __i) const noexcept;
133 operator<(const error_category& __other) const noexcept
134 { return less<const error_category*>()(this, &__other); }
137 operator==(const error_category& __other) const noexcept
138 { return this == &__other; }
141 operator!=(const error_category& __other) const noexcept
142 { return this != &__other; }
147 /// Error category for `errno` error codes.
148 _GLIBCXX_CONST const error_category& generic_category() noexcept;
150 /// Error category for other error codes defined by the OS.
151 _GLIBCXX_CONST const error_category& system_category() noexcept;
153 } // end inline namespace
155 error_code make_error_code(errc) noexcept;
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
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.
173 error_code() noexcept
174 : _M_value(0), _M_cat(&system_category()) { }
176 error_code(int __v, const error_category& __cat) noexcept
177 : _M_value(__v), _M_cat(&__cat) { }
179 template<typename _ErrorCodeEnum, typename = typename
180 enable_if<is_error_code_enum<_ErrorCodeEnum>::value>::type>
181 error_code(_ErrorCodeEnum __e) noexcept
182 { *this = make_error_code(__e); }
185 assign(int __v, const error_category& __cat) noexcept
193 { assign(0, system_category()); }
196 template<typename _ErrorCodeEnum>
197 typename enable_if<is_error_code_enum<_ErrorCodeEnum>::value,
199 operator=(_ErrorCodeEnum __e) noexcept
200 { return *this = make_error_code(__e); }
203 value() const noexcept { return _M_value; }
205 const error_category&
206 category() const noexcept { return *_M_cat; }
209 default_error_condition() const noexcept;
211 _GLIBCXX_DEFAULT_ABI_TAG
214 { return category().message(value()); }
216 explicit operator bool() const noexcept
217 { return _M_value != 0; }
222 const error_category* _M_cat;
225 // 19.4.2.6 non-member functions
227 /// @relates error_code @{
230 make_error_code(errc __e) noexcept
231 { return error_code(static_cast<int>(__e), generic_category()); }
234 operator<(const error_code& __lhs, const error_code& __rhs) noexcept
236 return (__lhs.category() < __rhs.category()
237 || (__lhs.category() == __rhs.category()
238 && __lhs.value() < __rhs.value()));
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()); }
248 error_condition make_error_condition(errc) noexcept;
250 /** Class error_condition
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`.
256 * An `error_condition` represents something that the program can test for,
257 * and subsequently take appropriate action.
259 struct error_condition
261 error_condition() noexcept
262 : _M_value(0), _M_cat(&generic_category()) { }
264 error_condition(int __v, const error_category& __cat) noexcept
265 : _M_value(__v), _M_cat(&__cat) { }
267 template<typename _ErrorConditionEnum, typename = typename
268 enable_if<is_error_condition_enum<_ErrorConditionEnum>::value>::type>
269 error_condition(_ErrorConditionEnum __e) noexcept
270 { *this = make_error_condition(__e); }
273 assign(int __v, const error_category& __cat) noexcept
280 template<typename _ErrorConditionEnum>
281 typename enable_if<is_error_condition_enum
282 <_ErrorConditionEnum>::value, error_condition&>::type
283 operator=(_ErrorConditionEnum __e) noexcept
284 { return *this = make_error_condition(__e); }
288 { assign(0, generic_category()); }
290 // 19.4.3.4 observers
292 value() const noexcept { return _M_value; }
294 const error_category&
295 category() const noexcept { return *_M_cat; }
297 _GLIBCXX_DEFAULT_ABI_TAG
300 { return category().message(value()); }
302 explicit operator bool() const noexcept
303 { return _M_value != 0; }
308 const error_category* _M_cat;
311 // 19.4.3.6 non-member functions
313 /// Create an `error_condition` representing a standard `errc` condition.
314 /// @relates error_condition
315 inline error_condition
316 make_error_condition(errc __e) noexcept
317 { return error_condition(static_cast<int>(__e), generic_category()); }
319 /// Define an ordering for error_condition objects.
320 /// @relates error_condition
322 operator<(const error_condition& __lhs,
323 const error_condition& __rhs) noexcept
325 return (__lhs.category() < __rhs.category()
326 || (__lhs.category() == __rhs.category()
327 && __lhs.value() < __rhs.value()));
330 // 19.4.4 Comparison operators
332 /// @relates error_code
334 operator==(const error_code& __lhs, const error_code& __rhs) noexcept
335 { return (__lhs.category() == __rhs.category()
336 && __lhs.value() == __rhs.value()); }
338 /// @relates error_code
339 /// @relates error_condition
341 operator==(const error_code& __lhs, const error_condition& __rhs) noexcept
343 return (__lhs.category().equivalent(__lhs.value(), __rhs)
344 || __rhs.category().equivalent(__lhs, __rhs.value()));
347 /// @relates error_code
348 /// @relates error_condition
350 operator==(const error_condition& __lhs, const error_code& __rhs) noexcept
352 return (__rhs.category().equivalent(__rhs.value(), __lhs)
353 || __lhs.category().equivalent(__rhs, __lhs.value()));
356 /// @relates error_condition
358 operator==(const error_condition& __lhs,
359 const error_condition& __rhs) noexcept
361 return (__lhs.category() == __rhs.category()
362 && __lhs.value() == __rhs.value());
365 /// @relates error_code
367 operator!=(const error_code& __lhs, const error_code& __rhs) noexcept
368 { return !(__lhs == __rhs); }
370 /// @relates error_code
371 /// @relates error_condition
373 operator!=(const error_code& __lhs, const error_condition& __rhs) noexcept
374 { return !(__lhs == __rhs); }
376 /// @relates error_code
377 /// @relates error_condition
379 operator!=(const error_condition& __lhs, const error_code& __rhs) noexcept
380 { return !(__lhs == __rhs); }
382 /// @relates error_condition
384 operator!=(const error_condition& __lhs,
385 const error_condition& __rhs) noexcept
386 { return !(__lhs == __rhs); }
390 * @brief An exception type that includes an `error_code` value.
392 * Typically used to report errors from the operating system and other
395 * @ingroup exceptions
397 class system_error : public std::runtime_error
403 system_error(error_code __ec = error_code())
404 : runtime_error(__ec.message()), _M_code(__ec) { }
406 system_error(error_code __ec, const string& __what)
407 : runtime_error(__what + ": " + __ec.message()), _M_code(__ec) { }
409 system_error(error_code __ec, const char* __what)
410 : runtime_error(__what + (": " + __ec.message())), _M_code(__ec) { }
412 system_error(int __v, const error_category& __ecat, const char* __what)
413 : system_error(error_code(__v, __ecat), __what) { }
415 system_error(int __v, const error_category& __ecat)
416 : runtime_error(error_code(__v, __ecat).message()),
417 _M_code(__v, __ecat) { }
419 system_error(int __v, const error_category& __ecat, const string& __what)
420 : runtime_error(__what + ": " + error_code(__v, __ecat).message()),
421 _M_code(__v, __ecat) { }
423 #if __cplusplus >= 201103L
424 system_error (const system_error &) = default;
425 system_error &operator= (const system_error &) = default;
428 virtual ~system_error() noexcept;
431 code() const noexcept { return _M_code; }
434 _GLIBCXX_END_NAMESPACE_VERSION
437 #include <bits/functional_hash.h>
439 namespace std _GLIBCXX_VISIBILITY(default)
441 _GLIBCXX_BEGIN_NAMESPACE_VERSION
443 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
445 /// std::hash specialization for error_code.
446 /// @relates error_code
448 struct hash<error_code>
449 : public __hash_base<size_t, error_code>
452 operator()(const error_code& __e) const noexcept
454 const size_t __tmp = std::_Hash_impl::hash(__e.value());
455 return std::_Hash_impl::__hash_combine(&__e.category(), __tmp);
458 #endif // _GLIBCXX_COMPATIBILITY_CXX0X
460 #if __cplusplus >= 201703L
462 /// std::hash specialization for error_condition.
463 /// @relates error_condition
465 struct hash<error_condition>
466 : public __hash_base<size_t, error_condition>
469 operator()(const error_condition& __e) const noexcept
471 const size_t __tmp = std::_Hash_impl::hash(__e.value());
472 return std::_Hash_impl::__hash_combine(&__e.category(), __tmp);
477 _GLIBCXX_END_NAMESPACE_VERSION
482 #endif // _GLIBCXX_SYSTEM_ERROR