error.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright 2010-2021 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
  4. */
  5. #ifndef BX_ERROR_H_HEADER_GUARD
  6. #define BX_ERROR_H_HEADER_GUARD
  7. #include "string.h"
  8. #define BX_ERROR_SET(_ptr, _result, _msg) \
  9. BX_MACRO_BLOCK_BEGIN \
  10. (_ptr)->setError(_result, "" _msg); \
  11. BX_MACRO_BLOCK_END
  12. #define BX_ERROR_USE_TEMP_WHEN_NULL(_ptr) \
  13. const bx::Error tmpError; /* It should not be used directly! */ \
  14. _ptr = NULL == _ptr ? const_cast<bx::Error*>(&tmpError) : _ptr
  15. #define BX_ERROR_SCOPE(_ptr, ...) \
  16. BX_ERROR_USE_TEMP_WHEN_NULL(_ptr); \
  17. bx::ErrorScope bxErrorScope(const_cast<bx::Error*>(&tmpError), "" __VA_ARGS__)
  18. #define BX_ERROR_RESULT(_err, _code) \
  19. BX_STATIC_ASSERT(_code != 0, "ErrorCode 0 is reserved!"); \
  20. static constexpr bx::ErrorResult _err = { _code }
  21. namespace bx
  22. {
  23. ///
  24. struct ErrorResult
  25. {
  26. uint32_t code;
  27. };
  28. ///
  29. class Error
  30. {
  31. BX_CLASS(Error
  32. , NO_COPY
  33. , NO_ASSIGNMENT
  34. );
  35. public:
  36. ///
  37. Error();
  38. ///
  39. void reset();
  40. ///
  41. void setError(ErrorResult _errorResult, const StringView& _msg);
  42. ///
  43. bool isOk() const;
  44. ///
  45. ErrorResult get() const;
  46. ///
  47. const StringView& getMessage() const;
  48. ///
  49. bool operator==(const ErrorResult& _rhs) const;
  50. ///
  51. bool operator!=(const ErrorResult& _rhs) const;
  52. private:
  53. StringView m_msg;
  54. uint32_t m_code;
  55. };
  56. ///
  57. class ErrorScope
  58. {
  59. BX_CLASS(ErrorScope
  60. , NO_COPY
  61. , NO_ASSIGNMENT
  62. );
  63. public:
  64. ///
  65. ErrorScope(Error* _err, const StringView& _name);
  66. ///
  67. ~ErrorScope();
  68. ///
  69. const StringView& getName() const;
  70. private:
  71. Error* m_err;
  72. const StringView m_name;
  73. };
  74. } // namespace bx
  75. #include "inline/error.inl"
  76. #endif // BX_ERROR_H_HEADER_GUARD