コード例 #1
0
 public override CodeGeneratedMethod CreateParseMethodBody(ParserCodeGenContext context)
 {
     return(new CodeGeneratedMethod($"return {context.InputBufferVariableName}.{this.InputBufferReadMethodName}({context.OffsetVariableName});")
     {
         IsMethodInline = true,
     });
 }
コード例 #2
0
    internal static string CreateParseBody(
        ITypeModel itemTypeModel,
        string createFlatBufferVector,
        ParserCodeGenContext context)
    {
        FlatSharpInternal.Assert(!string.IsNullOrEmpty(context.TableFieldContextVariableName), "expecting table field context");

        if (context.Options.GreedyDeserialize)
        {
            string body = $"({createFlatBufferVector}).FlatBufferVectorToList()";
            if (!context.Options.GenerateMutableObjects)
            {
                body += ".AsReadOnly()";
            }

            return($"return {body};");
        }
        else if (context.Options.Lazy)
        {
            return($"return {createFlatBufferVector};");
        }
        else
        {
            FlatSharpInternal.Assert(context.Options.Progressive, "expecting progressive");
            return($"return new FlatBufferProgressiveVector<{itemTypeModel.GetGlobalCompilableTypeName()}, {context.InputBufferTypeName}>({createFlatBufferVector});");
        }
    }
コード例 #3
0
    public override CodeGeneratedMethod CreateParseMethodBody(ParserCodeGenContext context)
    {
        if (!context.Options.GreedyDeserialize)
        {
            throw new InvalidFlatBufferDefinitionException("Array vectors may only be used with Greedy serializers.");
        }

        var(classDef, className) = FlatBufferVectorHelpers.CreateFlatBufferVectorOfUnionSubclass(
            this.ItemTypeModel,
            context);

        FlatSharpInternal.Assert(!string.IsNullOrEmpty(context.TableFieldContextVariableName), "expecting table field context");

        string createFlatBufferVector =
            $@"new {className}<{context.InputBufferTypeName}>(
            {context.InputBufferVariableName}, 
            {context.OffsetVariableName}.offset0 + {context.InputBufferVariableName}.{nameof(InputBufferExtensions.ReadUOffset)}({context.OffsetVariableName}.offset0), 
            {context.OffsetVariableName}.offset1 + {context.InputBufferVariableName}.{nameof(InputBufferExtensions.ReadUOffset)}({context.OffsetVariableName}.offset1),
            {context.TableFieldContextVariableName})";

        string body = $"return ({createFlatBufferVector}).ToArray();";

        return(new CodeGeneratedMethod(body)
        {
            ClassDefinition = classDef
        });
    }
コード例 #4
0
    public override CodeGeneratedMethod CreateParseMethodBody(ParserCodeGenContext context)
    {
        ValidateWriteThrough(
            writeThroughSupported: false,
            this,
            context.AllFieldContexts,
            context.Options);

        string method = nameof(InputBufferExtensions.ReadByteMemoryBlock);

        if (this.isReadOnly)
        {
            method = nameof(InputBufferExtensions.ReadByteReadOnlyMemoryBlock);
        }

        string memoryVectorRead = $"{context.InputBufferVariableName}.{method}({context.OffsetVariableName})";

        string body;

        if (context.Options.GreedyDeserialize)
        {
            body = $"return {memoryVectorRead}.ToArray().AsMemory();";
        }
        else
        {
            body = $"return {memoryVectorRead};";
        }

        return(new CodeGeneratedMethod(body));
    }
コード例 #5
0
        public override CodeGeneratedMethod CreateParseMethodBody(ParserCodeGenContext context)
        {
            string method = nameof(InputBufferExtensions.ReadByteMemoryBlock);

            if (this.isReadOnly)
            {
                method = nameof(InputBufferExtensions.ReadByteReadOnlyMemoryBlock);
            }

            string memoryVectorRead = $"{context.InputBufferVariableName}.{method}({context.OffsetVariableName})";

            string body;

            if (context.Options.GreedyDeserialize)
            {
                body = $"return {memoryVectorRead}.ToArray().AsMemory();";
            }
            else
            {
                body = $"return {memoryVectorRead};";
            }

            return(new CodeGeneratedMethod {
                MethodBody = body
            });
        }
