2 * Copyright 2014 Google Inc. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 using System.Runtime.InteropServices;
24 /// All tables in the generated code derive from this struct, and add their own accessors.
28 public int bb_pos { get; private set; }
29 public ByteBuffer bb { get; private set; }
31 public ByteBuffer ByteBuffer { get { return bb; } }
33 // Re-init the internal state with an external buffer {@code ByteBuffer} and an offset within.
34 public Table(int _i, ByteBuffer _bb) : this()
40 // Look up a field in the vtable, return an offset into the object, or 0 if the field is not
42 public int __offset(int vtableOffset)
44 int vtable = bb_pos - bb.GetInt(bb_pos);
45 return vtableOffset < bb.GetShort(vtable) ? (int)bb.GetShort(vtable + vtableOffset) : 0;
48 public static int __offset(int vtableOffset, int offset, ByteBuffer bb)
50 int vtable = bb.Length - offset;
51 return (int)bb.GetShort(vtable + vtableOffset - bb.GetInt(vtable)) + vtable;
54 // Retrieve the relative offset stored at "offset"
55 public int __indirect(int offset)
57 return offset + bb.GetInt(offset);
60 public static int __indirect(int offset, ByteBuffer bb)
62 return offset + bb.GetInt(offset);
65 // Create a .NET String from UTF-8 data stored inside the flatbuffer.
66 public string __string(int offset)
68 offset += bb.GetInt(offset);
69 var len = bb.GetInt(offset);
70 var startPos = offset + sizeof(int);
71 return bb.GetStringUTF8(startPos, len);
74 // Get the length of a vector whose offset is stored at "offset" in this object.
75 public int __vector_len(int offset)
78 offset += bb.GetInt(offset);
79 return bb.GetInt(offset);
82 // Get the start of data of a vector whose offset is stored at "offset" in this object.
83 public int __vector(int offset)
86 return offset + bb.GetInt(offset) + sizeof(int); // data starts after the length
89 #if ENABLE_SPAN_T && (UNSAFE_BYTEBUFFER || NETSTANDARD2_1)
90 // Get the data of a vector whoses offset is stored at "offset" in this object as an
91 // Spant<byte>. If the vector is not present in the ByteBuffer,
92 // then an empty span will be returned.
93 public Span<T> __vector_as_span<T>(int offset, int elementSize) where T : struct
95 if (!BitConverter.IsLittleEndian)
97 throw new NotSupportedException("Getting typed span on a Big Endian " +
98 "system is not support");
101 var o = this.__offset(offset);
104 return new Span<T>();
107 var pos = this.__vector(o);
108 var len = this.__vector_len(o);
109 return MemoryMarshal.Cast<byte, T>(bb.ToSpan(pos, len * elementSize));
112 // Get the data of a vector whoses offset is stored at "offset" in this object as an
113 // ArraySegment<byte>. If the vector is not present in the ByteBuffer,
114 // then a null value will be returned.
115 public ArraySegment<byte>? __vector_as_arraysegment(int offset)
117 var o = this.__offset(offset);
123 var pos = this.__vector(o);
124 var len = this.__vector_len(o);
125 return bb.ToArraySegment(pos, len);
129 // Get the data of a vector whoses offset is stored at "offset" in this object as an
130 // T[]. If the vector is not present in the ByteBuffer, then a null value will be
132 public T[] __vector_as_array<T>(int offset)
135 if(!BitConverter.IsLittleEndian)
137 throw new NotSupportedException("Getting typed arrays on a Big Endian " +
138 "system is not support");
141 var o = this.__offset(offset);
147 var pos = this.__vector(o);
148 var len = this.__vector_len(o);
149 return bb.ToArray<T>(pos, len);
152 // Initialize any Table-derived type to point to the union at the given offset.
153 public T __union<T>(int offset) where T : struct, IFlatbufferObject
156 t.__init(__indirect(offset), bb);
160 public static bool __has_identifier(ByteBuffer bb, string ident)
162 if (ident.Length != FlatBufferConstants.FileIdentifierLength)
163 throw new ArgumentException("FlatBuffers: file identifier must be length " + FlatBufferConstants.FileIdentifierLength, "ident");
165 for (var i = 0; i < FlatBufferConstants.FileIdentifierLength; i++)
167 if (ident[i] != (char)bb.Get(bb.Position + sizeof(int) + i)) return false;
173 // Compare strings in the ByteBuffer.
174 public static int CompareStrings(int offset_1, int offset_2, ByteBuffer bb)
176 offset_1 += bb.GetInt(offset_1);
177 offset_2 += bb.GetInt(offset_2);
178 var len_1 = bb.GetInt(offset_1);
179 var len_2 = bb.GetInt(offset_2);
180 var startPos_1 = offset_1 + sizeof(int);
181 var startPos_2 = offset_2 + sizeof(int);
182 var len = Math.Min(len_1, len_2);
183 for(int i = 0; i < len; i++) {
184 byte b1 = bb.Get(i + startPos_1);
185 byte b2 = bb.Get(i + startPos_2);
189 return len_1 - len_2;
192 // Compare string from the ByteBuffer with the string object
193 public static int CompareStrings(int offset_1, byte[] key, ByteBuffer bb)
195 offset_1 += bb.GetInt(offset_1);
196 var len_1 = bb.GetInt(offset_1);
197 var len_2 = key.Length;
198 var startPos_1 = offset_1 + sizeof(int);
199 var len = Math.Min(len_1, len_2);
200 for (int i = 0; i < len; i++) {
201 byte b = bb.Get(i + startPos_1);
205 return len_1 - len_2;