Added helpers to access objects while creating the flatbuffer.

GetObject and GetMutableObject are similar to GetRoot and GetMutableRoot,
and can be useful when wanting to access data that has just been created.
Unfortunately there is a danger in using these methods, as it is possible
that the buffer reallocates which will invalidate the pointers.
This commit is contained in:
Martin Ankerl 2016-05-03 17:20:14 +02:00
parent 77742a3fba
commit 023fec627e
1 changed files with 11 additions and 0 deletions

View File

@ -1151,6 +1151,17 @@ template<typename T> const T *GetRoot(const void *buf) {
return GetMutableRoot<T>(const_cast<void *>(buf));
}
/// Helpers to get a typed pointer to objects that are currently beeing built.
/// @warning Creating new objects will lead to reallocations and invalidates the pointer!
template<typename T> T *GetMutableObject(FlatBufferBuilder &fbb, Offset<T> offset) {
return reinterpret_cast<T *>(fbb.GetCurrentBufferPointer() +
fbb.GetSize() - offset.o);
}
template<typename T> const T *GetObject(FlatBufferBuilder &fbb, Offset<T> offset) {
return GetMutableObject<T>(fbb, offset);
}
// 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),