コード例 #6
0
 public override CodeGeneratedMethod CreateParseMethodBody(ParserCodeGenContext context)
 {
     return(new CodeGeneratedMethod
     {
         MethodBody = $"return {context.InputBufferVariableName}.{nameof(InputBufferExtensions.ReadSharedString)}({context.OffsetVariableName});",
     });
 }
コード例 #7
0
        public override CodeGeneratedMethod CreateParseMethodBody(ParserCodeGenContext context)
        {
            string body;
            string keyTypeName   = CSharpHelpers.GetCompilableTypeName(this.keyTypeModel.ClrType);
            string valueTypeName = CSharpHelpers.GetCompilableTypeName(this.valueTypeModel.ClrType);

            (string vectorClassDef, string vectorClassName) = FlatBufferVectorHelpers.CreateFlatBufferVectorSubclass(
                this.valueTypeModel.ClrType,
                context.InputBufferTypeName,
                context.MethodNameMap[this.valueTypeModel.ClrType]);

            string createFlatBufferVector =
                $@"new {vectorClassName}<{context.InputBufferTypeName}>(
                    {context.InputBufferVariableName}, 
                    {context.OffsetVariableName} + {context.InputBufferVariableName}.{nameof(InputBufferExtensions.ReadUOffset)}({context.OffsetVariableName}), 
                    {this.PaddedMemberInlineSize})";

            if (context.Options.PreallocateVectors)
            {
                // Eager indexed vector.
                string mutable = context.Options.GenerateMutableObjects.ToString().ToLowerInvariant();
                body = $@"return new {nameof(IndexedVector<string, string>)}<{keyTypeName}, {valueTypeName}>({createFlatBufferVector}, {mutable});";
            }
            else
            {
                // Lazy indexed vector.
                body = $@"return new {nameof(FlatBufferIndexedVector<string, string>)}<{keyTypeName}, {valueTypeName}>({createFlatBufferVector});";
            }

            return(new CodeGeneratedMethod(body)
            {
                IsMethodInline = true, ClassDefinition = vectorClassDef
            });
        }
コード例 #8
0
    public override CodeGeneratedMethod CreateParseMethodBody(ParserCodeGenContext context)
    {
        Type   underlyingType = this.underlyingTypeModel.ClrType;
        string body           = $"return ({this.GetCompilableTypeName()}){context.GetParseInvocation(underlyingType)};";

        return(new CodeGeneratedMethod(body)
        {
            IsMethodInline = true,
        });
    }
コード例 #9
0
        public override CodeGeneratedMethod CreateParseMethodBody(ParserCodeGenContext context)
        {
            string innerParse = context.GetParseInvocation(this.underlyingType);
            string body       = $"return {innerParse};";

            return(new CodeGeneratedMethod(body)
            {
                IsMethodInline = true,
            });
        }
コード例 #10
0
    public override string CreateReadItemBody(
        ParserCodeGenContext context,
        string vtableVariableName)
    {
        context = context with
        {
            OffsetVariableName = $"{context.OffsetVariableName} + {this.Offset}",
        };

        return($"return {context.GetParseInvocation(this.ItemTypeModel.ClrType)};");
    }
コード例 #11
0
            public CodeGeneratedMethod CreateParseMethodBody(ParserCodeGenContext context)
            {
                // Parse buffer as underlying type name.
                string parseUnderlying = context.GetParseInvocation(typeof(TUnderlying));
                string body            = $"return {GetConvertFromUnderlyingInvocation(parseUnderlying)};";

                return(new CodeGeneratedMethod(body)
                {
                    IsMethodInline = true,
                });
            }
コード例 #12
0
        public override CodeGeneratedMethod CreateParseMethodBody(ParserCodeGenContext context)
        {
            Type   underlyingType = this.underlyingTypeModel.ClrType;
            string typeName       = CSharpHelpers.GetCompilableTypeName(this.ClrType);

            return(new CodeGeneratedMethod
            {
                MethodBody = $"return ({typeName}){context.MethodNameMap[underlyingType]}({context.InputBufferVariableName}, {context.OffsetVariableName});",
                IsMethodInline = true,
            });
        }
