1 /* Copyright (c) 2016-2021. 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
17 /** A `error_category` suitable to be used with `errno`
19 * It is not clear which error we are supposed to generate
20 * when getting an errno:
22 * * `system_error` clearly cannot be used for this on Windows;
24 * * `generic_error` might not be used for non-standard `errno`.
26 * Let's just define a function which gives us the correct
30 const std::error_category& errno_category() noexcept
32 return std::generic_category();
35 /** Create a `error_code` from an `errno` value
37 * This is expected to to whatever is right to create a
38 * `error_code` from a given `errno` value.
41 std::error_code errno_code(int errnum)
43 return std::error_code(errnum, errno_category());
46 /** Create an `error_code` from `errno` (and clear it) */
48 std::error_code errno_code()
52 return errno_code(errnum);
55 /** Create a `system_error` from an `errno` value
57 * This is expected to to whatever is right to create a
58 * `system_error` from a given `errno` value.
61 std::system_error errno_error(int errnum)
63 return std::system_error(errno_code(errnum));
67 std::system_error errno_error(int errnum, const char* what)
69 return std::system_error(errno_code(errnum), what);
72 /** Create a `system_code` from `errno` (and clear it) */
74 std::system_error errno_error()
76 return std::system_error(errno_code());
80 std::system_error errno_error(const char* what)
82 return std::system_error(errno_code(), what);