Skip to content

Commit

Permalink
Changed __debugbreak() to C++ standard exceptions
Browse files Browse the repository at this point in the history
Added static assertions for type sizes
  • Loading branch information
marcizhu committed Apr 1, 2017
1 parent 8c221ef commit e774195
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
6 changes: 3 additions & 3 deletions Cereal/Cereal/src/Database.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ namespace Cereal {
}

default:
__debugbreak(); break;
throw new std::invalid_argument("The version is not valid!"); break;
}
}

Expand Down Expand Up @@ -101,7 +101,7 @@ namespace Cereal {
break;

default:
__debugbreak(); break;
throw new std::invalid_argument("The version is not valid!"); break;
}

return true;
Expand All @@ -117,7 +117,7 @@ namespace Cereal {
ret += sizeof(short) + name.length() + sizeof(int) + sizeof(short); break;

default:
__debugbreak(); break; // Invalid version
throw new std::invalid_argument("The version is not valid!"); break; // Invalid version
}

for (const Object* obj : objects)
Expand Down
2 changes: 1 addition & 1 deletion Cereal/Cereal/src/Field.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ namespace Cereal {
case DataType::DATA_FLOAT: setData<float>(dataType, buffer.readBytes<float>()); break;
case DataType::DATA_DOUBLE: setData<double>(dataType, buffer.readBytes<double>()); break;
case DataType::DATA_STRING: setData<std::string>(dataType, buffer.readBytes<std::string>()); break;
default: __debugbreak(); break;
default: throw new std::invalid_argument("The data type is not valid!"); break;
}
}

Expand Down
2 changes: 1 addition & 1 deletion Cereal/Cereal/src/Header.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ namespace Cereal {
buffer.writeBytes<unsigned short>(MAGIC_NUMBER);
buffer.writeBytes<byte>((byte)databases.size());

unsigned int offset = sizeof(short) + sizeof(byte) + (sizeof(unsigned int) * databases.size());
unsigned int offset = sizeof(unsigned short) + sizeof(byte) + (sizeof(unsigned int) * databases.size());

for (unsigned int i = 0; i < databases.size(); i++)
{
Expand Down
16 changes: 16 additions & 0 deletions Cereal/Cereal/src/Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ namespace Cereal {

typedef unsigned char byte;

static_assert(sizeof(byte) == 1, "Invalid byte size!");

static_assert(sizeof(char) == 1, "Invalid char size!");
static_assert(sizeof(short) == 2, "Invalid short size!");
static_assert(sizeof(int) == 4, "Invalid int size!");
static_assert(sizeof(long) == 4, "Invalid long size!");
static_assert(sizeof(float) == 4, "Invalid float size!");
static_assert(sizeof(double) == 8, "Invalid double size!");

static_assert(sizeof(unsigned char) == 1, "Invalid unsigned char size!");
static_assert(sizeof(unsigned short) == 2, "Invalid unsigned short size!");
static_assert(sizeof(unsigned int) == 4, "Invalid unsigned int size!");
static_assert(sizeof(unsigned long) == 4, "Invalid unsigned long size!");
static_assert(sizeof(unsigned long long) == 8, "Invalid unsigned long long size!");


enum Version
{
VERSION_1_0 = 0x0100, // first byte = major version, second = minor (revision)
Expand Down

0 comments on commit e774195

Please sign in to comment.