コード例 #13
0
    public static (string classDef, string className) CreateFlatBufferVectorSubclass(
        ITypeModel itemTypeModel,
        ParserCodeGenContext parseContext)
    {
        Type   itemType  = itemTypeModel.ClrType;
        string className = $"FlatBufferVector_{Guid.NewGuid():n}";

        parseContext = parseContext with
        {
            InputBufferTypeName           = "TInputBuffer",
            InputBufferVariableName       = "memory",
            IsOffsetByRef                 = false,
            TableFieldContextVariableName = "fieldContext",
        };

        var    serializeContext = parseContext.GetWriteThroughContext("data", "item", "0");
        string writeThroughBody = serializeContext.GetSerializeInvocation(itemType);

        if (itemTypeModel.SerializeMethodRequiresContext)
        {
            writeThroughBody = "throw new NotSupportedException(\"write through is not available for this type\")";
        }

        string classDef = $@"
                public sealed class {className}<{parseContext.InputBufferTypeName}> : FlatBufferVector<{itemType.GetGlobalCompilableTypeName()}, {parseContext.InputBufferTypeName}>
                    where {parseContext.InputBufferTypeName} : {nameof(IInputBuffer)}
                {{
                    public {className}(
                        {parseContext.InputBufferTypeName} memory,
                        int offset,
                        int itemSize,
                        {nameof(TableFieldContext)} fieldContext) : base(memory, offset, itemSize, fieldContext)
                    {{
                    }}

                    protected override void ParseItem(
                        {parseContext.InputBufferTypeName} memory,
                        int offset,
                        {nameof(TableFieldContext)} fieldContext,
                        out {itemType.GetGlobalCompilableTypeName()} item)
                    {{
                        item = {parseContext.GetParseInvocation(itemType)};
                    }}

                    protected override void WriteThrough({itemType.GetGlobalCompilableTypeName()} {serializeContext.ValueVariableName}, Span<byte> {serializeContext.SpanVariableName})
                    {{
                        {writeThroughBody};
                    }}
                }}
            ";

        return(classDef, className);
    }
コード例 #14
0
    public override CodeGeneratedMethod CreateParseMethodBody(ParserCodeGenContext context)
    {
        string body;

        FlatSharpInternal.Assert(this.ItemTypeModel is not null, "Flatsharp internal error: ItemTypeModel null");

        if (!context.Options.GreedyDeserialize)
        {
            throw new InvalidFlatBufferDefinitionException("Array vectors may only be used with Greedy serializers.");
        }

        ValidateWriteThrough(
            writeThroughSupported: false,
            this,
            context.AllFieldContexts,
            context.Options);

        (string vectorClassDef, string vectorClassName) = (string.Empty, string.Empty);

        if (this.ItemTypeModel.ClrType == typeof(byte))
        {
            // can handle this as memory.
            string method           = nameof(InputBufferExtensions.ReadByteReadOnlyMemoryBlock);
            string memoryVectorRead = $"{context.InputBufferVariableName}.{method}({context.OffsetVariableName})";
            body = $"return {memoryVectorRead}.ToArray();";
        }
        else
        {
            (vectorClassDef, vectorClassName) = FlatBufferVectorHelpers.CreateFlatBufferVectorSubclass(
                this.ItemTypeModel,
                context);

            FlatSharpInternal.Assert(!string.IsNullOrEmpty(context.TableFieldContextVariableName), "expecting table field context");

            string createFlatBufferVector =
                $@"new {vectorClassName}<{context.InputBufferTypeName}>(
                    {context.InputBufferVariableName}, 
                    {context.OffsetVariableName} + {context.InputBufferVariableName}.{nameof(InputBufferExtensions.ReadUOffset)}({context.OffsetVariableName}), 
                    {this.PaddedMemberInlineSize},
                    {context.TableFieldContextVariableName})";

            body = $"return ({createFlatBufferVector}).ToArray();";
        }

        return(new CodeGeneratedMethod(body)
        {
            ClassDefinition = vectorClassDef
        });
    }
