public override CodeGeneratedMethod CreateGetMaxSizeMethodBody(GetMaxSizeCodeGenContext context)
        {
            string lengthProperty = $"{context.ValueVariableName}.{this.LengthPropertyName}";

            string body;

            if (this.ItemTypeModel.IsFixedSize)
            {
                // Constant size items. We can reduce these reasonably well.
                body = $"return {VectorMinSize} + {SerializationHelpers.GetMaxPadding(this.ItemTypeModel.PhysicalLayout[0].Alignment)} + ({this.PaddedMemberInlineSize} * {lengthProperty});";
            }
            else
            {
                var itemContext = context.With(valueVariableName: "itemTemp");

                body =
                    $@"
                    int length = {lengthProperty};
                    int runningSum = {VectorMinSize} + {this.MaxInlineSize};
                    for (int i = 0; i < length; ++i)
                    {{
                        var itemTemp = {context.ValueVariableName}[i];
                        {this.ItemTypeModel.GetThrowIfNullInvocation("itemTemp")};
                        runningSum += {itemContext.GetMaxSizeInvocation(this.ItemTypeModel.ClrType)};
                    }}
                    return runningSum;";
            }

            return(new CodeGeneratedMethod
            {
                MethodBody = body,
            });
        }
Beispiel #2
0
 public override CodeGeneratedMethod CreateGetMaxSizeMethodBody(GetMaxSizeCodeGenContext context)
 {
     return(new CodeGeneratedMethod
     {
         MethodBody = $"return {this.MaxInlineSize};",
     });
 }
Beispiel #3
0
        public override CodeGeneratedMethod CreateGetMaxSizeMethodBody(GetMaxSizeCodeGenContext context)
        {
            List <string> switchCases = new List <string>();

            for (int i = 0; i < this.UnionElementTypeModel.Length; ++i)
            {
                var    unionMember = this.UnionElementTypeModel[i];
                int    unionIndex  = i + 1;
                string @case       =
                    $@"
                    case {unionIndex}:
                        return {context.MethodNameMap[unionMember.ClrType]}({context.ValueVariableName}.Item{unionIndex});";

                switchCases.Add(@case);
            }
            string discriminatorPropertyName = nameof(FlatBufferUnion <int, int> .Discriminator);

            string body =
                $@"
            switch ({context.ValueVariableName}.{discriminatorPropertyName})
            {{
                {string.Join("\r\n", switchCases)}
                default:
                    throw new System.InvalidOperationException(""Exception determining type of union. Discriminator = "" + {context.ValueVariableName}.{discriminatorPropertyName});
            }}
";

            return(new CodeGeneratedMethod {
                MethodBody = body
            });
        }
Beispiel #4
0
 public override CodeGeneratedMethod CreateGetMaxSizeMethodBody(GetMaxSizeCodeGenContext context)
 {
     return(new CodeGeneratedMethod($"return {this.MaxInlineSize};")
     {
         IsMethodInline = true,
     });
 }
    public override CodeGeneratedMethod CreateGetMaxSizeMethodBody(GetMaxSizeCodeGenContext context)
    {
        // 2 vectors.
        int baseSize = 2 * (sizeof(int) + SerializationHelpers.GetMaxPadding(sizeof(int)));

        var itemContext = context with
        {
            ValueVariableName = "current",
        };

        string body =
        $@"
            int count = {context.ValueVariableName}.{this.LengthPropertyName};
            int length = {baseSize} + (count * (sizeof(byte) + sizeof(int)));

            for (int i = 0; i < count; ++i)
            {{
                    var {itemContext.ValueVariableName} = {context.ValueVariableName}{this.Indexer("i")};
                    length += {itemContext.GetMaxSizeInvocation(this.ItemTypeModel.ClrType)};
            }}

            return length;";

        return new CodeGeneratedMethod(body);
    }
 public override CodeGeneratedMethod CreateGetMaxSizeMethodBody(GetMaxSizeCodeGenContext context)
 {
     return(new CodeGeneratedMethod
     {
         MethodBody = $"return {nameof(SerializationHelpers)}.{nameof(SerializationHelpers.GetMaxSize)}({context.ValueVariableName});",
     });
 }
