error.inl 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright 2010-2026 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx/blob/master/LICENSE
  4. */
  5. #ifndef BX_ERROR_H_HEADER_GUARD
  6. # error "Must be included from bx/error!"
  7. #endif // BX_ERROR_H_HEADER_GUARD
  8. #include <bx/debug.h>
  9. namespace bx
  10. {
  11. inline Error::Error()
  12. : m_code(0)
  13. {
  14. }
  15. inline void Error::reset()
  16. {
  17. m_code = 0;
  18. m_msg.clear();
  19. }
  20. inline void Error::setError(ErrorResult _errorResult, const StringLiteral& _msg, const Location& _location)
  21. {
  22. BX_ASSERT(0 != _errorResult.code, "Invalid ErrorResult passed to setError!");
  23. if (!isOk() )
  24. {
  25. return;
  26. }
  27. m_location = _location;
  28. m_code = _errorResult.code;
  29. m_msg = _msg;
  30. }
  31. inline bool Error::isOk() const
  32. {
  33. return 0 == m_code;
  34. }
  35. inline ErrorResult Error::get() const
  36. {
  37. ErrorResult result = { m_code };
  38. return result;
  39. }
  40. inline const StringLiteral& Error::getMessage() const
  41. {
  42. return m_msg;
  43. }
  44. inline const Location& Error::getLocation() const
  45. {
  46. return m_location;
  47. }
  48. inline bool Error::operator==(const ErrorResult& _rhs) const
  49. {
  50. return _rhs.code == m_code;
  51. }
  52. inline bool Error::operator!=(const ErrorResult& _rhs) const
  53. {
  54. return _rhs.code != m_code;
  55. }
  56. inline ErrorIgnore::operator Error*()
  57. {
  58. return this;
  59. }
  60. inline ErrorAssert::~ErrorAssert()
  61. {
  62. BX_ASSERT_LOC(getLocation(), isOk(), "ErrorAssert: 0x%08x `%S`"
  63. , get().code
  64. , &getMessage()
  65. );
  66. }
  67. inline ErrorFatal::operator Error*()
  68. {
  69. return this;
  70. }
  71. inline ErrorFatal::~ErrorFatal()
  72. {
  73. _BX_ASSERT_LOC(getLocation(), isOk(), "ErrorFatal: 0x%08x `%S`"
  74. , get().code
  75. , &getMessage()
  76. );
  77. }
  78. inline ErrorAssert::operator Error*()
  79. {
  80. return this;
  81. }
  82. inline ErrorScope::ErrorScope(Error* _err, const StringLiteral& _name)
  83. : m_err(_err)
  84. , m_name(_name)
  85. {
  86. BX_UNUSED(m_err);
  87. BX_ASSERT(NULL != _err, "_err can't be NULL");
  88. }
  89. inline ErrorScope::~ErrorScope()
  90. {
  91. if (m_name.isEmpty() )
  92. {
  93. BX_ASSERT_LOC(
  94. m_err->getLocation()
  95. , m_err->isOk()
  96. , "ErrorScope: 0x%08x `%S`"
  97. , m_err->get().code
  98. , &m_err->getMessage()
  99. );
  100. }
  101. else
  102. {
  103. BX_ASSERT_LOC(
  104. m_err->getLocation()
  105. , m_err->isOk()
  106. , "ErrorScope: %S - 0x%08x `%S`"
  107. , &m_name
  108. , m_err->get().code
  109. , &m_err->getMessage()
  110. );
  111. }
  112. }
  113. inline const StringLiteral& ErrorScope::getName() const
  114. {
  115. return m_name;
  116. }
  117. } // namespace bx