system_error 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. // -*- C++ -*-
  2. //===---------------------------- system_error ----------------------------===//
  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 size_t is_error_condition_enum_v = is_error_condition_enum<_Tp>::value; // C++17
  39. template <class _Tp>
  40. inline constexpr size_t 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 <__errc>
  117. #include <type_traits>
  118. #include <stdexcept>
  119. #include <__functional_base>
  120. #include <string>
  121. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  122. #pragma GCC system_header
  123. #endif
  124. _LIBCPP_BEGIN_NAMESPACE_STD
  125. // is_error_code_enum
  126. template <class _Tp>
  127. struct _LIBCPP_TEMPLATE_VIS is_error_code_enum
  128. : public false_type {};
  129. #if _LIBCPP_STD_VER > 14
  130. template <class _Tp>
  131. _LIBCPP_INLINE_VAR constexpr size_t is_error_code_enum_v = is_error_code_enum<_Tp>::value;
  132. #endif
  133. // is_error_condition_enum
  134. template <class _Tp>
  135. struct _LIBCPP_TEMPLATE_VIS is_error_condition_enum
  136. : public false_type {};
  137. #if _LIBCPP_STD_VER > 14
  138. template <class _Tp>
  139. _LIBCPP_INLINE_VAR constexpr size_t is_error_condition_enum_v = is_error_condition_enum<_Tp>::value;
  140. #endif
  141. template <>
  142. struct _LIBCPP_TEMPLATE_VIS is_error_condition_enum<errc>
  143. : true_type { };
  144. #ifdef _LIBCPP_HAS_NO_STRONG_ENUMS
  145. template <>
  146. struct _LIBCPP_TEMPLATE_VIS is_error_condition_enum<errc::__lx>
  147. : true_type { };
  148. #endif
  149. class _LIBCPP_TYPE_VIS error_condition;
  150. class _LIBCPP_TYPE_VIS error_code;
  151. // class error_category
  152. class _LIBCPP_HIDDEN __do_message;
  153. class _LIBCPP_TYPE_VIS error_category
  154. {
  155. public:
  156. virtual ~error_category() _NOEXCEPT;
  157. #if defined(_LIBCPP_BUILDING_LIBRARY) && \
  158. defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
  159. error_category() _NOEXCEPT;
  160. #else
  161. _LIBCPP_INLINE_VISIBILITY
  162. _LIBCPP_CONSTEXPR_AFTER_CXX11 error_category() _NOEXCEPT _LIBCPP_DEFAULT
  163. #endif
  164. private:
  165. error_category(const error_category&);// = delete;
  166. error_category& operator=(const error_category&);// = delete;
  167. public:
  168. virtual const char* name() const _NOEXCEPT = 0;
  169. virtual error_condition default_error_condition(int __ev) const _NOEXCEPT;
  170. virtual bool equivalent(int __code, const error_condition& __condition) const _NOEXCEPT;
  171. virtual bool equivalent(const error_code& __code, int __condition) const _NOEXCEPT;
  172. virtual string message(int __ev) const = 0;
  173. _LIBCPP_INLINE_VISIBILITY
  174. bool operator==(const error_category& __rhs) const _NOEXCEPT {return this == &__rhs;}
  175. _LIBCPP_INLINE_VISIBILITY
  176. bool operator!=(const error_category& __rhs) const _NOEXCEPT {return !(*this == __rhs);}
  177. _LIBCPP_INLINE_VISIBILITY
  178. bool operator< (const error_category& __rhs) const _NOEXCEPT {return this < &__rhs;}
  179. friend class _LIBCPP_HIDDEN __do_message;
  180. };
  181. class _LIBCPP_HIDDEN __do_message
  182. : public error_category
  183. {
  184. public:
  185. virtual string message(int ev) const;
  186. };
  187. _LIBCPP_FUNC_VIS const error_category& generic_category() _NOEXCEPT;
  188. _LIBCPP_FUNC_VIS const error_category& system_category() _NOEXCEPT;
  189. class _LIBCPP_TYPE_VIS error_condition
  190. {
  191. int __val_;
  192. const error_category* __cat_;
  193. public:
  194. _LIBCPP_INLINE_VISIBILITY
  195. error_condition() _NOEXCEPT : __val_(0), __cat_(&generic_category()) {}
  196. _LIBCPP_INLINE_VISIBILITY
  197. error_condition(int __val, const error_category& __cat) _NOEXCEPT
  198. : __val_(__val), __cat_(&__cat) {}
  199. template <class _Ep>
  200. _LIBCPP_INLINE_VISIBILITY
  201. error_condition(_Ep __e,
  202. typename enable_if<is_error_condition_enum<_Ep>::value>::type* = 0
  203. ) _NOEXCEPT
  204. {*this = make_error_condition(__e);}
  205. _LIBCPP_INLINE_VISIBILITY
  206. void assign(int __val, const error_category& __cat) _NOEXCEPT
  207. {
  208. __val_ = __val;
  209. __cat_ = &__cat;
  210. }
  211. template <class _Ep>
  212. _LIBCPP_INLINE_VISIBILITY
  213. typename enable_if
  214. <
  215. is_error_condition_enum<_Ep>::value,
  216. error_condition&
  217. >::type
  218. operator=(_Ep __e) _NOEXCEPT
  219. {*this = make_error_condition(__e); return *this;}
  220. _LIBCPP_INLINE_VISIBILITY
  221. void clear() _NOEXCEPT
  222. {
  223. __val_ = 0;
  224. __cat_ = &generic_category();
  225. }
  226. _LIBCPP_INLINE_VISIBILITY
  227. int value() const _NOEXCEPT {return __val_;}
  228. _LIBCPP_INLINE_VISIBILITY
  229. const error_category& category() const _NOEXCEPT {return *__cat_;}
  230. string message() const;
  231. _LIBCPP_INLINE_VISIBILITY
  232. _LIBCPP_EXPLICIT
  233. operator bool() const _NOEXCEPT {return __val_ != 0;}
  234. };
  235. inline _LIBCPP_INLINE_VISIBILITY
  236. error_condition
  237. make_error_condition(errc __e) _NOEXCEPT
  238. {
  239. return error_condition(static_cast<int>(__e), generic_category());
  240. }
  241. inline _LIBCPP_INLINE_VISIBILITY
  242. bool
  243. operator<(const error_condition& __x, const error_condition& __y) _NOEXCEPT
  244. {
  245. return __x.category() < __y.category()
  246. || (__x.category() == __y.category() && __x.value() < __y.value());
  247. }
  248. // error_code
  249. class _LIBCPP_TYPE_VIS error_code
  250. {
  251. int __val_;
  252. const error_category* __cat_;
  253. public:
  254. _LIBCPP_INLINE_VISIBILITY
  255. error_code() _NOEXCEPT : __val_(0), __cat_(&system_category()) {}
  256. _LIBCPP_INLINE_VISIBILITY
  257. error_code(int __val, const error_category& __cat) _NOEXCEPT
  258. : __val_(__val), __cat_(&__cat) {}
  259. template <class _Ep>
  260. _LIBCPP_INLINE_VISIBILITY
  261. error_code(_Ep __e,
  262. typename enable_if<is_error_code_enum<_Ep>::value>::type* = 0
  263. ) _NOEXCEPT
  264. {*this = make_error_code(__e);}
  265. _LIBCPP_INLINE_VISIBILITY
  266. void assign(int __val, const error_category& __cat) _NOEXCEPT
  267. {
  268. __val_ = __val;
  269. __cat_ = &__cat;
  270. }
  271. template <class _Ep>
  272. _LIBCPP_INLINE_VISIBILITY
  273. typename enable_if
  274. <
  275. is_error_code_enum<_Ep>::value,
  276. error_code&
  277. >::type
  278. operator=(_Ep __e) _NOEXCEPT
  279. {*this = make_error_code(__e); return *this;}
  280. _LIBCPP_INLINE_VISIBILITY
  281. void clear() _NOEXCEPT
  282. {
  283. __val_ = 0;
  284. __cat_ = &system_category();
  285. }
  286. _LIBCPP_INLINE_VISIBILITY
  287. int value() const _NOEXCEPT {return __val_;}
  288. _LIBCPP_INLINE_VISIBILITY
  289. const error_category& category() const _NOEXCEPT {return *__cat_;}
  290. _LIBCPP_INLINE_VISIBILITY
  291. error_condition default_error_condition() const _NOEXCEPT
  292. {return __cat_->default_error_condition(__val_);}
  293. string message() const;
  294. _LIBCPP_INLINE_VISIBILITY
  295. _LIBCPP_EXPLICIT
  296. operator bool() const _NOEXCEPT {return __val_ != 0;}
  297. };
  298. inline _LIBCPP_INLINE_VISIBILITY
  299. error_code
  300. make_error_code(errc __e) _NOEXCEPT
  301. {
  302. return error_code(static_cast<int>(__e), generic_category());
  303. }
  304. inline _LIBCPP_INLINE_VISIBILITY
  305. bool
  306. operator<(const error_code& __x, const error_code& __y) _NOEXCEPT
  307. {
  308. return __x.category() < __y.category()
  309. || (__x.category() == __y.category() && __x.value() < __y.value());
  310. }
  311. inline _LIBCPP_INLINE_VISIBILITY
  312. bool
  313. operator==(const error_code& __x, const error_code& __y) _NOEXCEPT
  314. {
  315. return __x.category() == __y.category() && __x.value() == __y.value();
  316. }
  317. inline _LIBCPP_INLINE_VISIBILITY
  318. bool
  319. operator==(const error_code& __x, const error_condition& __y) _NOEXCEPT
  320. {
  321. return __x.category().equivalent(__x.value(), __y)
  322. || __y.category().equivalent(__x, __y.value());
  323. }
  324. inline _LIBCPP_INLINE_VISIBILITY
  325. bool
  326. operator==(const error_condition& __x, const error_code& __y) _NOEXCEPT
  327. {
  328. return __y == __x;
  329. }
  330. inline _LIBCPP_INLINE_VISIBILITY
  331. bool
  332. operator==(const error_condition& __x, const error_condition& __y) _NOEXCEPT
  333. {
  334. return __x.category() == __y.category() && __x.value() == __y.value();
  335. }
  336. inline _LIBCPP_INLINE_VISIBILITY
  337. bool
  338. operator!=(const error_code& __x, const error_code& __y) _NOEXCEPT
  339. {return !(__x == __y);}
  340. inline _LIBCPP_INLINE_VISIBILITY
  341. bool
  342. operator!=(const error_code& __x, const error_condition& __y) _NOEXCEPT
  343. {return !(__x == __y);}
  344. inline _LIBCPP_INLINE_VISIBILITY
  345. bool
  346. operator!=(const error_condition& __x, const error_code& __y) _NOEXCEPT
  347. {return !(__x == __y);}
  348. inline _LIBCPP_INLINE_VISIBILITY
  349. bool
  350. operator!=(const error_condition& __x, const error_condition& __y) _NOEXCEPT
  351. {return !(__x == __y);}
  352. template <>
  353. struct _LIBCPP_TEMPLATE_VIS hash<error_code>
  354. : public unary_function<error_code, size_t>
  355. {
  356. _LIBCPP_INLINE_VISIBILITY
  357. size_t operator()(const error_code& __ec) const _NOEXCEPT
  358. {
  359. return static_cast<size_t>(__ec.value());
  360. }
  361. };
  362. template <>
  363. struct _LIBCPP_TEMPLATE_VIS hash<error_condition>
  364. : public unary_function<error_condition, size_t>
  365. {
  366. _LIBCPP_INLINE_VISIBILITY
  367. size_t operator()(const error_condition& __ec) const _NOEXCEPT
  368. {
  369. return static_cast<size_t>(__ec.value());
  370. }
  371. };
  372. // system_error
  373. class _LIBCPP_TYPE_VIS system_error
  374. : public runtime_error
  375. {
  376. error_code __ec_;
  377. public:
  378. system_error(error_code __ec, const string& __what_arg);
  379. system_error(error_code __ec, const char* __what_arg);
  380. system_error(error_code __ec);
  381. system_error(int __ev, const error_category& __ecat, const string& __what_arg);
  382. system_error(int __ev, const error_category& __ecat, const char* __what_arg);
  383. system_error(int __ev, const error_category& __ecat);
  384. ~system_error() _NOEXCEPT;
  385. _LIBCPP_INLINE_VISIBILITY
  386. const error_code& code() const _NOEXCEPT {return __ec_;}
  387. private:
  388. static string __init(const error_code&, string);
  389. };
  390. _LIBCPP_NORETURN _LIBCPP_FUNC_VIS
  391. void __throw_system_error(int ev, const char* what_arg);
  392. _LIBCPP_END_NAMESPACE_STD
  393. #endif // _LIBCPP_SYSTEM_ERROR