Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception stuff #511

Merged
merged 8 commits into from
Aug 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Sources/Plasma/Apps/plPageOptimizer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ int main(int argc, char* argv[])
plResManager* resMgr = new plResManager;
hsgResMgr::Init(resMgr);
#ifndef _DEBUG
} catch (std::exception &e) {
printf(" ***crashed on init: %s\n", e.what());
return 2;
} catch (...) {
puts(" ***crashed on init");
return 2;
Expand All @@ -75,6 +78,10 @@ int main(int argc, char* argv[])
optimizer.Optimize();
}
#ifndef _DEBUG
catch (std::exception &e) {
printf(" ***crashed on optimizing: %s\n", e.what());
return 2;
}
catch (...) {
puts(" ***crashed on optimizing");
return 2;
Expand All @@ -92,6 +99,9 @@ int main(int argc, char* argv[])

hsgResMgr::Shutdown();
#ifndef _DEBUG
} catch (std::exception &e) {
printf(" ***crashed on shutdown: %s\n", e.what());
return 2;
} catch (...) {
puts(" ***crashed on shutdown");
return 2;
Expand Down
2 changes: 2 additions & 0 deletions Sources/Plasma/CoreLib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ set(CoreLib_SOURCES
hsBounds.cpp
hsCpuID.cpp
hsCritSect.cpp
hsExceptions.cpp
hsExceptionStack.cpp
hsFastMath.cpp
hsGeometry3.cpp
Expand Down Expand Up @@ -59,6 +60,7 @@ set(CoreLib_HEADERS
hsCpuID.h
hsCritSect.h
hsExceptions.h
hsExceptionStack.h
hsFastMath.h
hsGeometry3.h
hsHashTable.h
Expand Down
16 changes: 16 additions & 0 deletions Sources/Plasma/CoreLib/HeadSpin.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,22 @@ typedef int32_t hsError;
# define hsDeprecated(message)
#endif

#ifdef HAVE_OVERRIDE
# define HS_OVERRIDE override
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been around since MSVC++2012. Do we actually have to test for it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've specified our minimum MSVC compiler, but I don't know what we're targetting for GCC now that it has better support. IIRC, GCC only added override/final in version 4.7. I'm fine with making GCC 4.7 the minimum version though and cleaning this up :)

FYI, this minimum would exclude Ubuntu 12.04 LTS -- a distro I've usually still tried to support with libhsplasma.

# define HS_FINAL final
#else
# define HS_OVERRIDE
# define HS_FINAL
#endif

#ifdef HAVE_NOEXCEPT
# define HS_NOEXCEPT noexcept
# define HS_NOEXCEPT_IF(cond) noexcept(cond)
#else
# define HS_NOEXCEPT throw()
# define HS_NOEXCEPT_IF(cond)
#endif

//======================================
// Endian swap funcitions
//======================================
Expand Down
2 changes: 1 addition & 1 deletion Sources/Plasma/CoreLib/hsExceptionStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ void hsExceptionStack::FreeInstance()
{
delete fExceptionStack;
fExceptionStack = nil;
}
}
22 changes: 22 additions & 0 deletions Sources/Plasma/CoreLib/hsExceptions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "hsExceptions.h"

#include <cstring>

hsException::hsException(hsErrorEnum error, long param) HS_NOEXCEPT
: fError(error), fParam(param)
{
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(fWhat, arrsize(fWhat), "%s (%ld)", kErrorNames[fError], fParam);
else
snprintf(fWhat, arrsize(fWhat), "Unknown hsException error %d (%ld)", fError, fParam);
}
82 changes: 13 additions & 69 deletions Sources/Plasma/CoreLib/hsExceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,64 +43,52 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
#define hsExceptionDefined

#include "HeadSpin.h"

// #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.


#include <exception>

enum hsErrorEnum {
kNo_hsError,
kBadAlloc_hsError,
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;
char fWhat[64];

class hsBadAllocException : public hsException {
public:
hsBadAllocException() : hsException(kBadAlloc_hsError) {}
hsException(hsErrorEnum error, long param = 0) HS_NOEXCEPT;
const char *what() const HS_NOEXCEPT HS_OVERRIDE { return fWhat; }
};

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

/////////////////////////////////////////////////////////////////////////////////

#ifndef HS_NO_EXCEPTIONS
#define hsThrow(a) {hsAssert(0,#a);throw a;}
#define hsCatch(a) catch (a)
#define hsCatch2(a,b) catch (a b)

#define hsTry try
#define hsThrow(a) { hsAssert(0,#a); throw a; }

inline void hsThrowIfNilParam(const void* p)
{
Expand Down Expand Up @@ -165,48 +153,4 @@ inline void hsThrowIfFalse(bool condition, const char message[])
}
}

#else
#define hsThrow(a) {hsAssert(0,#a);}
#define hsCatch(a) if(0)
#define hsCatch2(a,b) if(0)
#define hsTry

inline void hsThrowIfNilParam(const void* p)
{
hsAssert(p!=nil,"hsThrowIfNilParam");
}

inline void hsThrowIfBadParam(bool trueIfBadParam)
{
hsAssert(!trueIfBadParam,"hsThrowIfBadParam");
}

inline void hsThrowIfOSErr(long osErr)
{
hsAssert(osErr==0,"hsThrowIfOSErr");
}

inline void hsThrowIfTrue(bool condition)
{
hsAssert(!condition,"hsThrowIfTrue");
}

inline void hsThrowIfFalse(bool condition)
{
hsAssert(condition,"hsThrowIfFalse");
}

inline void hsThrowIfTrue(bool condition, const char message[])
{
hsAssert(!condition,message);
}

inline void hsThrowIfFalse(bool condition, const char message[])
{
hsAssert(condition,message);
}


#endif

#endif
51 changes: 0 additions & 51 deletions Sources/Plasma/CoreLib/hsMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,44 +110,6 @@ void HSMemory::Clear(void* m, uint32_t byteLen)

//////////////////////////////////////////////////////////////////////////////////////

#if 0
template <class T> T* hsSoftNew(T*& obj)
{
try {
obj = new T;
}
catch (...) {
obj = nil;
}
return obj;
}

inline template <class T> T* hsSoftNew(T*& obj, unsigned count)
{
try {
obj = new T[count];
}
catch (...) {
obj = nil;
}
return obj;
}
#endif

void* HSMemory::SoftNew(uint32_t size)
{
uint32_t* p;

hsTry {
p = new uint32_t[(size + 3) >> 2];
} hsCatch(...) {
p = nil;
}
return p;
}

//////////////////////////////////////////////////////////////////////////////////////

struct hsPrivateChunk {
hsPrivateChunk* fNext;
char* fAvailableAddr;
Expand Down Expand Up @@ -222,19 +184,6 @@ void* hsChunkAllocator::Allocate(uint32_t size, const void* data)
return addr;
}

void* hsChunkAllocator::SoftAllocate(uint32_t size, const void* data)
{
void* addr;

hsTry {
addr = this->Allocate(size, data);
}
hsCatch(...) {
addr = nil;
}
return addr;
}

//////////////////////////////////////////////////////////////////////////////////////

struct hsAppenderHead {
Expand Down
5 changes: 1 addition & 4 deletions Sources/Plasma/CoreLib/hsMemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ class HSMemory {
static void* New(uint32_t size);
static void Delete(void* block);
static void* Copy(uint32_t length, const void* source);

static void* SoftNew(uint32_t size); // returns nil if can't allocate
};

///////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -110,8 +108,7 @@ class hsChunkAllocator {

void Reset();
void SetChunkSize(uint32_t size);
void* Allocate(uint32_t size, const void* data = nil); // throws if fails
void* SoftAllocate(uint32_t size, const void* data = nil); // returns nil if fails
void* Allocate(uint32_t size, const void* data = nil);
};

///////////////////////////////////////////////////////////////////////////////////////////
Expand Down
14 changes: 7 additions & 7 deletions Sources/Plasma/CoreLib/plString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,23 +489,23 @@ plUnicodeBuffer plString::GetUnicodeArray() const
return result;
}

