private void ImplementListVectorInlineWriteMethod(VectorTypeModel vectorModel)
        {
            var    type          = vectorModel.ClrType;
            var    itemTypeModel = vectorModel.ItemTypeModel;
            string propertyName  = vectorModel.LengthPropertyName;

            string body = $@"
                int count = item.{propertyName};
                int vectorOffset = context.{nameof(SerializationContext.AllocateVector)}({itemTypeModel.Alignment}, count, {vectorModel.PaddedMemberInlineSize});
                writer.{nameof(SpanWriter.WriteUOffset)}(span, originalOffset, vectorOffset, context);
                writer.{nameof(SpanWriter.WriteInt)}(span, count, vectorOffset, context);
                vectorOffset += sizeof(int);
                for (int i = 0; i < count; ++i)
                {{
                      var current = item[i];
                      {CSharpHelpers.GetNonNullCheckInvocation(itemTypeModel, "current")};
                      {this.GetSerializeInvocation(itemTypeModel.ClrType, "current", "vectorOffset")}
                      vectorOffset += {vectorModel.PaddedMemberInlineSize};
                }}";

            this.GenerateSerializeMethod(type, body);
        }
Ejemplo n.º 2
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);
        }