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. 2
      Sources/Plasma/CoreLib/hsExceptionStack.cpp
  3. 25
      Sources/Plasma/CoreLib/hsExceptions.cpp
  4. 23
      Sources/Plasma/CoreLib/hsExceptions.h

2
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

2
Sources/Plasma/CoreLib/hsExceptionStack.cpp

@ -48,4 +48,4 @@ void hsExceptionStack::FreeInstance()
{
delete fExceptionStack;
fExceptionStack = nil;
}
}

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;
}

23
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 <exception>
// #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) {}
};
/////////////////////////////////////////////////////////////////////////////////

Loading…
Cancel
Save