system_error 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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;
  29. bool operator<(const error_category& rhs) const noexcept;
  30. };
  31. const error_category& generic_category() noexcept;
  32. const error_category& system_category() noexcept;
  33. template <class T> struct is_error_code_enum
  34. : public false_type {};
  35. template <class T> struct is_error_condition_enum
  36. : public false_type {};
  37. template <class _Tp>
  38. inline constexpr bool is_error_condition_enum_v = is_error_condition_enum<_Tp>::value; // C++17
  39. template <class _Tp>
  40. inline constexpr bool is_error_code_enum_v = is_error_code_enum<_Tp>::value; // C++17
  41. class error_code
  42. {
  43. public:
  44. // constructors:
  45. error_code() noexcept;
  46. error_code(int val, const error_category& cat) noexcept;
  47. template <class ErrorCodeEnum>
  48. error_code(ErrorCodeEnum e) noexcept;
  49. // modifiers:
  50. void assign(int val, const error_category& cat) noexcept;
  51. template <class ErrorCodeEnum>
  52. error_code& operator=(ErrorCodeEnum e) noexcept;
  53. void clear() noexcept;
  54. // observers:
  55. int value() const noexcept;
  56. const error_category& category() const noexcept;
  57. error_condition default_error_condition() const noexcept;
  58. string message() const;
  59. explicit operator bool() const noexcept;
  60. };
  61. // non-member functions:
  62. bool operator<(const error_code& lhs, const error_code& rhs) noexcept;
  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. bool operator<(const error_condition& lhs, const error_condition& rhs) noexcept;
  86. class system_error
  87. : public runtime_error
  88. {
  89. public:
  90. system_error(error_code ec, const string& what_arg);
  91. system_error(error_code ec, const char* what_arg);
  92. system_error(error_code ec);
  93. system_error(int ev, const error_category& ecat, const string& what_arg);
  94. system_error(int ev, const error_category& ecat, const char* what_arg);
  95. system_error(int ev, const error_category& ecat);
  96. const error_code& code() const noexcept;
  97. const char* what() const noexcept;
  98. };
  99. template <> struct is_error_condition_enum<errc>
  100. : true_type { }
  101. error_code make_error_code(errc e) noexcept;
  102. error_condition make_error_condition(errc e) noexcept;
  103. // Comparison operators:
  104. bool operator==(const error_code& lhs, const error_code& rhs) noexcept;
  105. bool operator==(const error_code& lhs, const error_condition& rhs) noexcept;
  106. bool operator==(const error_condition& lhs, const error_code& rhs) noexcept;
  107. bool operator==(const error_condition& lhs, const error_condition& rhs) noexcept;
  108. bool operator!=(const error_code& lhs, const error_code& rhs) noexcept;
  109. bool operator!=(const error_code& lhs, const error_condition& rhs) noexcept;
  110. bool operator!=(const error_condition& lhs, const error_code& rhs) noexcept;
  111. bool operator!=(const error_condition& lhs, const error_condition& rhs) noexcept;
  112. template <> struct hash<std::error_code>;
  113. template <> struct hash<std::error_condition>;
  114. } // std
  115. */
  116. #include <__config>
  117. #include <__errc>
  118. #include <__functional/unary_function.h>
  119. #include <__functional_base>
  120. #include <compare>
  121. #include <stdexcept>
  122. #include <string>
  123. #include <type_traits>
  124. #include <version>
  125. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  126. #pragma GCC system_header
  127. #endif
  128. _LIBCPP_BEGIN_NAMESPACE_STD
  129. // is_error_code_enum
  130. template <class _Tp>
  131. struct _LIBCPP_TEMPLATE_VIS is_error_code_enum
  132. : public false_type {};
  133. #if _LIBCPP_STD_VER > 14
  134. template <class _Tp>
  135. inline constexpr bool is_error_code_enum_v = is_error_code_enum<_Tp>::value;
  136. #endif
  137. // is_error_condition_enum
  138. template <class _Tp>
  139. struct _LIBCPP_TEMPLATE_VIS is_error_condition_enum
  140. : public false_type {};
  141. #if _LIBCPP_STD_VER > 14
  142. template <class _Tp>
  143. inline constexpr bool is_error_condition_enum_v = is_error_condition_enum<_Tp>::value;
  144. #endif
  145. template <>
  146. struct _LIBCPP_TEMPLATE_VIS is_error_condition_enum<errc>
  147. : true_type { };
  148. #ifdef _LIBCPP_HAS_NO_STRONG_ENUMS
  149. template <>
  150. struct _LIBCPP_TEMPLATE_VIS is_error_condition_enum<errc::__lx>
  151. : true_type { };
  152. #endif
  153. class _LIBCPP_TYPE_VIS error_condition;
  154. class _LIBCPP_TYPE_VIS error_code;
  155. // class error_category
  156. class _LIBCPP_HIDDEN __do_message;
  157. class _LIBCPP_TYPE_VIS error_category
  158. {
  159. public:
  160. virtual ~error_category() _NOEXCEPT;
  161. // ODR violation is used for binary compatibility with older versions of libc++.
  162. // We don't have old libc++ versions for MSVC, so we don't need it.
  163. #if defined(_LIBCPP_BUILDING_LIBRARY) && \
  164. defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
  165. error_category() _NOEXCEPT;
  166. #else
  167. _LIBCPP_INLINE_VISIBILITY
  168. _LIBCPP_CONSTEXPR_AFTER_CXX11 error_category() _NOEXCEPT = default;
  169. #endif
  170. error_category(const error_category&) = delete;
  171. error_category& operator=(const error_category&) = delete;
  172. virtual const char* name() const _NOEXCEPT = 0;
  173. virtual error_condition default_error_condition(int __ev) const _NOEXCEPT;
  174. virtual bool equivalent(int __code, const error_condition& __condition) const _NOEXCEPT;
  175. virtual bool equivalent(const error_code& __code, int __condition) const _NOEXCEPT;
  176. virtual string message(int __ev) const = 0;
  177. _LIBCPP_INLINE_VISIBILITY
  178. bool operator==(const error_category& __rhs) const _NOEXCEPT {return this == &__rhs;}
  179. _LIBCPP_INLINE_VISIBILITY
  180. bool operator!=(const error_category& __rhs) const _NOEXCEPT {return !(*this == __rhs);}
  181. _LIBCPP_INLINE_VISIBILITY
  182. bool operator< (const error_category& __rhs) const _NOEXCEPT {return this < &__rhs;}
  183. friend class _LIBCPP_HIDDEN __do_message;
  184. };
  185. class _LIBCPP_HIDDEN __do_message
  186. : public error_category
  187. {
  188. public:
  189. virtual string message(int ev) const;
  190. };
  191. _LIBCPP_FUNC_VIS const error_category& generic_category() _NOEXCEPT;
  192. _LIBCPP_FUNC_VIS const error_category& system_category() _NOEXCEPT;
  193. class _LIBCPP_TYPE_VIS error_condition
  194. {
  195. int __val_;
  196. const error_category* __cat_;
  197. public:
  198. _LIBCPP_INLINE_VISIBILITY
  199. error_condition() _NOEXCEPT : __val_(0), __cat_(&generic_category()) {}
  200. _LIBCPP_INLINE_VISIBILITY
  201. error_condition(int __val, const error_category& __cat) _NOEXCEPT
  202. : __val_(__val), __cat_(&__cat) {}
  203. template <class _Ep>
  204. _LIBCPP_INLINE_VISIBILITY
  205. error_condition(_Ep __e,
  206. typename enable_if<is_error_condition_enum<_Ep>::value>::type* = nullptr
  207. ) _NOEXCEPT
  208. {*this = make_error_condition(__e);}
  209. _LIBCPP_INLINE_VISIBILITY
  210. void assign(int __val, const error_category& __cat) _NOEXCEPT
  211. {
  212. __val_ = __val;
  213. __cat_ = &__cat;
  214. }
  215. template <class _Ep>
  216. _LIBCPP_INLINE_VISIBILITY
  217. typename enable_if
  218. <
  219. is_error_condition_enum<_Ep>::value,
  220. error_condition&
  221. >::type
  222. operator=(_Ep __e) _NOEXCEPT
  223. {*this = make_error_condition(__e); return *this;}
  224. _LIBCPP_INLINE_VISIBILITY
  225. void clear() _NOEXCEPT
  226. {
  227. __val_ = 0;
  228. __cat_ = &generic_category();
  229. }
  230. _LIBCPP_INLINE_VISIBILITY
  231. int value() const _NOEXCEPT {return __val_;}
  232. _LIBCPP_INLINE_VISIBILITY
  233. const error_category& category() const _NOEXCEPT {return *__cat_;}
  234. string message() const;
  235. _LIBCPP_INLINE_VISIBILITY
  236. explicit operator bool() const _NOEXCEPT {return __val_ != 0;}
  237. };
  238. inline _LIBCPP_INLINE_VISIBILITY
  239. error_condition
  240. make_error_condition(errc __e) _NOEXCEPT
  241. {
  242. return error_condition(static_cast<int>(__e), generic_category());
  243. }
  244. inline _LIBCPP_INLINE_VISIBILITY
  245. bool
  246. operator<(const error_condition& __x, const error_condition& __y) _NOEXCEPT
  247. {
  248. return __x.category() < __y.category()
  249. || (__x.category() == __y.category() && __x.value() < __y.value());
  250. }
  251. // error_code
  252. class _LIBCPP_TYPE_VIS error_code
  253. {
  254. int __val_;
  255. const error_category* __cat_;
  256. public:
  257. _LIBCPP_INLINE_VISIBILITY
  258. error_code() _NOEXCEPT : __val_(0), __cat_(&system_category()) {}
  259. _LIBCPP_INLINE_VISIBILITY
  260. error_code(int __val, const error_category& __cat) _NOEXCEPT
  261. : __val_(__val), __cat_(&__cat) {}
  262. template <class _Ep>
  263. _LIBCPP_INLINE_VISIBILITY
  264. error_code(_Ep __e,
  265. typename enable_if<is_error_code_enum<_Ep>::value>::type* = nullptr
  266. ) _NOEXCEPT
  267. {*this = make_error_code(__e);}
  268. _LIBCPP_INLINE_VISIBILITY
  269. void assign(int __val, const error_category& __cat) _NOEXCEPT
  270. {
  271. __val_ = __val;
  272. __cat_ = &__cat;
  273. }
  274. template <class _Ep>
  275. _LIBCPP_INLINE_VISIBILITY
  276. typename enable_if
  277. <
  278. is_error_code_enum<_Ep>::value,
  279. error_code&
  280. >::type
  281. operator=(_Ep __e) _NOEXCEPT
  282. {*this = make_error_code(__e); return *this;}
  283. _LIBCPP_INLINE_VISIBILITY
  284. void clear() _NOEXCEPT
  285. {
  286. __val_ = 0;
  287. __cat_ = &system_category();
  288. }
  289. _LIBCPP_INLINE_VISIBILITY
  290. int value() const _NOEXCEPT {return __val_;}
  291. _LIBCPP_INLINE_VISIBILITY
  292. const error_category& category() const _NOEXCEPT {return *__cat_;}
  293. _LIBCPP_INLINE_VISIBILITY
  294. error_condition default_error_condition() const _NOEXCEPT
  295. {return __cat_->default_error_condition(__val_);}
  296. string message() const;
  297. _LIBCPP_INLINE_VISIBILITY
  298. explicit operator bool() const _NOEXCEPT {return __val_ != 0;}
  299. };
  300. inline _LIBCPP_INLINE_VISIBILITY
  301. error_code
  302. make_error_code(errc __e) _NOEXCEPT
  303. {
  304. return error_code(static_cast<int>(__e), generic_category());
  305. }
  306. inline _LIBCPP_INLINE_VISIBILITY
  307. bool
  308. operator<(const error_code& __x, const error_code& __y) _NOEXCEPT
  309. {
  310. return __x.category() < __y.category()
  311. || (__x.category() == __y.category() && __x.value() < __y.value());
  312. }
  313. inline _LIBCPP_INLINE_VISIBILITY
  314. bool
  315. operator==(const error_code& __x, const error_code& __y) _NOEXCEPT
  316. {
  317. return __x.category() == __y.category() && __x.value() == __y.value();
  318. }
  319. inline _LIBCPP_INLINE_VISIBILITY
  320. bool
  321. operator==(const error_code& __x, const error_condition& __y) _NOEXCEPT
  322. {
  323. return __x.category().equivalent(__x.value(), __y)
  324. || __y.category().equivalent(__x, __y.value());
  325. }
  326. inline _LIBCPP_INLINE_VISIBILITY
  327. bool
  328. operator==(const error_condition& __x, const error_code& __y) _NOEXCEPT
  329. {
  330. return __y == __x;
  331. }
  332. inline _LIBCPP_INLINE_VISIBILITY
  333. bool
  334. operator==(const error_condition& __x, const error_condition& __y) _NOEXCEPT
  335. {
  336. return __x.category() == __y.category() && __x.value() == __y.value();
  337. }
  338. inline _LIBCPP_INLINE_VISIBILITY
  339. bool
  340. operator!=(const error_code& __x, const error_code& __y) _NOEXCEPT
  341. {return !(__x == __y);}
  342. inline _LIBCPP_INLINE_VISIBILITY
  343. bool
  344. operator!=(const error_code& __x, const error_condition& __y) _NOEXCEPT
  345. {return !(__x == __y);}
  346. inline _LIBCPP_INLINE_VISIBILITY
  347. bool
  348. operator!=(const error_condition& __x, const error_code& __y) _NOEXCEPT
  349. {return !(__x == __y);}
  350. inline _LIBCPP_INLINE_VISIBILITY
  351. bool
  352. operator!=(const error_condition& __x, const error_condition& __y) _NOEXCEPT
  353. {return !(__x == __y);}
  354. template <>
  355. struct _LIBCPP_TEMPLATE_VIS hash<error_code>
  356. : public unary_function<error_code, size_t>
  357. {
  358. _LIBCPP_INLINE_VISIBILITY
  359. size_t operator()(const error_code& __ec) const _NOEXCEPT
  360. {
  361. return static_cast<size_t>(__ec.value());
  362. }
  363. };
  364. template <>
  365. struct _LIBCPP_TEMPLATE_VIS hash<error_condition>
  366. : public unary_function<error_condition, size_t>
  367. {
  368. _LIBCPP_INLINE_VISIBILITY
  369. size_t operator()(const error_condition& __ec) const _NOEXCEPT
  370. {
  371. return static_cast<size_t>(__ec.value());
  372. }
  373. };
  374. // system_error
  375. class _LIBCPP_TYPE_VIS system_error
  376. : public runtime_error
  377. {
  378. error_code __ec_;
  379. public:
  380. system_error(error_code __ec, const string& __what_arg);
  381. system_error(error_code __ec, const char* __what_arg);
  382. system_error(error_code __ec);
  383. system_error(int __ev, const error_category& __ecat, const string& __what_arg);
  384. system_error(int __ev, const error_category& __ecat, const char* __what_arg);
  385. system_error(int __ev, const error_category& __ecat);
  386. system_error(const system_error&) _NOEXCEPT = default;
  387. ~system_error() _NOEXCEPT;
  388. _LIBCPP_INLINE_VISIBILITY
  389. const error_code& code() const _NOEXCEPT {return __ec_;}
  390. private:
  391. static string __init(const error_code&, string);
  392. };
  393. _LIBCPP_NORETURN _LIBCPP_FUNC_VIS
  394. void __throw_system_error(int ev, const char* what_arg);
  395. _LIBCPP_END_NAMESPACE_STD
  396. #endif // _LIBCPP_SYSTEM_ERROR