xref: /freebsd/contrib/llvm-project/libcxx/include/system_error (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
10b57cec5SDimitry Andric// -*- C++ -*-
2349cc55cSDimitry Andric//===----------------------------------------------------------------------===//
30b57cec5SDimitry Andric//
40b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
50b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information.
60b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
70b57cec5SDimitry Andric//
80b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
90b57cec5SDimitry Andric
100b57cec5SDimitry Andric#ifndef _LIBCPP_SYSTEM_ERROR
110b57cec5SDimitry Andric#define _LIBCPP_SYSTEM_ERROR
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric/*
140b57cec5SDimitry Andric    system_error synopsis
150b57cec5SDimitry Andric
160b57cec5SDimitry Andricnamespace std
170b57cec5SDimitry Andric{
180b57cec5SDimitry Andric
190b57cec5SDimitry Andricclass error_category
200b57cec5SDimitry Andric{
210b57cec5SDimitry Andricpublic:
220b57cec5SDimitry Andric    virtual ~error_category() noexcept;
230b57cec5SDimitry Andric
240b57cec5SDimitry Andric    constexpr error_category();
250b57cec5SDimitry Andric    error_category(const error_category&) = delete;
260b57cec5SDimitry Andric    error_category& operator=(const error_category&) = delete;
270b57cec5SDimitry Andric
280b57cec5SDimitry Andric    virtual const char* name() const noexcept = 0;
290b57cec5SDimitry Andric    virtual error_condition default_error_condition(int ev) const noexcept;
300b57cec5SDimitry Andric    virtual bool equivalent(int code, const error_condition& condition) const noexcept;
310b57cec5SDimitry Andric    virtual bool equivalent(const error_code& code, int condition) const noexcept;
320b57cec5SDimitry Andric    virtual string message(int ev) const = 0;
330b57cec5SDimitry Andric
340b57cec5SDimitry Andric    bool operator==(const error_category& rhs) const noexcept;
35*bdd1243dSDimitry Andric    bool operator!=(const error_category& rhs) const noexcept;              // removed in C++20
36*bdd1243dSDimitry Andric    bool operator<(const error_category& rhs) const noexcept;               // removed in C++20
37*bdd1243dSDimitry Andric    strong_ordering operator<=>(const error_category& rhs) const noexcept;  // C++20
380b57cec5SDimitry Andric};
390b57cec5SDimitry Andric
400b57cec5SDimitry Andricconst error_category& generic_category() noexcept;
410b57cec5SDimitry Andricconst error_category& system_category() noexcept;
420b57cec5SDimitry Andric
430b57cec5SDimitry Andrictemplate <class T> struct is_error_code_enum
440b57cec5SDimitry Andric    : public false_type {};
450b57cec5SDimitry Andric
460b57cec5SDimitry Andrictemplate <class T> struct is_error_condition_enum
470b57cec5SDimitry Andric    : public false_type {};
480b57cec5SDimitry Andric
490b57cec5SDimitry Andrictemplate <class _Tp>
50349cc55cSDimitry Andricinline constexpr bool is_error_condition_enum_v = is_error_condition_enum<_Tp>::value; // C++17
510b57cec5SDimitry Andric
520b57cec5SDimitry Andrictemplate <class _Tp>
53349cc55cSDimitry Andricinline constexpr bool is_error_code_enum_v = is_error_code_enum<_Tp>::value; // C++17
540b57cec5SDimitry Andric
550b57cec5SDimitry Andricclass error_code
560b57cec5SDimitry Andric{
570b57cec5SDimitry Andricpublic:
580b57cec5SDimitry Andric    // constructors:
590b57cec5SDimitry Andric    error_code() noexcept;
600b57cec5SDimitry Andric    error_code(int val, const error_category& cat) noexcept;
610b57cec5SDimitry Andric    template <class ErrorCodeEnum>
620b57cec5SDimitry Andric        error_code(ErrorCodeEnum e) noexcept;
630b57cec5SDimitry Andric
640b57cec5SDimitry Andric    // modifiers:
650b57cec5SDimitry Andric    void assign(int val, const error_category& cat) noexcept;
660b57cec5SDimitry Andric    template <class ErrorCodeEnum>
670b57cec5SDimitry Andric        error_code& operator=(ErrorCodeEnum e) noexcept;
680b57cec5SDimitry Andric    void clear() noexcept;
690b57cec5SDimitry Andric
700b57cec5SDimitry Andric    // observers:
710b57cec5SDimitry Andric    int value() const noexcept;
720b57cec5SDimitry Andric    const error_category& category() const noexcept;
730b57cec5SDimitry Andric    error_condition default_error_condition() const noexcept;
740b57cec5SDimitry Andric    string message() const;
750b57cec5SDimitry Andric    explicit operator bool() const noexcept;
760b57cec5SDimitry Andric};
770b57cec5SDimitry Andric
780b57cec5SDimitry Andric// non-member functions:
790b57cec5SDimitry Andrictemplate <class charT, class traits>
800b57cec5SDimitry Andric    basic_ostream<charT,traits>&
810b57cec5SDimitry Andric    operator<<(basic_ostream<charT,traits>& os, const error_code& ec);
820b57cec5SDimitry Andric
830b57cec5SDimitry Andricclass error_condition
840b57cec5SDimitry Andric{
850b57cec5SDimitry Andricpublic:
860b57cec5SDimitry Andric    // constructors:
870b57cec5SDimitry Andric    error_condition() noexcept;
880b57cec5SDimitry Andric    error_condition(int val, const error_category& cat) noexcept;
890b57cec5SDimitry Andric    template <class ErrorConditionEnum>
900b57cec5SDimitry Andric        error_condition(ErrorConditionEnum e) noexcept;
910b57cec5SDimitry Andric
920b57cec5SDimitry Andric    // modifiers:
930b57cec5SDimitry Andric    void assign(int val, const error_category& cat) noexcept;
940b57cec5SDimitry Andric    template <class ErrorConditionEnum>
950b57cec5SDimitry Andric        error_condition& operator=(ErrorConditionEnum e) noexcept;
960b57cec5SDimitry Andric    void clear() noexcept;
970b57cec5SDimitry Andric
980b57cec5SDimitry Andric    // observers:
990b57cec5SDimitry Andric    int value() const noexcept;
1000b57cec5SDimitry Andric    const error_category& category() const noexcept;
1010b57cec5SDimitry Andric    string message() const noexcept;
1020b57cec5SDimitry Andric    explicit operator bool() const noexcept;
1030b57cec5SDimitry Andric};
1040b57cec5SDimitry Andric
1050b57cec5SDimitry Andricclass system_error
1060b57cec5SDimitry Andric    : public runtime_error
1070b57cec5SDimitry Andric{
1080b57cec5SDimitry Andricpublic:
1090b57cec5SDimitry Andric    system_error(error_code ec, const string& what_arg);
1100b57cec5SDimitry Andric    system_error(error_code ec, const char* what_arg);
1110b57cec5SDimitry Andric    system_error(error_code ec);
1120b57cec5SDimitry Andric    system_error(int ev, const error_category& ecat, const string& what_arg);
1130b57cec5SDimitry Andric    system_error(int ev, const error_category& ecat, const char* what_arg);
1140b57cec5SDimitry Andric    system_error(int ev, const error_category& ecat);
1150b57cec5SDimitry Andric
1160b57cec5SDimitry Andric    const error_code& code() const noexcept;
1170b57cec5SDimitry Andric    const char* what() const noexcept;
1180b57cec5SDimitry Andric};
1190b57cec5SDimitry Andric
1200b57cec5SDimitry Andrictemplate <> struct is_error_condition_enum<errc>
1210b57cec5SDimitry Andric    : true_type { }
1220b57cec5SDimitry Andric
1230b57cec5SDimitry Andricerror_code make_error_code(errc e) noexcept;
1240b57cec5SDimitry Andricerror_condition make_error_condition(errc e) noexcept;
1250b57cec5SDimitry Andric
1260b57cec5SDimitry Andric// Comparison operators:
1270b57cec5SDimitry Andricbool operator==(const error_code& lhs, const error_code& rhs) noexcept;
1280b57cec5SDimitry Andricbool operator==(const error_code& lhs, const error_condition& rhs) noexcept;
129*bdd1243dSDimitry Andricbool operator==(const error_condition& lhs, const error_code& rhs) noexcept;                  // removed in C++20
1300b57cec5SDimitry Andricbool operator==(const error_condition& lhs, const error_condition& rhs) noexcept;
131*bdd1243dSDimitry Andricbool operator!=(const error_code& lhs, const error_code& rhs) noexcept;                       // removed in C++20
132*bdd1243dSDimitry Andricbool operator!=(const error_code& lhs, const error_condition& rhs) noexcept;                  // removed in C++20
133*bdd1243dSDimitry Andricbool operator!=(const error_condition& lhs, const error_code& rhs) noexcept;                  // removed in C++20
134*bdd1243dSDimitry Andricbool operator!=(const error_condition& lhs, const error_condition& rhs) noexcept;             // removed in C++20
135*bdd1243dSDimitry Andricbool operator<(const error_condition& lhs, const error_condition& rhs) noexcept;              // removed in C++20
136*bdd1243dSDimitry Andricbool operator<(const error_code& lhs, const error_code& rhs) noexcept;                        // removed in C++20
137*bdd1243dSDimitry Andricstrong_ordering operator<=>(const error_code& lhs, const error_code& rhs) noexcept;           // C++20
138*bdd1243dSDimitry Andricstrong_ordering operator<=>(const error_condition& lhs, const error_condition& rhs) noexcept; // C++20
1390b57cec5SDimitry Andric
1400b57cec5SDimitry Andrictemplate <> struct hash<std::error_code>;
1410b57cec5SDimitry Andrictemplate <> struct hash<std::error_condition>;
1420b57cec5SDimitry Andric
1430b57cec5SDimitry Andric}  // std
1440b57cec5SDimitry Andric
1450b57cec5SDimitry Andric*/
1460b57cec5SDimitry Andric
14781ad6265SDimitry Andric#include <__assert> // all public C++ headers provide the assertion handler
148fe6060f1SDimitry Andric#include <__config>
1490b57cec5SDimitry Andric#include <__errc>
15081ad6265SDimitry Andric#include <__functional/hash.h>
151fe6060f1SDimitry Andric#include <__functional/unary_function.h>
152*bdd1243dSDimitry Andric#include <__memory/addressof.h>
153fe6060f1SDimitry Andric#include <stdexcept>
1540b57cec5SDimitry Andric#include <string>
155fe6060f1SDimitry Andric#include <type_traits>
15604eeddc0SDimitry Andric#include <version>
1570b57cec5SDimitry Andric
15881ad6265SDimitry Andric// standard-mandated includes
159*bdd1243dSDimitry Andric
160*bdd1243dSDimitry Andric// [system.error.syn]
16181ad6265SDimitry Andric#include <compare>
16281ad6265SDimitry Andric
1630b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1640b57cec5SDimitry Andric#  pragma GCC system_header
1650b57cec5SDimitry Andric#endif
1660b57cec5SDimitry Andric
1670b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
1680b57cec5SDimitry Andric
1690b57cec5SDimitry Andric// is_error_code_enum
1700b57cec5SDimitry Andric
1710b57cec5SDimitry Andrictemplate <class _Tp>
1720b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS is_error_code_enum
1730b57cec5SDimitry Andric    : public false_type {};
1740b57cec5SDimitry Andric
1750b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14
1760b57cec5SDimitry Andrictemplate <class _Tp>
177349cc55cSDimitry Andricinline constexpr bool is_error_code_enum_v = is_error_code_enum<_Tp>::value;
1780b57cec5SDimitry Andric#endif
1790b57cec5SDimitry Andric
1800b57cec5SDimitry Andric// is_error_condition_enum
1810b57cec5SDimitry Andric
1820b57cec5SDimitry Andrictemplate <class _Tp>
1830b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS is_error_condition_enum
1840b57cec5SDimitry Andric    : public false_type {};
1850b57cec5SDimitry Andric
1860b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14
1870b57cec5SDimitry Andrictemplate <class _Tp>
188349cc55cSDimitry Andricinline constexpr bool is_error_condition_enum_v = is_error_condition_enum<_Tp>::value;
1890b57cec5SDimitry Andric#endif
1900b57cec5SDimitry Andric
1910b57cec5SDimitry Andrictemplate <>
1920b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS is_error_condition_enum<errc>
1930b57cec5SDimitry Andric    : true_type { };
1940b57cec5SDimitry Andric
19581ad6265SDimitry Andric#ifdef _LIBCPP_CXX03_LANG
1960b57cec5SDimitry Andrictemplate <>
1970b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS is_error_condition_enum<errc::__lx>
1980b57cec5SDimitry Andric    : true_type { };
1990b57cec5SDimitry Andric#endif
2000b57cec5SDimitry Andric
2010b57cec5SDimitry Andricclass _LIBCPP_TYPE_VIS error_condition;
2020b57cec5SDimitry Andricclass _LIBCPP_TYPE_VIS error_code;
2030b57cec5SDimitry Andric
2040b57cec5SDimitry Andric// class error_category
2050b57cec5SDimitry Andric
2060b57cec5SDimitry Andricclass _LIBCPP_HIDDEN __do_message;
2070b57cec5SDimitry Andric
2080b57cec5SDimitry Andricclass _LIBCPP_TYPE_VIS error_category
2090b57cec5SDimitry Andric{
2100b57cec5SDimitry Andricpublic:
2110b57cec5SDimitry Andric    virtual ~error_category() _NOEXCEPT;
2120b57cec5SDimitry Andric
21381ad6265SDimitry Andric#if defined(_LIBCPP_ERROR_CATEGORY_DEFINE_LEGACY_INLINE_FUNCTIONS)
21481ad6265SDimitry Andric    error_category() noexcept;
2150b57cec5SDimitry Andric#else
2160b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
217*bdd1243dSDimitry Andric    _LIBCPP_CONSTEXPR_SINCE_CXX14 error_category() _NOEXCEPT = default;
2180b57cec5SDimitry Andric#endif
2190eae32dcSDimitry Andric    error_category(const error_category&) = delete;
2200eae32dcSDimitry Andric    error_category& operator=(const error_category&) = delete;
2210b57cec5SDimitry Andric
2220b57cec5SDimitry Andric    virtual const char* name() const _NOEXCEPT = 0;
2230b57cec5SDimitry Andric    virtual error_condition default_error_condition(int __ev) const _NOEXCEPT;
2240b57cec5SDimitry Andric    virtual bool equivalent(int __code, const error_condition& __condition) const _NOEXCEPT;
2250b57cec5SDimitry Andric    virtual bool equivalent(const error_code& __code, int __condition) const _NOEXCEPT;
2260b57cec5SDimitry Andric    virtual string message(int __ev) const = 0;
2270b57cec5SDimitry Andric
2280b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
2290b57cec5SDimitry Andric    bool operator==(const error_category& __rhs) const _NOEXCEPT {return this == &__rhs;}
2300b57cec5SDimitry Andric
231*bdd1243dSDimitry Andric#if _LIBCPP_STD_VER > 17
232*bdd1243dSDimitry Andric
233*bdd1243dSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
234*bdd1243dSDimitry Andric    strong_ordering operator<=>(const error_category& __rhs) const noexcept {return compare_three_way()(this, std::addressof(__rhs));}
235*bdd1243dSDimitry Andric
236*bdd1243dSDimitry Andric#else // _LIBCPP_STD_VER > 17
237*bdd1243dSDimitry Andric
2380b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
2390b57cec5SDimitry Andric    bool operator!=(const error_category& __rhs) const _NOEXCEPT {return !(*this == __rhs);}
2400b57cec5SDimitry Andric
2410b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
2420b57cec5SDimitry Andric    bool operator< (const error_category& __rhs) const _NOEXCEPT {return this < &__rhs;}
2430b57cec5SDimitry Andric
244*bdd1243dSDimitry Andric#endif // _LIBCPP_STD_VER > 17
245*bdd1243dSDimitry Andric
2460b57cec5SDimitry Andric    friend class _LIBCPP_HIDDEN __do_message;
2470b57cec5SDimitry Andric};
2480b57cec5SDimitry Andric
2490b57cec5SDimitry Andricclass _LIBCPP_HIDDEN __do_message
2500b57cec5SDimitry Andric    : public error_category
2510b57cec5SDimitry Andric{
2520b57cec5SDimitry Andricpublic:
253*bdd1243dSDimitry Andric    string message(int __ev) const override;
2540b57cec5SDimitry Andric};
2550b57cec5SDimitry Andric
2560b57cec5SDimitry Andric_LIBCPP_FUNC_VIS const error_category& generic_category() _NOEXCEPT;
2570b57cec5SDimitry Andric_LIBCPP_FUNC_VIS const error_category& system_category() _NOEXCEPT;
2580b57cec5SDimitry Andric
259*bdd1243dSDimitry Andricnamespace __adl_only {
260*bdd1243dSDimitry Andric    // Those cause ADL to trigger but they are not viable candidates,
261*bdd1243dSDimitry Andric    // so they are never actually selected.
262*bdd1243dSDimitry Andric    void make_error_condition() = delete;
263*bdd1243dSDimitry Andric    void make_error_code() = delete;
264*bdd1243dSDimitry Andric} // namespace __adl_only
265*bdd1243dSDimitry Andric
2660b57cec5SDimitry Andricclass _LIBCPP_TYPE_VIS error_condition
2670b57cec5SDimitry Andric{
2680b57cec5SDimitry Andric    int __val_;
2690b57cec5SDimitry Andric    const error_category* __cat_;
2700b57cec5SDimitry Andricpublic:
2710b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
2720b57cec5SDimitry Andric    error_condition() _NOEXCEPT : __val_(0), __cat_(&generic_category()) {}
2730b57cec5SDimitry Andric
2740b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
2750b57cec5SDimitry Andric    error_condition(int __val, const error_category& __cat) _NOEXCEPT
2760b57cec5SDimitry Andric        : __val_(__val), __cat_(&__cat) {}
2770b57cec5SDimitry Andric
2780b57cec5SDimitry Andric    template <class _Ep>
2790b57cec5SDimitry Andric        _LIBCPP_INLINE_VISIBILITY
2800b57cec5SDimitry Andric        error_condition(_Ep __e,
281e8d8bef9SDimitry Andric              typename enable_if<is_error_condition_enum<_Ep>::value>::type* = nullptr
2820b57cec5SDimitry Andric                                                                     ) _NOEXCEPT
283*bdd1243dSDimitry Andric            {
284*bdd1243dSDimitry Andric                using __adl_only::make_error_condition;
285*bdd1243dSDimitry Andric                *this = make_error_condition(__e);
286*bdd1243dSDimitry Andric            }
2870b57cec5SDimitry Andric
2880b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
2890b57cec5SDimitry Andric    void assign(int __val, const error_category& __cat) _NOEXCEPT
2900b57cec5SDimitry Andric    {
2910b57cec5SDimitry Andric        __val_ = __val;
2920b57cec5SDimitry Andric        __cat_ = &__cat;
2930b57cec5SDimitry Andric    }
2940b57cec5SDimitry Andric
2950b57cec5SDimitry Andric    template <class _Ep>
2960b57cec5SDimitry Andric        _LIBCPP_INLINE_VISIBILITY
2970b57cec5SDimitry Andric        typename enable_if
2980b57cec5SDimitry Andric        <
2990b57cec5SDimitry Andric            is_error_condition_enum<_Ep>::value,
3000b57cec5SDimitry Andric            error_condition&
3010b57cec5SDimitry Andric        >::type
3020b57cec5SDimitry Andric        operator=(_Ep __e) _NOEXCEPT
303*bdd1243dSDimitry Andric            {
304*bdd1243dSDimitry Andric                using __adl_only::make_error_condition;
305*bdd1243dSDimitry Andric                *this = make_error_condition(__e);
306*bdd1243dSDimitry Andric                return *this;
307*bdd1243dSDimitry Andric            }
3080b57cec5SDimitry Andric
3090b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
3100b57cec5SDimitry Andric    void clear() _NOEXCEPT
3110b57cec5SDimitry Andric    {
3120b57cec5SDimitry Andric        __val_ = 0;
3130b57cec5SDimitry Andric        __cat_ = &generic_category();
3140b57cec5SDimitry Andric    }
3150b57cec5SDimitry Andric
3160b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
3170b57cec5SDimitry Andric    int value() const _NOEXCEPT {return __val_;}
3180b57cec5SDimitry Andric
3190b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
3200b57cec5SDimitry Andric    const error_category& category() const _NOEXCEPT {return *__cat_;}
3210b57cec5SDimitry Andric    string message() const;
3220b57cec5SDimitry Andric
3230b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
324fe6060f1SDimitry Andric    explicit operator bool() const _NOEXCEPT {return __val_ != 0;}
3250b57cec5SDimitry Andric};
3260b57cec5SDimitry Andric
3270b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
3280b57cec5SDimitry Andricerror_condition
3290b57cec5SDimitry Andricmake_error_condition(errc __e) _NOEXCEPT
3300b57cec5SDimitry Andric{
3310b57cec5SDimitry Andric    return error_condition(static_cast<int>(__e), generic_category());
3320b57cec5SDimitry Andric}
3330b57cec5SDimitry Andric
3340b57cec5SDimitry Andric// error_code
3350b57cec5SDimitry Andric
3360b57cec5SDimitry Andricclass _LIBCPP_TYPE_VIS error_code
3370b57cec5SDimitry Andric{
3380b57cec5SDimitry Andric    int __val_;
3390b57cec5SDimitry Andric    const error_category* __cat_;
3400b57cec5SDimitry Andricpublic:
3410b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
3420b57cec5SDimitry Andric    error_code() _NOEXCEPT : __val_(0), __cat_(&system_category()) {}
3430b57cec5SDimitry Andric
3440b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
3450b57cec5SDimitry Andric    error_code(int __val, const error_category& __cat) _NOEXCEPT
3460b57cec5SDimitry Andric        : __val_(__val), __cat_(&__cat) {}
3470b57cec5SDimitry Andric
3480b57cec5SDimitry Andric    template <class _Ep>
3490b57cec5SDimitry Andric        _LIBCPP_INLINE_VISIBILITY
3500b57cec5SDimitry Andric        error_code(_Ep __e,
351e8d8bef9SDimitry Andric                   typename enable_if<is_error_code_enum<_Ep>::value>::type* = nullptr
3520b57cec5SDimitry Andric                                                                     ) _NOEXCEPT
353*bdd1243dSDimitry Andric            {
354*bdd1243dSDimitry Andric                using __adl_only::make_error_code;
355*bdd1243dSDimitry Andric                *this = make_error_code(__e);
356*bdd1243dSDimitry Andric            }
3570b57cec5SDimitry Andric
3580b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
3590b57cec5SDimitry Andric    void assign(int __val, const error_category& __cat) _NOEXCEPT
3600b57cec5SDimitry Andric    {
3610b57cec5SDimitry Andric        __val_ = __val;
3620b57cec5SDimitry Andric        __cat_ = &__cat;
3630b57cec5SDimitry Andric    }
3640b57cec5SDimitry Andric
3650b57cec5SDimitry Andric    template <class _Ep>
3660b57cec5SDimitry Andric        _LIBCPP_INLINE_VISIBILITY
3670b57cec5SDimitry Andric        typename enable_if
3680b57cec5SDimitry Andric        <
3690b57cec5SDimitry Andric            is_error_code_enum<_Ep>::value,
3700b57cec5SDimitry Andric            error_code&
3710b57cec5SDimitry Andric        >::type
3720b57cec5SDimitry Andric        operator=(_Ep __e) _NOEXCEPT
373*bdd1243dSDimitry Andric            {
374*bdd1243dSDimitry Andric                using __adl_only::make_error_code;
375*bdd1243dSDimitry Andric                *this = make_error_code(__e);
376*bdd1243dSDimitry Andric                return *this;
377*bdd1243dSDimitry Andric            }
3780b57cec5SDimitry Andric
3790b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
3800b57cec5SDimitry Andric    void clear() _NOEXCEPT
3810b57cec5SDimitry Andric    {
3820b57cec5SDimitry Andric        __val_ = 0;
3830b57cec5SDimitry Andric        __cat_ = &system_category();
3840b57cec5SDimitry Andric    }
3850b57cec5SDimitry Andric
3860b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
3870b57cec5SDimitry Andric    int value() const _NOEXCEPT {return __val_;}
3880b57cec5SDimitry Andric
3890b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
3900b57cec5SDimitry Andric    const error_category& category() const _NOEXCEPT {return *__cat_;}
3910b57cec5SDimitry Andric
3920b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
3930b57cec5SDimitry Andric    error_condition default_error_condition() const _NOEXCEPT
3940b57cec5SDimitry Andric        {return __cat_->default_error_condition(__val_);}
3950b57cec5SDimitry Andric
3960b57cec5SDimitry Andric    string message() const;
3970b57cec5SDimitry Andric
3980b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
399fe6060f1SDimitry Andric    explicit operator bool() const _NOEXCEPT {return __val_ != 0;}
4000b57cec5SDimitry Andric};
4010b57cec5SDimitry Andric
4020b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
4030b57cec5SDimitry Andricerror_code
4040b57cec5SDimitry Andricmake_error_code(errc __e) _NOEXCEPT
4050b57cec5SDimitry Andric{
4060b57cec5SDimitry Andric    return error_code(static_cast<int>(__e), generic_category());
4070b57cec5SDimitry Andric}
4080b57cec5SDimitry Andric
4090b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
4100b57cec5SDimitry Andricbool
4110b57cec5SDimitry Andricoperator==(const error_code& __x, const error_code& __y) _NOEXCEPT
4120b57cec5SDimitry Andric{
4130b57cec5SDimitry Andric    return __x.category() == __y.category() && __x.value() == __y.value();
4140b57cec5SDimitry Andric}
4150b57cec5SDimitry Andric
4160b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
4170b57cec5SDimitry Andricbool
4180b57cec5SDimitry Andricoperator==(const error_code& __x, const error_condition& __y) _NOEXCEPT
4190b57cec5SDimitry Andric{
4200b57cec5SDimitry Andric    return __x.category().equivalent(__x.value(), __y)
4210b57cec5SDimitry Andric        || __y.category().equivalent(__x, __y.value());
4220b57cec5SDimitry Andric}
4230b57cec5SDimitry Andric
424*bdd1243dSDimitry Andric#if _LIBCPP_STD_VER <= 17
4250b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
4260b57cec5SDimitry Andricbool
4270b57cec5SDimitry Andricoperator==(const error_condition& __x, const error_code& __y) _NOEXCEPT
4280b57cec5SDimitry Andric{
4290b57cec5SDimitry Andric    return __y == __x;
4300b57cec5SDimitry Andric}
431*bdd1243dSDimitry Andric#endif
4320b57cec5SDimitry Andric
4330b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
4340b57cec5SDimitry Andricbool
4350b57cec5SDimitry Andricoperator==(const error_condition& __x, const error_condition& __y) _NOEXCEPT
4360b57cec5SDimitry Andric{
4370b57cec5SDimitry Andric    return __x.category() == __y.category() && __x.value() == __y.value();
4380b57cec5SDimitry Andric}
4390b57cec5SDimitry Andric
440*bdd1243dSDimitry Andric#if _LIBCPP_STD_VER <= 17
441*bdd1243dSDimitry Andric
4420b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
4430b57cec5SDimitry Andricbool
4440b57cec5SDimitry Andricoperator!=(const error_code& __x, const error_code& __y) _NOEXCEPT
4450b57cec5SDimitry Andric{return !(__x == __y);}
4460b57cec5SDimitry Andric
4470b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
4480b57cec5SDimitry Andricbool
4490b57cec5SDimitry Andricoperator!=(const error_code& __x, const error_condition& __y) _NOEXCEPT
4500b57cec5SDimitry Andric{return !(__x == __y);}
4510b57cec5SDimitry Andric
4520b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
4530b57cec5SDimitry Andricbool
4540b57cec5SDimitry Andricoperator!=(const error_condition& __x, const error_code& __y) _NOEXCEPT
4550b57cec5SDimitry Andric{return !(__x == __y);}
4560b57cec5SDimitry Andric
4570b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
4580b57cec5SDimitry Andricbool
4590b57cec5SDimitry Andricoperator!=(const error_condition& __x, const error_condition& __y) _NOEXCEPT
4600b57cec5SDimitry Andric{return !(__x == __y);}
4610b57cec5SDimitry Andric
462*bdd1243dSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
463*bdd1243dSDimitry Andricbool
464*bdd1243dSDimitry Andricoperator<(const error_condition& __x, const error_condition& __y) _NOEXCEPT
465*bdd1243dSDimitry Andric{
466*bdd1243dSDimitry Andric    return __x.category() < __y.category()
467*bdd1243dSDimitry Andric        || (__x.category() == __y.category() && __x.value() < __y.value());
468*bdd1243dSDimitry Andric}
469*bdd1243dSDimitry Andric
470*bdd1243dSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
471*bdd1243dSDimitry Andricbool
472*bdd1243dSDimitry Andricoperator<(const error_code& __x, const error_code& __y) _NOEXCEPT
473*bdd1243dSDimitry Andric{
474*bdd1243dSDimitry Andric    return __x.category() < __y.category()
475*bdd1243dSDimitry Andric        || (__x.category() == __y.category() && __x.value() < __y.value());
476*bdd1243dSDimitry Andric}
477*bdd1243dSDimitry Andric
478*bdd1243dSDimitry Andric#else // _LIBCPP_STD_VER <= 17
479*bdd1243dSDimitry Andric
480*bdd1243dSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI strong_ordering
481*bdd1243dSDimitry Andricoperator<=>(const error_code& __x, const error_code& __y) noexcept
482*bdd1243dSDimitry Andric{
483*bdd1243dSDimitry Andric    if (auto __c = __x.category() <=> __y.category(); __c != 0)
484*bdd1243dSDimitry Andric        return __c;
485*bdd1243dSDimitry Andric    return __x.value() <=> __y.value();
486*bdd1243dSDimitry Andric}
487*bdd1243dSDimitry Andric
488*bdd1243dSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI strong_ordering
489*bdd1243dSDimitry Andricoperator<=>(const error_condition& __x, const error_condition& __y) noexcept
490*bdd1243dSDimitry Andric{
491*bdd1243dSDimitry Andric    if (auto __c = __x.category() <=> __y.category(); __c != 0)
492*bdd1243dSDimitry Andric       return __c;
493*bdd1243dSDimitry Andric    return __x.value() <=> __y.value();
494*bdd1243dSDimitry Andric}
495*bdd1243dSDimitry Andric
496*bdd1243dSDimitry Andric#endif // _LIBCPP_STD_VER <= 17
497*bdd1243dSDimitry Andric
4980b57cec5SDimitry Andrictemplate <>
4990b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash<error_code>
50081ad6265SDimitry Andric    : public __unary_function<error_code, size_t>
5010b57cec5SDimitry Andric{
5020b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
5030b57cec5SDimitry Andric    size_t operator()(const error_code& __ec) const _NOEXCEPT
5040b57cec5SDimitry Andric    {
5050b57cec5SDimitry Andric        return static_cast<size_t>(__ec.value());
5060b57cec5SDimitry Andric    }
5070b57cec5SDimitry Andric};
5080b57cec5SDimitry Andric
5090b57cec5SDimitry Andrictemplate <>
5100b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash<error_condition>
51181ad6265SDimitry Andric    : public __unary_function<error_condition, size_t>
5120b57cec5SDimitry Andric{
5130b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
5140b57cec5SDimitry Andric    size_t operator()(const error_condition& __ec) const _NOEXCEPT
5150b57cec5SDimitry Andric    {
5160b57cec5SDimitry Andric        return static_cast<size_t>(__ec.value());
5170b57cec5SDimitry Andric    }
5180b57cec5SDimitry Andric};
5190b57cec5SDimitry Andric
5200b57cec5SDimitry Andric// system_error
5210b57cec5SDimitry Andric
5220b57cec5SDimitry Andricclass _LIBCPP_TYPE_VIS system_error
5230b57cec5SDimitry Andric    : public runtime_error
5240b57cec5SDimitry Andric{
5250b57cec5SDimitry Andric    error_code __ec_;
5260b57cec5SDimitry Andricpublic:
5270b57cec5SDimitry Andric    system_error(error_code __ec, const string& __what_arg);
5280b57cec5SDimitry Andric    system_error(error_code __ec, const char* __what_arg);
5290b57cec5SDimitry Andric    system_error(error_code __ec);
5300b57cec5SDimitry Andric    system_error(int __ev, const error_category& __ecat, const string& __what_arg);
5310b57cec5SDimitry Andric    system_error(int __ev, const error_category& __ecat, const char* __what_arg);
5320b57cec5SDimitry Andric    system_error(int __ev, const error_category& __ecat);
5339ec406dcSDimitry Andric    system_error(const system_error&) _NOEXCEPT = default;
534*bdd1243dSDimitry Andric    ~system_error() _NOEXCEPT override;
5350b57cec5SDimitry Andric
5360b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
5370b57cec5SDimitry Andric    const error_code& code() const _NOEXCEPT {return __ec_;}
5380b57cec5SDimitry Andric
5390b57cec5SDimitry Andricprivate:
5400b57cec5SDimitry Andric    static string __init(const error_code&, string);
5410b57cec5SDimitry Andric};
5420b57cec5SDimitry Andric
5430b57cec5SDimitry Andric_LIBCPP_NORETURN _LIBCPP_FUNC_VIS
544753f127fSDimitry Andricvoid __throw_system_error(int __ev, const char* __what_arg);
5450b57cec5SDimitry Andric
5460b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
5470b57cec5SDimitry Andric
5480b57cec5SDimitry Andric#endif // _LIBCPP_SYSTEM_ERROR
549