autobahn.flatbuffers.builder

Attributes

Exceptions

BuilderNotFinishedError

Error caused by not calling Finish before calling Output.

BuilderSizeError

Error caused by causing a Builder to exceed the hardcoded limit of 2

EndVectorLengthMismatched

The number of elements passed to EndVector does not match the number

IsNestedError

Error caused by using a Builder to begin an Object when an Object is

IsNotNestedError

Error caused by using a Builder to write Object data when not inside

OffsetArithmeticError

Error caused by an Offset arithmetic error.

StructIsNotInlineError

Error caused by using a Builder to write a Struct at a location that

Classes

Builder

A Builder is used to construct one or more FlatBuffers.

Functions

vtableEqual(a, objectStart, b)

vtableEqual compares an unwritten vtable to a written vtable.

Module Contents

class Builder(initialSize=1024)[source]

Bases: object

A Builder is used to construct one or more FlatBuffers.

Typically, Builder objects will be used from code generated by the flatc compiler.

A Builder constructs byte buffers in a last-first manner for simplicity and performance during reading.

Internally, a Builder is a state machine for creating FlatBuffer objects.

It holds the following internal state:
  • Bytes: an array of bytes.

  • current_vtable: a list of integers.

  • vtables: a hash of vtable entries.

Bytes[source]

The internal bytearray for the Builder.

finished[source]

A boolean determining if the Builder has been finalized.

Bytes[source]
Clear() None[source]
CreateByteVector(x)[source]

CreateString writes a byte vector.

CreateNumpyVector(x)[source]

CreateNumpyVector writes a numpy array into the buffer.

CreateSharedString(s, encoding='utf-8', errors='strict')[source]

CreateSharedString checks if the string is already written to the buffer

before calling CreateString.

CreateString(s, encoding='utf-8', errors='strict')[source]

CreateString writes a null-terminated byte string as a vector.

EndObject()[source]

EndObject writes data necessary to finish object construction.

EndVector(numElems=None)[source]

EndVector writes data necessary to finish vector construction.

Finish(rootTable, file_identifier=None)[source]

Finish finalizes a buffer, pointing to the given rootTable.

FinishSizePrefixed(rootTable, file_identifier=None)[source]

Finish finalizes a buffer, pointing to the given rootTable,

with the size prefixed.

ForceDefaults(forceDefaults)[source]

In order to save space, fields that are set to their default value

don’t get serialized into the buffer. Forcing defaults provides a way to manually disable this optimization. When set to True, will always serialize default values.

Head()[source]

Get the start of useful data in the underlying byte buffer.

Note: unlike other functions, this value is interpreted as from the left.

MAX_BUFFER_SIZE = 2147483648[source]
Offset()[source]

Offset relative to the end of the buffer.

Output()[source]

Return the portion of the buffer that has been used for writing data.

This is the typical way to access the FlatBuffer data inside the builder. If you try to access Builder.Bytes directly, you would need to manually index it with Head(), since the buffer is constructed backwards.

It raises BuilderNotFinishedError if the buffer has not been finished with Finish.

Pad(n)[source]

Pad places zeros at the current offset.

Place(x, flags)[source]

Place prepends a value specified by flags to the Builder,

without checking for available space.

PlaceSOffsetT(x)[source]

PlaceSOffsetT prepends a SOffsetT to the Builder, without checking

for space.

PlaceUOffsetT(x)[source]

PlaceUOffsetT prepends a UOffsetT to the Builder, without checking

for space.

PlaceVOffsetT(x)[source]

PlaceVOffsetT prepends a VOffsetT to the Builder, without checking

for space.

Prep(size, additionalBytes)[source]

Prep prepares to write an element of size after additional_bytes

have been written, e.g. if you write a string, you need to align such the int length field is aligned to SizeInt32, and the string data follows it directly. If all you need to do is align, additionalBytes will be 0.

Prepend(flags, off)[source]
PrependBool(x)[source]

Prepend a bool to the Builder buffer.

Note: aligns and checks for space.

PrependBoolSlot(*args)[source]
PrependByte(x)[source]

Prepend a byte to the Builder buffer.

Note: aligns and checks for space.

PrependByteSlot(*args)[source]
PrependFloat32(x)[source]

Prepend a float32 to the Builder buffer.

Note: aligns and checks for space.

PrependFloat32Slot(*args)[source]
PrependFloat64(x)[source]

Prepend a float64 to the Builder buffer.

Note: aligns and checks for space.

PrependFloat64Slot(*args)[source]
PrependInt16(x)[source]

Prepend an int16 to the Builder buffer.

Note: aligns and checks for space.

PrependInt16Slot(*args)[source]
PrependInt32(x)[source]

Prepend an int32 to the Builder buffer.

Note: aligns and checks for space.

PrependInt32Slot(*args)[source]
PrependInt64(x)[source]

Prepend an int64 to the Builder buffer.

Note: aligns and checks for space.

