The asserts replaced by FLATBUFFERS_ASSERT. (#4701)

* The asserts replaced by FLATBUFFERS_ASSERT. Several asserts have converted to static_asserts.

* Regenerate header monster generate_code.sh
This commit is contained in:
Vladimir Glavnyy
2018-04-16 22:57:59 +07:00
committed by Wouter van Oortmerssen
parent 86153fd740
commit a66f9e769b
23 changed files with 157 additions and 149 deletions

View File

@@ -9,6 +9,10 @@
#include <assert.h>
#if !defined(FLATBUFFERS_ASSERT)
#define FLATBUFFERS_ASSERT assert
#endif
#ifndef ARDUINO
#include <cstdint>
#endif
@@ -209,7 +213,7 @@ template<typename T> T EndianSwap(T t) {
u.i = FLATBUFFERS_BYTESWAP64(u.i);
return u.t;
} else {
assert(0);
FLATBUFFERS_ASSERT(0);
}
}

View File

@@ -33,7 +33,8 @@ template<typename T> struct Offset {
inline void EndianCheck() {
int endiantest = 1;
// If this fails, see FLATBUFFERS_LITTLEENDIAN above.
assert(*reinterpret_cast<char *>(&endiantest) == FLATBUFFERS_LITTLEENDIAN);
FLATBUFFERS_ASSERT(*reinterpret_cast<char *>(&endiantest) ==
FLATBUFFERS_LITTLEENDIAN);
(void)endiantest;
}
@@ -194,7 +195,7 @@ template<typename T> class Vector {
typedef typename IndirectHelper<T>::mutable_return_type mutable_return_type;
return_type Get(uoffset_t i) const {
assert(i < size());
FLATBUFFERS_ASSERT(i < size());
return IndirectHelper<T>::Read(Data(), i);
}
@@ -232,7 +233,7 @@ template<typename T> class Vector {
// Change elements if you have a non-const pointer to this object.
// Scalars only. See reflection.h, and the documentation.
void Mutate(uoffset_t i, const T &val) {
assert(i < size());
FLATBUFFERS_ASSERT(i < size());
WriteScalar(data() + i, val);
}
@@ -240,15 +241,15 @@ template<typename T> class Vector {
// "val" points to the new table/string, as you can obtain from
// e.g. reflection::AddFlatBuffer().
void MutateOffset(uoffset_t i, const uint8_t *val) {
assert(i < size());
assert(sizeof(T) == sizeof(uoffset_t));
FLATBUFFERS_ASSERT(i < size());
static_assert(sizeof(T) == sizeof(uoffset_t), "Unrelated types");
WriteScalar(data() + i,
static_cast<uoffset_t>(val - (Data() + i * sizeof(uoffset_t))));
}
// Get a mutable pointer to tables/strings inside this vector.
mutable_return_type GetMutableObject(uoffset_t i) const {
assert(i < size());
FLATBUFFERS_ASSERT(i < size());
return const_cast<mutable_return_type>(IndirectHelper<T>::Read(Data(), i));
}
@@ -368,7 +369,7 @@ class Allocator {
virtual uint8_t *reallocate_downward(uint8_t *old_p, size_t old_size,
size_t new_size, size_t in_use_back,
size_t in_use_front) {
assert(new_size > old_size); // vector_downward only grows
FLATBUFFERS_ASSERT(new_size > old_size); // vector_downward only grows
uint8_t *new_p = allocate(new_size);
memcpy_downward(old_p, old_size, new_p, new_size, in_use_back,
in_use_front);
@@ -428,7 +429,7 @@ class DetachedBuffer {
reserved_(reserved),
cur_(cur),
size_(sz) {
assert(allocator_);
FLATBUFFERS_ASSERT(allocator_);
}
DetachedBuffer(DetachedBuffer &&other)
@@ -499,7 +500,7 @@ class DetachedBuffer {
inline void destroy() {
if (buf_) {
assert(allocator_);
FLATBUFFERS_ASSERT(allocator_);
allocator_->deallocate(buf_, reserved_);
}
if (own_allocator_ && allocator_) { delete allocator_; }
@@ -537,12 +538,12 @@ class vector_downward {
buf_(nullptr),
cur_(nullptr),
scratch_(nullptr) {
assert(allocator_);
FLATBUFFERS_ASSERT(allocator_);
}
~vector_downward() {
if (buf_) {
assert(allocator_);
FLATBUFFERS_ASSERT(allocator_);
allocator_->deallocate(buf_, reserved_);
}
if (own_allocator_ && allocator_) { delete allocator_; }
@@ -550,7 +551,7 @@ class vector_downward {
void reset() {
if (buf_) {
assert(allocator_);
FLATBUFFERS_ASSERT(allocator_);
allocator_->deallocate(buf_, reserved_);
buf_ = nullptr;
}
@@ -583,11 +584,11 @@ class vector_downward {
}
size_t ensure_space(size_t len) {
assert(cur_ >= scratch_ && scratch_ >= buf_);
FLATBUFFERS_ASSERT(cur_ >= scratch_ && scratch_ >= buf_);
if (len > static_cast<size_t>(cur_ - scratch_)) { reallocate(len); }
// Beyond this, signed offsets may not have enough range:
// (FlatBuffers > 2GB not supported).
assert(size() < FLATBUFFERS_MAX_BUFFER_SIZE);
FLATBUFFERS_ASSERT(size() < FLATBUFFERS_MAX_BUFFER_SIZE);
return len;
}
@@ -609,17 +610,17 @@ class vector_downward {
size_t capacity() const { return reserved_; }
uint8_t *data() const {
assert(cur_);
FLATBUFFERS_ASSERT(cur_);
return cur_;
}
uint8_t *scratch_data() const {
assert(buf_);
FLATBUFFERS_ASSERT(buf_);
return buf_;
}
uint8_t *scratch_end() const {
assert(scratch_);
FLATBUFFERS_ASSERT(scratch_);
return scratch_;
}
@@ -671,7 +672,7 @@ class vector_downward {
uint8_t *scratch_; // Points to the end of the scratchpad in use.
void reallocate(size_t len) {
assert(allocator_);
FLATBUFFERS_ASSERT(allocator_);
auto old_reserved = reserved_;
auto old_size = size();
auto old_scratch_size = scratch_size();
@@ -815,7 +816,7 @@ class FlatBufferBuilder {
// FlatBufferBuilder::Finish with your root table.
// If you really need to access an unfinished buffer, call
// GetCurrentBufferPointer instead.
assert(finished);
FLATBUFFERS_ASSERT(finished);
}
/// @endcond
@@ -908,7 +909,7 @@ class FlatBufferBuilder {
// Align to ensure GetSize() below is correct.
Align(sizeof(uoffset_t));
// Offset must refer to something already in buffer.
assert(off && off <= GetSize());
FLATBUFFERS_ASSERT(off && off <= GetSize());
return GetSize() - off + static_cast<uoffset_t>(sizeof(uoffset_t));
}
@@ -921,9 +922,9 @@ class FlatBufferBuilder {
// Ignoring this assert may appear to work in simple cases, but the reason
// it is here is that storing objects in-line may cause vtable offsets
// to not fit anymore. It also leads to vtable duplication.
assert(!nested);
FLATBUFFERS_ASSERT(!nested);
// If you hit this, fields were added outside the scope of a table.
assert(!num_field_loc);
FLATBUFFERS_ASSERT(!num_field_loc);
}
// From generated code (or from the parser), we call StartTable/EndTable
@@ -939,7 +940,7 @@ class FlatBufferBuilder {
// resulting vtable offset.
uoffset_t EndTable(uoffset_t start) {
// If you get this assert, a corresponding StartTable wasn't called.
assert(nested);
FLATBUFFERS_ASSERT(nested);
// Write the vtable offset, which is the start of any Table.
// We fill it's value later.
auto vtableoffsetloc = PushElement<soffset_t>(0);
@@ -953,7 +954,8 @@ class FlatBufferBuilder {
FieldIndexToOffset(0));
buf_.fill_big(max_voffset_);
auto table_object_size = vtableoffsetloc - start;
assert(table_object_size < 0x10000); // Vtable use 16bit offsets.
// Vtable use 16bit offsets.
FLATBUFFERS_ASSERT(table_object_size < 0x10000);
WriteScalar<voffset_t>(buf_.data() + sizeof(voffset_t),
static_cast<voffset_t>(table_object_size));
WriteScalar<voffset_t>(buf_.data(), max_voffset_);
@@ -963,7 +965,8 @@ class FlatBufferBuilder {
auto field_location = reinterpret_cast<FieldLoc *>(it);
auto pos = static_cast<voffset_t>(vtableoffsetloc - field_location->off);
// If this asserts, it means you've set a field twice.
assert(!ReadScalar<voffset_t>(buf_.data() + field_location->id));
FLATBUFFERS_ASSERT(
!ReadScalar<voffset_t>(buf_.data() + field_location->id));
WriteScalar<voffset_t>(buf_.data() + field_location->id, pos);
}
ClearOffsets();
@@ -1011,7 +1014,7 @@ class FlatBufferBuilder {
auto vtable_ptr = table_ptr - ReadScalar<soffset_t>(table_ptr);
bool ok = ReadScalar<voffset_t>(vtable_ptr + field) != 0;
// If this fails, the caller will show what field needs to be set.
assert(ok);
FLATBUFFERS_ASSERT(ok);
(void)ok;
}
@@ -1143,7 +1146,7 @@ class FlatBufferBuilder {
/// @cond FLATBUFFERS_INTERNAL
uoffset_t EndVector(size_t len) {
assert(nested); // Hit if no corresponding StartVector.
FLATBUFFERS_ASSERT(nested); // Hit if no corresponding StartVector.
nested = false;
return PushElement(static_cast<uoffset_t>(len));
}
@@ -1559,7 +1562,7 @@ class FlatBufferBuilder {
(file_identifier ? kFileIdentifierLength : 0),
minalign_);
if (file_identifier) {
assert(strlen(file_identifier) == kFileIdentifierLength);
FLATBUFFERS_ASSERT(strlen(file_identifier) == kFileIdentifierLength);
PushBytes(reinterpret_cast<const uint8_t *>(file_identifier),
kFileIdentifierLength);
}
@@ -1698,7 +1701,7 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
bool Check(bool ok) const {
// clang-format off
#ifdef FLATBUFFERS_DEBUG_VERIFICATION_FAILURE
assert(ok);
FLATBUFFERS_ASSERT(ok);
#endif
#ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
if (!ok)
@@ -2066,7 +2069,7 @@ inline const uint8_t *GetBufferStartFromRootPointer(const void *root) {
// or the buffer is corrupt.
// Assert, because calling this function with bad data may cause reads
// outside of buffer boundaries.
assert(false);
FLATBUFFERS_ASSERT(false);
return nullptr;
}

View File

@@ -97,23 +97,23 @@ inline bool IsFixedTypedVector(Type t) {
}
inline Type ToTypedVector(Type t, size_t fixed_len = 0) {
assert(IsTypedVectorElementType(t));
FLATBUFFERS_ASSERT(IsTypedVectorElementType(t));
switch (fixed_len) {
case 0: return static_cast<Type>(t - TYPE_INT + TYPE_VECTOR_INT);
case 2: return static_cast<Type>(t - TYPE_INT + TYPE_VECTOR_INT2);
case 3: return static_cast<Type>(t - TYPE_INT + TYPE_VECTOR_INT3);
case 4: return static_cast<Type>(t - TYPE_INT + TYPE_VECTOR_INT4);
default: assert(0); return TYPE_NULL;
default: FLATBUFFERS_ASSERT(0); return TYPE_NULL;
}
}
inline Type ToTypedVectorElementType(Type t) {
assert(IsTypedVector(t));
FLATBUFFERS_ASSERT(IsTypedVector(t));
return static_cast<Type>(t - TYPE_VECTOR_INT + TYPE_INT);
}
inline Type ToFixedTypedVectorElementType(Type t, uint8_t *len) {
assert(IsFixedTypedVector(t));
FLATBUFFERS_ASSERT(IsFixedTypedVector(t));
auto fixed_type = t - TYPE_VECTOR_INT2;
*len = static_cast<uint8_t>(fixed_type / 3 +
2); // 3 types each, starting from length 2.
@@ -690,7 +690,7 @@ class Reference {
return Mutate(dest, static_cast<double>(t), byte_width, value_width);
if (byte_width == sizeof(float))
return Mutate(dest, static_cast<float>(t), byte_width, value_width);
assert(false);
FLATBUFFERS_ASSERT(false);
return false;
}
@@ -1026,11 +1026,11 @@ class Builder FLATBUFFERS_FINAL_CLASS {
// We should have interleaved keys and values on the stack.
// Make sure it is an even number:
auto len = stack_.size() - start;
assert(!(len & 1));
FLATBUFFERS_ASSERT(!(len & 1));
len /= 2;
// Make sure keys are all strings:
for (auto key = start; key < stack_.size(); key += 2) {
assert(stack_[key].type_ == TYPE_KEY);
FLATBUFFERS_ASSERT(stack_[key].type_ == TYPE_KEY);
}
// Now sort values, so later we can do a binary seach lookup.
// We want to sort 2 array elements at a time.
@@ -1061,7 +1061,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
// TODO: Have to check for pointer equality, as some sort
// implementation apparently call this function with the same
// element?? Why?
assert(comp || &a == &b);
FLATBUFFERS_ASSERT(comp || &a == &b);
return comp < 0;
});
// First create a vector out of all keys.
@@ -1141,9 +1141,9 @@ class Builder FLATBUFFERS_FINAL_CLASS {
template<typename T> size_t FixedTypedVector(const T *elems, size_t len) {
// We only support a few fixed vector lengths. Anything bigger use a
// regular typed vector.
assert(len >= 2 && len <= 4);
FLATBUFFERS_ASSERT(len >= 2 && len <= 4);
// And only scalar values.
assert(flatbuffers::is_scalar<T>::value);
static_assert(flatbuffers::is_scalar<T>::value, "Unrelated types");
return ScalarVector(elems, len, true);
}
@@ -1222,7 +1222,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
// in a parent. You need to have exactly one root to finish a buffer.
// Check your Start/End calls are matched, and all objects are inside
// some other object.
assert(stack_.size() == 1);
FLATBUFFERS_ASSERT(stack_.size() == 1);
// Write root value.
auto byte_width = Align(stack_[0].ElemWidth(buf_.size(), 0));
@@ -1240,7 +1240,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
// If you get this assert, you're attempting to get access a buffer
// which hasn't been finished yet. Be sure to call
// Builder::Finish with your root object.
assert(finished_);
FLATBUFFERS_ASSERT(finished_);
}
// Align to prepare for writing a scalar with a certain size.
@@ -1257,7 +1257,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
}
template<typename T> void Write(T val, size_t byte_width) {
assert(sizeof(T) >= byte_width);
FLATBUFFERS_ASSERT(sizeof(T) >= byte_width);
val = flatbuffers::EndianScalar(val);
WriteBytes(&val, byte_width);
}
@@ -1268,13 +1268,13 @@ class Builder FLATBUFFERS_FINAL_CLASS {
case 4: Write(static_cast<float>(f), byte_width); break;
// case 2: Write(static_cast<half>(f), byte_width); break;
// case 1: Write(static_cast<quarter>(f), byte_width); break;
default: assert(0);
default: FLATBUFFERS_ASSERT(0);
}
}
void WriteOffset(uint64_t o, uint8_t byte_width) {
auto reloff = buf_.size() - o;
assert(byte_width == 8 || reloff < 1ULL << (byte_width * 8));
FLATBUFFERS_ASSERT(byte_width == 8 || reloff < 1ULL << (byte_width * 8));
Write(reloff, byte_width);
}
@@ -1291,12 +1291,12 @@ class Builder FLATBUFFERS_FINAL_CLASS {
case 2: return BIT_WIDTH_16;
case 4: return BIT_WIDTH_32;
case 8: return BIT_WIDTH_64;
default: assert(false); return BIT_WIDTH_64;
default: FLATBUFFERS_ASSERT(false); return BIT_WIDTH_64;
}
}
template<typename T> static Type GetScalarType() {
assert(flatbuffers::is_scalar<T>::value);
static_assert(flatbuffers::is_scalar<T>::value, "Unrelated types");
return flatbuffers::is_floating_point<T>::value
? TYPE_FLOAT
: flatbuffers::is_same<T, bool>::value
@@ -1360,7 +1360,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
byte_width)
return bit_width;
}
assert(false); // Must match one of the sizes above.
FLATBUFFERS_ASSERT(false); // Must match one of the sizes above.
return BIT_WIDTH_64;
}
}
@@ -1405,7 +1405,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
// byte vector > 255 elements). For such types, write a "blob" instead.
// TODO: instead of asserting, could write vector with larger elements
// instead, though that would be wasteful.
assert(WidthU(len) <= bit_width);
FLATBUFFERS_ASSERT(WidthU(len) <= bit_width);
if (!fixed) Write<uint64_t>(len, byte_width);
auto vloc = buf_.size();
for (size_t i = 0; i < len; i++) Write(elems[i], byte_width);
@@ -1437,13 +1437,13 @@ class Builder FLATBUFFERS_FINAL_CLASS {
} else {
// If you get this assert, you are writing a typed vector with
// elements that are not all the same type.
assert(vector_type == stack_[i].type_);
FLATBUFFERS_ASSERT(vector_type == stack_[i].type_);
}
}
}
// If you get this assert, your fixed types are not one of:
// Int / UInt / Float / Key.
assert(IsTypedVectorElementType(vector_type));
FLATBUFFERS_ASSERT(IsTypedVectorElementType(vector_type));
auto byte_width = Align(bit_width);
// Write vector. First the keys width/offset if available, and size.
if (keys) {

View File

@@ -91,14 +91,14 @@ class SliceAllocator : public Allocator {
virtual ~SliceAllocator() { grpc_slice_unref(slice_); }
virtual uint8_t *allocate(size_t size) override {
assert(GRPC_SLICE_IS_EMPTY(slice_));
FLATBUFFERS_ASSERT(GRPC_SLICE_IS_EMPTY(slice_));
slice_ = grpc_slice_malloc(size);
return GRPC_SLICE_START_PTR(slice_);
}
virtual void deallocate(uint8_t *p, size_t size) override {
assert(p == GRPC_SLICE_START_PTR(slice_));
assert(size == GRPC_SLICE_LENGTH(slice_));
FLATBUFFERS_ASSERT(p == GRPC_SLICE_START_PTR(slice_));
FLATBUFFERS_ASSERT(size == GRPC_SLICE_LENGTH(slice_));
grpc_slice_unref(slice_);
slice_ = grpc_empty_slice();
}
@@ -106,9 +106,9 @@ class SliceAllocator : public Allocator {
virtual uint8_t *reallocate_downward(uint8_t *old_p, size_t old_size,
size_t new_size, size_t in_use_back,
size_t in_use_front) override {
assert(old_p == GRPC_SLICE_START_PTR(slice_));
assert(old_size == GRPC_SLICE_LENGTH(slice_));
assert(new_size > old_size);
FLATBUFFERS_ASSERT(old_p == GRPC_SLICE_START_PTR(slice_));
FLATBUFFERS_ASSERT(old_size == GRPC_SLICE_LENGTH(slice_));
FLATBUFFERS_ASSERT(new_size > old_size);
grpc_slice old_slice = slice_;
grpc_slice new_slice = grpc_slice_malloc(new_size);
uint8_t *new_p = GRPC_SLICE_START_PTR(new_slice);
@@ -121,8 +121,8 @@ class SliceAllocator : public Allocator {
private:
grpc_slice &get_slice(uint8_t *p, size_t size) {
assert(p == GRPC_SLICE_START_PTR(slice_));
assert(size == GRPC_SLICE_LENGTH(slice_));
FLATBUFFERS_ASSERT(p == GRPC_SLICE_START_PTR(slice_));
FLATBUFFERS_ASSERT(size == GRPC_SLICE_LENGTH(slice_));
return slice_;
}
@@ -162,10 +162,10 @@ class MessageBuilder : private detail::SliceAllocatorMember,
auto msg_data = buf_.data(); // pointer to msg
auto msg_size = buf_.size(); // size of msg
// Do some sanity checks on data/size
assert(msg_data);
assert(msg_size);
assert(msg_data >= buf_data);
assert(msg_data + msg_size <= buf_data + buf_size);
FLATBUFFERS_ASSERT(msg_data);
FLATBUFFERS_ASSERT(msg_size);
FLATBUFFERS_ASSERT(msg_data >= buf_data);
FLATBUFFERS_ASSERT(msg_data + msg_size <= buf_data + buf_size);
// Calculate offsets from the buffer start
auto begin = msg_data - buf_data;
auto end = begin + msg_size;

View File

@@ -181,7 +181,7 @@ template<typename T> class SymbolTable {
dict.erase(it);
dict[newname] = obj;
} else {
assert(false);
FLATBUFFERS_ASSERT(false);
}
}
@@ -485,7 +485,7 @@ class CheckedError {
*this = other; // Use assignment operator.
}
~CheckedError() { assert(has_been_checked_); }
~CheckedError() { FLATBUFFERS_ASSERT(has_been_checked_); }
bool Check() {
has_been_checked_ = true;

View File

@@ -89,9 +89,9 @@ inline size_t InlineSize(ElementaryType type, const TypeTable *type_table) {
case ST_TABLE:
case ST_UNION: return 4;
case ST_STRUCT: return type_table->values[type_table->num_elems];
default: assert(false); return 1;
default: FLATBUFFERS_ASSERT(false); return 1;
}
default: assert(false); return 1;
default: FLATBUFFERS_ASSERT(false); return 1;
}
}
@@ -190,7 +190,7 @@ inline void IterateValue(ElementaryType type, const uint8_t *val,
case ST_STRUCT: IterateObject(val, type_table, visitor); break;
case ST_UNION: {
val += ReadScalar<uoffset_t>(val);
assert(prev_val);
FLATBUFFERS_ASSERT(prev_val);
auto union_type = *prev_val; // Always a uint8_t.
if (vector_index >= 0) {
auto type_vec = reinterpret_cast<const Vector<uint8_t> *>(prev_val);
@@ -217,7 +217,7 @@ inline void IterateValue(ElementaryType type, const uint8_t *val,
}
break;
}
case ST_ENUM: assert(false); break;
case ST_ENUM: FLATBUFFERS_ASSERT(false); break;
}
break;
}

View File

@@ -72,20 +72,20 @@ inline const Table *GetAnyRoot(const uint8_t *flatbuf) {
// Get a field's default, if you know it's an integer, and its exact type.
template<typename T> T GetFieldDefaultI(const reflection::Field &field) {
assert(sizeof(T) == GetTypeSize(field.type()->base_type()));
FLATBUFFERS_ASSERT(sizeof(T) == GetTypeSize(field.type()->base_type()));
return static_cast<T>(field.default_integer());
}
// Get a field's default, if you know it's floating point and its exact type.
template<typename T> T GetFieldDefaultF(const reflection::Field &field) {
assert(sizeof(T) == GetTypeSize(field.type()->base_type()));
FLATBUFFERS_ASSERT(sizeof(T) == GetTypeSize(field.type()->base_type()));
return static_cast<T>(field.default_real());
}
// Get a field, if you know it's an integer, and its exact type.
template<typename T>
T GetFieldI(const Table &table, const reflection::Field &field) {
assert(sizeof(T) == GetTypeSize(field.type()->base_type()));
FLATBUFFERS_ASSERT(sizeof(T) == GetTypeSize(field.type()->base_type()));
return table.GetField<T>(field.offset(),
static_cast<T>(field.default_integer()));
}
@@ -93,7 +93,7 @@ T GetFieldI(const Table &table, const reflection::Field &field) {
// Get a field, if you know it's floating point and its exact type.
template<typename T>
T GetFieldF(const Table &table, const reflection::Field &field) {
assert(sizeof(T) == GetTypeSize(field.type()->base_type()));
FLATBUFFERS_ASSERT(sizeof(T) == GetTypeSize(field.type()->base_type()));
return table.GetField<T>(field.offset(),
static_cast<T>(field.default_real()));
}
@@ -101,15 +101,15 @@ T GetFieldF(const Table &table, const reflection::Field &field) {
// Get a field, if you know it's a string.
inline const String *GetFieldS(const Table &table,
const reflection::Field &field) {
assert(field.type()->base_type() == reflection::String);
FLATBUFFERS_ASSERT(field.type()->base_type() == reflection::String);
return table.GetPointer<const String *>(field.offset());
}
// Get a field, if you know it's a vector.
template<typename T>
Vector<T> *GetFieldV(const Table &table, const reflection::Field &field) {
assert(field.type()->base_type() == reflection::Vector &&
sizeof(T) == GetTypeSize(field.type()->element()));
FLATBUFFERS_ASSERT(field.type()->base_type() == reflection::Vector &&
sizeof(T) == GetTypeSize(field.type()->element()));
return table.GetPointer<Vector<T> *>(field.offset());
}
@@ -123,8 +123,8 @@ inline VectorOfAny *GetFieldAnyV(const Table &table,
// Get a field, if you know it's a table.
inline Table *GetFieldT(const Table &table, const reflection::Field &field) {
assert(field.type()->base_type() == reflection::Obj ||
field.type()->base_type() == reflection::Union);
FLATBUFFERS_ASSERT(field.type()->base_type() == reflection::Obj ||
field.type()->base_type() == reflection::Union);
return table.GetPointer<Table *>(field.offset());
}
@@ -133,14 +133,14 @@ inline const Struct *GetFieldStruct(const Table &table,
const reflection::Field &field) {
// TODO: This does NOT check if the field is a table or struct, but we'd need
// access to the schema to check the is_struct flag.
assert(field.type()->base_type() == reflection::Obj);
FLATBUFFERS_ASSERT(field.type()->base_type() == reflection::Obj);
return table.GetStruct<const Struct *>(field.offset());
}
// Get a structure's field, if you know it's a struct.
inline const Struct *GetFieldStruct(const Struct &structure,
const reflection::Field &field) {
assert(field.type()->base_type() == reflection::Obj);
FLATBUFFERS_ASSERT(field.type()->base_type() == reflection::Obj);
return structure.GetStruct<const Struct *>(field.offset());
}
@@ -262,12 +262,12 @@ template<typename T>
bool SetField(Table *table, const reflection::Field &field, T val) {
reflection::BaseType type = field.type()->base_type();
if (!IsScalar(type)) { return false; }
assert(sizeof(T) == GetTypeSize(type));
FLATBUFFERS_ASSERT(sizeof(T) == GetTypeSize(type));
T def;
if (IsInteger(type)) {
def = GetFieldDefaultI<T>(field);
} else {
assert(IsFloat(type));
FLATBUFFERS_ASSERT(IsFloat(type));
def = GetFieldDefaultF<T>(field);
}
return table->SetField(field.offset(), val, def);
@@ -386,7 +386,7 @@ inline const reflection::Object &GetUnionType(
// TODO: this is clumsy and slow, but no other way to find it?
auto type_field = parent.fields()->LookupByKey(
(unionfield.name()->str() + UnionTypeFieldSuffix()).c_str());
assert(type_field);
FLATBUFFERS_ASSERT(type_field);
auto union_type = GetFieldI<uint8_t>(table, *type_field);
auto enumval = enumdef->values()->LookupByKey(union_type);
return *enumval->object();
@@ -444,7 +444,8 @@ const uint8_t *AddFlatBuffer(std::vector<uint8_t> &flatbuf,
inline bool SetFieldT(Table *table, const reflection::Field &field,
const uint8_t *val) {
assert(sizeof(uoffset_t) == GetTypeSize(field.type()->base_type()));
FLATBUFFERS_ASSERT(sizeof(uoffset_t) ==
GetTypeSize(field.type()->base_type()));
return table->SetPointer(field.offset(), val);
}

View File

@@ -284,7 +284,7 @@ inline std::string AbsolutePath(const std::string &filepath) {
// Convert a unicode code point into a UTF-8 representation by appending it
// to a string. Returns the number of bytes generated.
inline int ToUTF8(uint32_t ucc, std::string *out) {
assert(!(ucc & 0x80000000)); // Top bit can't be set.
FLATBUFFERS_ASSERT(!(ucc & 0x80000000)); // Top bit can't be set.
// 6 possible encodings: http://en.wikipedia.org/wiki/UTF-8
for (int i = 0; i < 6; i++) {
// Max bits this encoding can represent.
@@ -302,7 +302,7 @@ inline int ToUTF8(uint32_t ucc, std::string *out) {
return i + 1; // Return the number of bytes added.
}
}
assert(0); // Impossible to arrive here.
FLATBUFFERS_ASSERT(0); // Impossible to arrive here.
return -1;
}