Table.cs revision 8fdced4e1141a98fb37e952058874423702347ca
1/*
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 class, and add their own accessors.
24    /// </summary>
25    public abstract class Table
26    {
27        protected int bb_pos;
28        protected ByteBuffer bb;
29
30        public ByteBuffer ByteBuffer { get { return bb; } }
31
32        // Look up a field in the vtable, return an offset into the object, or 0 if the field is not
33        // present.
34        protected int __offset(int vtableOffset)
35        {
36            int vtable = bb_pos - bb.GetInt(bb_pos);
37            return vtableOffset < bb.GetShort(vtable) ? (int)bb.GetShort(vtable + vtableOffset) : 0;
38        }
39
40        protected static int __offset(int vtableOffset, int offset, ByteBuffer bb)
41        {
42            int vtable = bb.Length - offset;
43            return (int)bb.GetShort(vtable + vtableOffset - bb.GetInt(vtable)) + vtable;
44        }
45
46        // Retrieve the relative offset stored at "offset"
47        protected int __indirect(int offset)
48        {
49            return offset + bb.GetInt(offset);
50        }
51
52        // Create a .NET String from UTF-8 data stored inside the flatbuffer.
53        protected string __string(int offset)
54        {
55            offset += bb.GetInt(offset);
56            var len = bb.GetInt(offset);
57            var startPos = offset + sizeof(int);
58            return Encoding.UTF8.GetString(bb.Data, startPos , len);
59        }
60
61        // Get the length of a vector whose offset is stored at "offset" in this object.
62        protected int __vector_len(int offset)
63        {
64            offset += bb_pos;
65            offset += bb.GetInt(offset);
66            return bb.GetInt(offset);
67        }
68
69        // Get the start of data of a vector whose offset is stored at "offset" in this object.
70        protected int __vector(int offset)
71        {
72            offset += bb_pos;
73            return offset + bb.GetInt(offset) + sizeof(int);  // data starts after the length
74        }
75
76        // Get the data of a vector whoses offset is stored at "offset" in this object as an
77        // ArraySegment&lt;byte&gt;. If the vector is not present in the ByteBuffer,
78        // then a null value will be returned.
79        protected ArraySegment<byte>? __vector_as_arraysegment(int offset) {
80            var o = this.__offset(offset);
81            if (0 == o)
82            {
83                return null;
84            }
85
86            var pos = this.__vector(o);
87            var len = this.__vector_len(o);
88            return new ArraySegment<byte>(this.bb.Data, pos, len);
89        }
90
91        // Initialize any Table-derived type to point to the union at the given offset.
92        protected TTable __union<TTable>(TTable t, int offset) where TTable : Table
93        {
94            offset += bb_pos;
95            t.bb_pos = offset + bb.GetInt(offset);
96            t.bb = bb;
97            return t;
98        }
99
100        protected static bool __has_identifier(ByteBuffer bb, string ident)
101        {
102            if (ident.Length != FlatBufferConstants.FileIdentifierLength)
103                throw new ArgumentException("FlatBuffers: file identifier must be length " + FlatBufferConstants.FileIdentifierLength, "ident");
104
105            for (var i = 0; i < FlatBufferConstants.FileIdentifierLength; i++)
106            {
107                if (ident[i] != (char)bb.Get(bb.Position + sizeof(int) + i)) return false;
108            }
109
110            return true;
111        }
112
113        // Compare strings in the ByteBuffer.
114        protected static int CompareStrings(int offset_1, int offset_2, ByteBuffer bb)
115        {
116            offset_1 += bb.GetInt(offset_1);
117            offset_2 += bb.GetInt(offset_2);
118            var len_1 = bb.GetInt(offset_1);
119            var len_2 = bb.GetInt(offset_2);
120            var startPos_1 = offset_1 + sizeof(int);
121            var startPos_2 = offset_2 + sizeof(int);
122            var len = Math.Min(len_1, len_2);
123            for(int i = 0; i < len; i++) {
124                if (bb.Data[i + startPos_1] != bb.Data[i + startPos_2])
125                    return bb.Data[i + startPos_1] - bb.Data[i + startPos_2];
126            }
127            if (len_1 < len_2) return -1;
128            if (len_1 > len_2) return 1;
129            return 0;
130        }
131
132    }
133}
134