From 023fec627e1a6fdbefa2a0d0a767bd4ccd35d276 Mon Sep 17 00:00:00 2001 From: Martin Ankerl Date: Tue, 3 May 2016 17:20:14 +0200 Subject: [PATCH 1/2] 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. --- include/flatbuffers/flatbuffers.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index c78583cad..07b7cadec 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -1151,6 +1151,17 @@ template const T *GetRoot(const void *buf) { return GetMutableRoot(const_cast(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 T *GetMutableObject(FlatBufferBuilder &fbb, Offset offset) { + return reinterpret_cast(fbb.GetCurrentBufferPointer() + + fbb.GetSize() - offset.o); +} + +template const T *GetObject(FlatBufferBuilder &fbb, Offset offset) { + return GetMutableObject(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(buf) + sizeof(uoffset_t), From b3c35750c2a40d09b6fa8982663dd10337d3b5dd Mon Sep 17 00:00:00 2001 From: Martin Ankerl Date: Wed, 1 Jun 2016 13:13:00 +0200 Subject: [PATCH 2/2] renamed functions More descriptive name, show that it's really just a temporary object. --- include/flatbuffers/flatbuffers.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index 07b7cadec..e94d0de70 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -1153,13 +1153,13 @@ template const T *GetRoot(const 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 T *GetMutableObject(FlatBufferBuilder &fbb, Offset offset) { +template T *GetMutableTemporaryPointer(FlatBufferBuilder &fbb, Offset offset) { return reinterpret_cast(fbb.GetCurrentBufferPointer() + fbb.GetSize() - offset.o); } -template const T *GetObject(FlatBufferBuilder &fbb, Offset offset) { - return GetMutableObject(fbb, offset); +template const T *GetTemporaryPointer(FlatBufferBuilder &fbb, Offset offset) { + return GetMutableTemporaryPointer(fbb, offset); } // Helper to see if the identifier in a buffer has the expected value.