File: system_error.hpp

package info (click to toggle)
simgrid 4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 39,192 kB
  • sloc: cpp: 124,913; ansic: 66,744; python: 8,560; java: 6,773; fortran: 6,079; f90: 5,123; xml: 4,587; sh: 2,194; perl: 1,436; makefile: 111; lisp: 49; javascript: 7; sed: 6
file content (85 lines) | stat: -rw-r--r-- 1,941 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* Copyright (c) 2016-2025. The SimGrid Team. All rights reserved.          */

/* This program is free software; you can redistribute it and/or modify it
 * under the terms of the license (GNU LGPL) which comes with this package. */

#include <cerrno>

#include <system_error>

#ifndef SIMGRID_MC_SYSTEM_ERROR_HPP
#define SIMGRID_MC_SYSTEM_ERROR_HPP

namespace simgrid::xbt {

/** A `error_category` suitable to be used with `errno`
 *
 *  It is not clear which error we are supposed to generate
 *  when getting an errno:
 *
 *  * `system_error` clearly cannot be used for this on Windows;
 *
 *  * `generic_error` might not be used for non-standard `errno`.
 *
 *  Let's just define a function which gives us the correct
 *  category.
 */
inline
const std::error_category& errno_category() noexcept
{
  return std::generic_category();
}

/** Create a `error_code` from an `errno` value
 *
 *  This is expected to to whatever is right to create a
 *  `error_code` from a given `errno` value.
 */
inline
std::error_code errno_code(int errnum)
{
  return std::error_code(errnum, errno_category());
}

/** Create an `error_code` from `errno` (and clear it) */
inline
std::error_code errno_code()
{
  int errnum = errno;
  errno = 0;
  return errno_code(errnum);
}

/** Create a `system_error` from an `errno` value
 *
 *  This is expected to to whatever is right to create a
 *  `system_error` from a given `errno` value.
 */
inline
std::system_error errno_error(int errnum)
{
  return std::system_error(errno_code(errnum));
}

inline
std::system_error errno_error(int errnum, const char* what)
{
  return std::system_error(errno_code(errnum), what);
}

/** Create a `system_code` from `errno` (and clear it) */
inline
std::system_error errno_error()
{
  return std::system_error(errno_code());
}

inline
std::system_error errno_error(const char* what)
{
  return std::system_error(errno_code(), what);
}

} // namespace simgrid::xbt

#endif