Adding a method to get the file identifier from a flatbuffer.

Change-Id: Ie28fd1f0b463aac23647d38921600f15c8b7c10a
This commit is contained in:
Wouter van Oortmerssen 2017-06-16 12:30:36 -07:00
parent f325cce6fd
commit 3c3742a54a
1 changed files with 13 additions and 1 deletions

View File

@ -1516,9 +1516,21 @@ template<typename T> const T *GetTemporaryPointer(FlatBufferBuilder &fbb,
return GetMutableTemporaryPointer<T>(fbb, offset);
}
/// @brief Get a pointer to the the file_identifier section of the buffer.
/// @return Returns a const char pointer to the start of the file_identifier
/// characters in the buffer. The returned char * has length
/// 'flatbuffers::FlatBufferBuilder::kFileIdentifierLength'.
/// This function is UNDEFINED for FlatBuffers whose schema does not include
/// a file_identifier (likely points at padding or the start of a the root
/// vtable).
inline const char *GetBufferIdentifier(const void *buf) {
return reinterpret_cast<const char *>(buf) + sizeof(uoffset_t);
}
// Helper to see if the identifier in a buffer has the expected value.
inline bool BufferHasIdentifier(const void *buf, const char *identifier) {
return strncmp(reinterpret_cast<const char *>(buf) + sizeof(uoffset_t),
return strncmp(GetBufferIdentifier(buf),
identifier, FlatBufferBuilder::kFileIdentifierLength) == 0;
}