Skip to content

Commit

Permalink
Added VectorLength helper function that works on nullptr.
Browse files Browse the repository at this point in the history
Change-Id: Ie62096f7337a476bee7a6d46d652e594fb3124d2
Tested: on Linux.
Bug: 18201051
  • Loading branch information
Wouter van Oortmerssen committed Dec 9, 2014
1 parent 3aa2025 commit 7e124e5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
6 changes: 6 additions & 0 deletions include/flatbuffers/flatbuffers.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,12 @@ template<typename T> class Vector {
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()); }
};
Expand Down
1 change: 1 addition & 0 deletions tests/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 7e124e5

Please sign in to comment.