Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4a2ae7ae442ee9d1fa63d032a50767868304473a
[simgrid.git] / include / xbt / system_error.hpp
1 /* Copyright (c) 2016. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include <cerrno>
8
9 #include <system_error>
10
11 #ifndef SIMGRID_MC_SYSTEM_ERROR_HPP
12 #define SIMGRID_MC_SYSTEM_ERROR_HPP
13
14 namespace simgrid {
15 namespace xbt {
16
17 /** A `error_category` suitable to be used with `errno`
18  *
19  *  It is not clear which error we are supposed to generate
20  *  when getting a errno:
21  *
22  *  * `system_error` clearly cannot be used for this on Windows;
23  *
24  *  * `generic_error` might not be used for non-standard `errno`.
25  *
26  *  Let's just define a function which gives us the correct
27  *  category.
28  */
29 inline
30 const std::error_category& errno_category() noexcept
31 {
32   return std::generic_category();
33 }
34
35 /** Create a `error_code` from an `errno` value
36  *
37  *  This is expected to to whatever is right to create a
38  *  `error_code` from a given `errno` value.
39  */
40 inline
41 std::error_code errno_code(int errnum)
42 {
43   return std::error_code(errnum, errno_category());
44 }
45
46 /** Create a `system_error` from an `errno` value
47  *
48  *  This is expected to to whatever is right to create a
49  *  `system_error` from a given `errno` value.
50  */
51 inline
52 std::system_error errno_error(int errnum)
53 {
54   return std::system_error(errno_code(errnum));
55 }
56
57 inline
58 std::system_error errno_error(int errnum, const char* what)
59 {
60   return std::system_error(errno_code(errnum), what);
61 }
62
63 }
64 }
65
66 #endif