Beispiel #1
0
        /// <summary>
        /// Gets the max size of the vector itself, not the uoffset_t as part of the containing table.
        /// </summary>
        private void ImplementVectorGetMaxSizeMethod(VectorTypeModel vectorModel)
        {
            var    itemModel = vectorModel.ItemTypeModel;
            string body;

            // count of items + padding(uoffset_t);
            int    fixedSize      = sizeof(uint) + SerializationHelpers.GetMaxPadding(sizeof(uint));
            string lengthProperty = $"item.{vectorModel.LengthPropertyName}";

            // Constant size items. We can reduce these reasonably well.
            if (itemModel.IsFixedSize)
            {
                body = $"return {fixedSize} + {SerializationHelpers.GetMaxPadding(itemModel.Alignment)} + ({vectorModel.PaddedMemberInlineSize} * {lengthProperty});";
            }
            else if (itemModel.SchemaType == FlatBufferSchemaType.Table || itemModel.SchemaType == FlatBufferSchemaType.String)
            {
                Debug.Assert(itemModel.Alignment == sizeof(uint));
                Debug.Assert(itemModel.InlineSize == sizeof(uint));

                body =
                    $@"
                    int length = {lengthProperty};
                    int runningSum = {fixedSize} + {SerializationHelpers.GetMaxPadding(itemModel.Alignment)} + ({vectorModel.PaddedMemberInlineSize} * length);
                    for (int i = 0; i < length; ++i)
                    {{
                        var itemTemp = item[i];
                        {CSharpHelpers.GetNonNullCheckInvocation(itemModel, "itemTemp")};
                        runningSum += {this.InvokeGetMaxSizeMethod(itemModel, "itemTemp")};
                    }}
                    return runningSum;";
            }
            else
            {
                throw new NotImplementedException("Vector.GetMaxSize is not implemented for schema type of " + itemModel.SchemaType);
            }

            this.GenerateGetMaxSizeMethod(vectorModel.ClrType, body);
        }