Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 1 | package flatbuffers |
| 2 | |
| 3 | // FlatBuffer is the interface that represents a flatbuffer. |
| 4 | type FlatBuffer interface { |
| 5 | Table() Table |
| 6 | Init(buf []byte, i UOffsetT) |
| 7 | } |
| 8 | |
| 9 | // GetRootAs is a generic helper to initialize a FlatBuffer with the provided buffer bytes and its data offset. |
| 10 | func GetRootAs(buf []byte, offset UOffsetT, fb FlatBuffer) { |
| 11 | n := GetUOffsetT(buf[offset:]) |
| 12 | fb.Init(buf, n+offset) |
| 13 | } |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 14 | |
| 15 | // GetSizePrefixedRootAs is a generic helper to initialize a FlatBuffer with the provided size-prefixed buffer |
| 16 | // bytes and its data offset |
| 17 | func GetSizePrefixedRootAs(buf []byte, offset UOffsetT, fb FlatBuffer) { |
| 18 | n := GetUOffsetT(buf[offset+sizePrefixLength:]) |
| 19 | fb.Init(buf, n+offset+sizePrefixLength) |
| 20 | } |
| 21 | |
| 22 | // GetSizePrefix reads the size from a size-prefixed flatbuffer |
| 23 | func GetSizePrefix(buf []byte, offset UOffsetT) uint32 { |
| 24 | return GetUint32(buf[offset:]) |
| 25 | } |