Merge "Added VectorLength helper function that works on nullptr." into ub-games-master
This commit is contained in:
commit
d72c478128
|
@ -296,6 +296,12 @@ protected:
|
|||
uoffset_t length_;
|
||||
};
|
||||
|
||||
// Convenient helper function to get the length of any vector, regardless
|
||||
// of wether it is null or not (the field is not set).
|
||||
template<typename T> static inline size_t VectorLength(const Vector<T> *v) {
|
||||
return v ? v->Length() : 0;
|
||||
}
|
||||
|
||||
struct String : public Vector<char> {
|
||||
const char *c_str() const { return reinterpret_cast<const char *>(Data()); }
|
||||
};
|
||||
|
|
|
@ -137,6 +137,7 @@ void AccessFlatBufferTest(const std::string &flatbuf) {
|
|||
TEST_EQ(pos->test3().b(), 20);
|
||||
|
||||
auto inventory = monster->inventory();
|
||||
TEST_EQ(VectorLength(inventory), 10); // Works even if inventory is null.
|
||||
TEST_NOTNULL(inventory);
|
||||
unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
for (auto it = inventory->begin(); it != inventory->end(); ++it)
|
||||
|
|
Loading…
Reference in New Issue