コード例 #15
0
    public override CodeGeneratedMethod CreateParseMethodBody(ParserCodeGenContext context)
    {
        ValidateWriteThrough(
            writeThroughSupported: false,
            this,
            context.AllFieldContexts,
            context.Options);

        string body;
        string keyTypeName   = CSharpHelpers.GetGlobalCompilableTypeName(this.keyTypeModel.ClrType);
        string valueTypeName = CSharpHelpers.GetGlobalCompilableTypeName(this.valueTypeModel.ClrType);

        (string vectorClassDef, string vectorClassName) = FlatBufferVectorHelpers.CreateFlatBufferVectorSubclass(
            this.valueTypeModel,
            context);

        FlatSharpInternal.Assert(!string.IsNullOrEmpty(context.TableFieldContextVariableName), "field context was null/empty");

        string createFlatBufferVector =
            $@"new {vectorClassName}<{context.InputBufferTypeName}>(
            {context.InputBufferVariableName}, 
            {context.OffsetVariableName} + {context.InputBufferVariableName}.{nameof(InputBufferExtensions.ReadUOffset)}({context.OffsetVariableName}), 
            {this.PaddedMemberInlineSize},
            {context.TableFieldContextVariableName})";

        string mutable = context.Options.GenerateMutableObjects.ToString().ToLowerInvariant();

        if (context.Options.GreedyDeserialize)
        {
            // Eager indexed vector.
            body = $@"return new {nameof(IndexedVector<string, string>)}<{keyTypeName}, {valueTypeName}>({createFlatBufferVector}, {mutable});";
        }
        else if (context.Options.Lazy)
        {
            // Lazy indexed vector.
            body = $@"return new {nameof(FlatBufferIndexedVector<string, string, IInputBuffer>)}<{keyTypeName}, {valueTypeName}, {context.InputBufferTypeName}>({createFlatBufferVector});";
        }
        else
        {
            FlatSharpInternal.Assert(context.Options.Progressive, "expecting progressive");
            body = $@"return new {nameof(FlatBufferProgressiveIndexedVector<string, string, IInputBuffer>)}<{keyTypeName}, {valueTypeName}, {context.InputBufferTypeName}>({createFlatBufferVector});";
        }

        return(new CodeGeneratedMethod(body)
        {
            IsMethodInline = true, ClassDefinition = vectorClassDef
        });
    }
コード例 #16
0
        public override CodeGeneratedMethod CreateParseMethodBody(ParserCodeGenContext context)
        {
            // We have to implement two items: The table class and the overall "read" method.
            // Let's start with the read method.
            string className = "structReader_" + Guid.NewGuid().ToString("n");

            string classDefinition;

            // Implement the class
            {
                // Build up a list of property overrides.
                var propertyOverrides = new List <GeneratedProperty>();
                for (int index = 0; index < this.Members.Count; ++index)
                {
                    var          value        = this.Members[index];
                    PropertyInfo propertyInfo = value.PropertyInfo;
                    Type         propertyType = propertyInfo.PropertyType;

                    GeneratedProperty generatedProperty = new GeneratedProperty(context.Options, index, propertyInfo);

                    var propContext = context.With(offset: $"({context.OffsetVariableName} + {value.Offset})", inputBuffer: "buffer");

                    // These are always inline as they are only invoked from one place.
                    generatedProperty.ReadValueMethodDefinition =
                        $@"
                    [MethodImpl(MethodImplOptions.AggressiveInlining)]
                    private static {CSharpHelpers.GetCompilableTypeName(propertyType)} {generatedProperty.ReadValueMethodName}({context.InputBufferTypeName} buffer, int offset)
                    {{
                        return {propContext.GetParseInvocation(propertyType)};
                    }}
";

                    propertyOverrides.Add(generatedProperty);
                }

                classDefinition = CSharpHelpers.CreateDeserializeClass(
                    className,
                    this.ClrType,
                    propertyOverrides,
                    context.Options);
            }

            return(new CodeGeneratedMethod
            {
                ClassDefinition = classDefinition,
                MethodBody = $"return new {className}<{context.InputBufferTypeName}>({context.InputBufferVariableName}, {context.OffsetVariableName});",
            });
        }
