Example #1
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
            });
        }
Example #2
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)
            });
        }
Example #3
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
            });
        }
        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
            });
        }