blob: e10ba95e9cd85e7b45c10c5608539f631d340537 [file] [log] [blame]
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001/*
2 * Copyright 2014 Google Inc. All rights reserved.
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17using System;
18using System.Text;
19
20namespace FlatBuffers
21{
22 /// <summary>
23 /// All tables in the generated code derive from this struct, and add their own accessors.
24 /// </summary>
25 public struct Table
26 {
27 public int bb_pos { get; private set; }
28 public ByteBuffer bb { get; private set; }
29
30 public ByteBuffer ByteBuffer { get { return bb; } }
31
32 // Re-init the internal state with an external buffer {@code ByteBuffer} and an offset within.
33 public Table(int _i, ByteBuffer _bb)
34 {
35 bb = _bb;
36 bb_pos = _i;
37 }
38
39 // Look up a field in the vtable, return an offset into the object, or 0 if the field is not
40 // present.
41 public int __offset(int vtableOffset)
42 {
43 int vtable = bb_pos - bb.GetInt(bb_pos);
44 return vtableOffset < bb.GetShort(vtable) ? (int)bb.GetShort(vtable + vtableOffset) : 0;
45 }
46
47 public static int __offset(int vtableOffset, int offset, ByteBuffer bb)
48 {
49 int vtable = bb.Length - offset;
50 return (int)bb.GetShort(vtable + vtableOffset - bb.GetInt(vtable)) + vtable;
51 }
52
53 // Retrieve the relative offset stored at "offset"
54 public int __indirect(int offset)
55 {
56 return offset + bb.GetInt(offset);
57 }
58
59 public static int __indirect(int offset, ByteBuffer bb)
60 {
61 return offset + bb.GetInt(offset);
62 }
63
64 // Create a .NET String from UTF-8 data stored inside the flatbuffer.
65 public string __string(int offset)
66 {
67 offset += bb.GetInt(offset);
68 var len = bb.GetInt(offset);
69 var startPos = offset + sizeof(int);
70 return bb.GetStringUTF8(startPos, len);
71 }
72
73 // Get the length of a vector whose offset is stored at "offset" in this object.
74 public int __vector_len(int offset)
75 {
76 offset += bb_pos;
77 offset += bb.GetInt(offset);
78 return bb.GetInt(offset);
79 }
80
81 // Get the start of data of a vector whose offset is stored at "offset" in this object.
82 public int __vector(int offset)
83 {
84 offset += bb_pos;
85 return offset + bb.GetInt(offset) + sizeof(int); // data starts after the length
86 }
87
88#if ENABLE_SPAN_T
89 // Get the data of a vector whoses offset is stored at "offset" in this object as an
90 // Spant&lt;byte&gt;. If the vector is not present in the ByteBuffer,
91 // then an empty span will be returned.
92 public Span<byte> __vector_as_span(int offset)
93 {
94 var o = this.__offset(offset);
95 if (0 == o)
96 {
97 return new Span<byte>();
98 }
99
100 var pos = this.__vector(o);
101 var len = this.__vector_len(o);
102 return bb.ToSpan(pos, len);
103 }
104#else
105 // Get the data of a vector whoses offset is stored at "offset" in this object as an
106 // ArraySegment&lt;byte&gt;. If the vector is not present in the ByteBuffer,
107 // then a null value will be returned.
108 public ArraySegment<byte>? __vector_as_arraysegment(int offset)
109 {
110 var o = this.__offset(offset);
111 if (0 == o)
112 {
113 return null;
114 }
115
116 var pos = this.__vector(o);
117 var len = this.__vector_len(o);
118 return bb.ToArraySegment(pos, len);
119 }
120#endif
121
122 // Get the data of a vector whoses offset is stored at "offset" in this object as an
123 // T[]. If the vector is not present in the ByteBuffer, then a null value will be
124 // returned.
125 public T[] __vector_as_array<T>(int offset)
126 where T : struct
127 {
128 if(!BitConverter.IsLittleEndian)
129 {
130 throw new NotSupportedException("Getting typed arrays on a Big Endian " +
131 "system is not support");
132 }
133
134 var o = this.__offset(offset);
135 if (0 == o)
136 {
137 return null;
138 }
139
140 var pos = this.__vector(o);
141 var len = this.__vector_len(o);
142 return bb.ToArray<T>(pos, len);
143 }
144
145 // Initialize any Table-derived type to point to the union at the given offset.
146 public T __union<T>(int offset) where T : struct, IFlatbufferObject
147 {
148 offset += bb_pos;
149 T t = new T();
150 t.__init(offset + bb.GetInt(offset), bb);
151 return t;
152 }
153
154 public static bool __has_identifier(ByteBuffer bb, string ident)
155 {
156 if (ident.Length != FlatBufferConstants.FileIdentifierLength)
157 throw new ArgumentException("FlatBuffers: file identifier must be length " + FlatBufferConstants.FileIdentifierLength, "ident");
158
159 for (var i = 0; i < FlatBufferConstants.FileIdentifierLength; i++)
160 {
161 if (ident[i] != (char)bb.Get(bb.Position + sizeof(int) + i)) return false;
162 }
163
164 return true;
165 }
166
167 // Compare strings in the ByteBuffer.
168 public static int CompareStrings(int offset_1, int offset_2, ByteBuffer bb)
169 {
170 offset_1 += bb.GetInt(offset_1);
171 offset_2 += bb.GetInt(offset_2);
172 var len_1 = bb.GetInt(offset_1);
173 var len_2 = bb.GetInt(offset_2);
174 var startPos_1 = offset_1 + sizeof(int);
175 var startPos_2 = offset_2 + sizeof(int);
176 var len = Math.Min(len_1, len_2);
177 for(int i = 0; i < len; i++) {
178 byte b1 = bb.Get(i + startPos_1);
179 byte b2 = bb.Get(i + startPos_2);
180 if (b1 != b2)
181 return b1 - b2;
182 }
183 return len_1 - len_2;
184 }
185
186 // Compare string from the ByteBuffer with the string object
187 public static int CompareStrings(int offset_1, byte[] key, ByteBuffer bb)
188 {
189 offset_1 += bb.GetInt(offset_1);
190 var len_1 = bb.GetInt(offset_1);
191 var len_2 = key.Length;
192 var startPos_1 = offset_1 + sizeof(int);
193 var len = Math.Min(len_1, len_2);
194 for (int i = 0; i < len; i++) {
195 byte b = bb.Get(i + startPos_1);
196 if (b != key[i])
197 return b - key[i];
198 }
199 return len_1 - len_2;
200 }
201 }
202}