コード例 #17
0
        public override CodeGeneratedMethod CreateParseMethodBody(ParserCodeGenContext context)
        {
            string body;
            string keyTypeName   = CSharpHelpers.GetCompilableTypeName(this.keyTypeModel.ClrType);
            string valueTypeName = CSharpHelpers.GetCompilableTypeName(this.valueTypeModel.ClrType);

            (string vectorClassDef, string vectorClassName) = FlatBufferVectorHelpers.CreateFlatBufferVectorSubclass(
                this.valueTypeModel.ClrType,
                context.InputBufferTypeName,
                context.MethodNameMap[this.valueTypeModel.ClrType]);

            (string dictionaryClassDef, string dictionaryClassName) = FlatBufferVectorHelpers.CreateFlatBufferDictionarySubclass(
                this.valueTypeModel.ClrType,
                this.keyTypeModel.ClrType,
                this.keyMemberModel.PropertyInfo.Name);

            string createFlatBufferVector =
                $@"new {vectorClassName}<{context.InputBufferTypeName}>(
                    {context.InputBufferVariableName}, 
                    {context.OffsetVariableName} + {context.InputBufferVariableName}.{nameof(InputBufferExtensions.ReadUOffset)}({context.OffsetVariableName}), 
                    {this.PaddedMemberInlineSize})";

            string createDictionary = $@"new {dictionaryClassName}({createFlatBufferVector})";

            if (context.Options.PreallocateVectors)
            {
                // We just call .ToDictionary() when in preallocate mode. Note that when full greedy mode is on, these items will be
                // greedily initialized as we traverse the list. Otherwise, they'll be allocated lazily.
                body = $"({createDictionary}).ToDictionary()";
                if (!context.Options.GenerateMutableObjects)
                {
                    // Finally, if we're not in the business of making mutable objects, then convert the list to read only.
                    body = $"new System.Collections.ObjectModel.ReadOnlyDictionary<{keyTypeName}, {valueTypeName}>({body})";
                }

                body = $"return {body};";
            }
            else
            {
                body = $"return {createDictionary};";
            }

            return(new CodeGeneratedMethod {
                MethodBody = body, IsMethodInline = true, ClassDefinition = string.Join("\r\n", vectorClassDef, dictionaryClassDef)
            });
        }
コード例 #18
0
    public override CodeGeneratedMethod CreateParseMethodBody(ParserCodeGenContext 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 inlineAdjustment = string.Empty;
            if (unionMember.SerializesInline)
            {
                inlineAdjustment = $"offsetLocation += buffer.{nameof(InputBufferExtensions.ReadUOffset)}(offsetLocation);";
            }

            var itemContext = context with
            {
                OffsetVariableName = "offsetLocation",
                IsOffsetByRef      = false,
            };

            string @case =
                $@"
                case {unionIndex}:
                    {inlineAdjustment}
                    return new {this.GetGlobalCompilableTypeName()}({itemContext.GetParseInvocation(unionMember.ClrType)});
";
            switchCases.Add(@case);
        }

        string body = $@"
            byte discriminator = {context.InputBufferVariableName}.{nameof(IInputBuffer.ReadByte)}({context.OffsetVariableName}.offset0);
            int offsetLocation = {context.OffsetVariableName}.offset1;
            if (discriminator != 0 && offsetLocation == 0)
                throw new System.IO.InvalidDataException(""FlatBuffer union had discriminator set but no offset."");

            switch (discriminator)
            {{
                {string.Join("\r\n", switchCases)}
                default:
                    throw new System.InvalidOperationException(""Exception parsing union '{this.GetCompilableTypeName()}'. Discriminator = "" + discriminator);
            }}
        ";

        return(new CodeGeneratedMethod(body));
    }
コード例 #19
0
        public override CodeGeneratedMethod CreateParseMethodBody(ParserCodeGenContext context)
        {
            string body;

            if (this.itemTypeModel is null)
            {
                throw new InvalidOperationException($"Flatsharp internal error: ItemTypeModel null");
            }

            (string vectorClassDef, string vectorClassName) = (string.Empty, string.Empty);

            if (this.itemTypeModel.ClrType == typeof(byte))
            {
                // can handle this as memory.
                string method = nameof(InputBufferExtensions.ReadByteMemoryBlock);
                if (this.ClrType == typeof(ReadOnlyMemory <byte>))
                {
                    method = nameof(InputBufferExtensions.ReadByteReadOnlyMemoryBlock);
                }

                string memoryVectorRead = $"{context.InputBufferVariableName}.{method}({context.OffsetVariableName})";
                body = $"return {memoryVectorRead}.ToArray();";
            }
            else
            {
                (vectorClassDef, vectorClassName) = FlatBufferVectorHelpers.CreateFlatBufferVectorSubclass(
                    this.itemTypeModel.ClrType,
                    context.InputBufferTypeName,
                    context.MethodNameMap[this.itemTypeModel.ClrType]);

                string createFlatBufferVector =
                    $@"new {vectorClassName}<{context.InputBufferTypeName}>(
                    {context.InputBufferVariableName}, 
                    {context.OffsetVariableName} + {context.InputBufferVariableName}.{nameof(InputBufferExtensions.ReadUOffset)}({context.OffsetVariableName}), 
                    {this.PaddedMemberInlineSize})";

                body = $"return ({createFlatBufferVector}).ToArray();";
            }

            return(new CodeGeneratedMethod(body)
            {
                ClassDefinition = vectorClassDef
            });
        }
