system_error 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 <compare>
  120. #include <stdexcept>
  121. #include <string>
  122. #include <type_traits>
  123. #include <version>
  124. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  125. # pragma GCC system_header
  126. #endif
  127. _LIBCPP_BEGIN_NAMESPACE_STD
  128. // is_error_code_enum
  129. template <class _Tp>
  130. struct _LIBCPP_TEMPLATE_VIS is_error_code_enum
  131. : public false_type {};
  132. #if _LIBCPP_STD_VER > 14
  133. template <class _Tp>
  134. inline constexpr bool is_error_code_enum_v = is_error_code_enum<_Tp>::value;
  135. #endif
  136. // is_error_condition_enum
  137. template <class _Tp>
  138. struct _LIBCPP_TEMPLATE_VIS is_error_condition_enum
  139. : public false_type {};
  140. #if _LIBCPP_STD_VER > 14
  141. template <class _Tp>
  142. inline constexpr bool is_error_condition_enum_v = is_error_condition_enum<_Tp>::value;
  143. #endif
  144. template <>
  145. struct _LIBCPP_TEMPLATE_VIS is_error_condition_enum<errc>
  146. : true_type { };
  147. #ifdef _LIBCPP_HAS_NO_STRONG_ENUMS
  148. template <>
  149. struct _LIBCPP_TEMPLATE_VIS is_error_condition_enum<errc::__lx>
  150. : true_type { };
  151. #endif
  152. class _LIBCPP_TYPE_VIS error_condition;
  153. class _LIBCPP_TYPE_VIS error_code;
  154. // class error_category
  155. class _LIBCPP_HIDDEN __do_message;
  156. class _LIBCPP_TYPE_VIS error_category
  157. {
  158. public:
  159. virtual ~error_category() _NOEXCEPT;
  160. #if defined(_LIBCPP_BUILDING_LIBRARY) && \
  161. defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
  162. error_category() _NOEXCEPT;
  163. #else
  164. _LIBCPP_INLINE_VISIBILITY
  165. _LIBCPP_CONSTEXPR_AFTER_CXX11 error_category() _NOEXCEPT = default;
  166. #endif
  167. error_category(const error_category&) = delete;
  168. error_category& operator=(const error_category&) = delete;
  169. virtual const char* name() const _NOEXCEPT = 0;
  170. virtual error_condition default_error_condition(int __ev) const _NOEXCEPT;
  171. virtual bool equivalent(int __code, const error_condition& __condition) const _NOEXCEPT;
  172. virtual bool equivalent(const error_code& __code, int __condition) const _NOEXCEPT;
  173. virtual string message(int __ev) const = 0;
  174. _LIBCPP_INLINE_VISIBILITY
  175. bool operator==(const error_category& __rhs) const _NOEXCEPT {return this == &__rhs;}
  176. _LIBCPP_INLINE_VISIBILITY
  177. bool operator!=(const error_category& __rhs) const _NOEXCEPT {return !(*this == __rhs);}
  178. _LIBCPP_INLINE_VISIBILITY
  179. bool operator< (const error_category& __rhs) const _NOEXCEPT {return this < &__rhs;}
  180. friend class _LIBCPP_HIDDEN __do_message;
  181. };
  182. class _LIBCPP_HIDDEN __do_message
  183. : public error_category
  184. {
  185. public:
  186. virtual string message(int ev) const;
  187. };
  188. _LIBCPP_FUNC_VIS const error_category& generic_category() _NOEXCEPT;
  189. _LIBCPP_FUNC_VIS const error_category& system_category() _NOEXCEPT;
  190. class _LIBCPP_TYPE_VIS error_condition
  191. {
  192. int __val_;
  193. const error_category* __cat_;
  194. public:
  195. _LIBCPP_INLINE_VISIBILITY
  196. error_condition() _NOEXCEPT : __val_(0), __cat_(&generic_category()) {}
  197. _LIBCPP_INLINE_VISIBILITY
  198. error_condition(int __val, const error_category& __cat) _NOEXCEPT
  199. : __val_(__val), __cat_(&__cat) {}
  200. template <class _Ep>
  201. _LIBCPP_INLINE_VISIBILITY
  202. error_condition(_Ep __e,
  203. typename enable_if<is_error_condition_enum<_Ep>::value>::type* = nullptr
  204. ) _NOEXCEPT
  205. {*this = make_error_condition(__e);}
  206. _LIBCPP_INLINE_VISIBILITY
  207. void assign(int __val, const error_category& __cat) _NOEXCEPT
  208. {
  209. __val_ = __val;
  210. __cat_ = &__cat;
  211. }
  212. template <class _Ep>
  213. _LIBCPP_INLINE_VISIBILITY
  214. typename enable_if
  215. <
  216. is_error_condition_enum<_Ep>::value,
  217. error_condition&
  218. >::type
  219. operator=(_Ep __e) _NOEXCEPT
  220. {*this = make_error_condition(__e); return *this;}
  221. _LIBCPP_INLINE_VISIBILITY
  222. void clear() _NOEXCEPT
  223. {
  224. __val_ = 0;
  225. __cat_ = &generic_category();
  226. }
  227. _LIBCPP_INLINE_VISIBILITY
  228. int value() const _NOEXCEPT {return __val_;}
  229. _LIBCPP_INLINE_VISIBILITY
  230. const error_category& category() const _NOEXCEPT {return *__cat_;}
  231. string message() const;
  232. _LIBCPP_INLINE_VISIBILITY
  233. explicit 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* = nullptr
  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. explicit operator bool() const _NOEXCEPT {return __val_ != 0;}
  296. };
  297. inline _LIBCPP_INLINE_VISIBILITY
  298. error_code
  299. make_error_code(errc __e) _NOEXCEPT
  300. {
  301. return error_code(static_cast<int>(__e), generic_category());
  302. }
  303. inline _LIBCPP_INLINE_VISIBILITY
  304. bool
  305. operator<(const error_code& __x, const error_code& __y) _NOEXCEPT
  306. {
  307. return __x.category() < __y.category()
  308. || (__x.category() == __y.category() && __x.value() < __y.value());
  309. }
  310. inline _LIBCPP_INLINE_VISIBILITY
  311. bool
  312. operator==(const error_code& __x, const error_code& __y) _NOEXCEPT
  313. {
  314. return __x.category() == __y.category() && __x.value() == __y.value();
  315. }
  316. inline _LIBCPP_INLINE_VISIBILITY
  317. bool
  318. operator==(const error_code& __x, const error_condition& __y) _NOEXCEPT
  319. {
  320. return __x.category().equivalent(__x.value(), __y)
  321. || __y.category().equivalent(__x, __y.value());
  322. }
  323. inline _LIBCPP_INLINE_VISIBILITY
  324. bool
  325. operator==(const error_condition& __x, const error_code& __y) _NOEXCEPT
  326. {
  327. return __y == __x;
  328. }
  329. inline _LIBCPP_INLINE_VISIBILITY
  330. bool
  331. operator==(const error_condition& __x, const error_condition& __y) _NOEXCEPT
  332. {
  333. return __x.category() == __y.category() && __x.value() == __y.value();
  334. }
  335. inline _LIBCPP_INLINE_VISIBILITY
  336. bool
  337. operator!=(const error_code& __x, const error_code& __y) _NOEXCEPT
  338. {return !(__x == __y);}
  339. inline _LIBCPP_INLINE_VISIBILITY
  340. bool
  341. operator!=(const error_code& __x, const error_condition& __y) _NOEXCEPT
  342. {return !(__x == __y);}
  343. inline _LIBCPP_INLINE_VISIBILITY
  344. bool
  345. operator!=(const error_condition& __x, const error_code& __y) _NOEXCEPT
  346. {return !(__x == __y);}
  347. inline _LIBCPP_INLINE_VISIBILITY
  348. bool
  349. operator!=(const error_condition& __x, const error_condition& __y) _NOEXCEPT
  350. {return !(__x == __y);}
  351. template <>
  352. struct _LIBCPP_TEMPLATE_VIS hash<error_code>
  353. : public unary_function<error_code, size_t>
  354. {
  355. _LIBCPP_INLINE_VISIBILITY
  356. size_t operator()(const error_code& __ec) const _NOEXCEPT
  357. {
  358. return static_cast<size_t>(__ec.value());
  359. }
  360. };
  361. template <>
  362. struct _LIBCPP_TEMPLATE_VIS hash<error_condition>
  363. : public unary_function<error_condition, size_t>
  364. {
  365. _LIBCPP_INLINE_VISIBILITY
  366. size_t operator()(const error_condition& __ec) const _NOEXCEPT
  367. {
  368. return static_cast<size_t>(__ec.value());
  369. }
  370. };
  371. // system_error
  372. class _LIBCPP_TYPE_VIS system_error
  373. : public runtime_error
  374. {
  375. error_code __ec_;
  376. public:
  377. system_error(error_code __ec, const string& __what_arg);
  378. system_error(error_code __ec, const char* __what_arg);
  379. system_error(error_code __ec);
  380. system_error(int __ev, const error_category& __ecat, const string& __what_arg);
  381. system_error(int __ev, const error_category& __ecat, const char* __what_arg);
  382. system_error(int __ev, const error_category& __ecat);
  383. system_error(const system_error&) _NOEXCEPT = default;
  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