Exemplo n.º 1
0
 public GeneratedProperty(FlatBufferSerializerOptions options, int index, PropertyInfo propertyInfo)
 {
     this.options             = options;
     this.propertyInfo        = propertyInfo;
     this.HasValueFieldName   = options.PropertyCache ? $"__hasIndex{index}" : null;
     this.BackingFieldName    = !options.Lazy ? $"__index{index}" : null;
     this.ReadValueMethodName = $"__ReadIndex{index}Value";
 }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a new FlatBufferSerializer using the given options.
        /// </summary>
        public FlatBufferSerializer(FlatBufferSerializerOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            this.Options = options;
        }
Exemplo n.º 3
0
        internal static string CreateDeserializeClass(
            string className,
            Type baseType,
            IEnumerable <GeneratedProperty> propertyOverrides,
            FlatBufferSerializerOptions options)
        {
            string inputBufferFieldDef = "private readonly TInputBuffer buffer;";
            string offsetFieldDef      = "private readonly int offset;";

            List <string> ctorStatements = new List <string>
            {
                "this.buffer = buffer;",
                "this.offset = offset;"
            };

            if (options.GreedyDeserialize)
            {
                inputBufferFieldDef = string.Empty;
                offsetFieldDef      = string.Empty;
                ctorStatements.Clear();
            }

            foreach (var property in propertyOverrides)
            {
                if (property.MemberModel.IsVirtual)
                {
                    if (options.GreedyDeserialize)
                    {
                        ctorStatements.Add(
                            $"this.{property.BackingFieldName} = {property.ReadValueMethodName}(buffer, offset);");
                    }
                }
                else
                {
                    ctorStatements.Add(
                        $"base.{property.MemberModel.PropertyInfo.Name} = {property.ReadValueMethodName}(buffer, offset);");
                }
            }

            return
                ($@"
                private sealed class {className}<TInputBuffer> : {GetCompilableTypeName(baseType)} where TInputBuffer : IInputBuffer
                {{
                    {inputBufferFieldDef}
                    {offsetFieldDef}
        
                    public {className}(TInputBuffer buffer, int offset)
                    {{
                        {string.Join("\r\n", ctorStatements)}
                    }}

                    {string.Join("\r\n", propertyOverrides)}
                }}
");
        }
Exemplo n.º 4
0
 public ParserCodeGenContext(
     string inputBufferVariableName,
     string offsetVariableName,
     string inputBufferTypeName,
     IReadOnlyDictionary <Type, string> methodNameMap,
     FlatBufferSerializerOptions options)
 {
     this.InputBufferVariableName = inputBufferVariableName;
     this.OffsetVariableName      = offsetVariableName;
     this.InputBufferTypeName     = inputBufferTypeName;
     this.MethodNameMap           = methodNameMap;
     this.Options = options;
 }
Exemplo n.º 5
0
        public GeneratedProperty(FlatBufferSerializerOptions options, int index, ItemMemberModel memberModel)
        {
            this.options      = options;
            this.propertyInfo = memberModel.PropertyInfo;
            this.MemberModel  = memberModel;

            this.ReadValueMethodName = $"__ReadIndex{index}Value";

            if (memberModel.IsVirtual)
            {
                this.HasValueFieldName = options.PropertyCache ? $"__hasIndex{index}" : null;
                this.BackingFieldName  = !options.Lazy ? $"__index{index}" : null;
            }
        }
 public SerializationCodeGenContext(
     string serializationContextVariableName,
     string spanVariableName,
     string spanWriterVariableName,
     string valueVariableName,
     string offsetVariableName,
     IReadOnlyDictionary <Type, string> methodNameMap,
     FlatBufferSerializerOptions options)
 {
     this.SerializationContextVariableName = serializationContextVariableName;
     this.SpanWriterVariableName           = spanWriterVariableName;
     this.SpanVariableName   = spanVariableName;
     this.ValueVariableName  = valueVariableName;
     this.OffsetVariableName = offsetVariableName;
     this.MethodNameMap      = methodNameMap;
     this.Options            = options;
 }
Exemplo n.º 7
0
        internal static string CreateDeserializeClass(
            string className,
            Type baseType,
            IEnumerable <GeneratedProperty> propertyOverrides,
            FlatBufferSerializerOptions options)
        {
            string inputBufferFieldDef = "private readonly TInputBuffer buffer;";
            string offsetFieldDef      = "private readonly int offset;";

            string ctorBody =
                $@"
                this.buffer = buffer;
                this.offset = offset;
";

            if (options.GreedyDeserialize)
            {
                inputBufferFieldDef = string.Empty;
                offsetFieldDef      = string.Empty;
                ctorBody            = string.Join("\r\n", propertyOverrides.Select(x => $"this.{x.BackingFieldName} = {x.ReadValueMethodName}(buffer, offset);"));
            }

            return
                ($@"
                private sealed class {className}<TInputBuffer> : {CSharpHelpers.GetCompilableTypeName(baseType)} where TInputBuffer : IInputBuffer
                {{
                    {inputBufferFieldDef}
                    {offsetFieldDef}
        
                    public {className}(TInputBuffer buffer, int offset)
                    {{
                        {ctorBody}
                    }}

                    {string.Join("\r\n", propertyOverrides)}
                }}
");
        }
Exemplo n.º 8
0
 public ParserCodeGenerator(FlatBufferSerializerOptions options, IReadOnlyDictionary <Type, string> methodNames)
 {
     this.options     = options;
     this.MethodNames = methodNames;
 }
Exemplo n.º 9
0
 public GetMaxSizeCodeGenContext(string valueVariableName, IReadOnlyDictionary <Type, string> methodNameMap, FlatBufferSerializerOptions options)
 {
     this.ValueVariableName = valueVariableName;
     this.MethodNameMap     = methodNameMap;
     this.Options           = options;
 }
Exemplo n.º 10
0
 /// <summary>
 /// Creates a new FlatBufferSerializer using the given options and type model provider.
 /// </summary>
 public FlatBufferSerializer(FlatBufferSerializerOptions options, TypeModelContainer typeModelContainer)
 {
     this.container = typeModelContainer ?? throw new ArgumentNullException(nameof(typeModelContainer));
     this.Options   = options ?? throw new ArgumentNullException(nameof(options));
 }
Exemplo n.º 11
0
 /// <summary>
 /// Creates a new FlatBufferSerializer using the given options.
 /// </summary>
 public FlatBufferSerializer(FlatBufferSerializerOptions options)
     : this(options, TypeModelContainer.CreateDefault())
 {
 }