Beispiel #7
0
    public sealed override CodeGeneratedMethod CreateGetMaxSizeMethodBody(GetMaxSizeCodeGenContext context)
    {
        string lengthProperty = $"{context.ValueVariableName}.{this.LengthPropertyName}";

        string body;

        if (this.ItemTypeModel.IsFixedSize)
        {
            // Constant size items. We can reduce these reasonably well.
            body = $"return {VectorMinSize + SerializationHelpers.GetMaxPadding(this.ItemTypeModel.PhysicalLayout[0].Alignment)} + ({this.PaddedMemberInlineSize} * {lengthProperty});";
        }
        else
        {
            var itemContext = context with
            {
                ValueVariableName = "current",
            };

            string loopBody = $@"
                {this.GetThrowIfNullStatement("current")}
                runningSum += {itemContext.GetMaxSizeInvocation(this.ItemTypeModel.ClrType)};";

            body = $@"
                int count = {lengthProperty};
                int runningSum = {this.MaxInlineSize + VectorMinSize};
                {this.CreateLoop(context.Options, context.ValueVariableName, "count", "current", loopBody)}

                return runningSum;";
        }

        return(new CodeGeneratedMethod(body));
    }
    public override CodeGeneratedMethod CreateGetMaxSizeMethodBody(GetMaxSizeCodeGenContext context)
    {
        List <string> switchCases = new List <string>();

        for (int i = 0; i < this.UnionElementTypeModel.Length; ++i)
        {
            var unionMember = this.UnionElementTypeModel[i];
            int unionIndex  = i + 1;

            var itemContext = context with
            {
                ValueVariableName = $"{context.ValueVariableName}.Item{unionIndex}",
            };

            string @case =
                $@"
                case {unionIndex}:
                    return {sizeof(uint) + SerializationHelpers.GetMaxPadding(sizeof(uint))} + {itemContext.GetMaxSizeInvocation(unionMember.ClrType)};";

            switchCases.Add(@case);
        }
        string discriminatorPropertyName = nameof(FlatBufferUnion <int, int> .Discriminator);

        string body =
            $@"
            switch ({context.ValueVariableName}.{discriminatorPropertyName})
            {{
                {string.Join("\r\n", switchCases)}
                default:
                    throw new System.InvalidOperationException(""Exception determining type of union. Discriminator = "" + {context.ValueVariableName}.{discriminatorPropertyName});
            }}
";

        return(new CodeGeneratedMethod(body));
    }
Beispiel #9
0
            public CodeGeneratedMethod CreateGetMaxSizeMethodBody(GetMaxSizeCodeGenContext context)
            {
                var body = context
                           .With(GetConvertToUnderlyingInvocation(context.ValueVariableName))
                           .GetMaxSizeInvocation(this.underlyingModel.ClrType);

                return(new CodeGeneratedMethod($"return {body};")
                {
                    IsMethodInline = true
                });
            }
Beispiel #10
0
        public override CodeGeneratedMethod CreateGetMaxSizeMethodBody(GetMaxSizeCodeGenContext context)
        {
            Type   underlyingType     = this.underlyingTypeModel.ClrType;
            string underlyingTypeName = CSharpHelpers.GetCompilableTypeName(underlyingType);

            return(new CodeGeneratedMethod
            {
                MethodBody = $"return {context.MethodNameMap[underlyingType]}(({underlyingTypeName}){context.ValueVariableName});",
                IsMethodInline = true,
            });
        }
Beispiel #11
0
        public override CodeGeneratedMethod CreateGetMaxSizeMethodBody(GetMaxSizeCodeGenContext context)
        {
            Type   underlyingType     = this.underlyingTypeModel.ClrType;
            string underlyingTypeName = CSharpHelpers.GetCompilableTypeName(underlyingType);
            string body = context.With(valueVariableName: $"({underlyingTypeName}){context.ValueVariableName}")
                          .GetMaxSizeInvocation(underlyingType);

            return(new CodeGeneratedMethod($"return {body};")
            {
                IsMethodInline = true,
            });
        }
    public override CodeGeneratedMethod CreateGetMaxSizeMethodBody(GetMaxSizeCodeGenContext context)
    {
        Type   underlyingType     = this.underlyingTypeModel.ClrType;
        string underlyingTypeName = CSharpHelpers.GetGlobalCompilableTypeName(underlyingType);

        var innerContext = context with
        {
            ValueVariableName = $"({underlyingTypeName}){context.ValueVariableName}"
        };

        return(new CodeGeneratedMethod($"return {innerContext.GetMaxSizeInvocation(underlyingType)};")
        {
            IsMethodInline = true,
        });
    }
Beispiel #13
0
        public override CodeGeneratedMethod CreateGetMaxSizeMethodBody(GetMaxSizeCodeGenContext context)
        {
            string body = $@"
                int runningSum = {this.MaxInlineSize + VectorMinSize};
                foreach (var pair in {context.ValueVariableName})
                {{
                    var current = pair.Value;
                    var key = pair.Key;
                    runningSum += {context.MethodNameMap[this.valueTypeModel.ClrType]}(current);
                }}

                return runningSum;";

            return(new CodeGeneratedMethod(body)
            {
                IsMethodInline = false,
            });
        }
        public override CodeGeneratedMethod CreateGetMaxSizeMethodBody(GetMaxSizeCodeGenContext context)
        {
            var ctx = context.With(valueVariableName: $"{context.ValueVariableName}.Value");

            string body = $@"
                if ({context.ValueVariableName}.HasValue)
                {{
                    return {ctx.GetMaxSizeInvocation(this.underlyingType)};
                }}

                return 0;
            ";

            return(new CodeGeneratedMethod(body)
            {
                IsMethodInline = true
            });
        }
 public abstract CodeGeneratedMethod CreateGetMaxSizeMethodBody(GetMaxSizeCodeGenContext context);