コード例 #20
0
    public static (string classDef, string className) CreateFlatBufferVectorOfUnionSubclass(
        ITypeModel typeModel,
        ParserCodeGenContext context)
    {
        string className = $"FlatBufferUnionVector_{Guid.NewGuid():n}";

        context = context with
        {
            InputBufferTypeName           = "TInputBuffer",
            InputBufferVariableName       = "memory",
            IsOffsetByRef                 = true,
            TableFieldContextVariableName = "fieldContext",
            OffsetVariableName            = "temp",
        };

        string classDef = $@"
                public sealed class {className}<{context.InputBufferTypeName}> : FlatBufferVectorOfUnion<{typeModel.GetGlobalCompilableTypeName()}, {context.InputBufferTypeName}>
                    where {context.InputBufferTypeName} : {nameof(IInputBuffer)}
                {{
                    public {className}(
                        {context.InputBufferTypeName} memory,
                        int discriminatorOffset,
                        int offsetVectorOffset,
                        {nameof(TableFieldContext)} fieldContext) : base(memory, discriminatorOffset, offsetVectorOffset, fieldContext)
                    {{
                    }}

                    protected override void ParseItem(
                        {context.InputBufferTypeName} memory,
                        int discriminatorOffset,
                        int offsetOffset,
                        {nameof(TableFieldContext)} {context.TableFieldContextVariableName},
                        out {typeModel.GetGlobalCompilableTypeName()} item)
                    {{
                        var {context.OffsetVariableName} = (discriminatorOffset, offsetOffset);
                        item = {context.GetParseInvocation(typeModel.ClrType)};
                    }}
                }}
            ";

        return(classDef, className);
    }
コード例 #21
0
        public override CodeGeneratedMethod CreateParseMethodBody(ParserCodeGenContext 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 inlineAdjustment = string.Empty;
                if (unionMember.SerializesInline)
                {
                    inlineAdjustment = $"offsetLocation += buffer.{nameof(InputBufferExtensions.ReadUOffset)}(offsetLocation);";
                }

                string @case =
                    $@"
                    case {unionIndex}:
                        {inlineAdjustment}
                        return new {CSharpHelpers.GetCompilableTypeName(this.ClrType)}({context.MethodNameMap[unionMember.ClrType]}(buffer, offsetLocation));
";
                switchCases.Add(@case);
            }

            string body = $@"
                byte discriminator = {context.InputBufferVariableName}.{nameof(IInputBuffer.ReadByte)}({context.OffsetVariableName}.offset0);
                int offsetLocation = {context.OffsetVariableName}.offset1;
                if (discriminator == 0 && offsetLocation != 0)
                    throw new System.IO.InvalidDataException(""FlatBuffer union had discriminator set but no offset."");

                switch (discriminator)
                {{
                    {string.Join("\r\n", switchCases)}
                    default:
                        return null;
                }}
            ";

            return(new CodeGeneratedMethod {
                MethodBody = body
            });
        }
コード例 #22
0
    public override CodeGeneratedMethod CreateParseMethodBody(ParserCodeGenContext context)
    {
        var(classDef, className) = FlatBufferVectorHelpers.CreateFlatBufferVectorOfUnionSubclass(
            this.ItemTypeModel,
            context);

        string createFlatBufferVector =
            $@"new {className}<{context.InputBufferTypeName}>(
                {context.InputBufferVariableName}, 
                {context.OffsetVariableName}.offset0 + {context.InputBufferVariableName}.{nameof(InputBufferExtensions.ReadUOffset)}({context.OffsetVariableName}.offset0), 
                {context.OffsetVariableName}.offset1 + {context.InputBufferVariableName}.{nameof(InputBufferExtensions.ReadUOffset)}({context.OffsetVariableName}.offset1),
                {context.TableFieldContextVariableName})";

        return(new CodeGeneratedMethod(ListVectorTypeModel.CreateParseBody(
                                           this.ItemTypeModel,
                                           createFlatBufferVector,
                                           context))
        {
            ClassDefinition = classDef
        });
    }
