system_error 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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 <__errc>
  122. #include <__functional/hash.h>
  123. #include <__functional/unary_function.h>
  124. #include <__memory/addressof.h>
  125. #include <stdexcept>
  126. #include <string>
  127. #include <type_traits>
  128. #include <version>
  129. // standard-mandated includes
  130. // [system.error.syn]
  131. #include <compare>
  132. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  133. # pragma GCC system_header
  134. #endif
  135. _LIBCPP_BEGIN_NAMESPACE_STD
  136. // is_error_code_enum
  137. template <class _Tp>
  138. struct _LIBCPP_TEMPLATE_VIS is_error_code_enum
  139. : public false_type {};
  140. #if _LIBCPP_STD_VER > 14
  141. template <class _Tp>
  142. inline constexpr bool is_error_code_enum_v = is_error_code_enum<_Tp>::value;
  143. #endif
  144. // is_error_condition_enum
  145. template <class _Tp>
  146. struct _LIBCPP_TEMPLATE_VIS is_error_condition_enum
  147. : public false_type {};
  148. #if _LIBCPP_STD_VER > 14
  149. template <class _Tp>
  150. inline constexpr bool is_error_condition_enum_v = is_error_condition_enum<_Tp>::value;
  151. #endif
  152. template <>
  153. struct _LIBCPP_TEMPLATE_VIS is_error_condition_enum<errc>
  154. : true_type { };
  155. #ifdef _LIBCPP_CXX03_LANG
  156. template <>
  157. struct _LIBCPP_TEMPLATE_VIS is_error_condition_enum<errc::__lx>
  158. : true_type { };
  159. #endif
  160. class _LIBCPP_TYPE_VIS error_condition;
  161. class _LIBCPP_TYPE_VIS error_code;
  162. // class error_category
  163. class _LIBCPP_HIDDEN __do_message;
  164. class _LIBCPP_TYPE_VIS error_category
  165. {
  166. public:
  167. virtual ~error_category() _NOEXCEPT;
  168. #if defined(_LIBCPP_ERROR_CATEGORY_DEFINE_LEGACY_INLINE_FUNCTIONS)
  169. error_category() noexcept;
  170. #else
  171. _LIBCPP_INLINE_VISIBILITY
  172. _LIBCPP_CONSTEXPR_SINCE_CXX14 error_category() _NOEXCEPT = default;
  173. #endif
  174. error_category(const error_category&) = delete;
  175. error_category& operator=(const error_category&) = delete;
  176. virtual const char* name() const _NOEXCEPT = 0;
  177. virtual error_condition default_error_condition(int __ev) const _NOEXCEPT;
  178. virtual bool equivalent(int __code, const error_condition& __condition) const _NOEXCEPT;
  179. virtual bool equivalent(const error_code& __code, int __condition) const _NOEXCEPT;
  180. virtual string message(int __ev) const = 0;
  181. _LIBCPP_INLINE_VISIBILITY
  182. bool operator==(const error_category& __rhs) const _NOEXCEPT {return this == &__rhs;}
  183. #if _LIBCPP_STD_VER > 17
  184. _LIBCPP_HIDE_FROM_ABI
  185. strong_ordering operator<=>(const error_category& __rhs) const noexcept {return compare_three_way()(this, std::addressof(__rhs));}
  186. #else // _LIBCPP_STD_VER > 17
  187. _LIBCPP_INLINE_VISIBILITY
  188. bool operator!=(const error_category& __rhs) const _NOEXCEPT {return !(*this == __rhs);}
  189. _LIBCPP_INLINE_VISIBILITY
  190. bool operator< (const error_category& __rhs) const _NOEXCEPT {return this < &__rhs;}
  191. #endif // _LIBCPP_STD_VER > 17
  192. friend class _LIBCPP_HIDDEN __do_message;
  193. };
  194. class _LIBCPP_HIDDEN __do_message
  195. : public error_category
  196. {
  197. public:
  198. string message(int __ev) const override;
  199. };
  200. _LIBCPP_FUNC_VIS const error_category& generic_category() _NOEXCEPT;
  201. _LIBCPP_FUNC_VIS const error_category& system_category() _NOEXCEPT;
  202. namespace __adl_only {
  203. // Those cause ADL to trigger but they are not viable candidates,
  204. // so they are never actually selected.
  205. void make_error_condition() = delete;
  206. void make_error_code() = delete;
  207. } // namespace __adl_only
  208. class _LIBCPP_TYPE_VIS error_condition
  209. {
  210. int __val_;
  211. const error_category* __cat_;
  212. public:
  213. _LIBCPP_INLINE_VISIBILITY
  214. error_condition() _NOEXCEPT : __val_(0), __cat_(&generic_category()) {}
  215. _LIBCPP_INLINE_VISIBILITY
  216. error_condition(int __val, const error_category& __cat) _NOEXCEPT
  217. : __val_(__val), __cat_(&__cat) {}
  218. template <class _Ep>
  219. _LIBCPP_INLINE_VISIBILITY
  220. error_condition(_Ep __e,
  221. typename enable_if<is_error_condition_enum<_Ep>::value>::type* = nullptr
  222. ) _NOEXCEPT
  223. {
  224. using __adl_only::make_error_condition;
  225. *this = make_error_condition(__e);
  226. }
  227. _LIBCPP_INLINE_VISIBILITY
  228. void assign(int __val, const error_category& __cat) _NOEXCEPT
  229. {
  230. __val_ = __val;
  231. __cat_ = &__cat;
  232. }
  233. template <class _Ep>
  234. _LIBCPP_INLINE_VISIBILITY
  235. typename enable_if
  236. <
  237. is_error_condition_enum<_Ep>::value,
  238. error_condition&
  239. >::type
  240. operator=(_Ep __e) _NOEXCEPT
  241. {
  242. using __adl_only::make_error_condition;
  243. *this = make_error_condition(__e);
  244. return *this;
  245. }
  246. _LIBCPP_INLINE_VISIBILITY
  247. void clear() _NOEXCEPT
  248. {
  249. __val_ = 0;
  250. __cat_ = &generic_category();
  251. }
  252. _LIBCPP_INLINE_VISIBILITY
  253. int value() const _NOEXCEPT {return __val_;}
  254. _LIBCPP_INLINE_VISIBILITY
  255. const error_category& category() const _NOEXCEPT {return *__cat_;}
  256. string message() const;
  257. _LIBCPP_INLINE_VISIBILITY
  258. explicit operator bool() const _NOEXCEPT {return __val_ != 0;}
  259. };
  260. inline _LIBCPP_INLINE_VISIBILITY
  261. error_condition
  262. make_error_condition(errc __e) _NOEXCEPT
  263. {
  264. return error_condition(static_cast<int>(__e), generic_category());
  265. }
  266. // error_code
  267. class _LIBCPP_TYPE_VIS error_code
  268. {
  269. int __val_;
  270. const error_category* __cat_;
  271. public:
  272. _LIBCPP_INLINE_VISIBILITY
  273. error_code() _NOEXCEPT : __val_(0), __cat_(&system_category()) {}
  274. _LIBCPP_INLINE_VISIBILITY
  275. error_code(int __val, const error_category& __cat) _NOEXCEPT
  276. : __val_(__val), __cat_(&__cat) {}
  277. template <class _Ep>
  278. _LIBCPP_INLINE_VISIBILITY
  279. error_code(_Ep __e,
  280. typename enable_if<is_error_code_enum<_Ep>::value>::type* = nullptr
  281. ) _NOEXCEPT
  282. {
  283. using __adl_only::make_error_code;
  284. *this = make_error_code(__e);
  285. }
  286. _LIBCPP_INLINE_VISIBILITY
  287. void assign(int __val, const error_category& __cat) _NOEXCEPT
  288. {
  289. __val_ = __val;
  290. __cat_ = &__cat;
  291. }
  292. template <class _Ep>
  293. _LIBCPP_INLINE_VISIBILITY
  294. typename enable_if
  295. <
  296. is_error_code_enum<_Ep>::value,
  297. error_code&
  298. >::type
  299. operator=(_Ep __e) _NOEXCEPT
  300. {
  301. using __adl_only::make_error_code;
  302. *this = make_error_code(__e);
  303. return *this;
  304. }
  305. _LIBCPP_INLINE_VISIBILITY
  306. void clear() _NOEXCEPT
  307. {
  308. __val_ = 0;
  309. __cat_ = &system_category();
  310. }
  311. _LIBCPP_INLINE_VISIBILITY
  312. int value() const _NOEXCEPT {return __val_;}
  313. _LIBCPP_INLINE_VISIBILITY
  314. const error_category& category() const _NOEXCEPT {return *__cat_;}
  315. _LIBCPP_INLINE_VISIBILITY
  316. error_condition default_error_condition() const _NOEXCEPT
  317. {return __cat_->default_error_condition(__val_);}
  318. string message() const;
  319. _LIBCPP_INLINE_VISIBILITY
  320. explicit operator bool() const _NOEXCEPT {return __val_ != 0;}
  321. };
  322. inline _LIBCPP_INLINE_VISIBILITY
  323. error_code
  324. make_error_code(errc __e) _NOEXCEPT
  325. {
  326. return error_code(static_cast<int>(__e), generic_category());
  327. }
  328. inline _LIBCPP_INLINE_VISIBILITY
  329. bool
  330. operator==(const error_code& __x, const error_code& __y) _NOEXCEPT
  331. {
  332. return __x.category() == __y.category() && __x.value() == __y.value();
  333. }
  334. inline _LIBCPP_INLINE_VISIBILITY
  335. bool
  336. operator==(const error_code& __x, const error_condition& __y) _NOEXCEPT
  337. {
  338. return __x.category().equivalent(__x.value(), __y)
  339. || __y.category().equivalent(__x, __y.value());
  340. }
  341. #if _LIBCPP_STD_VER <= 17
  342. inline _LIBCPP_INLINE_VISIBILITY
  343. bool
  344. operator==(const error_condition& __x, const error_code& __y) _NOEXCEPT
  345. {
  346. return __y == __x;
  347. }
  348. #endif
  349. inline _LIBCPP_INLINE_VISIBILITY
  350. bool
  351. operator==(const error_condition& __x, const error_condition& __y) _NOEXCEPT
  352. {
  353. return __x.category() == __y.category() && __x.value() == __y.value();
  354. }
  355. #if _LIBCPP_STD_VER <= 17
  356. inline _LIBCPP_INLINE_VISIBILITY
  357. bool
  358. operator!=(const error_code& __x, const error_code& __y) _NOEXCEPT
  359. {return !(__x == __y);}
  360. inline _LIBCPP_INLINE_VISIBILITY
  361. bool
  362. operator!=(const error_code& __x, const error_condition& __y) _NOEXCEPT
  363. {return !(__x == __y);}
  364. inline _LIBCPP_INLINE_VISIBILITY
  365. bool
  366. operator!=(const error_condition& __x, const error_code& __y) _NOEXCEPT
  367. {return !(__x == __y);}
  368. inline _LIBCPP_INLINE_VISIBILITY
  369. bool
  370. operator!=(const error_condition& __x, const error_condition& __y) _NOEXCEPT
  371. {return !(__x == __y);}
  372. inline _LIBCPP_INLINE_VISIBILITY
  373. bool
  374. operator<(const error_condition& __x, const error_condition& __y) _NOEXCEPT
  375. {
  376. return __x.category() < __y.category()
  377. || (__x.category() == __y.category() && __x.value() < __y.value());
  378. }
  379. inline _LIBCPP_INLINE_VISIBILITY
  380. bool
  381. operator<(const error_code& __x, const error_code& __y) _NOEXCEPT
  382. {
  383. return __x.category() < __y.category()
  384. || (__x.category() == __y.category() && __x.value() < __y.value());
  385. }
  386. #else // _LIBCPP_STD_VER <= 17
  387. inline _LIBCPP_HIDE_FROM_ABI strong_ordering
  388. operator<=>(const error_code& __x, const error_code& __y) noexcept
  389. {
  390. if (auto __c = __x.category() <=> __y.category(); __c != 0)
  391. return __c;
  392. return __x.value() <=> __y.value();
  393. }
  394. inline _LIBCPP_HIDE_FROM_ABI strong_ordering
  395. operator<=>(const error_condition& __x, const error_condition& __y) noexcept
  396. {
  397. if (auto __c = __x.category() <=> __y.category(); __c != 0)
  398. return __c;
  399. return __x.value() <=> __y.value();
  400. }
  401. #endif // _LIBCPP_STD_VER <= 17
  402. template <>
  403. struct _LIBCPP_TEMPLATE_VIS hash<error_code>
  404. : public __unary_function<error_code, size_t>
  405. {
  406. _LIBCPP_INLINE_VISIBILITY
  407. size_t operator()(const error_code& __ec) const _NOEXCEPT
  408. {
  409. return static_cast<size_t>(__ec.value());
  410. }
  411. };
  412. template <>
  413. struct _LIBCPP_TEMPLATE_VIS hash<error_condition>
  414. : public __unary_function<error_condition, size_t>
  415. {
  416. _LIBCPP_INLINE_VISIBILITY
  417. size_t operator()(const error_condition& __ec) const _NOEXCEPT
  418. {
  419. return static_cast<size_t>(__ec.value());
  420. }
  421. };
  422. // system_error
  423. class _LIBCPP_TYPE_VIS system_error
  424. : public runtime_error
  425. {
  426. error_code __ec_;
  427. public:
  428. system_error(error_code __ec, const string& __what_arg);
  429. system_error(error_code __ec, const char* __what_arg);
  430. system_error(error_code __ec);
  431. system_error(int __ev, const error_category& __ecat, const string& __what_arg);
  432. system_error(int __ev, const error_category& __ecat, const char* __what_arg);
  433. system_error(int __ev, const error_category& __ecat);
  434. system_error(const system_error&) _NOEXCEPT = default;
  435. ~system_error() _NOEXCEPT override;
  436. _LIBCPP_INLINE_VISIBILITY
  437. const error_code& code() const _NOEXCEPT {return __ec_;}
  438. private:
  439. static string __init(const error_code&, string);
  440. };
  441. _LIBCPP_NORETURN _LIBCPP_FUNC_VIS
  442. void __throw_system_error(int __ev, const char* __what_arg);
  443. _LIBCPP_END_NAMESPACE_STD
  444. #endif // _LIBCPP_SYSTEM_ERROR