1588564d74f2968808557d96b6f71e5dc7cd62622RevenantX/*
29a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional * Copyright 2014 Google Inc. All rights reserved.
39a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional *
49a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional * Licensed under the Apache License, Version 2.0 (the "License");
59a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional * you may not use this file except in compliance with the License.
69a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional * You may obtain a copy of the License at
79a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional *
89a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional *     http://www.apache.org/licenses/LICENSE-2.0
99a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional *
109a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional * Unless required by applicable law or agreed to in writing, software
119a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional * distributed under the License is distributed on an "AS IS" BASIS,
129a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional * See the License for the specific language governing permissions and
149a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional * limitations under the License.
159a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional */
169a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
17d5a113e5bf60c145364ec79344646513dc6aca26belldon
189a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutionalusing System;
199a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutionalusing System.Text;
209a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
2169a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara/// @file
2269a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara/// @addtogroup flatbuffers_csharp_api
2369a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara/// @{
24d5a113e5bf60c145364ec79344646513dc6aca26belldon
259a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutionalnamespace FlatBuffers
269a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional{
279a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional    /// <summary>
2869a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara    /// Responsible for building up and accessing a FlatBuffer formatted byte
2969a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara    /// array (via ByteBuffer).
309a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional    /// </summary>
319a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional    public class FlatBufferBuilder
329a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional    {
339a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        private int _space;
349a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        private ByteBuffer _bb;
359a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        private int _minAlign = 1;
369a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
37d5a113e5bf60c145364ec79344646513dc6aca26belldon        // The vtable for the current table (if _vtableSize >= 0)
38d5a113e5bf60c145364ec79344646513dc6aca26belldon        private int[] _vtable = new int[16];
39d5a113e5bf60c145364ec79344646513dc6aca26belldon        // The size of the vtable. -1 indicates no vtable
40d5a113e5bf60c145364ec79344646513dc6aca26belldon        private int _vtableSize = -1;
419a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        // Starting offset of the current struct/table.
429a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        private int _objectStart;
439a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        // List of offsets of all vtables.
449a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        private int[] _vtables = new int[16];
459a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        // Number of entries in `vtables` in use.
469a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        private int _numVtables = 0;
479a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        // For the current vector being built.
489a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        private int _vectorNumElems = 0;
499a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
5069a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <summary>
5169a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// Create a FlatBufferBuilder with a given initial size.
5269a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// </summary>
5369a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <param name="initialSize">
5469a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// The initial size to use for the internal buffer.
5569a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// </param>
569a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        public FlatBufferBuilder(int initialSize)
579a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        {
589a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            if (initialSize <= 0)
599a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                throw new ArgumentOutOfRangeException("initialSize",
609a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                    initialSize, "Must be greater than zero");
619a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            _space = initialSize;
629a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            _bb = new ByteBuffer(new byte[initialSize]);
639a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        }
649a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
6569a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <summary>
6669a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// Reset the FlatBufferBuilder by purging all data that it holds.
6769a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// </summary>
68588564d74f2968808557d96b6f71e5dc7cd62622RevenantX        public void Clear()
69588564d74f2968808557d96b6f71e5dc7cd62622RevenantX        {
70588564d74f2968808557d96b6f71e5dc7cd62622RevenantX            _space = _bb.Length;
71588564d74f2968808557d96b6f71e5dc7cd62622RevenantX            _bb.Reset();
72588564d74f2968808557d96b6f71e5dc7cd62622RevenantX            _minAlign = 1;
73d5a113e5bf60c145364ec79344646513dc6aca26belldon            while (_vtableSize > 0) _vtable[--_vtableSize] = 0;
74d5a113e5bf60c145364ec79344646513dc6aca26belldon            _vtableSize = -1;
75588564d74f2968808557d96b6f71e5dc7cd62622RevenantX            _objectStart = 0;
76588564d74f2968808557d96b6f71e5dc7cd62622RevenantX            _numVtables = 0;
77588564d74f2968808557d96b6f71e5dc7cd62622RevenantX            _vectorNumElems = 0;
78588564d74f2968808557d96b6f71e5dc7cd62622RevenantX        }
799a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
80fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <summary>
81fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// Gets and sets a Boolean to disable the optimization when serializing
82fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// default values to a Table.
838c0d56d55abcc0559f1472979ed31a817e678731Wouter van Oortmerssen        ///
84fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// In order to save space, fields that are set to their default value
858c0d56d55abcc0559f1472979ed31a817e678731Wouter van Oortmerssen        /// don't get serialized into the buffer.
86fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// </summary>
87fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        public bool ForceDefaults { get; set; }
88fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli
8969a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// @cond FLATBUFFERS_INTERNAL
9069a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara
910ee1b99c5d38636c2ab8eb8e05dcd42b328c0ccaMormegil        public int Offset { get { return _bb.Length - _space; } }
929a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
939a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        public void Pad(int size)
949a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        {
95be11d2b6ef71f367817000b853724982992d2e45Oli Wilkinson             _bb.PutByte(_space -= size, 0, size);
969a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        }
979a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
989a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        // Doubles the size of the ByteBuffer, and copies the old data towards
999a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        // the end of the new buffer (since we build the buffer backwards).
1009a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        void GrowBuffer()
1019a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        {
1029a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            var oldBuf = _bb.Data;
1039a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            var oldBufSize = oldBuf.Length;
1049a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            if ((oldBufSize & 0xC0000000) != 0)
1059a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                throw new Exception(
1069a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                    "FlatBuffers: cannot grow buffer beyond 2 gigabytes.");
1079a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
1089a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            var newBufSize = oldBufSize << 1;
1099a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            var newBuf = new byte[newBufSize];
1109a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
1119a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            Buffer.BlockCopy(oldBuf, 0, newBuf, newBufSize - oldBufSize,
1129a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                             oldBufSize);
113e4c3bf3d2cec00522fd1a8edd8704413cdc1303bMaor Itzkovitch            _bb = new ByteBuffer(newBuf, newBufSize);
1149a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        }
1159a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
1169a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        // Prepare to write an element of `size` after `additional_bytes`
1179a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        // have been written, e.g. if you write a string, you need to align
1189a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        // such the int length field is aligned to SIZEOF_INT, and the string
1199a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        // data follows it directly.
1209a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        // If all you need to do is align, `additional_bytes` will be 0.
1219a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        public void Prep(int size, int additionalBytes)
1229a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        {
1239a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            // Track the biggest thing we've ever aligned to.
1249a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            if (size > _minAlign)
1259a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                _minAlign = size;
1269a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            // Find the amount of alignment needed such that `size` is properly
1279a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            // aligned after `additional_bytes`
1289a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            var alignSize =
1299a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                ((~((int)_bb.Length - _space + additionalBytes)) + 1) &
1309a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                (size - 1);
1319a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            // Reallocate the buffer if needed.
1329a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            while (_space < alignSize + size + additionalBytes)
1339a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            {
1349a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                var oldBufSize = (int)_bb.Length;
1359a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                GrowBuffer();
1369a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                _space += (int)_bb.Length - oldBufSize;
1379a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
1389a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            }
139be11d2b6ef71f367817000b853724982992d2e45Oli Wilkinson            if (alignSize > 0)
140be11d2b6ef71f367817000b853724982992d2e45Oli Wilkinson                Pad(alignSize);
1419a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        }
1429a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
1434fb5a764df4c4fa249c4a85b3f89be2a3e6deb9bWouter van Oortmerssen        public void PutBool(bool x)
1444fb5a764df4c4fa249c4a85b3f89be2a3e6deb9bWouter van Oortmerssen        {
1454fb5a764df4c4fa249c4a85b3f89be2a3e6deb9bWouter van Oortmerssen          _bb.PutByte(_space -= sizeof(byte), (byte)(x ? 1 : 0));
1464fb5a764df4c4fa249c4a85b3f89be2a3e6deb9bWouter van Oortmerssen        }
1474fb5a764df4c4fa249c4a85b3f89be2a3e6deb9bWouter van Oortmerssen
148557c88c0396220e79e9a43c07f8393a5c68b739dWouter van Oortmerssen        public void PutSbyte(sbyte x)
149557c88c0396220e79e9a43c07f8393a5c68b739dWouter van Oortmerssen        {
150557c88c0396220e79e9a43c07f8393a5c68b739dWouter van Oortmerssen          _bb.PutSbyte(_space -= sizeof(sbyte), x);
151557c88c0396220e79e9a43c07f8393a5c68b739dWouter van Oortmerssen        }
152557c88c0396220e79e9a43c07f8393a5c68b739dWouter van Oortmerssen
1539a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        public void PutByte(byte x)
1549a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        {
1559a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            _bb.PutByte(_space -= sizeof(byte), x);
1569a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        }
1579a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
1589a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        public void PutShort(short x)
1599a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        {
1609a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            _bb.PutShort(_space -= sizeof(short), x);
1619a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        }
1629a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
163557c88c0396220e79e9a43c07f8393a5c68b739dWouter van Oortmerssen        public void PutUshort(ushort x)
164557c88c0396220e79e9a43c07f8393a5c68b739dWouter van Oortmerssen        {
165557c88c0396220e79e9a43c07f8393a5c68b739dWouter van Oortmerssen          _bb.PutUshort(_space -= sizeof(ushort), x);
166557c88c0396220e79e9a43c07f8393a5c68b739dWouter van Oortmerssen        }
167557c88c0396220e79e9a43c07f8393a5c68b739dWouter van Oortmerssen
168557c88c0396220e79e9a43c07f8393a5c68b739dWouter van Oortmerssen        public void PutInt(int x)
1699a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        {
1709a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            _bb.PutInt(_space -= sizeof(int), x);
1719a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        }
1729a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
173557c88c0396220e79e9a43c07f8393a5c68b739dWouter van Oortmerssen        public void PutUint(uint x)
174557c88c0396220e79e9a43c07f8393a5c68b739dWouter van Oortmerssen        {
175557c88c0396220e79e9a43c07f8393a5c68b739dWouter van Oortmerssen          _bb.PutUint(_space -= sizeof(uint), x);
176557c88c0396220e79e9a43c07f8393a5c68b739dWouter van Oortmerssen        }
177557c88c0396220e79e9a43c07f8393a5c68b739dWouter van Oortmerssen
178557c88c0396220e79e9a43c07f8393a5c68b739dWouter van Oortmerssen        public void PutLong(long x)
1799a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        {
1809a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            _bb.PutLong(_space -= sizeof(long), x);
1819a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        }
1829a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
183557c88c0396220e79e9a43c07f8393a5c68b739dWouter van Oortmerssen        public void PutUlong(ulong x)
184557c88c0396220e79e9a43c07f8393a5c68b739dWouter van Oortmerssen        {
185557c88c0396220e79e9a43c07f8393a5c68b739dWouter van Oortmerssen          _bb.PutUlong(_space -= sizeof(ulong), x);
186557c88c0396220e79e9a43c07f8393a5c68b739dWouter van Oortmerssen        }
187557c88c0396220e79e9a43c07f8393a5c68b739dWouter van Oortmerssen
1889a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        public void PutFloat(float x)
1899a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        {
1909a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            _bb.PutFloat(_space -= sizeof(float), x);
1919a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        }
1929a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
1939a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        public void PutDouble(double x)
1949a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        {
1959a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            _bb.PutDouble(_space -= sizeof(double), x);
1969a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        }
19769a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// @endcond
1989a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
19969a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <summary>
20069a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// Add a `bool` to the buffer (aligns the data and grows if necessary).
20169a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// </summary>
20269a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <param name="x">The `bool` to add to the buffer.</param>
2034fb5a764df4c4fa249c4a85b3f89be2a3e6deb9bWouter van Oortmerssen        public void AddBool(bool x) { Prep(sizeof(byte), 0); PutBool(x); }
20469a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara
20569a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <summary>
20669a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// Add a `sbyte` to the buffer (aligns the data and grows if necessary).
20769a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// </summary>
20869a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <param name="x">The `sbyte` to add to the buffer.</param>
209557c88c0396220e79e9a43c07f8393a5c68b739dWouter van Oortmerssen        public void AddSbyte(sbyte x) { Prep(sizeof(sbyte), 0); PutSbyte(x); }
21069a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara
21169a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <summary>
21269a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// Add a `byte` to the buffer (aligns the data and grows if necessary).
21369a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// </summary>
21469a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <param name="x">The `byte` to add to the buffer.</param>
2159a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        public void AddByte(byte x) { Prep(sizeof(byte), 0); PutByte(x); }
21669a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara
21769a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <summary>
21869a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// Add a `short` to the buffer (aligns the data and grows if necessary).
21969a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// </summary>
22069a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <param name="x">The `short` to add to the buffer.</param>
2219a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        public void AddShort(short x) { Prep(sizeof(short), 0); PutShort(x); }
22269a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara
22369a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <summary>
22469a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// Add an `ushort` to the buffer (aligns the data and grows if necessary).
22569a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// </summary>
22669a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <param name="x">The `ushort` to add to the buffer.</param>
227557c88c0396220e79e9a43c07f8393a5c68b739dWouter van Oortmerssen        public void AddUshort(ushort x) { Prep(sizeof(ushort), 0); PutUshort(x); }
22869a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara
22969a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <summary>
23069a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// Add an `int` to the buffer (aligns the data and grows if necessary).
23169a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// </summary>
23269a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <param name="x">The `int` to add to the buffer.</param>
233557c88c0396220e79e9a43c07f8393a5c68b739dWouter van Oortmerssen        public void AddInt(int x) { Prep(sizeof(int), 0); PutInt(x); }
23469a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara
23569a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <summary>
23669a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// Add an `uint` to the buffer (aligns the data and grows if necessary).
23769a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// </summary>
23869a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <param name="x">The `uint` to add to the buffer.</param>
239557c88c0396220e79e9a43c07f8393a5c68b739dWouter van Oortmerssen        public void AddUint(uint x) { Prep(sizeof(uint), 0); PutUint(x); }
24069a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara
24169a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <summary>
24269a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// Add a `long` to the buffer (aligns the data and grows if necessary).
24369a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// </summary>
24469a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <param name="x">The `long` to add to the buffer.</param>
245557c88c0396220e79e9a43c07f8393a5c68b739dWouter van Oortmerssen        public void AddLong(long x) { Prep(sizeof(long), 0); PutLong(x); }
24669a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara
24769a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <summary>
24869a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// Add an `ulong` to the buffer (aligns the data and grows if necessary).
24969a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// </summary>
25069a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <param name="x">The `ulong` to add to the buffer.</param>
2517ef2fc251735f019a21b5e0aa88053a1faffd7c7Wouter van Oortmerssen        public void AddUlong(ulong x) { Prep(sizeof(ulong), 0); PutUlong(x); }
25269a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara
25369a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <summary>
25469a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// Add a `float` to the buffer (aligns the data and grows if necessary).
25569a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// </summary>
25669a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <param name="x">The `float` to add to the buffer.</param>
2579a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        public void AddFloat(float x) { Prep(sizeof(float), 0); PutFloat(x); }
25869a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara
25969a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <summary>
26069a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// Add a `double` to the buffer (aligns the data and grows if necessary).
26169a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// </summary>
26269a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <param name="x">The `double` to add to the buffer.</param>
2639a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        public void AddDouble(double x) { Prep(sizeof(double), 0);
2649a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                                          PutDouble(x); }
2659a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
26669a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <summary>
26769a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// Adds an offset, relative to where it will be written.
26869a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// </summary>
26969a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <param name="off">The offset to add to the buffer.</param>
2709a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        public void AddOffset(int off)
2719a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        {
2729a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            Prep(sizeof(int), 0);  // Ensure alignment is already done.
2730ee1b99c5d38636c2ab8eb8e05dcd42b328c0ccaMormegil            if (off > Offset)
2749a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                throw new ArgumentException();
2759a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
2760ee1b99c5d38636c2ab8eb8e05dcd42b328c0ccaMormegil            off = Offset - off + sizeof(int);
277557c88c0396220e79e9a43c07f8393a5c68b739dWouter van Oortmerssen            PutInt(off);
2789a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        }
2799a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
28069a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// @cond FLATBUFFERS_INTERNAL
2819a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        public void StartVector(int elemSize, int count, int alignment)
2829a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        {
2839a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            NotNested();
2849a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            _vectorNumElems = count;
2859a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            Prep(sizeof(int), elemSize * count);
2869a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            Prep(alignment, elemSize * count); // Just in case alignment > int.
2879a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        }
28869a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// @endcond
2899a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
29069a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <summary>
29169a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// Writes data necessary to finish a vector construction.
29269a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// </summary>
293588564d74f2968808557d96b6f71e5dc7cd62622RevenantX        public VectorOffset EndVector()
2949a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        {
295557c88c0396220e79e9a43c07f8393a5c68b739dWouter van Oortmerssen            PutInt(_vectorNumElems);
296588564d74f2968808557d96b6f71e5dc7cd62622RevenantX            return new VectorOffset(Offset);
2979a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        }
29852ca75506abd82b5616bdef4d28e9535262c1d65Wouter van Oortmerssen
299dc7f5bc0d80730ef45b368be90ac1208c1394707TGIshib        /// <summary>
300dc7f5bc0d80730ef45b368be90ac1208c1394707TGIshib        /// Creates a vector of tables.
301dc7f5bc0d80730ef45b368be90ac1208c1394707TGIshib        /// </summary>
302dc7f5bc0d80730ef45b368be90ac1208c1394707TGIshib        /// <param name="offsets">Offsets of the tables.</param>
30352ca75506abd82b5616bdef4d28e9535262c1d65Wouter van Oortmerssen        public VectorOffset CreateVectorOfTables<T>(Offset<T>[] offsets) where T : struct
304dc7f5bc0d80730ef45b368be90ac1208c1394707TGIshib        {
305dc7f5bc0d80730ef45b368be90ac1208c1394707TGIshib            NotNested();
306dc7f5bc0d80730ef45b368be90ac1208c1394707TGIshib            StartVector(sizeof(int), offsets.Length, sizeof(int));
307dc7f5bc0d80730ef45b368be90ac1208c1394707TGIshib            for (int i = offsets.Length - 1; i >= 0; i--) AddOffset(offsets[i].Value);
308dc7f5bc0d80730ef45b368be90ac1208c1394707TGIshib            return EndVector();
309dc7f5bc0d80730ef45b368be90ac1208c1394707TGIshib        }
3109a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
31169a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// @cond FLATBUFFERS_INTENRAL
3129a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        public void Nested(int obj)
3139a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        {
3149a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            // Structs are always stored inline, so need to be created right
3159a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            // where they are used. You'll get this assert if you created it
3169a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            // elsewhere.
3170ee1b99c5d38636c2ab8eb8e05dcd42b328c0ccaMormegil            if (obj != Offset)
3189a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                throw new Exception(
3199a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                    "FlatBuffers: struct must be serialized inline.");
3209a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        }
3219a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
3229a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        public void NotNested()
3239a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        {
3249a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            // You should not be creating any other objects or strings/vectors
3259a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            // while an object is being constructed
326d5a113e5bf60c145364ec79344646513dc6aca26belldon            if (_vtableSize >= 0)
3279a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                throw new Exception(
3289a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                    "FlatBuffers: object serialization must not be nested.");
3299a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        }
3309a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
3319a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        public void StartObject(int numfields)
3329a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        {
333d5a113e5bf60c145364ec79344646513dc6aca26belldon            if (numfields < 0)
334d5a113e5bf60c145364ec79344646513dc6aca26belldon                throw new ArgumentOutOfRangeException("Flatbuffers: invalid numfields");
335d5a113e5bf60c145364ec79344646513dc6aca26belldon
3369a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            NotNested();
337d5a113e5bf60c145364ec79344646513dc6aca26belldon
338d5a113e5bf60c145364ec79344646513dc6aca26belldon            if (_vtable.Length < numfields)
339d5a113e5bf60c145364ec79344646513dc6aca26belldon                _vtable = new int[numfields];
340d5a113e5bf60c145364ec79344646513dc6aca26belldon
341d5a113e5bf60c145364ec79344646513dc6aca26belldon            _vtableSize = numfields;
3420ee1b99c5d38636c2ab8eb8e05dcd42b328c0ccaMormegil            _objectStart = Offset;
3439a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        }
3449a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
3459a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
3469a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        // Set the current vtable at `voffset` to the current location in the
3479a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        // buffer.
3489a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        public void Slot(int voffset)
3499a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        {
350d5a113e5bf60c145364ec79344646513dc6aca26belldon            if (voffset >= _vtableSize)
351d5a113e5bf60c145364ec79344646513dc6aca26belldon                throw new IndexOutOfRangeException("Flatbuffers: invalid voffset");
352d5a113e5bf60c145364ec79344646513dc6aca26belldon
3530ee1b99c5d38636c2ab8eb8e05dcd42b328c0ccaMormegil            _vtable[voffset] = Offset;
3549a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        }
3559a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
356fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <summary>
357fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// Adds a Boolean to the Table at index `o` in its vtable using the value `x` and default `d`
358fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// </summary>
359fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="o">The index into the vtable</param>
360fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="x">The value to put into the buffer. If the value is equal to the default
361fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// and <see cref="ForceDefaults"/> is false, the value will be skipped.</param>
362fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="d">The default value to compare the value against</param>
363fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        public void AddBool(int o, bool x, bool d) { if (ForceDefaults || x != d) { AddBool(x); Slot(o); } }
3648c0d56d55abcc0559f1472979ed31a817e678731Wouter van Oortmerssen
365fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <summary>
366fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// Adds a SByte to the Table at index `o` in its vtable using the value `x` and default `d`
367fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// </summary>
368fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="o">The index into the vtable</param>
369fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="x">The value to put into the buffer. If the value is equal to the default
370fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// and <see cref="ForceDefaults"/> is false, the value will be skipped.</param>
371fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="d">The default value to compare the value against</param>
372fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        public void AddSbyte(int o, sbyte x, sbyte d) { if (ForceDefaults || x != d) { AddSbyte(x); Slot(o); } }
3738c0d56d55abcc0559f1472979ed31a817e678731Wouter van Oortmerssen
374fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <summary>
375fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// Adds a Byte to the Table at index `o` in its vtable using the value `x` and default `d`
376fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// </summary>
377fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="o">The index into the vtable</param>
378fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="x">The value to put into the buffer. If the value is equal to the default
379fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// and <see cref="ForceDefaults"/> is false, the value will be skipped.</param>
380fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="d">The default value to compare the value against</param>
381fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        public void AddByte(int o, byte x, byte d) { if (ForceDefaults || x != d) { AddByte(x); Slot(o); } }
382fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli
383fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <summary>
384fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// Adds a Int16 to the Table at index `o` in its vtable using the value `x` and default `d`
385fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// </summary>
386fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="o">The index into the vtable</param>
387fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="x">The value to put into the buffer. If the value is equal to the default
388fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// and <see cref="ForceDefaults"/> is false, the value will be skipped.</param>
389fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="d">The default value to compare the value against</param>
390fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        public void AddShort(int o, short x, int d) { if (ForceDefaults || x != d) { AddShort(x); Slot(o); } }
391fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli
392fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <summary>
393fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// Adds a UInt16 to the Table at index `o` in its vtable using the value `x` and default `d`
394fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// </summary>
395fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="o">The index into the vtable</param>
396fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="x">The value to put into the buffer. If the value is equal to the default
397fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// and <see cref="ForceDefaults"/> is false, the value will be skipped.</param>
398fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="d">The default value to compare the value against</param>
399fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        public void AddUshort(int o, ushort x, ushort d) { if (ForceDefaults || x != d) { AddUshort(x); Slot(o); } }
400fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli
401fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <summary>
402fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// Adds an Int32 to the Table at index `o` in its vtable using the value `x` and default `d`
403fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// </summary>
404fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="o">The index into the vtable</param>
405fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="x">The value to put into the buffer. If the value is equal to the default
406fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// and <see cref="ForceDefaults"/> is false, the value will be skipped.</param>
407fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="d">The default value to compare the value against</param>
408fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        public void AddInt(int o, int x, int d) { if (ForceDefaults || x != d) { AddInt(x); Slot(o); } }
409fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli
410fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <summary>
411fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// Adds a UInt32 to the Table at index `o` in its vtable using the value `x` and default `d`
412fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// </summary>
413fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="o">The index into the vtable</param>
414fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="x">The value to put into the buffer. If the value is equal to the default
415fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// and <see cref="ForceDefaults"/> is false, the value will be skipped.</param>
416fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="d">The default value to compare the value against</param>
417fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        public void AddUint(int o, uint x, uint d) { if (ForceDefaults || x != d) { AddUint(x); Slot(o); } }
418fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli
419fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <summary>
420fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// Adds an Int64 to the Table at index `o` in its vtable using the value `x` and default `d`
421fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// </summary>
422fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="o">The index into the vtable</param>
423fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="x">The value to put into the buffer. If the value is equal to the default
424fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// and <see cref="ForceDefaults"/> is false, the value will be skipped.</param>
425fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="d">The default value to compare the value against</param>
426fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        public void AddLong(int o, long x, long d) { if (ForceDefaults || x != d) { AddLong(x); Slot(o); } }
427fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli
428fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <summary>
429fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// Adds a UInt64 to the Table at index `o` in its vtable using the value `x` and default `d`
430fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// </summary>
431fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="o">The index into the vtable</param>
432fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="x">The value to put into the buffer. If the value is equal to the default
433fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// and <see cref="ForceDefaults"/> is false, the value will be skipped.</param>
434fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="d">The default value to compare the value against</param>
435fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        public void AddUlong(int o, ulong x, ulong d) { if (ForceDefaults || x != d) { AddUlong(x); Slot(o); } }
436fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli
437fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <summary>
438fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// Adds a Single to the Table at index `o` in its vtable using the value `x` and default `d`
439fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// </summary>
440fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="o">The index into the vtable</param>
441fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="x">The value to put into the buffer. If the value is equal to the default
442fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// and <see cref="ForceDefaults"/> is false, the value will be skipped.</param>
443fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="d">The default value to compare the value against</param>
444fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        public void AddFloat(int o, float x, double d) { if (ForceDefaults || x != d) { AddFloat(x); Slot(o); } }
445fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli
446fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <summary>
447fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// Adds a Double to the Table at index `o` in its vtable using the value `x` and default `d`
448fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// </summary>
449fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="o">The index into the vtable</param>
450fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="x">The value to put into the buffer. If the value is equal to the default
451fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// and <see cref="ForceDefaults"/> is false, the value will be skipped.</param>
452fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="d">The default value to compare the value against</param>
453fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        public void AddDouble(int o, double x, double d) { if (ForceDefaults || x != d) { AddDouble(x); Slot(o); } }
454fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli
455fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <summary>
456fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// Adds a buffer offset to the Table at index `o` in its vtable using the value `x` and default `d`
457fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// </summary>
458fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="o">The index into the vtable</param>
459fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="x">The value to put into the buffer. If the value is equal to the default
460fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// and <see cref="ForceDefaults"/> is false, the value will be skipped.</param>
461fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        /// <param name="d">The default value to compare the value against</param>
462fff4590faf2a0dd57e741bb6ee4efbc7e7667a36Oli Wilkinson <Oli        public void AddOffset(int o, int x, int d) { if (ForceDefaults || x != d) { AddOffset(x); Slot(o); } }
46369a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// @endcond
46469a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara
46569a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <summary>
46669a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// Encode the string `s` in the buffer using UTF-8.
46769a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// </summary>
46869a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <param name="s">The string to encode.</param>
46969a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <returns>
47069a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// The offset in the buffer where the encoded string starts.
47169a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// </returns>
472588564d74f2968808557d96b6f71e5dc7cd62622RevenantX        public StringOffset CreateString(string s)
4739a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        {
47469a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara            NotNested();
475b8187e5b8231148ba532a3f5dae035adfb43346bOli Wilkinson            AddByte(0);
476b8187e5b8231148ba532a3f5dae035adfb43346bOli Wilkinson            var utf8StringLen = Encoding.UTF8.GetByteCount(s);
477b8187e5b8231148ba532a3f5dae035adfb43346bOli Wilkinson            StartVector(1, utf8StringLen, 1);
478b8187e5b8231148ba532a3f5dae035adfb43346bOli Wilkinson            Encoding.UTF8.GetBytes(s, 0, s.Length, _bb.Data, _space -= utf8StringLen);
479588564d74f2968808557d96b6f71e5dc7cd62622RevenantX            return new StringOffset(EndVector().Value);
4809a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        }
4819a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
48269a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// @cond FLATBUFFERS_INTERNAL
4839a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        // Structs are stored inline, so nothing additional is being added.
4849a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        // `d` is always 0.
4859a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        public void AddStruct(int voffset, int x, int d)
4869a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        {
4879a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            if (x != d)
4889a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            {
4899a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                Nested(x);
4909a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                Slot(voffset);
4919a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            }
4929a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        }
4939a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
4949a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        public int EndObject()
4959a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        {
496d5a113e5bf60c145364ec79344646513dc6aca26belldon            if (_vtableSize < 0)
4979a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                throw new InvalidOperationException(
4989a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                  "Flatbuffers: calling endObject without a startObject");
4999a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
5009a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            AddInt((int)0);
5010ee1b99c5d38636c2ab8eb8e05dcd42b328c0ccaMormegil            var vtableloc = Offset;
5029a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            // Write out the current vtable.
503ac1015e3c417ecb18d8f449a4e6aaaff3c4f53b9Wouter van Oortmerssen            int i = _vtableSize - 1;
504ac1015e3c417ecb18d8f449a4e6aaaff3c4f53b9Wouter van Oortmerssen            // Trim trailing zeroes.
505ac1015e3c417ecb18d8f449a4e6aaaff3c4f53b9Wouter van Oortmerssen            for (; i >= 0 && _vtable[i] == 0; i--) {}
506ac1015e3c417ecb18d8f449a4e6aaaff3c4f53b9Wouter van Oortmerssen            int trimmedSize = i + 1;
507ac1015e3c417ecb18d8f449a4e6aaaff3c4f53b9Wouter van Oortmerssen            for (; i >= 0 ; i--) {
5089a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                // Offset relative to the start of the table.
5099a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                short off = (short)(_vtable[i] != 0
5109a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                                        ? vtableloc - _vtable[i]
5119a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                                        : 0);
5129a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                AddShort(off);
513d5a113e5bf60c145364ec79344646513dc6aca26belldon
514d5a113e5bf60c145364ec79344646513dc6aca26belldon                // clear out written entry
515d5a113e5bf60c145364ec79344646513dc6aca26belldon                _vtable[i] = 0;
5169a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            }
5179a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
5189a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            const int standardFields = 2; // The fields below:
5199a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            AddShort((short)(vtableloc - _objectStart));
520ac1015e3c417ecb18d8f449a4e6aaaff3c4f53b9Wouter van Oortmerssen            AddShort((short)((trimmedSize + standardFields) *
5219a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                             sizeof(short)));
5229a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
5239a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            // Search for an existing vtable that matches the current one.
5249a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            int existingVtable = 0;
525ac1015e3c417ecb18d8f449a4e6aaaff3c4f53b9Wouter van Oortmerssen            for (i = 0; i < _numVtables; i++) {
5269a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                int vt1 = _bb.Length - _vtables[i];
5279a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                int vt2 = _space;
5289a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                short len = _bb.GetShort(vt1);
5299a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                if (len == _bb.GetShort(vt2)) {
5309a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                    for (int j = sizeof(short); j < len; j += sizeof(short)) {
5319a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                        if (_bb.GetShort(vt1 + j) != _bb.GetShort(vt2 + j)) {
5329a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                            goto endLoop;
5339a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                        }
5349a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                    }
5359a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                    existingVtable = _vtables[i];
5369a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                    break;
5379a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                }
5389a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
5399a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            endLoop: { }
5409a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            }
5419a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
5429a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            if (existingVtable != 0) {
5439a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                // Found a match:
5449a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                // Remove the current vtable.
5459a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                _space = _bb.Length - vtableloc;
5469a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                // Point table to existing vtable.
5479a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                _bb.PutInt(_space, existingVtable - vtableloc);
5489a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            } else {
5499a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                // No match:
5509a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                // Add the location of the current vtable to the list of
5519a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                // vtables.
5529a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                if (_numVtables == _vtables.Length)
5539a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                {
5549a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                    // Arrays.CopyOf(vtables num_vtables * 2);
5559a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                    var newvtables = new int[ _numVtables * 2];
5569a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                    Array.Copy(_vtables, newvtables, _vtables.Length);
5579a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
5589a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                    _vtables = newvtables;
5599a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                };
5600ee1b99c5d38636c2ab8eb8e05dcd42b328c0ccaMormegil                _vtables[_numVtables++] = Offset;
5619a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                // Point table to current vtable.
5620ee1b99c5d38636c2ab8eb8e05dcd42b328c0ccaMormegil                _bb.PutInt(_bb.Length - vtableloc, Offset - vtableloc);
5639a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            }
5649a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
565d5a113e5bf60c145364ec79344646513dc6aca26belldon            _vtableSize = -1;
5669a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            return vtableloc;
5679a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        }
5689a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
569517c964fe2099ecc0810db33cfd45b406b3f3132Wouter van Oortmerssen        // This checks a required field has been set in a given table that has
570517c964fe2099ecc0810db33cfd45b406b3f3132Wouter van Oortmerssen        // just been constructed.
571517c964fe2099ecc0810db33cfd45b406b3f3132Wouter van Oortmerssen        public void Required(int table, int field)
572517c964fe2099ecc0810db33cfd45b406b3f3132Wouter van Oortmerssen        {
573517c964fe2099ecc0810db33cfd45b406b3f3132Wouter van Oortmerssen          int table_start = _bb.Length - table;
574517c964fe2099ecc0810db33cfd45b406b3f3132Wouter van Oortmerssen          int vtable_start = table_start - _bb.GetInt(table_start);
575517c964fe2099ecc0810db33cfd45b406b3f3132Wouter van Oortmerssen          bool ok = _bb.GetShort(vtable_start + field) != 0;
576517c964fe2099ecc0810db33cfd45b406b3f3132Wouter van Oortmerssen          // If this fails, the caller will show what field needs to be set.
577517c964fe2099ecc0810db33cfd45b406b3f3132Wouter van Oortmerssen          if (!ok)
578517c964fe2099ecc0810db33cfd45b406b3f3132Wouter van Oortmerssen            throw new InvalidOperationException("FlatBuffers: field " + field +
579517c964fe2099ecc0810db33cfd45b406b3f3132Wouter van Oortmerssen                                                " must be set");
580517c964fe2099ecc0810db33cfd45b406b3f3132Wouter van Oortmerssen        }
58169a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// @endcond
58269a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara
58369a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <summary>
58469a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// Finalize a buffer, pointing to the given `root_table`.
58569a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// </summary>
58669a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <param name="rootTable">
58769a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// An offset to be added to the buffer.
58869a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// </param>
5899a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        public void Finish(int rootTable)
5909a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        {
5919a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            Prep(_minAlign, sizeof(int));
5929a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            AddOffset(rootTable);
593e4c3bf3d2cec00522fd1a8edd8704413cdc1303bMaor Itzkovitch            _bb.Position = _space;
5949a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        }
5959a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
59669a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <summary>
59769a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// Get the ByteBuffer representing the FlatBuffer.
59869a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// </summary>
59969a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <remarks>
60069a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// This is typically only called after you call `Finish()`.
60118d67ed83bfd54de4d248f4650f5ff1900b1ed6bWouter van Oortmerssen        /// The actual data starts at the ByteBuffer's current position,
60218d67ed83bfd54de4d248f4650f5ff1900b1ed6bWouter van Oortmerssen        /// not necessarily at `0`.
60369a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// </remarks>
60469a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <returns>
60569a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// Returns the ByteBuffer for this FlatBuffer.
60669a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// </returns>
6070ee1b99c5d38636c2ab8eb8e05dcd42b328c0ccaMormegil        public ByteBuffer DataBuffer { get { return _bb; } }
6089a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
60969a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <summary>
61069a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// A utility function to copy and return the ByteBuffer data as a
61169a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// `byte[]`.
61269a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// </summary>
61369a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// <returns>
61469a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// A full copy of the FlatBuffer data.
61569a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara        /// </returns>
6169a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        public byte[] SizedByteArray()
6179a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        {
6180ee1b99c5d38636c2ab8eb8e05dcd42b328c0ccaMormegil            var newArray = new byte[_bb.Data.Length - _bb.Position];
6190ee1b99c5d38636c2ab8eb8e05dcd42b328c0ccaMormegil            Buffer.BlockCopy(_bb.Data, _bb.Position, newArray, 0,
6200ee1b99c5d38636c2ab8eb8e05dcd42b328c0ccaMormegil                             _bb.Data.Length - _bb.Position);
6219a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional            return newArray;
6229a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        }
6239a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
62469a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara         /// <summary>
62569a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara         /// Finalize a buffer, pointing to the given `rootTable`.
62669a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara         /// </summary>
62769a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara         /// <param name="rootTable">
62869a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara         /// An offset to be added to the buffer.
62969a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara         /// </param>
63069a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara         /// <param name="fileIdentifier">
63169a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara         /// A FlatBuffer file identifier to be added to the buffer before
63269a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara         /// `root_table`.
63369a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara         /// </param>
6349a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional         public void Finish(int rootTable, string fileIdentifier)
6359a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional         {
6369a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional             Prep(_minAlign, sizeof(int) +
6379a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                             FlatBufferConstants.FileIdentifierLength);
6389a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional             if (fileIdentifier.Length !=
6399a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                 FlatBufferConstants.FileIdentifierLength)
6409a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                 throw new ArgumentException(
6419a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                     "FlatBuffers: file identifier must be length " +
6429a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                     FlatBufferConstants.FileIdentifierLength,
6439a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                     "fileIdentifier");
6449a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional             for (int i = FlatBufferConstants.FileIdentifierLength - 1; i >= 0;
6459a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                  i--)
6469a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional             {
6479a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional                AddByte((byte)fileIdentifier[i]);
6489a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional             }
649e4c3bf3d2cec00522fd1a8edd8704413cdc1303bMaor Itzkovitch             Finish(rootTable);
6509a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional        }
6519a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
6529a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional
6539a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional    }
6549a1f7be6fd318ddd9545926b5925cf0a10a083e4evolutional}
65569a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara
65669a31b807a85e9a5ca4efb839f37ecb6dcf3eed5Mark Klara/// @}
657