PageRenderTime 57ms CodeModel.GetById 32ms 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
Possible License(s): BSD-3-Clause, 0BSD, MPL-2.0-no-copyleft-exception
  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. #ifndef ERROR_H_
  8. #define ERROR_H_
  9. #include <errno.h>
  10. struct error {
  11. #ifdef __cplusplus
  12. public:
  13. explicit error() : _errno(0) {}
  14. explicit error(int _errno) : _errno(_errno) {}
  15. error(const error& e) = default;
  16. bool bad() const { return _errno != 0; }
  17. int get() const { return _errno; }
  18. int to_libc() const;
  19. private:
  20. #endif
  21. int _errno;
  22. };
  23. typedef struct error error;
  24. static inline error no_error()
  25. {
  26. return (error) { 0 };
  27. }
  28. static inline struct error make_error(int _errno)
  29. {
  30. return (error) { _errno };
  31. }
  32. static inline bool error_bad(error e)
  33. {
  34. #ifdef __cplusplus
  35. return e.bad();
  36. #else
  37. return e._errno != 0;
  38. #endif
  39. }
  40. static inline int error_get(error e)
  41. {
  42. #ifdef __cplusplus
  43. return e.get();
  44. #else
  45. return e._errno;
  46. #endif
  47. }
  48. inline static int error_to_libc(error e)
  49. {
  50. if (!error_bad(e)) {
  51. return 0;
  52. } else {
  53. errno = error_get(e);
  54. return -1;
  55. }
  56. }
  57. #ifdef __cplusplus
  58. inline int error::to_libc() const
  59. {
  60. return error_to_libc(*this);
  61. }
  62. #endif
  63. #endif /* ERROR_H_ */