Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
snake_case simix/blocking_simcall.hpp
[simgrid.git] / include / xbt / system_error.hpp
1 /* Copyright (c) 2016-2018. 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 an `error_code` from `errno` (and clear it) */
47 inline
48 std::error_code errno_code()
49 {
50   int errnum = errno;
51   errno = 0;
52   return errno_code(errnum);
53 }
54
55 /** Create a `system_error` from an `errno` value
56  *
57  *  This is expected to to whatever is right to create a
58  *  `system_error` from a given `errno` value.
59  */
60 inline
61 std::system_error errno_error(int errnum)
62 {
63   return std::system_error(errno_code(errnum));
64 }
65
66 inline
67 std::system_error errno_error(int errnum, const char* what)
68 {
69   return std::system_error(errno_code(errnum), what);
70 }
71
72 /** Create a `system_code` from `errno` (and clear it) */
73 inline
74 std::system_error errno_error()
75 {
76   return std::system_error(errno_code());
77 }
78
79 inline
80 std::system_error errno_error(const char* what)
81 {
82   return std::system_error(errno_code(), what);
83 }
84
85 }
86 }
87
88 #endif