From a8250a1899ce9a4728bfe4b8ae485f21bfc80447 Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Sun, 2 Aug 2015 19:07:16 -0700 Subject: [PATCH] Derive hsException from std::exception and implement a reasonable what() --- Sources/Plasma/CoreLib/CMakeLists.txt | 2 ++ Sources/Plasma/CoreLib/hsExceptionStack.cpp | 2 +- Sources/Plasma/CoreLib/hsExceptions.cpp | 25 +++++++++++++++++++++ Sources/Plasma/CoreLib/hsExceptions.h | 23 +++++++++++-------- 4 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 Sources/Plasma/CoreLib/hsExceptions.cpp diff --git a/Sources/Plasma/CoreLib/CMakeLists.txt b/Sources/Plasma/CoreLib/CMakeLists.txt index 21abff68..759e6384 100644 --- a/Sources/Plasma/CoreLib/CMakeLists.txt +++ b/Sources/Plasma/CoreLib/CMakeLists.txt @@ -14,6 +14,7 @@ set(CoreLib_SOURCES hsBounds.cpp hsCpuID.cpp hsCritSect.cpp + hsExceptions.cpp hsExceptionStack.cpp hsFastMath.cpp hsGeometry3.cpp @@ -59,6 +60,7 @@ set(CoreLib_HEADERS hsCpuID.h hsCritSect.h hsExceptions.h + hsExceptionStack.h hsFastMath.h hsGeometry3.h hsHashTable.h diff --git a/Sources/Plasma/CoreLib/hsExceptionStack.cpp b/Sources/Plasma/CoreLib/hsExceptionStack.cpp index 4e9d47b7..44eadd19 100644 --- a/Sources/Plasma/CoreLib/hsExceptionStack.cpp +++ b/Sources/Plasma/CoreLib/hsExceptionStack.cpp @@ -48,4 +48,4 @@ void hsExceptionStack::FreeInstance() { delete fExceptionStack; fExceptionStack = nil; -} \ No newline at end of file +} diff --git a/Sources/Plasma/CoreLib/hsExceptions.cpp b/Sources/Plasma/CoreLib/hsExceptions.cpp new file mode 100644 index 00000000..bb48e8b1 --- /dev/null +++ b/Sources/Plasma/CoreLib/hsExceptions.cpp @@ -0,0 +1,25 @@ +#include "hsExceptions.h" + +#include + +const char *hsException::what() const HS_NOEXCEPT +{ + char buffer[64]; + + static const char *kErrorNames[] = { + "kNo_hsError", + "kNilParam_hsError", + "kBadParam_hsError", + "kInternal_hsError", + "kOS_hsError" + }; + static_assert(arrsize(kErrorNames) == hsErrorEnum_MAX, + "kErrorNames not in sync with hsErrorEnum"); + + if (fError >= 0 && fError < hsErrorEnum_MAX) + snprintf(buffer, arrsize(buffer), "%s (%ld)", kErrorNames[fError], fParam); + else + snprintf(buffer, arrsize(buffer), "Unknown hsException error %d (%ld)", fError, fParam); + + return buffer; +} diff --git a/Sources/Plasma/CoreLib/hsExceptions.h b/Sources/Plasma/CoreLib/hsExceptions.h index 2ce7f5f5..134afdce 100644 --- a/Sources/Plasma/CoreLib/hsExceptions.h +++ b/Sources/Plasma/CoreLib/hsExceptions.h @@ -43,6 +43,7 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #define hsExceptionDefined #include "HeadSpin.h" +#include // #define HS_NO_EXCEPTIONS -- this will turn off execptions you might want // to do it with -D or equivalent instead of here since who knows who includes this. @@ -54,37 +55,41 @@ enum hsErrorEnum { kNilParam_hsError, kBadParam_hsError, kInternal_hsError, - kOS_hsError + kOS_hsError, + hsErrorEnum_MAX }; ////////////////////////////////////////////////////////////////////////////// -class hsException { +class hsException : public std::exception { public: hsErrorEnum fError; - long fParam; - - hsException(hsErrorEnum error, long param = 0) : fError(error), fParam(param) {} + long fParam; + + hsException(hsErrorEnum error, long param = 0) HS_NOEXCEPT + : fError(error), fParam(param) {} + + const char *what() const HS_NOEXCEPT HS_OVERRIDE; }; class hsNilParamException : public hsException { public: - hsNilParamException() : hsException(kNilParam_hsError) {} + hsNilParamException() HS_NOEXCEPT : hsException(kNilParam_hsError) {} }; class hsBadParamException : public hsException { public: - hsBadParamException() : hsException(kBadParam_hsError) {} + hsBadParamException() HS_NOEXCEPT : hsException(kBadParam_hsError) {} }; class hsInternalException : public hsException { public: - hsInternalException() : hsException(kInternal_hsError) {} + hsInternalException() HS_NOEXCEPT : hsException(kInternal_hsError) {} }; class hsOSException : public hsException { public: - hsOSException(long error) : hsException(kOS_hsError, error) {} + hsOSException(long error) HS_NOEXCEPT : hsException(kOS_hsError, error) {} }; /////////////////////////////////////////////////////////////////////////////////