コード例 #23
0
        public override CodeGeneratedMethod CreateParseMethodBody(ParserCodeGenContext context)
        {
            (string vectorClassDef, string vectorClassName) = FlatBufferVectorHelpers.CreateFlatBufferVectorSubclass(
                this.itemTypeModel.ClrType,
                context.InputBufferTypeName,
                context.MethodNameMap[this.itemTypeModel.ClrType]);

            string body;

            string createFlatBufferVector =
                $@"new {vectorClassName}<{context.InputBufferTypeName}>(
                    {context.InputBufferVariableName}, 
                    {context.OffsetVariableName} + {context.InputBufferVariableName}.{nameof(InputBufferExtensions.ReadUOffset)}({context.OffsetVariableName}), 
                    {this.PaddedMemberInlineSize})";

            if (context.Options.PreallocateVectors)
            {
                // We just call .ToList(). Note that when full greedy mode is on, these items will be
                // greedily initialized as we traverse the list. Otherwise, they'll be allocated lazily.
                body = $"({createFlatBufferVector}).FlatBufferVectorToList()";

                if (!context.Options.GenerateMutableObjects)
                {
                    // Finally, if we're not in the business of making mutable objects, then convert the list to read only.
                    body += ".AsReadOnly()";
                }

                body = $"return {body};";
            }
            else
            {
                body = $"return {createFlatBufferVector};";
            }

            return(new CodeGeneratedMethod(body)
            {
                ClassDefinition = vectorClassDef
            });
        }
コード例 #24
0
    public override CodeGeneratedMethod CreateParseMethodBody(ParserCodeGenContext context)
    {
        ValidateWriteThrough(
            writeThroughSupported: !this.isReadOnly,
            this,
            context.AllFieldContexts,
            context.Options);

        (string vectorClassDef, string vectorClassName) = FlatBufferVectorHelpers.CreateFlatBufferVectorSubclass(
            this.ItemTypeModel,
            context);

        string createFlatBufferVector =
            $@"new {vectorClassName}<{context.InputBufferTypeName}>(
                {context.InputBufferVariableName}, 
                {context.OffsetVariableName} + {context.InputBufferVariableName}.{nameof(InputBufferExtensions.ReadUOffset)}({context.OffsetVariableName}), 
                {this.PaddedMemberInlineSize},
                {context.TableFieldContextVariableName})";

        return(new CodeGeneratedMethod(CreateParseBody(this.ItemTypeModel, createFlatBufferVector, context))
        {
            ClassDefinition = vectorClassDef
        });
    }
コード例 #25
0
    public override CodeGeneratedMethod CreateParseMethodBody(ParserCodeGenContext context)
    {
        // We have to implement two items: The table class and the overall "read" method.
        // Let's start with the read method.
        string className = "structReader_" + Guid.NewGuid().ToString("n");
        DeserializeClassDefinition classDef = DeserializeClassDefinition.Create(
            className,
            this.onDeserializeMethod,
            this,
            -1,
            context.Options);

        // Build up a list of property overrides.
        for (int index = 0; index < this.Members.Count; ++index)
        {
            var value = this.Members[index];
            classDef.AddProperty(value, context);
        }

        return(new CodeGeneratedMethod($"return {className}<{context.InputBufferTypeName}>.GetOrCreate({context.InputBufferVariableName}, {context.OffsetVariableName});")
        {
            ClassDefinition = classDef.ToString(),
        });
    }
コード例 #26
0
 public abstract CodeGeneratedMethod CreateParseMethodBody(ParserCodeGenContext context);
コード例 #27
0
ファイル: StringTypeModel.cs プロジェクト: IoTSharp/FlatSharp
 public override CodeGeneratedMethod CreateParseMethodBody(ParserCodeGenContext context)
 {
     return(new CodeGeneratedMethod($"return {context.InputBufferVariableName}.{nameof(IInputBuffer.ReadString)}({context.OffsetVariableName});"));
 }