Browse Source

Derive hsException from std::exception and implement a reasonable what()

Michael Hansen 10 years ago
parent
commit
a8250a1899
  1. 2
      Sources/Plasma/CoreLib/CMakeLists.txt
  2. 25
      Sources/Plasma/CoreLib/hsExceptions.cpp
  3. 19
      Sources/Plasma/CoreLib/hsExceptions.h

2
Sources/Plasma/CoreLib/CMakeLists.txt

@ -14,6 +14,7 @@ set(CoreLib_SOURCES
hsBounds.cpp hsBounds.cpp
hsCpuID.cpp hsCpuID.cpp
hsCritSect.cpp hsCritSect.cpp
hsExceptions.cpp
hsExceptionStack.cpp hsExceptionStack.cpp
hsFastMath.cpp hsFastMath.cpp
hsGeometry3.cpp hsGeometry3.cpp
@ -59,6 +60,7 @@ set(CoreLib_HEADERS
hsCpuID.h hsCpuID.h
hsCritSect.h hsCritSect.h
hsExceptions.h hsExceptions.h
hsExceptionStack.h
hsFastMath.h hsFastMath.h
hsGeometry3.h hsGeometry3.h
hsHashTable.h hsHashTable.h

25
Sources/Plasma/CoreLib/hsExceptions.cpp

@ -0,0 +1,25 @@
#include "hsExceptions.h"
#include <cstring>
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;
}

19
Sources/Plasma/CoreLib/hsExceptions.h

@ -43,6 +43,7 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
#define hsExceptionDefined #define hsExceptionDefined
#include "HeadSpin.h" #include "HeadSpin.h"
#include <exception>
// #define HS_NO_EXCEPTIONS -- this will turn off execptions you might want // #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. // to do it with -D or equivalent instead of here since who knows who includes this.
@ -54,37 +55,41 @@ enum hsErrorEnum {
kNilParam_hsError, kNilParam_hsError,
kBadParam_hsError, kBadParam_hsError,
kInternal_hsError, kInternal_hsError,
kOS_hsError kOS_hsError,
hsErrorEnum_MAX
}; };
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
class hsException { class hsException : public std::exception {
public: public:
hsErrorEnum fError; hsErrorEnum fError;
long fParam; long fParam;
hsException(hsErrorEnum error, long param = 0) : fError(error), fParam(param) {} hsException(hsErrorEnum error, long param = 0) HS_NOEXCEPT
: fError(error), fParam(param) {}
const char *what() const HS_NOEXCEPT HS_OVERRIDE;
}; };
class hsNilParamException : public hsException { class hsNilParamException : public hsException {
public: public:
hsNilParamException() : hsException(kNilParam_hsError) {} hsNilParamException() HS_NOEXCEPT : hsException(kNilParam_hsError) {}
}; };
class hsBadParamException : public hsException { class hsBadParamException : public hsException {
public: public:
hsBadParamException() : hsException(kBadParam_hsError) {} hsBadParamException() HS_NOEXCEPT : hsException(kBadParam_hsError) {}
}; };
class hsInternalException : public hsException { class hsInternalException : public hsException {
public: public:
hsInternalException() : hsException(kInternal_hsError) {} hsInternalException() HS_NOEXCEPT : hsException(kInternal_hsError) {}
}; };
class hsOSException : public hsException { class hsOSException : public hsException {
public: public:
hsOSException(long error) : hsException(kOS_hsError, error) {} hsOSException(long error) HS_NOEXCEPT : hsException(kOS_hsError, error) {}
}; };
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////

Loading…
Cancel
Save