PrependInt64Slot(*args)[source]
PrependInt8(x)[source]

Prepend an int8 to the Builder buffer.

Note: aligns and checks for space.

PrependInt8Slot(*args)[source]
PrependSOffsetTRelative(off)[source]

PrependSOffsetTRelative prepends an SOffsetT, relative to where it

will be written.

PrependSlot(flags, o, x, d)[source]
PrependStructSlot(v, x, d)[source]

PrependStructSlot prepends a struct onto the object at vtable slot o.

Structs are stored inline, so nothing additional is being added. In generated code, d is always 0.

PrependUOffsetTRelative(off)[source]

Prepends an unsigned offset into vector data, relative to where it

will be written.

PrependUOffsetTRelativeSlot(o, x, d)[source]

PrependUOffsetTRelativeSlot prepends an UOffsetT onto the object at

vtable slot o. If value x equals default d, then the slot will be set to zero and no other data will be written.

PrependUint16(x)[source]

Prepend an uint16 to the Builder buffer.

Note: aligns and checks for space.

PrependUint16Slot(*args)[source]
PrependUint32(x)[source]

Prepend an uint32 to the Builder buffer.

Note: aligns and checks for space.

PrependUint32Slot(*args)[source]
PrependUint64(x)[source]

Prepend an uint64 to the Builder buffer.

Note: aligns and checks for space.

PrependUint64Slot(*args)[source]
PrependUint8(x)[source]

Prepend an uint8 to the Builder buffer.

Note: aligns and checks for space.

PrependUint8Slot(*args)[source]
PrependVOffsetT(x)[source]
Slot(slotnum)[source]

Slot sets the vtable key voffset to the current location in the

buffer.

StartObject(numfields)[source]

StartObject initializes bookkeeping for writing a new object.

StartVector(elemSize, numElems, alignment)[source]

StartVector initializes bookkeeping for writing a new vector.

A vector has the following format:
  • <UOffsetT: number of elements in this vector>

  • <T: data>+, where T is the type of elements of this vector.

WriteVtable()[source]

WriteVtable serializes the vtable for the current object, if needed.

Before writing out the vtable, this checks pre-existing vtables for equality to this one. If an equal vtable is found, point the object to the existing vtable and return.

Because vtable values are sensitive to alignment of object data, not all logically-equal vtables will be deduplicated.

A vtable has the following format:

<VOffsetT: size of the vtable in bytes, including this value> <VOffsetT: size of the object in bytes, including the vtable offset> <VOffsetT: offset for a field> * N, where N is the number of fields

in the schema for this type. Includes deprecated fields.

Thus, a vtable is made of 2 + N elements, each VOffsetT bytes wide.

An object has the following format:

<SOffsetT: offset to this object’s vtable (may be negative)> <byte: data>+

__Finish(rootTable, sizePrefix, file_identifier=None)[source]

Finish finalizes a buffer, pointing to the given rootTable.

__slots__ = ('Bytes', 'current_vtable', 'head', 'minalign', 'objectEnd', 'vtables', 'nested',...[source]

Maximum buffer size constant, in bytes.

Builder will never allow it’s buffer grow over this size. Currently equals 2Gb.

assertNested()[source]

Check that we are in the process of building an object.

assertNotNested()[source]

Check that no other objects are being built while making this object.

If not, raise an exception.

assertStructIsInline(obj)[source]

Structs are always stored inline, so need to be created right

where they are used. You’ll get this error if you created it elsewhere.

current_vtable = None[source]
finished = False[source]
forceDefaults = False[source]
growByteBuffer()[source]

Doubles the size of the byteslice, and copies the old data towards

the end of the new buffer (since we build the buffer backwards).

head[source]
minalign = 1[source]
nested = False[source]
objectEnd = None[source]
sharedStrings[source]
vtables[source]
exception BuilderNotFinishedError[source]

Bases: RuntimeError

Error caused by not calling Finish before calling Output.

exception BuilderSizeError[source]

Bases: RuntimeError

Error caused by causing a Builder to exceed the hardcoded limit of 2

gigabytes.

exception EndVectorLengthMismatched[source]

Bases: RuntimeError

The number of elements passed to EndVector does not match the number

specified in StartVector.

exception IsNestedError[source]

Bases: RuntimeError

Error caused by using a Builder to begin an Object when an Object is

already being built.

exception IsNotNestedError[source]

Bases: RuntimeError

Error caused by using a Builder to write Object data when not inside

an Object.

exception OffsetArithmeticError[source]

Bases: RuntimeError

Error caused by an Offset arithmetic error.

Probably caused by bad writing of fields. This is considered an unreachable situation in normal circumstances.

exception StructIsNotInlineError[source]

Bases: RuntimeError

Error caused by using a Builder to write a Struct at a location that

is not the current Offset.

VtableMetadataFields = 2[source]
np[source]
vtableEqual(a, objectStart, b)[source]

vtableEqual compares an unwritten vtable to a written vtable.