xref: /freebsd/contrib/llvm-project/libcxx/include/system_error (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
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;
36    bool operator<(const error_category& rhs) const noexcept;
37};
38
39const error_category& generic_category() noexcept;
40const error_category& system_category() noexcept;
41
42template <class T> struct is_error_code_enum
43    : public false_type {};
44
45template <class T> struct is_error_condition_enum
46    : public false_type {};
47
48template <class _Tp>
49inline constexpr bool is_error_condition_enum_v = is_error_condition_enum<_Tp>::value; // C++17
50
51template <class _Tp>
52inline constexpr bool is_error_code_enum_v = is_error_code_enum<_Tp>::value; // C++17
53
54class error_code
55{
56public:
57    // constructors:
58    error_code() noexcept;
59    error_code(int val, const error_category& cat) noexcept;
60    template <class ErrorCodeEnum>
61        error_code(ErrorCodeEnum e) noexcept;
62
63    // modifiers:
64    void assign(int val, const error_category& cat) noexcept;
65    template <class ErrorCodeEnum>
66        error_code& operator=(ErrorCodeEnum e) noexcept;
67    void clear() noexcept;
68
69    // observers:
70    int value() const noexcept;
71    const error_category& category() const noexcept;
72    error_condition default_error_condition() const noexcept;
73    string message() const;
74    explicit operator bool() const noexcept;
75};
76
77// non-member functions:
78bool operator<(const error_code& lhs, const error_code& rhs) noexcept;
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
105bool operator<(const error_condition& lhs, const error_condition& rhs) noexcept;
106
107class system_error
108    : public runtime_error
109{
110public:
111    system_error(error_code ec, const string& what_arg);
112    system_error(error_code ec, const char* what_arg);
113    system_error(error_code ec);
114    system_error(int ev, const error_category& ecat, const string& what_arg);
115    system_error(int ev, const error_category& ecat, const char* what_arg);
116    system_error(int ev, const error_category& ecat);
117
118    const error_code& code() const noexcept;
119    const char* what() const noexcept;
120};
121
122template <> struct is_error_condition_enum<errc>
123    : true_type { }
124
125error_code make_error_code(errc e) noexcept;
126error_condition make_error_condition(errc e) noexcept;
127
128// Comparison operators:
129bool operator==(const error_code& lhs, const error_code& rhs) noexcept;
130bool operator==(const error_code& lhs, const error_condition& rhs) noexcept;
131bool operator==(const error_condition& lhs, const error_code& rhs) noexcept;
132bool operator==(const error_condition& lhs, const error_condition& rhs) noexcept;
133bool operator!=(const error_code& lhs, const error_code& rhs) noexcept;
134bool operator!=(const error_code& lhs, const error_condition& rhs) noexcept;
135bool operator!=(const error_condition& lhs, const error_code& rhs) noexcept;
136bool operator!=(const error_condition& lhs, const error_condition& rhs) noexcept;
137
138template <> struct hash<std::error_code>;
139template <> struct hash<std::error_condition>;
140
141}  // std
142
143*/
144
145#include <__config>
146#include <__errc>
147#include <__functional/unary_function.h>
148#include <__functional_base>
149#include <compare>
150#include <stdexcept>
151#include <string>
152#include <type_traits>
153#include <version>
154
155#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
156#pragma GCC system_header
157#endif
158
159_LIBCPP_BEGIN_NAMESPACE_STD
160
161// is_error_code_enum
162
163template <class _Tp>
164struct _LIBCPP_TEMPLATE_VIS is_error_code_enum
165    : public false_type {};
166
167#if _LIBCPP_STD_VER > 14
168template <class _Tp>
169inline constexpr bool is_error_code_enum_v = is_error_code_enum<_Tp>::value;
170#endif
171
172// is_error_condition_enum
173
174template <class _Tp>
175struct _LIBCPP_TEMPLATE_VIS is_error_condition_enum
176    : public false_type {};
177
178#if _LIBCPP_STD_VER > 14
179template <class _Tp>
180inline constexpr bool is_error_condition_enum_v = is_error_condition_enum<_Tp>::value;
181#endif
182
183template <>
184struct _LIBCPP_TEMPLATE_VIS is_error_condition_enum<errc>
185    : true_type { };
186
187#ifdef _LIBCPP_HAS_NO_STRONG_ENUMS
188template <>
189struct _LIBCPP_TEMPLATE_VIS is_error_condition_enum<errc::__lx>
190    : true_type { };
191#endif
192
193class _LIBCPP_TYPE_VIS error_condition;
194class _LIBCPP_TYPE_VIS error_code;
195
196// class error_category
197
198class _LIBCPP_HIDDEN __do_message;
199
200class _LIBCPP_TYPE_VIS error_category
201{
202public:
203    virtual ~error_category() _NOEXCEPT;
204
205#if defined(_LIBCPP_BUILDING_LIBRARY) && \
206    defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
207    error_category() _NOEXCEPT;
208#else
209    _LIBCPP_INLINE_VISIBILITY
210    _LIBCPP_CONSTEXPR_AFTER_CXX11 error_category() _NOEXCEPT = default;
211#endif
212    error_category(const error_category&) = delete;
213    error_category& operator=(const error_category&) = delete;
214
215    virtual const char* name() const _NOEXCEPT = 0;
216    virtual error_condition default_error_condition(int __ev) const _NOEXCEPT;
217    virtual bool equivalent(int __code, const error_condition& __condition) const _NOEXCEPT;
218    virtual bool equivalent(const error_code& __code, int __condition) const _NOEXCEPT;
219    virtual string message(int __ev) const = 0;
220
221    _LIBCPP_INLINE_VISIBILITY
222    bool operator==(const error_category& __rhs) const _NOEXCEPT {return this == &__rhs;}
223
224    _LIBCPP_INLINE_VISIBILITY
225    bool operator!=(const error_category& __rhs) const _NOEXCEPT {return !(*this == __rhs);}
226
227    _LIBCPP_INLINE_VISIBILITY
228    bool operator< (const error_category& __rhs) const _NOEXCEPT {return this < &__rhs;}
229
230    friend class _LIBCPP_HIDDEN __do_message;
231};
232
233class _LIBCPP_HIDDEN __do_message
234    : public error_category
235{
236public:
237    virtual string message(int ev) const;
238};
239
240_LIBCPP_FUNC_VIS const error_category& generic_category() _NOEXCEPT;
241_LIBCPP_FUNC_VIS const error_category& system_category() _NOEXCEPT;
242
243class _LIBCPP_TYPE_VIS error_condition
244{
245    int __val_;
246    const error_category* __cat_;
247public:
248    _LIBCPP_INLINE_VISIBILITY
249    error_condition() _NOEXCEPT : __val_(0), __cat_(&generic_category()) {}
250
251    _LIBCPP_INLINE_VISIBILITY
252    error_condition(int __val, const error_category& __cat) _NOEXCEPT
253        : __val_(__val), __cat_(&__cat) {}
254
255    template <class _Ep>
256        _LIBCPP_INLINE_VISIBILITY
257        error_condition(_Ep __e,
258              typename enable_if<is_error_condition_enum<_Ep>::value>::type* = nullptr
259                                                                     ) _NOEXCEPT
260            {*this = make_error_condition(__e);}
261
262    _LIBCPP_INLINE_VISIBILITY
263    void assign(int __val, const error_category& __cat) _NOEXCEPT
264    {
265        __val_ = __val;
266        __cat_ = &__cat;
267    }
268
269    template <class _Ep>
270        _LIBCPP_INLINE_VISIBILITY
271        typename enable_if
272        <
273            is_error_condition_enum<_Ep>::value,
274            error_condition&
275        >::type
276        operator=(_Ep __e) _NOEXCEPT
277            {*this = make_error_condition(__e); return *this;}
278
279    _LIBCPP_INLINE_VISIBILITY
280    void clear() _NOEXCEPT
281    {
282        __val_ = 0;
283        __cat_ = &generic_category();
284    }
285
286    _LIBCPP_INLINE_VISIBILITY
287    int value() const _NOEXCEPT {return __val_;}
288
289    _LIBCPP_INLINE_VISIBILITY
290    const error_category& category() const _NOEXCEPT {return *__cat_;}
291    string message() const;
292
293    _LIBCPP_INLINE_VISIBILITY
294    explicit operator bool() const _NOEXCEPT {return __val_ != 0;}
295};
296
297inline _LIBCPP_INLINE_VISIBILITY
298error_condition
299make_error_condition(errc __e) _NOEXCEPT
300{
301    return error_condition(static_cast<int>(__e), generic_category());
302}
303
304inline _LIBCPP_INLINE_VISIBILITY
305bool
306operator<(const error_condition& __x, const error_condition& __y) _NOEXCEPT
307{
308    return __x.category() < __y.category()
309        || (__x.category() == __y.category() && __x.value() < __y.value());
310}
311
312// error_code
313
314class _LIBCPP_TYPE_VIS error_code
315{
316    int __val_;
317    const error_category* __cat_;
318public:
319    _LIBCPP_INLINE_VISIBILITY
320    error_code() _NOEXCEPT : __val_(0), __cat_(&system_category()) {}
321
322    _LIBCPP_INLINE_VISIBILITY
323    error_code(int __val, const error_category& __cat) _NOEXCEPT
324        : __val_(__val), __cat_(&__cat) {}
325
326    template <class _Ep>
327        _LIBCPP_INLINE_VISIBILITY
328        error_code(_Ep __e,
329                   typename enable_if<is_error_code_enum<_Ep>::value>::type* = nullptr
330                                                                     ) _NOEXCEPT
331            {*this = make_error_code(__e);}
332
333    _LIBCPP_INLINE_VISIBILITY
334    void assign(int __val, const error_category& __cat) _NOEXCEPT
335    {
336        __val_ = __val;
337        __cat_ = &__cat;
338    }
339
340    template <class _Ep>
341        _LIBCPP_INLINE_VISIBILITY
342        typename enable_if
343        <
344            is_error_code_enum<_Ep>::value,
345            error_code&
346        >::type
347        operator=(_Ep __e) _NOEXCEPT
348            {*this = make_error_code(__e); return *this;}
349
350    _LIBCPP_INLINE_VISIBILITY
351    void clear() _NOEXCEPT
352    {
353        __val_ = 0;
354        __cat_ = &system_category();
355    }
356
357    _LIBCPP_INLINE_VISIBILITY
358    int value() const _NOEXCEPT {return __val_;}
359
360    _LIBCPP_INLINE_VISIBILITY
361    const error_category& category() const _NOEXCEPT {return *__cat_;}
362
363    _LIBCPP_INLINE_VISIBILITY
364    error_condition default_error_condition() const _NOEXCEPT
365        {return __cat_->default_error_condition(__val_);}
366
367    string message() const;
368
369    _LIBCPP_INLINE_VISIBILITY
370    explicit operator bool() const _NOEXCEPT {return __val_ != 0;}
371};
372
373inline _LIBCPP_INLINE_VISIBILITY
374error_code
375make_error_code(errc __e) _NOEXCEPT
376{
377    return error_code(static_cast<int>(__e), generic_category());
378}
379
380inline _LIBCPP_INLINE_VISIBILITY
381bool
382operator<(const error_code& __x, const error_code& __y) _NOEXCEPT
383{
384    return __x.category() < __y.category()
385        || (__x.category() == __y.category() && __x.value() < __y.value());
386}
387
388inline _LIBCPP_INLINE_VISIBILITY
389bool
390operator==(const error_code& __x, const error_code& __y) _NOEXCEPT
391{
392    return __x.category() == __y.category() && __x.value() == __y.value();
393}
394
395inline _LIBCPP_INLINE_VISIBILITY
396bool
397operator==(const error_code& __x, const error_condition& __y) _NOEXCEPT
398{
399    return __x.category().equivalent(__x.value(), __y)
400        || __y.category().equivalent(__x, __y.value());
401}
402
403inline _LIBCPP_INLINE_VISIBILITY
404bool
405operator==(const error_condition& __x, const error_code& __y) _NOEXCEPT
406{
407    return __y == __x;
408}
409
410inline _LIBCPP_INLINE_VISIBILITY
411bool
412operator==(const error_condition& __x, const error_condition& __y) _NOEXCEPT
413{
414    return __x.category() == __y.category() && __x.value() == __y.value();
415}
416
417inline _LIBCPP_INLINE_VISIBILITY
418bool
419operator!=(const error_code& __x, const error_code& __y) _NOEXCEPT
420{return !(__x == __y);}
421
422inline _LIBCPP_INLINE_VISIBILITY
423bool
424operator!=(const error_code& __x, const error_condition& __y) _NOEXCEPT
425{return !(__x == __y);}
426
427inline _LIBCPP_INLINE_VISIBILITY
428bool
429operator!=(const error_condition& __x, const error_code& __y) _NOEXCEPT
430{return !(__x == __y);}
431
432inline _LIBCPP_INLINE_VISIBILITY
433bool
434operator!=(const error_condition& __x, const error_condition& __y) _NOEXCEPT
435{return !(__x == __y);}
436
437template <>
438struct _LIBCPP_TEMPLATE_VIS hash<error_code>
439    : public unary_function<error_code, size_t>
440{
441    _LIBCPP_INLINE_VISIBILITY
442    size_t operator()(const error_code& __ec) const _NOEXCEPT
443    {
444        return static_cast<size_t>(__ec.value());
445    }
446};
447
448template <>
449struct _LIBCPP_TEMPLATE_VIS hash<error_condition>
450    : public unary_function<error_condition, size_t>
451{
452    _LIBCPP_INLINE_VISIBILITY
453    size_t operator()(const error_condition& __ec) const _NOEXCEPT
454    {
455        return static_cast<size_t>(__ec.value());
456    }
457};
458
459// system_error
460
461class _LIBCPP_TYPE_VIS system_error
462    : public runtime_error
463{
464    error_code __ec_;
465public:
466    system_error(error_code __ec, const string& __what_arg);
467    system_error(error_code __ec, const char* __what_arg);
468    system_error(error_code __ec);
469    system_error(int __ev, const error_category& __ecat, const string& __what_arg);
470    system_error(int __ev, const error_category& __ecat, const char* __what_arg);
471    system_error(int __ev, const error_category& __ecat);
472    system_error(const system_error&) _NOEXCEPT = default;
473    ~system_error() _NOEXCEPT;
474
475    _LIBCPP_INLINE_VISIBILITY
476    const error_code& code() const _NOEXCEPT {return __ec_;}
477
478private:
479    static string __init(const error_code&, string);
480};
481
482_LIBCPP_NORETURN _LIBCPP_FUNC_VIS
483void __throw_system_error(int ev, const char* what_arg);
484
485_LIBCPP_END_NAMESPACE_STD
486
487#endif // _LIBCPP_SYSTEM_ERROR
488