Merge "Added VectorLength helper function that works on nullptr." into ub-games-master

This commit is contained in:
Wouter van Oortmerssen 2014-12-09 02:01:30 +00:00 committed by Android (Google) Code Review
commit d72c478128
2 changed files with 7 additions and 0 deletions

View File

@ -296,6 +296,12 @@ protected:
uoffset_t length_; 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> { struct String : public Vector<char> {
const char *c_str() const { return reinterpret_cast<const char *>(Data()); } const char *c_str() const { return reinterpret_cast<const char *>(Data()); }
}; };

View File

@ -137,6 +137,7 @@ void AccessFlatBufferTest(const std::string &flatbuf) {
TEST_EQ(pos->test3().b(), 20); TEST_EQ(pos->test3().b(), 20);
auto inventory = monster->inventory(); auto inventory = monster->inventory();
TEST_EQ(VectorLength(inventory), 10); // Works even if inventory is null.
TEST_NOTNULL(inventory); TEST_NOTNULL(inventory);
unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for (auto it = inventory->begin(); it != inventory->end(); ++it) for (auto it = inventory->begin(); it != inventory->end(); ++it)