1 /* Copyright (c) 2016-2023. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include <system_error>
11 #ifndef SIMGRID_MC_SYSTEM_ERROR_HPP
12 #define SIMGRID_MC_SYSTEM_ERROR_HPP
14 namespace simgrid::xbt {
16 /** A `error_category` suitable to be used with `errno`
18 * It is not clear which error we are supposed to generate
19 * when getting an errno:
21 * * `system_error` clearly cannot be used for this on Windows;
23 * * `generic_error` might not be used for non-standard `errno`.
25 * Let's just define a function which gives us the correct
29 const std::error_category& errno_category() noexcept
31 return std::generic_category();
34 /** Create a `error_code` from an `errno` value
36 * This is expected to to whatever is right to create a
37 * `error_code` from a given `errno` value.
40 std::error_code errno_code(int errnum)
42 return std::error_code(errnum, errno_category());
45 /** Create an `error_code` from `errno` (and clear it) */
47 std::error_code errno_code()
51 return errno_code(errnum);
54 /** Create a `system_error` from an `errno` value
56 * This is expected to to whatever is right to create a
57 * `system_error` from a given `errno` value.
60 std::system_error errno_error(int errnum)
62 return std::system_error(errno_code(errnum));
66 std::system_error errno_error(int errnum, const char* what)
68 return std::system_error(errno_code(errnum), what);
71 /** Create a `system_code` from `errno` (and clear it) */
73 std::system_error errno_error()
75 return std::system_error(errno_code());
79 std::system_error errno_error(const char* what)
81 return std::system_error(errno_code(), what);
84 } // namespace simgrid::xbt