xref: /freebsd/contrib/llvm-project/libcxx/include/system_error (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_SYSTEM_ERROR
11#define _LIBCPP_SYSTEM_ERROR
12
13/*
14    system_error synopsis
15
16namespace std
17{
18
19class error_category
20{
21public:
22    virtual ~error_category() noexcept;
23
24    constexpr error_category();
25    error_category(const error_category&) = delete;
26    error_category& operator=(const error_category&) = delete;
27
28    virtual const char* name() const noexcept = 0;
29    virtual error_condition default_error_condition(int ev) const noexcept;
30    virtual bool equivalent(int code, const error_condition& condition) const noexcept;
31    virtual bool equivalent(const error_code& code, int condition) const noexcept;
32    virtual string message(int ev) const = 0;
33
34    bool operator==(const error_category& rhs) const noexcept;
35    bool operator!=(const error_category& rhs) const noexcept;              // removed in C++20
36    bool operator<(const error_category& rhs) const noexcept;               // removed in C++20
37    strong_ordering operator<=>(const error_category& rhs) const noexcept;  // C++20
38};
39
40const error_category& generic_category() noexcept;
41const error_category& system_category() noexcept;
42
43template <class T> struct is_error_code_enum
44    : public false_type {};
45
46template <class T> struct is_error_condition_enum
47    : public false_type {};
48
49template <class _Tp>
50inline constexpr bool is_error_condition_enum_v = is_error_condition_enum<_Tp>::value; // C++17
51
52template <class _Tp>
53inline constexpr bool is_error_code_enum_v = is_error_code_enum<_Tp>::value; // C++17
54
55class error_code
56{
57public:
58    // constructors:
59    error_code() noexcept;
60    error_code(int val, const error_category& cat) noexcept;
61    template <class ErrorCodeEnum>
62        error_code(ErrorCodeEnum e) noexcept;
63
64    // modifiers:
65    void assign(int val, const error_category& cat) noexcept;
66    template <class ErrorCodeEnum>
67        error_code& operator=(ErrorCodeEnum e) noexcept;
68    void clear() noexcept;
69
70    // observers:
71    int value() const noexcept;
72    const error_category& category() const noexcept;
73    error_condition default_error_condition() const noexcept;
74    string message() const;
75    explicit operator bool() const noexcept;
76};
77
78// non-member functions:
79template <class charT, class traits>
80    basic_ostream<charT,traits>&
81    operator<<(basic_ostream<charT,traits>& os, const error_code& ec);
82
83class error_condition
84{
85public:
86    // constructors:
87    error_condition() noexcept;
88    error_condition(int val, const error_category& cat) noexcept;
89    template <class ErrorConditionEnum>
90        error_condition(ErrorConditionEnum e) noexcept;
91
92    // modifiers:
93    void assign(int val, const error_category& cat) noexcept;
94    template <class ErrorConditionEnum>
95        error_condition& operator=(ErrorConditionEnum e) noexcept;
96    void clear() noexcept;
97
98    // observers:
99    int value() const noexcept;
100    const error_category& category() const noexcept;
101    string message() const noexcept;
102    explicit operator bool() const noexcept;
103};
104
105class system_error
106    : public runtime_error
107{
108public:
109    system_error(error_code ec, const string& what_arg);
110    system_error(error_code ec, const char* what_arg);
111    system_error(error_code ec);
112    system_error(int ev, const error_category& ecat, const string& what_arg);
113    system_error(int ev, const error_category& ecat, const char* what_arg);
114    system_error(int ev, const error_category& ecat);
115
116    const error_code& code() const noexcept;
117    const char* what() const noexcept;
118};
119
120template <> struct is_error_condition_enum<errc>
121    : true_type { }
122
123error_code make_error_code(errc e) noexcept;
124error_condition make_error_condition(errc e) noexcept;
125
126// Comparison operators:
127bool operator==(const error_code& lhs, const error_code& rhs) noexcept;
128bool operator==(const error_code& lhs, const error_condition& rhs) noexcept;
129bool operator==(const error_condition& lhs, const error_code& rhs) noexcept;                  // removed in C++20
130bool operator==(const error_condition& lhs, const error_condition& rhs) noexcept;
131bool operator!=(const error_code& lhs, const error_code& rhs) noexcept;                       // removed in C++20
132bool operator!=(const error_code& lhs, const error_condition& rhs) noexcept;                  // removed in C++20
133bool operator!=(const error_condition& lhs, const error_code& rhs) noexcept;                  // removed in C++20
134bool operator!=(const error_condition& lhs, const error_condition& rhs) noexcept;             // removed in C++20
135bool operator<(const error_condition& lhs, const error_condition& rhs) noexcept;              // removed in C++20
136bool operator<(const error_code& lhs, const error_code& rhs) noexcept;                        // removed in C++20
137strong_ordering operator<=>(const error_code& lhs, const error_code& rhs) noexcept;           // C++20
138strong_ordering operator<=>(const error_condition& lhs, const error_condition& rhs) noexcept; // C++20
139
140template <> struct hash<std::error_code>;
141template <> struct hash<std::error_condition>;
142
143}  // std
144
145*/
146
147#include <__assert> // all public C++ headers provide the assertion handler
148#include <__config>
149#include <__system_error/errc.h>
150#include <__system_error/error_category.h>
151#include <__system_error/error_code.h>
152#include <__system_error/error_condition.h>
153#include <__system_error/system_error.h>
154#include <version>
155
156// standard-mandated includes
157
158// [system.error.syn]
159#include <compare>
160
161#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
162#  pragma GCC system_header
163#endif
164
165#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
166#  include <cstdint>
167#  include <cstring>
168#  include <limits>
169#  include <type_traits>
170#endif
171
172#endif // _LIBCPP_SYSTEM_ERROR
173