error.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright 2010-2018 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. BX_TRACE("Error %d: %s", _result.code, "" _msg); \
  11. (_ptr)->setError(_result, "" _msg); \
  12. BX_MACRO_BLOCK_END
  13. #define BX_ERROR_USE_TEMP_WHEN_NULL(_ptr) \
  14. const bx::Error tmpError; /* It should not be used directly! */ \
  15. _ptr = NULL == _ptr ? const_cast<bx::Error*>(&tmpError) : _ptr
  16. #define BX_ERROR_SCOPE(_ptr) \
  17. BX_ERROR_USE_TEMP_WHEN_NULL(_ptr); \
  18. bx::ErrorScope bxErrorScope(const_cast<bx::Error*>(&tmpError) )
  19. #define BX_ERROR_RESULT(_err, _code) \
  20. BX_STATIC_ASSERT(_code != 0, "ErrorCode 0 is reserved!"); \
  21. static const bx::ErrorResult _err = { _code }
  22. namespace bx
  23. {
  24. ///
  25. struct ErrorResult
  26. {
  27. uint32_t code;
  28. };
  29. ///
  30. class Error
  31. {
  32. BX_CLASS(Error
  33. , NO_COPY
  34. , NO_ASSIGNMENT
  35. );
  36. public:
  37. ///
  38. Error();
  39. ///
  40. void reset();
  41. ///
  42. void setError(ErrorResult _errorResult, const StringView& _msg);
  43. ///
  44. bool isOk() const;
  45. ///
  46. ErrorResult get() const;
  47. ///
  48. const StringView& getMessage() const;
  49. ///
  50. bool operator==(const ErrorResult& _rhs) const;
  51. ///
  52. bool operator!=(const ErrorResult& _rhs) const;
  53. private:
  54. StringView m_msg;
  55. uint32_t m_code;
  56. };
  57. ///
  58. class ErrorScope
  59. {
  60. BX_CLASS(ErrorScope
  61. , NO_COPY
  62. , NO_ASSIGNMENT
  63. );
  64. public:
  65. ///
  66. ErrorScope(Error* _err);
  67. ///
  68. ~ErrorScope();
  69. private:
  70. Error* m_err;
  71. };
  72. } // namespace bx
  73. #include "inline/error.inl"
  74. #endif // BX_ERROR_H_HEADER_GUARD