verify_types: throw on failure

In release builds, we would like a verify_u32 (etc) failure to be more
than just an assertion.
This commit is contained in:
Justin Viiret 2017-04-26 17:22:22 +10:00 committed by Matthew Barr
parent 097d73c7ff
commit 16a00074c6

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2015, Intel Corporation * Copyright (c) 2015-2017, Intel Corporation
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
@ -30,45 +30,50 @@
#define UTIL_VERIFY_TYPES #define UTIL_VERIFY_TYPES
#include "ue2common.h" #include "ue2common.h"
#include "util/compile_error.h"
#include <cassert> #include <cassert>
namespace ue2 { namespace ue2 {
template<typename Int_T> template<typename To_T, typename From_T>
static UNUSED u8 verify_u8(Int_T val) { To_T verify_cast(From_T val) {
assert(val == (Int_T)((u8)val)); // there and back again To_T conv_val = static_cast<To_T>(val);
return (u8)(val); if (static_cast<From_T>(conv_val) != val) {
assert(0);
throw ResourceLimitError();
}
return conv_val;
} }
template<typename Int_T> template<typename T>
static UNUSED s8 verify_s8(Int_T val) { s8 verify_s8(T val) {
assert(val == (Int_T)((s8)val)); // there and back again return verify_cast<s8>(val);
return (s8)(val);
} }
template<typename Int_T> template<typename T>
static UNUSED s16 verify_s16(Int_T val) { u8 verify_u8(T val) {
assert(val == (Int_T)((s16)val)); // there and back again return verify_cast<u8>(val);
return (s16)(val);
} }
template<typename Int_T> template<typename T>
static UNUSED u16 verify_u16(Int_T val) { s16 verify_s16(T val) {
assert(val == (Int_T)((u16)val)); // there and back again return verify_cast<s16>(val);
return (u16)(val);
} }
template<typename Int_T> template<typename T>
static UNUSED s32 verify_s32(Int_T val) { u16 verify_u16(T val) {
assert(val == (Int_T)((s32)val)); // there and back again return verify_cast<u16>(val);
return (s32)(val);
} }
template<typename Int_T> template<typename T>
static UNUSED u32 verify_u32(Int_T val) { s32 verify_s32(T val) {
assert(val == (Int_T)((u32)val)); // there and back again return verify_cast<s32>(val);
return (u32)(val); }
template<typename T>
u32 verify_u32(T val) {
return verify_cast<u32>(val);
} }
} // namespace ue2 } // namespace ue2