int plString::ToInt(int base) const
int plString::ToInt(int base) const HS_NOEXCEPT
{
return static_cast<int>(strtol(c_str(), nullptr, base));
}

unsigned int plString::ToUInt(int base) const
unsigned int plString::ToUInt(int base) const HS_NOEXCEPT
{
return static_cast<unsigned int>(strtoul(c_str(), nullptr, base));
}

float plString::ToFloat() const
float plString::ToFloat() const HS_NOEXCEPT
{
// strtof is C99, which MS doesn't support...
return (float)strtod(c_str(), nullptr);
}

double plString::ToDouble() const
double plString::ToDouble() const HS_NOEXCEPT
{
return strtod(c_str(), nullptr);
}
Expand Down Expand Up @@ -550,7 +550,7 @@ plString plString::IFormat(const char *fmt, va_list vptr)
return plString::FromUtf8(buffer, chars);
}

ssize_t plString::Find(char ch, CaseSensitivity sense) const
ssize_t plString::Find(char ch, CaseSensitivity sense) const HS_NOEXCEPT
{
if (sense == kCaseSensitive) {
const char *cp = strchr(c_str(), ch);
Expand All @@ -566,7 +566,7 @@ ssize_t plString::Find(char ch, CaseSensitivity sense) const
}
}

ssize_t plString::FindLast(char ch, CaseSensitivity sense) const
ssize_t plString::FindLast(char ch, CaseSensitivity sense) const HS_NOEXCEPT
{
if (IsEmpty())
return -1;
Expand All @@ -586,7 +586,7 @@ ssize_t plString::FindLast(char ch, CaseSensitivity sense) const
}
}

ssize_t plString::Find(const char *str, CaseSensitivity sense) const
ssize_t plString::Find(const char *str, CaseSensitivity sense) const HS_NOEXCEPT
{
if (!str || !str[0])
return -1;
Expand Down
Loading