table.go 13 KB

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