Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
FlatBufferBuilder Class Reference
[Flatbuffers_cpp_api]
Helper class to hold data needed in creation of a FlatBuffer. More...
#include <flatbuffers.h>
Public Member Functions | |
FlatBufferBuilder (size_t initial_size=1024, Allocator *allocator=nullptr, bool own_allocator=false, size_t buffer_minalign=AlignOf< largest_scalar_t >()) | |
Default constructor for FlatBufferBuilder. | |
FlatBufferBuilder (FlatBufferBuilder &&other) FlatBufferBuilder(FlatBufferBuilder &other) | |
Move constructor for FlatBufferBuilder. | |
FlatBufferBuilder & | operator= (FlatBufferBuilder &&other) |
Move assignment operator for FlatBufferBuilder. | |
void | Clear () |
Reset all the state in this FlatBufferBuilder so it can be reused to construct another buffer. | |
uoffset_t | GetSize () const |
The current size of the serialized buffer, counting from the end. | |
uint8_t * | GetBufferPointer () const |
Get the serialized buffer (after you call `Finish()`). | |
uint8_t * | GetCurrentBufferPointer () const |
Get a pointer to an unfinished buffer. | |
FLATBUFFERS_ATTRIBUTE (deprecated("use Release() instead")) DetachedBuffer ReleaseBufferPointer() | |
Get the released pointer to the serialized buffer. | |
DetachedBuffer | Release () |
Get the released DetachedBuffer. | |
uint8_t * | ReleaseRaw (size_t &size, size_t &offset) |
Get the released pointer to the serialized buffer. | |
size_t | GetBufferMinAlignment () |
get the minimum alignment this buffer needs to be accessed properly. | |
void | ForceDefaults (bool fd) |
In order to save space, fields that are set to their default value don't get serialized into the buffer. | |
void | DedupVtables (bool dedup) |
By default vtables are deduped in order to save space. | |
Offset< String > | CreateString (const char *str, size_t len) |
Store a string in the buffer, which can contain any binary data. | |
Offset< String > | CreateString (const char *str) |
Store a string in the buffer, which is null-terminated. | |
Offset< String > | CreateString (char *str) |
Store a string in the buffer, which is null-terminated. | |
Offset< String > | CreateString (const std::string &str) |
Store a string in the buffer, which can contain any binary data. | |
Offset< String > | CreateString (flatbuffers::string_view str) |
Store a string in the buffer, which can contain any binary data. | |
Offset< String > | CreateString (const String *str) |
Store a string in the buffer, which can contain any binary data. | |
template<typename T > | |
Offset< String > | CreateString (const T &str) |
Store a string in the buffer, which can contain any binary data. | |
Offset< String > | CreateSharedString (const char *str, size_t len) |
Store a string in the buffer, which can contain any binary data. | |
Offset< String > | CreateSharedString (const char *str) |
Store a string in the buffer, which null-terminated. | |
Offset< String > | CreateSharedString (const std::string &str) |
Store a string in the buffer, which can contain any binary data. | |
Offset< String > | CreateSharedString (const String *str) |
Store a string in the buffer, which can contain any binary data. | |
Data Fields | |
Offset< Vector< T > > CreateVector(const T *v, size_t len){AssertScalarT< T > );StartVector(len, sizeof(T));#if FLATBUFFERS_LITTLEENDIANPushBytes(reinterpret_cast < const uint8_t * >v), len *sizeof(T));#elseif(sizeof(T)==1){PushBytes(reinterpret_cast < const uint8_t * >v), len);}else{for(auto i=len;i > 0;){PushElement(v[--i]);}}#endifreturn Offset< Vector< T > >EndVector(len));}template < typename T >Offset< Vector < Offset< T > > > CreateVector(const Offset< T > *v, size_t len){StartVector(len, sizeof(Offset< T >));for(auto i=len;i > | |
Serialize an array into a FlatBuffer `vector`. |
Detailed Description
Helper class to hold data needed in creation of a FlatBuffer.
To serialize data, you typically call one of the `Create*()` functions in the generated code, which in turn call a sequence of `StartTable`/ `PushElement`/`AddElement`/`EndTable`, or the builtin `CreateString`/ `CreateVector` functions. Do this is depth-first order to build up a tree to the root. `Finish()` wraps up the buffer ready for transport.
Definition at line 910 of file flatbuffers.h.
Constructor & Destructor Documentation
FlatBufferBuilder | ( | size_t | initial_size = 1024 , |
Allocator * | allocator = nullptr , |
||
bool | own_allocator = false , |
||
size_t | buffer_minalign = AlignOf<largest_scalar_t>() |
||
) | [explicit] |
Default constructor for FlatBufferBuilder.
- Parameters:
-
[in] initial_size The initial size of the buffer, in bytes. Defaults to `1024`. [in] allocator An `Allocator` to use. If null will use `DefaultAllocator`. [in] own_allocator Whether the builder/vector should own the allocator. Defaults to / `false`. [in] buffer_minalign Force the buffer to be aligned to the given minimum alignment upon reallocation. Only needed if you intend to store types with custom alignment AND you wish to read the buffer in-place directly after creation.
Definition at line 923 of file flatbuffers.h.
FlatBufferBuilder | ( | FlatBufferBuilder && | other ) |
Move constructor for FlatBufferBuilder.
Definition at line 943 of file flatbuffers.h.
Member Function Documentation
void Clear | ( | ) |
Reset all the state in this FlatBufferBuilder so it can be reused to construct another buffer.
Definition at line 1001 of file flatbuffers.h.
Offset<String> CreateSharedString | ( | const char * | str, |
size_t | len | ||
) |
Store a string in the buffer, which can contain any binary data.
If a string with this exact contents has already been serialized before, instead simply returns the offset of the existing string.
- Parameters:
-
[in] str A const char pointer to the data to be stored as a string. [in] len The number of bytes that should be stored from `str`.
- Returns:
- Returns the offset in the buffer where the string starts.
Definition at line 1360 of file flatbuffers.h.
Offset<String> CreateSharedString | ( | const char * | str ) |
Store a string in the buffer, which null-terminated.
If a string with this exact contents has already been serialized before, instead simply returns the offset of the existing string.
- Parameters:
-
[in] str A const char pointer to a C-string to add to the buffer.
- Returns:
- Returns the offset in the buffer where the string starts.
Definition at line 1384 of file flatbuffers.h.
Offset<String> CreateSharedString | ( | const std::string & | str ) |
Store a string in the buffer, which can contain any binary data.
If a string with this exact contents has already been serialized before, instead simply returns the offset of the existing string.
- Parameters:
-
[in] str A const reference to a std::string to store in the buffer.
- Returns:
- Returns the offset in the buffer where the string starts.
Definition at line 1393 of file flatbuffers.h.
Offset<String> CreateSharedString | ( | const String * | str ) |
Store a string in the buffer, which can contain any binary data.
If a string with this exact contents has already been serialized before, instead simply returns the offset of the existing string.
- Parameters:
-
[in] str A const pointer to a `String` struct to add to the buffer.
- Returns:
- Returns the offset in the buffer where the string starts
Definition at line 1402 of file flatbuffers.h.
Offset<String> CreateString | ( | const std::string & | str ) |
Store a string in the buffer, which can contain any binary data.
- Parameters:
-
[in] str A const reference to a std::string to store in the buffer.
- Returns:
- Returns the offset in the buffer where the string starts.
Definition at line 1324 of file flatbuffers.h.
Offset<String> CreateString | ( | flatbuffers::string_view | str ) |
Store a string in the buffer, which can contain any binary data.
- Parameters:
-
[in] str A const string_view to copy in to the buffer.
- Returns:
- Returns the offset in the buffer where the string starts.
Definition at line 1333 of file flatbuffers.h.
Offset<String> CreateString | ( | const String * | str ) |
Store a string in the buffer, which can contain any binary data.
- Parameters:
-
[in] str A const pointer to a `String` struct to add to the buffer.
- Returns:
- Returns the offset in the buffer where the string starts
Definition at line 1342 of file flatbuffers.h.
Offset<String> CreateString | ( | const T & | str ) |
Store a string in the buffer, which can contain any binary data.
- Parameters:
-
[in] str A const reference to a std::string like type with support of T::c_str() and T::length() to store in the buffer.
- Returns:
- Returns the offset in the buffer where the string starts.
Definition at line 1350 of file flatbuffers.h.
Offset<String> CreateString | ( | char * | str ) |
Store a string in the buffer, which is null-terminated.
- Parameters:
-
[in] str A char pointer to a C-string to add to the buffer.
- Returns:
- Returns the offset in the buffer where the string starts.
Definition at line 1317 of file flatbuffers.h.
Offset<String> CreateString | ( | const char * | str, |
size_t | len | ||
) |
Store a string in the buffer, which can contain any binary data.
- Parameters:
-
[in] str A const char pointer to the data to be stored as a string. [in] len The number of bytes that should be stored from `str`.
- Returns:
- Returns the offset in the buffer where the string starts.
Definition at line 1298 of file flatbuffers.h.
Offset<String> CreateString | ( | const char * | str ) |
Store a string in the buffer, which is null-terminated.
- Parameters:
-
[in] str A const char pointer to a C-string to add to the buffer.
- Returns:
- Returns the offset in the buffer where the string starts.
Definition at line 1310 of file flatbuffers.h.
void DedupVtables | ( | bool | dedup ) |
By default vtables are deduped in order to save space.
- Parameters:
-
[in] bool dedup When set to `true`, dedup vtables.
Definition at line 1085 of file flatbuffers.h.
FLATBUFFERS_ATTRIBUTE | ( | deprecated("use Release() instead") | ) |
Get the released pointer to the serialized buffer.
- Warning:
- Do NOT attempt to use this FlatBufferBuilder afterwards!
- Returns:
- A `FlatBuffer` that owns the buffer and its allocator and behaves similar to a `unique_ptr` with a deleter.
Definition at line 1030 of file flatbuffers.h.
void ForceDefaults | ( | bool | fd ) |
In order to save space, fields that are set to their default value don't get serialized into the buffer.
- Parameters:
-
[in] bool fd When set to `true`, always serializes default values that are set. Optional fields which are not set explicitly, will still not be serialized.
Definition at line 1081 of file flatbuffers.h.
size_t GetBufferMinAlignment | ( | ) |
get the minimum alignment this buffer needs to be accessed properly.
This is only known once all elements have been written (after you call Finish()). You can use this information if you need to embed a FlatBuffer in some other buffer, such that you can later read it without first having to copy it into its own buffer.
Definition at line 1061 of file flatbuffers.h.
uint8_t* GetBufferPointer | ( | ) | const |
Get the serialized buffer (after you call `Finish()`).
- Returns:
- Returns an `uint8_t` pointer to the FlatBuffer data inside the buffer.
Definition at line 1017 of file flatbuffers.h.
uint8_t* GetCurrentBufferPointer | ( | ) | const |
Get a pointer to an unfinished buffer.
- Returns:
- Returns a `uint8_t` pointer to the unfinished buffer.
Definition at line 1024 of file flatbuffers.h.
uoffset_t GetSize | ( | ) | const |
The current size of the serialized buffer, counting from the end.
- Returns:
- Returns an `uoffset_t` with the current size of the buffer.
Definition at line 1012 of file flatbuffers.h.
FlatBufferBuilder& operator= | ( | FlatBufferBuilder && | other ) |
Move assignment operator for FlatBufferBuilder.
Definition at line 967 of file flatbuffers.h.
DetachedBuffer Release | ( | ) |
Get the released DetachedBuffer.
- Returns:
- A `DetachedBuffer` that owns the buffer and its allocator.
Definition at line 1038 of file flatbuffers.h.
uint8_t* ReleaseRaw | ( | size_t & | size, |
size_t & | offset | ||
) |
Get the released pointer to the serialized buffer.
- Parameters:
-
The size of the memory block containing the serialized `FlatBuffer`. The offset from the released pointer where the finished `FlatBuffer` starts.
- Returns:
- A raw pointer to the start of the memory block containing the serialized `FlatBuffer`.
- Remarks:
- If the allocator is owned, it gets deleted when the destructor is called..
Definition at line 1051 of file flatbuffers.h.
Field Documentation
Offset<Vector<T>> CreateVector(const T *v, size_t len) { AssertScalarT<T>); StartVector(len, sizeof(T)); #if FLATBUFFERS_LITTLEENDIAN PushBytes(reinterpret_cast<const uint8_t *>v), len * sizeof(T)); #else if (sizeof(T) == 1) { PushBytes(reinterpret_cast<const uint8_t *>v), len); } else { for (auto i = len; i > 0; ) { PushElement(v[--i]); } } #endif return Offset<Vector<T> >EndVector(len)); } template<typename T> Offset<Vector<Offset<T> > > CreateVector(const Offset<T> *v, size_t len) { StartVector(len, sizeof(Offset<T>)); for (auto i = len; i > |
Serialize an array into a FlatBuffer `vector`.
- Template Parameters:
-
T The data type of the array elements.
- Parameters:
-
[in] v A pointer to the array of type `T` to serialize into the buffer as a `vector`. [in] len The number of elements to serialize.
- Returns:
- Returns a typed `Offset` into the serialized data indicating where the vector is stored.
Definition at line 1467 of file flatbuffers.h.
Generated on Wed Jul 13 2022 16:03:37 by
