Exemple #1
0
        private void ImplementStructReadMethod(StructTypeModel typeModel)
        {
            // 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");

            // Static factory method.
            this.GenerateMethodDefinition(typeModel.ClrType, $"return new {className}(memory, offset);");

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

                    GeneratedProperty generatedProperty = new GeneratedProperty(this.options, index, propertyInfo);
                    generatedProperty.ReadValueMethodDefinition =
                        $@"
                    [MethodImpl(MethodImplOptions.AggressiveInlining)]
                    private static {CSharpHelpers.GetCompilableTypeName(propertyType)} {generatedProperty.ReadValueMethodName}(InputBuffer buffer, int offset)
                    {{
                        return {this.GetReadInvocation(propertyType, "buffer", $"offset + {value.Offset}")};
                    }}
";

                    propertyOverrides.Add(generatedProperty);
                }

                string classDefinition = this.CreateClass(
                    className,
                    typeModel.ClrType,
                    typeModel.Members.Select(x => x.PropertyInfo.Name),
                    propertyOverrides);
                var node = CSharpSyntaxTree.ParseText(classDefinition, ParseOptions);
                this.methodDeclarations.Add(node.GetRoot());
            }
        }
        private void ImplementStructInlineWriteMethod(StructTypeModel structModel)
        {
            var type = structModel.ClrType;

            List <string> body = new List <string>();

            for (int i = 0; i < structModel.Members.Count; ++i)
            {
                var memberInfo = structModel.Members[i];

                string propertyAccessor = $"item.{memberInfo.PropertyInfo.Name}";
                if (memberInfo.ItemTypeModel.SchemaType == FlatBufferSchemaType.Struct)
                {
                    // Force structs to be non-null. FlatSharp doesn't declare structs as structs,
                    // so we need to be careful that structs-within-structs are not null.
                    propertyAccessor += $" ?? new {CSharpHelpers.GetCompilableTypeName(memberInfo.ItemTypeModel.ClrType)}()";
                }

                string invocation = this.GetSerializeInvocation(memberInfo.ItemTypeModel.ClrType, propertyAccessor, $"{memberInfo.Offset} + originalOffset");
                body.Add(invocation);
            }

            this.GenerateSerializeMethod(type, string.Join("\r\n", body));
        }
Exemple #3
0
        private void ImplementStructGetMaxSizeMethod(StructTypeModel structModel)
        {
            string body = $"return {structModel.MaxInlineSize};";

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