From 9063c0c968e6218b72866f55d02d97abfdd93963 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=80=D0=B0=D0=BD=D0=B8=D0=BC=D0=B8=D1=80=20=D0=9A?= =?UTF-8?q?=D0=B0=D1=80=D0=B0=D1=9F=D0=B8=D1=9B?= Date: Fri, 19 Jul 2024 21:57:10 -0700 Subject: [PATCH] Cleanup. --- include/bx/error.h | 18 +++++++++++------- include/bx/inline/error.inl | 32 ++++++++++++++++++++++---------- include/bx/inline/string.inl | 11 +++++++++++ include/bx/string.h | 7 +++++++ 4 files changed, 51 insertions(+), 17 deletions(-) diff --git a/include/bx/error.h b/include/bx/error.h index ab9fa86f6..7ccd2006b 100644 --- a/include/bx/error.h +++ b/include/bx/error.h @@ -48,7 +48,7 @@ namespace bx void reset(); /// - void setError(ErrorResult _errorResult, const StringView& _msg); + void setError(ErrorResult _errorResult, const StringLiteral& _msg, const Location& _location = Location::current() ); /// bool isOk() const; @@ -57,7 +57,10 @@ namespace bx ErrorResult get() const; /// - const StringView& getMessage() const; + const StringLiteral& getMessage() const; + + /// + const Location& getLocation() const; /// bool operator==(const ErrorResult& _rhs) const; @@ -66,8 +69,9 @@ namespace bx bool operator!=(const ErrorResult& _rhs) const; private: - StringView m_msg; - uint32_t m_code; + Location m_location; + StringLiteral m_msg; + uint32_t m_code; }; /// Do nothing even if error is set. @@ -110,17 +114,17 @@ namespace bx public: /// - ErrorScope(Error* _err, const StringView& _name); + ErrorScope(Error* _err, const StringLiteral& _name); /// ~ErrorScope(); /// - const StringView& getName() const; + const StringLiteral& getName() const; private: Error* m_err; - const StringView m_name; + const StringLiteral m_name; }; } // namespace bx diff --git a/include/bx/inline/error.inl b/include/bx/inline/error.inl index 176a60bd5..eac53c697 100644 --- a/include/bx/inline/error.inl +++ b/include/bx/inline/error.inl @@ -22,7 +22,7 @@ namespace bx m_msg.clear(); } - inline void Error::setError(ErrorResult _errorResult, const StringView& _msg) + inline void Error::setError(ErrorResult _errorResult, const StringLiteral& _msg, const Location& _location) { BX_ASSERT(0 != _errorResult.code, "Invalid ErrorResult passed to setError!"); @@ -31,8 +31,9 @@ namespace bx return; } - m_code = _errorResult.code; - m_msg = _msg; + m_location = _location; + m_code = _errorResult.code; + m_msg = _msg; } inline bool Error::isOk() const @@ -46,11 +47,16 @@ namespace bx return result; } - inline const StringView& Error::getMessage() const + inline const StringLiteral& Error::getMessage() const { return m_msg; } + inline const Location& Error::getLocation() const + { + return m_location; + } + inline bool Error::operator==(const ErrorResult& _rhs) const { return _rhs.code == m_code; @@ -68,7 +74,7 @@ namespace bx inline ErrorAssert::~ErrorAssert() { - BX_ASSERT(isOk(), "ErrorAssert: 0x%08x `%S`" + BX_ASSERT_LOC(getLocation(), isOk(), "ErrorAssert: 0x%08x `%S`" , get().code , &getMessage() ); @@ -81,7 +87,7 @@ namespace bx inline ErrorFatal::~ErrorFatal() { - _BX_ASSERT(isOk(), "ErrorFatal: 0x%08x `%S`" + _BX_ASSERT_LOC(getLocation(), isOk(), "ErrorFatal: 0x%08x `%S`" , get().code , &getMessage() ); @@ -92,7 +98,7 @@ namespace bx return this; } - inline ErrorScope::ErrorScope(Error* _err, const StringView& _name) + inline ErrorScope::ErrorScope(Error* _err, const StringLiteral& _name) : m_err(_err) , m_name(_name) { @@ -104,14 +110,20 @@ namespace bx { if (m_name.isEmpty() ) { - BX_ASSERT(m_err->isOk(), "Error: 0x%08x `%S`" + BX_ASSERT_LOC( + m_err->getLocation() + , m_err->isOk() + , "ErrorScope: 0x%08x `%S`" , m_err->get().code , &m_err->getMessage() ); } else { - BX_ASSERT(m_err->isOk(), "Error: %S - 0x%08x `%S`" + BX_ASSERT_LOC( + m_err->getLocation() + , m_err->isOk() + , "ErrorScope: %S - 0x%08x `%S`" , &m_name , m_err->get().code , &m_err->getMessage() @@ -119,7 +131,7 @@ namespace bx } } - inline const StringView& ErrorScope::getName() const + inline const StringLiteral& ErrorScope::getName() const { return m_name; } diff --git a/include/bx/inline/string.inl b/include/bx/inline/string.inl index 643a68467..bc755d375 100644 --- a/include/bx/inline/string.inl +++ b/include/bx/inline/string.inl @@ -61,6 +61,17 @@ namespace bx return m_ptr; } + inline void StringLiteral::clear() + { + m_ptr = ""; + m_len = 0; + } + + inline bool StringLiteral::isEmpty() const + { + return 0 == m_len; + } + inline StringView::StringView() { clear(); diff --git a/include/bx/string.h b/include/bx/string.h index 9bae38921..7b32e4ad9 100644 --- a/include/bx/string.h +++ b/include/bx/string.h @@ -42,6 +42,13 @@ namespace bx /// constexpr const char* getCPtr() const; + /// + void clear(); + + /// Returns `true` if string is empty. + /// + bool isEmpty() const; + private: const char* m_ptr; int32_t m_len;