generic_code.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /* Proposed SG14 status_code
  2. (C) 2018 - 2020 Niall Douglas <http://www.nedproductions.biz/> (5 commits)
  3. File Created: Feb 2018
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License in the accompanying file
  7. Licence.txt or at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. Distributed under the Boost Software License, Version 1.0.
  15. (See accompanying file Licence.txt or copy at
  16. http://www.boost.org/LICENSE_1_0.txt)
  17. */
  18. #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_GENERIC_CODE_HPP
  19. #define BOOST_OUTCOME_SYSTEM_ERROR2_GENERIC_CODE_HPP
  20. #include "status_error.hpp"
  21. #include <cerrno> // for error constants
  22. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
  23. //! The generic error coding (POSIX)
  24. enum class errc : int
  25. {
  26. success = 0,
  27. unknown = -1,
  28. address_family_not_supported = EAFNOSUPPORT,
  29. address_in_use = EADDRINUSE,
  30. address_not_available = EADDRNOTAVAIL,
  31. already_connected = EISCONN,
  32. argument_list_too_long = E2BIG,
  33. argument_out_of_domain = EDOM,
  34. bad_address = EFAULT,
  35. bad_file_descriptor = EBADF,
  36. bad_message = EBADMSG,
  37. broken_pipe = EPIPE,
  38. connection_aborted = ECONNABORTED,
  39. connection_already_in_progress = EALREADY,
  40. connection_refused = ECONNREFUSED,
  41. connection_reset = ECONNRESET,
  42. cross_device_link = EXDEV,
  43. destination_address_required = EDESTADDRREQ,
  44. device_or_resource_busy = EBUSY,
  45. directory_not_empty = ENOTEMPTY,
  46. executable_format_error = ENOEXEC,
  47. file_exists = EEXIST,
  48. file_too_large = EFBIG,
  49. filename_too_long = ENAMETOOLONG,
  50. function_not_supported = ENOSYS,
  51. host_unreachable = EHOSTUNREACH,
  52. identifier_removed = EIDRM,
  53. illegal_byte_sequence = EILSEQ,
  54. inappropriate_io_control_operation = ENOTTY,
  55. interrupted = EINTR,
  56. invalid_argument = EINVAL,
  57. invalid_seek = ESPIPE,
  58. io_error = EIO,
  59. is_a_directory = EISDIR,
  60. message_size = EMSGSIZE,
  61. network_down = ENETDOWN,
  62. network_reset = ENETRESET,
  63. network_unreachable = ENETUNREACH,
  64. no_buffer_space = ENOBUFS,
  65. no_child_process = ECHILD,
  66. no_link = ENOLINK,
  67. no_lock_available = ENOLCK,
  68. no_message = ENOMSG,
  69. no_protocol_option = ENOPROTOOPT,
  70. no_space_on_device = ENOSPC,
  71. no_stream_resources = ENOSR,
  72. no_such_device_or_address = ENXIO,
  73. no_such_device = ENODEV,
  74. no_such_file_or_directory = ENOENT,
  75. no_such_process = ESRCH,
  76. not_a_directory = ENOTDIR,
  77. not_a_socket = ENOTSOCK,
  78. not_a_stream = ENOSTR,
  79. not_connected = ENOTCONN,
  80. not_enough_memory = ENOMEM,
  81. not_supported = ENOTSUP,
  82. operation_canceled = ECANCELED,
  83. operation_in_progress = EINPROGRESS,
  84. operation_not_permitted = EPERM,
  85. operation_not_supported = EOPNOTSUPP,
  86. operation_would_block = EWOULDBLOCK,
  87. owner_dead = EOWNERDEAD,
  88. permission_denied = EACCES,
  89. protocol_error = EPROTO,
  90. protocol_not_supported = EPROTONOSUPPORT,
  91. read_only_file_system = EROFS,
  92. resource_deadlock_would_occur = EDEADLK,
  93. resource_unavailable_try_again = EAGAIN,
  94. result_out_of_range = ERANGE,
  95. state_not_recoverable = ENOTRECOVERABLE,
  96. stream_timeout = ETIME,
  97. text_file_busy = ETXTBSY,
  98. timed_out = ETIMEDOUT,
  99. too_many_files_open_in_system = ENFILE,
  100. too_many_files_open = EMFILE,
  101. too_many_links = EMLINK,
  102. too_many_symbolic_link_levels = ELOOP,
  103. value_too_large = EOVERFLOW,
  104. wrong_protocol_type = EPROTOTYPE
  105. };
  106. namespace detail
  107. {
  108. BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 inline const char *generic_code_message(errc code) noexcept
  109. {
  110. switch(code)
  111. {
  112. case errc::success:
  113. return "Success";
  114. case errc::address_family_not_supported:
  115. return "Address family not supported by protocol";
  116. case errc::address_in_use:
  117. return "Address already in use";
  118. case errc::address_not_available:
  119. return "Cannot assign requested address";
  120. case errc::already_connected:
  121. return "Transport endpoint is already connected";
  122. case errc::argument_list_too_long:
  123. return "Argument list too long";
  124. case errc::argument_out_of_domain:
  125. return "Numerical argument out of domain";
  126. case errc::bad_address:
  127. return "Bad address";
  128. case errc::bad_file_descriptor:
  129. return "Bad file descriptor";
  130. case errc::bad_message:
  131. return "Bad message";
  132. case errc::broken_pipe:
  133. return "Broken pipe";
  134. case errc::connection_aborted:
  135. return "Software caused connection abort";
  136. case errc::connection_already_in_progress:
  137. return "Operation already in progress";
  138. case errc::connection_refused:
  139. return "Connection refused";
  140. case errc::connection_reset:
  141. return "Connection reset by peer";
  142. case errc::cross_device_link:
  143. return "Invalid cross-device link";
  144. case errc::destination_address_required:
  145. return "Destination address required";
  146. case errc::device_or_resource_busy:
  147. return "Device or resource busy";
  148. case errc::directory_not_empty:
  149. return "Directory not empty";
  150. case errc::executable_format_error:
  151. return "Exec format error";
  152. case errc::file_exists:
  153. return "File exists";
  154. case errc::file_too_large:
  155. return "File too large";
  156. case errc::filename_too_long:
  157. return "File name too long";
  158. case errc::function_not_supported:
  159. return "Function not implemented";
  160. case errc::host_unreachable:
  161. return "No route to host";
  162. case errc::identifier_removed:
  163. return "Identifier removed";
  164. case errc::illegal_byte_sequence:
  165. return "Invalid or incomplete multibyte or wide character";
  166. case errc::inappropriate_io_control_operation:
  167. return "Inappropriate ioctl for device";
  168. case errc::interrupted:
  169. return "Interrupted system call";
  170. case errc::invalid_argument:
  171. return "Invalid argument";
  172. case errc::invalid_seek:
  173. return "Illegal seek";
  174. case errc::io_error:
  175. return "Input/output error";
  176. case errc::is_a_directory:
  177. return "Is a directory";
  178. case errc::message_size:
  179. return "Message too long";
  180. case errc::network_down:
  181. return "Network is down";
  182. case errc::network_reset:
  183. return "Network dropped connection on reset";
  184. case errc::network_unreachable:
  185. return "Network is unreachable";
  186. case errc::no_buffer_space:
  187. return "No buffer space available";
  188. case errc::no_child_process:
  189. return "No child processes";
  190. case errc::no_link:
  191. return "Link has been severed";
  192. case errc::no_lock_available:
  193. return "No locks available";
  194. case errc::no_message:
  195. return "No message of desired type";
  196. case errc::no_protocol_option:
  197. return "Protocol not available";
  198. case errc::no_space_on_device:
  199. return "No space left on device";
  200. case errc::no_stream_resources:
  201. return "Out of streams resources";
  202. case errc::no_such_device_or_address:
  203. return "No such device or address";
  204. case errc::no_such_device:
  205. return "No such device";
  206. case errc::no_such_file_or_directory:
  207. return "No such file or directory";
  208. case errc::no_such_process:
  209. return "No such process";
  210. case errc::not_a_directory:
  211. return "Not a directory";
  212. case errc::not_a_socket:
  213. return "Socket operation on non-socket";
  214. case errc::not_a_stream:
  215. return "Device not a stream";
  216. case errc::not_connected:
  217. return "Transport endpoint is not connected";
  218. case errc::not_enough_memory:
  219. return "Cannot allocate memory";
  220. #if ENOTSUP != EOPNOTSUPP
  221. case errc::not_supported:
  222. return "Operation not supported";
  223. #endif
  224. case errc::operation_canceled:
  225. return "Operation canceled";
  226. case errc::operation_in_progress:
  227. return "Operation now in progress";
  228. case errc::operation_not_permitted:
  229. return "Operation not permitted";
  230. case errc::operation_not_supported:
  231. return "Operation not supported";
  232. #if EAGAIN != EWOULDBLOCK
  233. case errc::operation_would_block:
  234. return "Resource temporarily unavailable";
  235. #endif
  236. case errc::owner_dead:
  237. return "Owner died";
  238. case errc::permission_denied:
  239. return "Permission denied";
  240. case errc::protocol_error:
  241. return "Protocol error";
  242. case errc::protocol_not_supported:
  243. return "Protocol not supported";
  244. case errc::read_only_file_system:
  245. return "Read-only file system";
  246. case errc::resource_deadlock_would_occur:
  247. return "Resource deadlock avoided";
  248. case errc::resource_unavailable_try_again:
  249. return "Resource temporarily unavailable";
  250. case errc::result_out_of_range:
  251. return "Numerical result out of range";
  252. case errc::state_not_recoverable:
  253. return "State not recoverable";
  254. case errc::stream_timeout:
  255. return "Timer expired";
  256. case errc::text_file_busy:
  257. return "Text file busy";
  258. case errc::timed_out:
  259. return "Connection timed out";
  260. case errc::too_many_files_open_in_system:
  261. return "Too many open files in system";
  262. case errc::too_many_files_open:
  263. return "Too many open files";
  264. case errc::too_many_links:
  265. return "Too many links";
  266. case errc::too_many_symbolic_link_levels:
  267. return "Too many levels of symbolic links";
  268. case errc::value_too_large:
  269. return "Value too large for defined data type";
  270. case errc::wrong_protocol_type:
  271. return "Protocol wrong type for socket";
  272. default:
  273. return "unknown";
  274. }
  275. }
  276. } // namespace detail
  277. /*! The implementation of the domain for generic status codes, those mapped by `errc` (POSIX).
  278. */
  279. class _generic_code_domain : public status_code_domain
  280. {
  281. template <class> friend class status_code;
  282. template <class StatusCode> friend class detail::indirecting_domain;
  283. using _base = status_code_domain;
  284. public:
  285. //! The value type of the generic code, which is an `errc` as per POSIX.
  286. using value_type = errc;
  287. using string_ref = _base::string_ref;
  288. public:
  289. //! Default constructor
  290. constexpr explicit _generic_code_domain(typename _base::unique_id_type id = 0x746d6354f4f733e9) noexcept
  291. : _base(id)
  292. {
  293. }
  294. _generic_code_domain(const _generic_code_domain &) = default;
  295. _generic_code_domain(_generic_code_domain &&) = default;
  296. _generic_code_domain &operator=(const _generic_code_domain &) = default;
  297. _generic_code_domain &operator=(_generic_code_domain &&) = default;
  298. ~_generic_code_domain() = default;
  299. //! Constexpr singleton getter. Returns the constexpr generic_code_domain variable.
  300. static inline constexpr const _generic_code_domain &get();
  301. virtual _base::string_ref name() const noexcept override { return string_ref("generic domain"); } // NOLINT
  302. protected:
  303. virtual bool _do_failure(const status_code<void> &code) const noexcept override // NOLINT
  304. {
  305. assert(code.domain() == *this); // NOLINT
  306. return static_cast<const generic_code &>(code).value() != errc::success; // NOLINT
  307. }
  308. virtual bool _do_equivalent(const status_code<void> &code1, const status_code<void> &code2) const noexcept override // NOLINT
  309. {
  310. assert(code1.domain() == *this); // NOLINT
  311. const auto &c1 = static_cast<const generic_code &>(code1); // NOLINT
  312. if(code2.domain() == *this)
  313. {
  314. const auto &c2 = static_cast<const generic_code &>(code2); // NOLINT
  315. return c1.value() == c2.value();
  316. }
  317. return false;
  318. }
  319. virtual generic_code _generic_code(const status_code<void> &code) const noexcept override // NOLINT
  320. {
  321. assert(code.domain() == *this); // NOLINT
  322. return static_cast<const generic_code &>(code); // NOLINT
  323. }
  324. virtual _base::string_ref _do_message(const status_code<void> &code) const noexcept override // NOLINT
  325. {
  326. assert(code.domain() == *this); // NOLINT
  327. const auto &c = static_cast<const generic_code &>(code); // NOLINT
  328. return string_ref(detail::generic_code_message(c.value()));
  329. }
  330. #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
  331. BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN virtual void _do_throw_exception(const status_code<void> &code) const override // NOLINT
  332. {
  333. assert(code.domain() == *this); // NOLINT
  334. const auto &c = static_cast<const generic_code &>(code); // NOLINT
  335. throw status_error<_generic_code_domain>(c);
  336. }
  337. #endif
  338. };
  339. //! A specialisation of `status_error` for the generic code domain.
  340. using generic_error = status_error<_generic_code_domain>;
  341. //! A constexpr source variable for the generic code domain, which is that of `errc` (POSIX). Returned by `_generic_code_domain::get()`.
  342. constexpr _generic_code_domain generic_code_domain;
  343. inline constexpr const _generic_code_domain &_generic_code_domain::get()
  344. {
  345. return generic_code_domain;
  346. }
  347. // Enable implicit construction of generic_code from errc
  348. BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 inline generic_code make_status_code(errc c) noexcept
  349. {
  350. return generic_code(in_place, c);
  351. }
  352. /*************************************************************************************************************/
  353. template <class T> inline bool status_code<void>::equivalent(const status_code<T> &o) const noexcept
  354. {
  355. if(_domain && o._domain)
  356. {
  357. if(_domain->_do_equivalent(*this, o))
  358. {
  359. return true;
  360. }
  361. if(o._domain->_do_equivalent(o, *this))
  362. {
  363. return true;
  364. }
  365. generic_code c1 = o._domain->_generic_code(o);
  366. if(c1.value() != errc::unknown && _domain->_do_equivalent(*this, c1))
  367. {
  368. return true;
  369. }
  370. generic_code c2 = _domain->_generic_code(*this);
  371. if(c2.value() != errc::unknown && o._domain->_do_equivalent(o, c2))
  372. {
  373. return true;
  374. }
  375. }
  376. // If we are both empty, we are equivalent, otherwise not equivalent
  377. return (!_domain && !o._domain);
  378. }
  379. //! True if the status code's are semantically equal via `equivalent()`.
  380. template <class DomainType1, class DomainType2> inline bool operator==(const status_code<DomainType1> &a, const status_code<DomainType2> &b) noexcept
  381. {
  382. return a.equivalent(b);
  383. }
  384. //! True if the status code's are not semantically equal via `equivalent()`.
  385. template <class DomainType1, class DomainType2> inline bool operator!=(const status_code<DomainType1> &a, const status_code<DomainType2> &b) noexcept
  386. {
  387. return !a.equivalent(b);
  388. }
  389. //! True if the status code's are semantically equal via `equivalent()` to `make_status_code(T)`.
  390. template <class DomainType1, class T, //
  391. class MakeStatusCodeResult = typename detail::safe_get_make_status_code_result<const T &>::type, // Safe ADL lookup of make_status_code(), returns void if not found
  392. typename std::enable_if<is_status_code<MakeStatusCodeResult>::value, bool>::type = true> // ADL makes a status code
  393. inline bool operator==(const status_code<DomainType1> &a, const T &b)
  394. {
  395. return a.equivalent(make_status_code(b));
  396. }
  397. //! True if the status code's are semantically equal via `equivalent()` to `make_status_code(T)`.
  398. template <class T, class DomainType1, //
  399. class MakeStatusCodeResult = typename detail::safe_get_make_status_code_result<const T &>::type, // Safe ADL lookup of make_status_code(), returns void if not found
  400. typename std::enable_if<is_status_code<MakeStatusCodeResult>::value, bool>::type = true> // ADL makes a status code
  401. inline bool operator==(const T &a, const status_code<DomainType1> &b)
  402. {
  403. return b.equivalent(make_status_code(a));
  404. }
  405. //! True if the status code's are not semantically equal via `equivalent()` to `make_status_code(T)`.
  406. template <class DomainType1, class T, //
  407. class MakeStatusCodeResult = typename detail::safe_get_make_status_code_result<const T &>::type, // Safe ADL lookup of make_status_code(), returns void if not found
  408. typename std::enable_if<is_status_code<MakeStatusCodeResult>::value, bool>::type = true> // ADL makes a status code
  409. inline bool operator!=(const status_code<DomainType1> &a, const T &b)
  410. {
  411. return !a.equivalent(make_status_code(b));
  412. }
  413. //! True if the status code's are semantically equal via `equivalent()` to `make_status_code(T)`.
  414. template <class T, class DomainType1, //
  415. class MakeStatusCodeResult = typename detail::safe_get_make_status_code_result<const T &>::type, // Safe ADL lookup of make_status_code(), returns void if not found
  416. typename std::enable_if<is_status_code<MakeStatusCodeResult>::value, bool>::type = true> // ADL makes a status code
  417. inline bool operator!=(const T &a, const status_code<DomainType1> &b)
  418. {
  419. return !b.equivalent(make_status_code(a));
  420. }
  421. //! True if the status code's are semantically equal via `equivalent()` to `quick_status_code_from_enum<T>::code_type(b)`.
  422. template <class DomainType1, class T, //
  423. class QuickStatusCodeType = typename quick_status_code_from_enum<T>::code_type // Enumeration has been activated
  424. >
  425. inline bool operator==(const status_code<DomainType1> &a, const T &b)
  426. {
  427. return a.equivalent(QuickStatusCodeType(b));
  428. }
  429. //! True if the status code's are semantically equal via `equivalent()` to `quick_status_code_from_enum<T>::code_type(a)`.
  430. template <class T, class DomainType1, //
  431. class QuickStatusCodeType = typename quick_status_code_from_enum<T>::code_type // Enumeration has been activated
  432. >
  433. inline bool operator==(const T &a, const status_code<DomainType1> &b)
  434. {
  435. return b.equivalent(QuickStatusCodeType(a));
  436. }
  437. //! True if the status code's are not semantically equal via `equivalent()` to `quick_status_code_from_enum<T>::code_type(b)`.
  438. template <class DomainType1, class T, //
  439. class QuickStatusCodeType = typename quick_status_code_from_enum<T>::code_type // Enumeration has been activated
  440. >
  441. inline bool operator!=(const status_code<DomainType1> &a, const T &b)
  442. {
  443. return !a.equivalent(QuickStatusCodeType(b));
  444. }
  445. //! True if the status code's are not semantically equal via `equivalent()` to `quick_status_code_from_enum<T>::code_type(a)`.
  446. template <class T, class DomainType1, //
  447. class QuickStatusCodeType = typename quick_status_code_from_enum<T>::code_type // Enumeration has been activated
  448. >
  449. inline bool operator!=(const T &a, const status_code<DomainType1> &b)
  450. {
  451. return !b.equivalent(QuickStatusCodeType(a));
  452. }
  453. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
  454. #endif