Răsfoiți Sursa

Added ErrorScope name.

Бранимир Караџић 4 ani în urmă
părinte
comite
51f25ba638
2 a modificat fișierele cu 43 adăugiri și 16 ștergeri
  1. 18 14
      include/bx/error.h
  2. 25 2
      include/bx/inline/error.inl

+ 18 - 14
include/bx/error.h

@@ -8,22 +8,22 @@
 
 
 #include "string.h"
 #include "string.h"
 
 
-#define BX_ERROR_SET(_ptr, _result, _msg)            \
-			BX_MACRO_BLOCK_BEGIN                     \
-				(_ptr)->setError(_result,  "" _msg); \
-			BX_MACRO_BLOCK_END
+#define BX_ERROR_SET(_ptr, _result, _msg)    \
+	BX_MACRO_BLOCK_BEGIN                     \
+		(_ptr)->setError(_result,  "" _msg); \
+	BX_MACRO_BLOCK_END
 
 
-#define BX_ERROR_USE_TEMP_WHEN_NULL(_ptr) \
-			const bx::Error tmpError; /* It should not be used directly! */ \
-			_ptr = NULL == _ptr ? const_cast<bx::Error*>(&tmpError) : _ptr
+#define BX_ERROR_USE_TEMP_WHEN_NULL(_ptr)                           \
+	const bx::Error tmpError; /* It should not be used directly! */ \
+	_ptr = NULL == _ptr ? const_cast<bx::Error*>(&tmpError) : _ptr
 
 
-#define BX_ERROR_SCOPE(_ptr)                                              \
-			BX_ERROR_USE_TEMP_WHEN_NULL(_ptr);                            \
-			bx::ErrorScope bxErrorScope(const_cast<bx::Error*>(&tmpError) )
+#define BX_ERROR_SCOPE(_ptr, ...)                                  \
+	BX_ERROR_USE_TEMP_WHEN_NULL(_ptr);                             \
+	bx::ErrorScope bxErrorScope(const_cast<bx::Error*>(&tmpError), "" __VA_ARGS__)
 
 
-#define BX_ERROR_RESULT(_err, _code)                                  \
-			BX_STATIC_ASSERT(_code != 0, "ErrorCode 0 is reserved!"); \
-			static constexpr bx::ErrorResult _err = { _code }
+#define BX_ERROR_RESULT(_err, _code)                          \
+	BX_STATIC_ASSERT(_code != 0, "ErrorCode 0 is reserved!"); \
+	static constexpr bx::ErrorResult _err = { _code }
 
 
 namespace bx
 namespace bx
 {
 {
@@ -81,13 +81,17 @@ namespace bx
 
 
 	public:
 	public:
 		///
 		///
-		ErrorScope(Error* _err);
+		ErrorScope(Error* _err, const StringView& _name);
 
 
 		///
 		///
 		~ErrorScope();
 		~ErrorScope();
 
 
+		///
+		const StringView& getName() const;
+
 	private:
 	private:
 		Error* m_err;
 		Error* m_err;
+		const StringView m_name;
 	};
 	};
 
 
 } // namespace bx
 } // namespace bx

+ 25 - 2
include/bx/inline/error.inl

@@ -59,15 +59,38 @@ namespace bx
 		return _rhs.code != m_code;
 		return _rhs.code != m_code;
 	}
 	}
 
 
-	inline ErrorScope::ErrorScope(Error* _err)
+	inline ErrorScope::ErrorScope(Error* _err, const StringView& _name)
 		: m_err(_err)
 		: m_err(_err)
+		, m_name(_name)
 	{
 	{
 		BX_ASSERT(NULL != _err, "_err can't be NULL");
 		BX_ASSERT(NULL != _err, "_err can't be NULL");
 	}
 	}
 
 
 	inline ErrorScope::~ErrorScope()
 	inline ErrorScope::~ErrorScope()
 	{
 	{
-		BX_ASSERT(m_err->isOk(), "Error: %d", m_err->get().code);
+		if (m_name.isEmpty() )
+		{
+			BX_ASSERT(m_err->isOk(), "Error: 0x%08x `%.*s`"
+				, m_err->get().code
+				, m_err->getMessage().getLength()
+				, m_err->getMessage().getPtr()
+				);
+		}
+		else
+		{
+			BX_ASSERT(m_err->isOk(), "Error: %.*s - 0x%08x `%.*s`"
+				, m_name.getLength()
+				, m_name.getPtr()
+				, m_err->get().code
+				, m_err->getMessage().getLength()
+				, m_err->getMessage().getPtr()
+				);
+		}
+	}
+
+	inline const StringView& ErrorScope::getName() const
+	{
+		return m_name;
 	}
 	}
 
 
 } // namespace bx
 } // namespace bx