system_error 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #ifndef _LIBCPP_SYSTEM_ERROR
  10. #define _LIBCPP_SYSTEM_ERROR
  11. /*
  12. system_error synopsis
  13. namespace std
  14. {
  15. class error_category
  16. {
  17. public:
  18. virtual ~error_category() noexcept;
  19. constexpr error_category();
  20. error_category(const error_category&) = delete;
  21. error_category& operator=(const error_category&) = delete;
  22. virtual const char* name() const noexcept = 0;
  23. virtual error_condition default_error_condition(int ev) const noexcept;
  24. virtual bool equivalent(int code, const error_condition& condition) const noexcept;
  25. virtual bool equivalent(const error_code& code, int condition) const noexcept;
  26. virtual string message(int ev) const = 0;
  27. bool operator==(const error_category& rhs) const noexcept;
  28. bool operator!=(const error_category& rhs) const noexcept; // removed in C++20
  29. bool operator<(const error_category& rhs) const noexcept; // removed in C++20
  30. strong_ordering operator<=>(const error_category& rhs) const noexcept; // C++20
  31. };
  32. const error_category& generic_category() noexcept;
  33. const error_category& system_category() noexcept;
  34. template <class T> struct is_error_code_enum
  35. : public false_type {};
  36. template <class T> struct is_error_condition_enum
  37. : public false_type {};
  38. template <class _Tp>
  39. inline constexpr bool is_error_condition_enum_v = is_error_condition_enum<_Tp>::value; // C++17
  40. template <class _Tp>
  41. inline constexpr bool is_error_code_enum_v = is_error_code_enum<_Tp>::value; // C++17
  42. class error_code
  43. {
  44. public:
  45. // constructors:
  46. error_code() noexcept;
  47. error_code(int val, const error_category& cat) noexcept;
  48. template <class ErrorCodeEnum>
  49. error_code(ErrorCodeEnum e) noexcept;
  50. // modifiers:
  51. void assign(int val, const error_category& cat) noexcept;
  52. template <class ErrorCodeEnum>
  53. error_code& operator=(ErrorCodeEnum e) noexcept;
  54. void clear() noexcept;
  55. // observers:
  56. int value() const noexcept;
  57. const error_category& category() const noexcept;
  58. error_condition default_error_condition() const noexcept;
  59. string message() const;
  60. explicit operator bool() const noexcept;
  61. };
  62. // non-member functions:
  63. template <class charT, class traits>
  64. basic_ostream<charT,traits>&
  65. operator<<(basic_ostream<charT,traits>& os, const error_code& ec);
  66. class error_condition
  67. {
  68. public:
  69. // constructors:
  70. error_condition() noexcept;
  71. error_condition(int val, const error_category& cat) noexcept;
  72. template <class ErrorConditionEnum>
  73. error_condition(ErrorConditionEnum e) noexcept;
  74. // modifiers:
  75. void assign(int val, const error_category& cat) noexcept;
  76. template <class ErrorConditionEnum>
  77. error_condition& operator=(ErrorConditionEnum e) noexcept;
  78. void clear() noexcept;
  79. // observers:
  80. int value() const noexcept;
  81. const error_category& category() const noexcept;
  82. string message() const noexcept;
  83. explicit operator bool() const noexcept;
  84. };
  85. class system_error
  86. : public runtime_error
  87. {
  88. public:
  89. system_error(error_code ec, const string& what_arg);
  90. system_error(error_code ec, const char* what_arg);
  91. system_error(error_code ec);
  92. system_error(int ev, const error_category& ecat, const string& what_arg);
  93. system_error(int ev, const error_category& ecat, const char* what_arg);
  94. system_error(int ev, const error_category& ecat);
  95. const error_code& code() const noexcept;
  96. const char* what() const noexcept;
  97. };
  98. template <> struct is_error_condition_enum<errc>
  99. : true_type { }
  100. error_code make_error_code(errc e) noexcept;
  101. error_condition make_error_condition(errc e) noexcept;
  102. // Comparison operators:
  103. bool operator==(const error_code& lhs, const error_code& rhs) noexcept;
  104. bool operator==(const error_code& lhs, const error_condition& rhs) noexcept;
  105. bool operator==(const error_condition& lhs, const error_code& rhs) noexcept; // removed in C++20
  106. bool operator==(const error_condition& lhs, const error_condition& rhs) noexcept;
  107. bool operator!=(const error_code& lhs, const error_code& rhs) noexcept; // removed in C++20
  108. bool operator!=(const error_code& lhs, const error_condition& rhs) noexcept; // removed in C++20
  109. bool operator!=(const error_condition& lhs, const error_code& rhs) noexcept; // removed in C++20
  110. bool operator!=(const error_condition& lhs, const error_condition& rhs) noexcept; // removed in C++20
  111. bool operator<(const error_condition& lhs, const error_condition& rhs) noexcept; // removed in C++20
  112. bool operator<(const error_code& lhs, const error_code& rhs) noexcept; // removed in C++20
  113. strong_ordering operator<=>(const error_code& lhs, const error_code& rhs) noexcept; // C++20
  114. strong_ordering operator<=>(const error_condition& lhs, const error_condition& rhs) noexcept; // C++20
  115. template <> struct hash<std::error_code>;
  116. template <> struct hash<std::error_condition>;
  117. } // std
  118. */
  119. #include <__assert> // all public C++ headers provide the assertion handler
  120. #include <__config>
  121. #include <__system_error/errc.h>
  122. #include <__system_error/error_category.h>
  123. #include <__system_error/error_code.h>
  124. #include <__system_error/error_condition.h>
  125. #include <__system_error/system_error.h>
  126. #include <version>
  127. // standard-mandated includes
  128. // [system.error.syn]
  129. #include <compare>
  130. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  131. # pragma GCC system_header
  132. #endif
  133. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  134. # include <cstdint>
  135. # include <cstring>
  136. # include <limits>
  137. # include <type_traits>
  138. #endif
  139. #endif // _LIBCPP_SYSTEM_ERROR