PageRenderTime 15ms CodeModel.GetById 10ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/include/osv/error.h

https://github.com/otokunaga2/osv
C++ Header | 76 lines | 57 code | 13 blank | 6 comment | 4 complexity | 70abb7005342a80c6b0aa8c8a331a1ea MD5 | raw file
 1/*
 2 * Copyright (C) 2013 Cloudius Systems, Ltd.
 3 *
 4 * This work is open source software, licensed under the terms of the
 5 * BSD license as described in the LICENSE file in the top-level directory.
 6 */
 7
 8#ifndef ERROR_H_
 9#define ERROR_H_
10
11#include <errno.h>
12
13struct error {
14#ifdef __cplusplus
15public:
16    explicit error() : _errno(0) {}
17    explicit error(int _errno) : _errno(_errno) {}
18    error(const error& e) = default;
19    bool bad() const { return _errno != 0; }
20    int get() const { return _errno; }
21    int to_libc() const;
22private:
23#endif
24    int _errno;
25};
26
27typedef struct error error;
28
29static inline error no_error()
30{
31    return (error) { 0 };
32}
33
34static inline struct error make_error(int _errno)
35{
36    return (error) { _errno };
37}
38
39static inline bool error_bad(error e)
40{
41#ifdef __cplusplus
42    return e.bad();
43#else
44    return e._errno != 0;
45#endif
46}
47
48static inline int error_get(error e)
49{
50#ifdef __cplusplus
51    return e.get();
52#else
53    return e._errno;
54#endif
55}
56
57inline static int error_to_libc(error e)
58{
59    if (!error_bad(e)) {
60        return 0;
61    } else {
62        errno = error_get(e);
63        return -1;
64    }
65}
66
67#ifdef __cplusplus
68
69inline int error::to_libc() const
70{
71    return error_to_libc(*this);
72}
73
74#endif
75
76#endif /* ERROR_H_ */