Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame^] | 1 | package flatbuffers |
| 2 | |
| 3 | // Table wraps a byte slice and provides read access to its data. |
| 4 | // |
| 5 | // The variable `Pos` indicates the root of the FlatBuffers object therein. |
| 6 | type Table struct { |
| 7 | Bytes []byte |
| 8 | Pos UOffsetT // Always < 1<<31. |
| 9 | } |
| 10 | |
| 11 | // Offset provides access into the Table's vtable. |
| 12 | // |
| 13 | // Fields which are deprecated are ignored by checking against the vtable's length. |
| 14 | func (t *Table) Offset(vtableOffset VOffsetT) VOffsetT { |
| 15 | vtable := UOffsetT(SOffsetT(t.Pos) - t.GetSOffsetT(t.Pos)) |
| 16 | if vtableOffset < t.GetVOffsetT(vtable) { |
| 17 | return t.GetVOffsetT(vtable + UOffsetT(vtableOffset)) |
| 18 | } |
| 19 | return 0 |
| 20 | } |
| 21 | |
| 22 | // Indirect retrieves the relative offset stored at `offset`. |
| 23 | func (t *Table) Indirect(off UOffsetT) UOffsetT { |
| 24 | return off + GetUOffsetT(t.Bytes[off:]) |
| 25 | } |
| 26 | |
| 27 | // String gets a string from data stored inside the flatbuffer. |
| 28 | func (t *Table) String(off UOffsetT) string { |
| 29 | b := t.ByteVector(off) |
| 30 | return byteSliceToString(b) |
| 31 | } |
| 32 | |
| 33 | // ByteVector gets a byte slice from data stored inside the flatbuffer. |
| 34 | func (t *Table) ByteVector(off UOffsetT) []byte { |
| 35 | off += GetUOffsetT(t.Bytes[off:]) |
| 36 | start := off + UOffsetT(SizeUOffsetT) |
| 37 | length := GetUOffsetT(t.Bytes[off:]) |
| 38 | return t.Bytes[start : start+length] |
| 39 | } |
| 40 | |
| 41 | // VectorLen retrieves the length of the vector whose offset is stored at |
| 42 | // "off" in this object. |
| 43 | func (t *Table) VectorLen(off UOffsetT) int { |
| 44 | off += t.Pos |
| 45 | off += GetUOffsetT(t.Bytes[off:]) |
| 46 | return int(GetUOffsetT(t.Bytes[off:])) |
| 47 | } |
| 48 | |
| 49 | // Vector retrieves the start of data of the vector whose offset is stored |
| 50 | // at "off" in this object. |
| 51 | func (t *Table) Vector(off UOffsetT) UOffsetT { |
| 52 | off += t.Pos |
| 53 | x := off + GetUOffsetT(t.Bytes[off:]) |
| 54 | // data starts after metadata containing the vector length |
| 55 | x += UOffsetT(SizeUOffsetT) |
| 56 | return x |
| 57 | } |
| 58 | |
| 59 | // Union initializes any Table-derived type to point to the union at the given |
| 60 | // offset. |
| 61 | func (t *Table) Union(t2 *Table, off UOffsetT) { |
| 62 | off += t.Pos |
| 63 | t2.Pos = off + t.GetUOffsetT(off) |
| 64 | t2.Bytes = t.Bytes |
| 65 | } |
| 66 | |
| 67 | // GetBool retrieves a bool at the given offset. |
| 68 | func (t *Table) GetBool(off UOffsetT) bool { |
| 69 | return GetBool(t.Bytes[off:]) |
| 70 | } |
| 71 | |
| 72 | // GetByte retrieves a byte at the given offset. |
| 73 | func (t *Table) GetByte(off UOffsetT) byte { |
| 74 | return GetByte(t.Bytes[off:]) |
| 75 | } |
| 76 | |
| 77 | // GetUint8 retrieves a uint8 at the given offset. |
| 78 | func (t *Table) GetUint8(off UOffsetT) uint8 { |
| 79 | return GetUint8(t.Bytes[off:]) |
| 80 | } |
| 81 | |
| 82 | // GetUint16 retrieves a uint16 at the given offset. |
| 83 | func (t *Table) GetUint16(off UOffsetT) uint16 { |
| 84 | return GetUint16(t.Bytes[off:]) |
| 85 | } |
| 86 | |
| 87 | // GetUint32 retrieves a uint32 at the given offset. |
| 88 | func (t *Table) GetUint32(off UOffsetT) uint32 { |
| 89 | return GetUint32(t.Bytes[off:]) |
| 90 | } |
| 91 | |
| 92 | // GetUint64 retrieves a uint64 at the given offset. |
| 93 | func (t *Table) GetUint64(off UOffsetT) uint64 { |
| 94 | return GetUint64(t.Bytes[off:]) |
| 95 | } |
| 96 | |
| 97 | // GetInt8 retrieves a int8 at the given offset. |
| 98 | func (t *Table) GetInt8(off UOffsetT) int8 { |
| 99 | return GetInt8(t.Bytes[off:]) |
| 100 | } |
| 101 | |
| 102 | // GetInt16 retrieves a int16 at the given offset. |
| 103 | func (t *Table) GetInt16(off UOffsetT) int16 { |
| 104 | return GetInt16(t.Bytes[off:]) |
| 105 | } |
| 106 | |
| 107 | // GetInt32 retrieves a int32 at the given offset. |
| 108 | func (t *Table) GetInt32(off UOffsetT) int32 { |
| 109 | return GetInt32(t.Bytes[off:]) |
| 110 | } |
| 111 | |
| 112 | // GetInt64 retrieves a int64 at the given offset. |
| 113 | func (t *Table) GetInt64(off UOffsetT) int64 { |
| 114 | return GetInt64(t.Bytes[off:]) |
| 115 | } |
| 116 | |
| 117 | // GetFloat32 retrieves a float32 at the given offset. |
| 118 | func (t *Table) GetFloat32(off UOffsetT) float32 { |
| 119 | return GetFloat32(t.Bytes[off:]) |
| 120 | } |
| 121 | |
| 122 | // GetFloat64 retrieves a float64 at the given offset. |
| 123 | func (t *Table) GetFloat64(off UOffsetT) float64 { |
| 124 | return GetFloat64(t.Bytes[off:]) |
| 125 | } |
| 126 | |
| 127 | // GetUOffsetT retrieves a UOffsetT at the given offset. |
| 128 | func (t *Table) GetUOffsetT(off UOffsetT) UOffsetT { |
| 129 | return GetUOffsetT(t.Bytes[off:]) |
| 130 | } |
| 131 | |
| 132 | // GetVOffsetT retrieves a VOffsetT at the given offset. |
| 133 | func (t *Table) GetVOffsetT(off UOffsetT) VOffsetT { |
| 134 | return GetVOffsetT(t.Bytes[off:]) |
| 135 | } |
| 136 | |
| 137 | // GetSOffsetT retrieves a SOffsetT at the given offset. |
| 138 | func (t *Table) GetSOffsetT(off UOffsetT) SOffsetT { |
| 139 | return GetSOffsetT(t.Bytes[off:]) |
| 140 | } |
| 141 | |
| 142 | // GetBoolSlot retrieves the bool that the given vtable location |
| 143 | // points to. If the vtable value is zero, the default value `d` |
| 144 | // will be returned. |
| 145 | func (t *Table) GetBoolSlot(slot VOffsetT, d bool) bool { |
| 146 | off := t.Offset(slot) |
| 147 | if off == 0 { |
| 148 | return d |
| 149 | } |
| 150 | |
| 151 | return t.GetBool(t.Pos + UOffsetT(off)) |
| 152 | } |
| 153 | |
| 154 | // GetByteSlot retrieves the byte that the given vtable location |
| 155 | // points to. If the vtable value is zero, the default value `d` |
| 156 | // will be returned. |
| 157 | func (t *Table) GetByteSlot(slot VOffsetT, d byte) byte { |
| 158 | off := t.Offset(slot) |
| 159 | if off == 0 { |
| 160 | return d |
| 161 | } |
| 162 | |
| 163 | return t.GetByte(t.Pos + UOffsetT(off)) |
| 164 | } |
| 165 | |
| 166 | // GetInt8Slot retrieves the int8 that the given vtable location |
| 167 | // points to. If the vtable value is zero, the default value `d` |
| 168 | // will be returned. |
| 169 | func (t *Table) GetInt8Slot(slot VOffsetT, d int8) int8 { |
| 170 | off := t.Offset(slot) |
| 171 | if off == 0 { |
| 172 | return d |
| 173 | } |
| 174 | |
| 175 | return t.GetInt8(t.Pos + UOffsetT(off)) |
| 176 | } |
| 177 | |
| 178 | // GetUint8Slot retrieves the uint8 that the given vtable location |
| 179 | // points to. If the vtable value is zero, the default value `d` |
| 180 | // will be returned. |
| 181 | func (t *Table) GetUint8Slot(slot VOffsetT, d uint8) uint8 { |
| 182 | off := t.Offset(slot) |
| 183 | if off == 0 { |
| 184 | return d |
| 185 | } |
| 186 | |
| 187 | return t.GetUint8(t.Pos + UOffsetT(off)) |
| 188 | } |
| 189 | |
| 190 | // GetInt16Slot retrieves the int16 that the given vtable location |
| 191 | // points to. If the vtable value is zero, the default value `d` |
| 192 | // will be returned. |
| 193 | func (t *Table) GetInt16Slot(slot VOffsetT, d int16) int16 { |
| 194 | off := t.Offset(slot) |
| 195 | if off == 0 { |
| 196 | return d |
| 197 | } |
| 198 | |
| 199 | return t.GetInt16(t.Pos + UOffsetT(off)) |
| 200 | } |
| 201 | |
| 202 | // GetUint16Slot retrieves the uint16 that the given vtable location |
| 203 | // points to. If the vtable value is zero, the default value `d` |
| 204 | // will be returned. |
| 205 | func (t *Table) GetUint16Slot(slot VOffsetT, d uint16) uint16 { |
| 206 | off := t.Offset(slot) |
| 207 | if off == 0 { |
| 208 | return d |
| 209 | } |
| 210 | |
| 211 | return t.GetUint16(t.Pos + UOffsetT(off)) |
| 212 | } |
| 213 | |
| 214 | // GetInt32Slot retrieves the int32 that the given vtable location |
| 215 | // points to. If the vtable value is zero, the default value `d` |
| 216 | // will be returned. |
| 217 | func (t *Table) GetInt32Slot(slot VOffsetT, d int32) int32 { |
| 218 | off := t.Offset(slot) |
| 219 | if off == 0 { |
| 220 | return d |
| 221 | } |
| 222 | |
| 223 | return t.GetInt32(t.Pos + UOffsetT(off)) |
| 224 | } |
| 225 | |
| 226 | // GetUint32Slot retrieves the uint32 that the given vtable location |
| 227 | // points to. If the vtable value is zero, the default value `d` |
| 228 | // will be returned. |
| 229 | func (t *Table) GetUint32Slot(slot VOffsetT, d uint32) uint32 { |
| 230 | off := t.Offset(slot) |
| 231 | if off == 0 { |
| 232 | return d |
| 233 | } |
| 234 | |
| 235 | return t.GetUint32(t.Pos + UOffsetT(off)) |
| 236 | } |
| 237 | |
| 238 | // GetInt64Slot retrieves the int64 that the given vtable location |
| 239 | // points to. If the vtable value is zero, the default value `d` |
| 240 | // will be returned. |
| 241 | func (t *Table) GetInt64Slot(slot VOffsetT, d int64) int64 { |
| 242 | off := t.Offset(slot) |
| 243 | if off == 0 { |
| 244 | return d |
| 245 | } |
| 246 | |
| 247 | return t.GetInt64(t.Pos + UOffsetT(off)) |
| 248 | } |
| 249 | |
| 250 | // GetUint64Slot retrieves the uint64 that the given vtable location |
| 251 | // points to. If the vtable value is zero, the default value `d` |
| 252 | // will be returned. |
| 253 | func (t *Table) GetUint64Slot(slot VOffsetT, d uint64) uint64 { |
| 254 | off := t.Offset(slot) |
| 255 | if off == 0 { |
| 256 | return d |
| 257 | } |
| 258 | |
| 259 | return t.GetUint64(t.Pos + UOffsetT(off)) |
| 260 | } |
| 261 | |
| 262 | // GetFloat32Slot retrieves the float32 that the given vtable location |
| 263 | // points to. If the vtable value is zero, the default value `d` |
| 264 | // will be returned. |
| 265 | func (t *Table) GetFloat32Slot(slot VOffsetT, d float32) float32 { |
| 266 | off := t.Offset(slot) |
| 267 | if off == 0 { |
| 268 | return d |
| 269 | } |
| 270 | |
| 271 | return t.GetFloat32(t.Pos + UOffsetT(off)) |
| 272 | } |
| 273 | |
| 274 | // GetFloat64Slot retrieves the float64 that the given vtable location |
| 275 | // points to. If the vtable value is zero, the default value `d` |
| 276 | // will be returned. |
| 277 | func (t *Table) GetFloat64Slot(slot VOffsetT, d float64) float64 { |
| 278 | off := t.Offset(slot) |
| 279 | if off == 0 { |
| 280 | return d |
| 281 | } |
| 282 | |
| 283 | return t.GetFloat64(t.Pos + UOffsetT(off)) |
| 284 | } |
| 285 | |
| 286 | // GetVOffsetTSlot retrieves the VOffsetT that the given vtable location |
| 287 | // points to. If the vtable value is zero, the default value `d` |
| 288 | // will be returned. |
| 289 | func (t *Table) GetVOffsetTSlot(slot VOffsetT, d VOffsetT) VOffsetT { |
| 290 | off := t.Offset(slot) |
| 291 | if off == 0 { |
| 292 | return d |
| 293 | } |
| 294 | return VOffsetT(off) |
| 295 | } |
| 296 | |
| 297 | // MutateBool updates a bool at the given offset. |
| 298 | func (t *Table) MutateBool(off UOffsetT, n bool) bool { |
| 299 | WriteBool(t.Bytes[off:], n) |
| 300 | return true |
| 301 | } |
| 302 | |
| 303 | // MutateByte updates a Byte at the given offset. |
| 304 | func (t *Table) MutateByte(off UOffsetT, n byte) bool { |
| 305 | WriteByte(t.Bytes[off:], n) |
| 306 | return true |
| 307 | } |
| 308 | |
| 309 | // MutateUint8 updates a Uint8 at the given offset. |
| 310 | func (t *Table) MutateUint8(off UOffsetT, n uint8) bool { |
| 311 | WriteUint8(t.Bytes[off:], n) |
| 312 | return true |
| 313 | } |
| 314 | |
| 315 | // MutateUint16 updates a Uint16 at the given offset. |
| 316 | func (t *Table) MutateUint16(off UOffsetT, n uint16) bool { |
| 317 | WriteUint16(t.Bytes[off:], n) |
| 318 | return true |
| 319 | } |
| 320 | |
| 321 | // MutateUint32 updates a Uint32 at the given offset. |
| 322 | func (t *Table) MutateUint32(off UOffsetT, n uint32) bool { |
| 323 | WriteUint32(t.Bytes[off:], n) |
| 324 | return true |
| 325 | } |
| 326 | |
| 327 | // MutateUint64 updates a Uint64 at the given offset. |
| 328 | func (t *Table) MutateUint64(off UOffsetT, n uint64) bool { |
| 329 | WriteUint64(t.Bytes[off:], n) |
| 330 | return true |
| 331 | } |
| 332 | |
| 333 | // MutateInt8 updates a Int8 at the given offset. |
| 334 | func (t *Table) MutateInt8(off UOffsetT, n int8) bool { |
| 335 | WriteInt8(t.Bytes[off:], n) |
| 336 | return true |
| 337 | } |
| 338 | |
| 339 | // MutateInt16 updates a Int16 at the given offset. |
| 340 | func (t *Table) MutateInt16(off UOffsetT, n int16) bool { |
| 341 | WriteInt16(t.Bytes[off:], n) |
| 342 | return true |
| 343 | } |
| 344 | |
| 345 | // MutateInt32 updates a Int32 at the given offset. |
| 346 | func (t *Table) MutateInt32(off UOffsetT, n int32) bool { |
| 347 | WriteInt32(t.Bytes[off:], n) |
| 348 | return true |
| 349 | } |
| 350 | |
| 351 | // MutateInt64 updates a Int64 at the given offset. |
| 352 | func (t *Table) MutateInt64(off UOffsetT, n int64) bool { |
| 353 | WriteInt64(t.Bytes[off:], n) |
| 354 | return true |
| 355 | } |
| 356 | |
| 357 | // MutateFloat32 updates a Float32 at the given offset. |
| 358 | func (t *Table) MutateFloat32(off UOffsetT, n float32) bool { |
| 359 | WriteFloat32(t.Bytes[off:], n) |
| 360 | return true |
| 361 | } |
| 362 | |
| 363 | // MutateFloat64 updates a Float64 at the given offset. |
| 364 | func (t *Table) MutateFloat64(off UOffsetT, n float64) bool { |
| 365 | WriteFloat64(t.Bytes[off:], n) |
| 366 | return true |
| 367 | } |
| 368 | |
| 369 | // MutateUOffsetT updates a UOffsetT at the given offset. |
| 370 | func (t *Table) MutateUOffsetT(off UOffsetT, n UOffsetT) bool { |
| 371 | WriteUOffsetT(t.Bytes[off:], n) |
| 372 | return true |
| 373 | } |
| 374 | |
| 375 | // MutateVOffsetT updates a VOffsetT at the given offset. |
| 376 | func (t *Table) MutateVOffsetT(off UOffsetT, n VOffsetT) bool { |
| 377 | WriteVOffsetT(t.Bytes[off:], n) |
| 378 | return true |
| 379 | } |
| 380 | |
| 381 | // MutateSOffsetT updates a SOffsetT at the given offset. |
| 382 | func (t *Table) MutateSOffsetT(off UOffsetT, n SOffsetT) bool { |
| 383 | WriteSOffsetT(t.Bytes[off:], n) |
| 384 | return true |
| 385 | } |
| 386 | |
| 387 | // MutateBoolSlot updates the bool at given vtable location |
| 388 | func (t *Table) MutateBoolSlot(slot VOffsetT, n bool) bool { |
| 389 | if off := t.Offset(slot); off != 0 { |
| 390 | t.MutateBool(t.Pos+UOffsetT(off), n) |
| 391 | return true |
| 392 | } |
| 393 | |
| 394 | return false |
| 395 | } |
| 396 | |
| 397 | // MutateByteSlot updates the byte at given vtable location |
| 398 | func (t *Table) MutateByteSlot(slot VOffsetT, n byte) bool { |
| 399 | if off := t.Offset(slot); off != 0 { |
| 400 | t.MutateByte(t.Pos+UOffsetT(off), n) |
| 401 | return true |
| 402 | } |
| 403 | |
| 404 | return false |
| 405 | } |
| 406 | |
| 407 | // MutateInt8Slot updates the int8 at given vtable location |
| 408 | func (t *Table) MutateInt8Slot(slot VOffsetT, n int8) bool { |
| 409 | if off := t.Offset(slot); off != 0 { |
| 410 | t.MutateInt8(t.Pos+UOffsetT(off), n) |
| 411 | return true |
| 412 | } |
| 413 | |
| 414 | return false |
| 415 | } |
| 416 | |
| 417 | // MutateUint8Slot updates the uint8 at given vtable location |
| 418 | func (t *Table) MutateUint8Slot(slot VOffsetT, n uint8) bool { |
| 419 | if off := t.Offset(slot); off != 0 { |
| 420 | t.MutateUint8(t.Pos+UOffsetT(off), n) |
| 421 | return true |
| 422 | } |
| 423 | |
| 424 | return false |
| 425 | } |
| 426 | |
| 427 | // MutateInt16Slot updates the int16 at given vtable location |
| 428 | func (t *Table) MutateInt16Slot(slot VOffsetT, n int16) bool { |
| 429 | if off := t.Offset(slot); off != 0 { |
| 430 | t.MutateInt16(t.Pos+UOffsetT(off), n) |
| 431 | return true |
| 432 | } |
| 433 | |
| 434 | return false |
| 435 | } |
| 436 | |
| 437 | // MutateUint16Slot updates the uint16 at given vtable location |
| 438 | func (t *Table) MutateUint16Slot(slot VOffsetT, n uint16) bool { |
| 439 | if off := t.Offset(slot); off != 0 { |
| 440 | t.MutateUint16(t.Pos+UOffsetT(off), n) |
| 441 | return true |
| 442 | } |
| 443 | |
| 444 | return false |
| 445 | } |
| 446 | |
| 447 | // MutateInt32Slot updates the int32 at given vtable location |
| 448 | func (t *Table) MutateInt32Slot(slot VOffsetT, n int32) bool { |
| 449 | if off := t.Offset(slot); off != 0 { |
| 450 | t.MutateInt32(t.Pos+UOffsetT(off), n) |
| 451 | return true |
| 452 | } |
| 453 | |
| 454 | return false |
| 455 | } |
| 456 | |
| 457 | // MutateUint32Slot updates the uint32 at given vtable location |
| 458 | func (t *Table) MutateUint32Slot(slot VOffsetT, n uint32) bool { |
| 459 | if off := t.Offset(slot); off != 0 { |
| 460 | t.MutateUint32(t.Pos+UOffsetT(off), n) |
| 461 | return true |
| 462 | } |
| 463 | |
| 464 | return false |
| 465 | } |
| 466 | |
| 467 | // MutateInt64Slot updates the int64 at given vtable location |
| 468 | func (t *Table) MutateInt64Slot(slot VOffsetT, n int64) bool { |
| 469 | if off := t.Offset(slot); off != 0 { |
| 470 | t.MutateInt64(t.Pos+UOffsetT(off), n) |
| 471 | return true |
| 472 | } |
| 473 | |
| 474 | return false |
| 475 | } |
| 476 | |
| 477 | // MutateUint64Slot updates the uint64 at given vtable location |
| 478 | func (t *Table) MutateUint64Slot(slot VOffsetT, n uint64) bool { |
| 479 | if off := t.Offset(slot); off != 0 { |
| 480 | t.MutateUint64(t.Pos+UOffsetT(off), n) |
| 481 | return true |
| 482 | } |
| 483 | |
| 484 | return false |
| 485 | } |
| 486 | |
| 487 | // MutateFloat32Slot updates the float32 at given vtable location |
| 488 | func (t *Table) MutateFloat32Slot(slot VOffsetT, n float32) bool { |
| 489 | if off := t.Offset(slot); off != 0 { |
| 490 | t.MutateFloat32(t.Pos+UOffsetT(off), n) |
| 491 | return true |
| 492 | } |
| 493 | |
| 494 | return false |
| 495 | } |
| 496 | |
| 497 | // MutateFloat64Slot updates the float64 at given vtable location |
| 498 | func (t *Table) MutateFloat64Slot(slot VOffsetT, n float64) bool { |
| 499 | if off := t.Offset(slot); off != 0 { |
| 500 | t.MutateFloat64(t.Pos+UOffsetT(off), n) |
| 501 | return true |
| 502 | } |
| 503 | |
| 504 | return false |
| 505 | } |