private void ImplementEnumInlineWriteMethod(EnumTypeModel enumModel)
        {
            var    type           = enumModel.ClrType;
            var    underlyingType = Enum.GetUnderlyingType(type);
            string body           = this.GetSerializeInvocation(underlyingType, $"({CSharpHelpers.GetCompilableTypeName(underlyingType)})item", "originalOffset");

            this.GenerateSerializeMethod(type, body);
        }
Exemple #2
0
 public static EnumDescriptor CreateEnumDescriptor(
     ICSharpClientBuilderContext context,
     EnumTypeModel model)
 {
     return(new EnumDescriptor(
                model.Name,
                context.Namespace,
                CreateEnumElementDescriptors(model)));
 }
Exemple #3
0
 public static EnumValueSerializerDescriptor CreateEnumValueSerializerDescriptor(
     ICSharpClientBuilderContext context,
     EnumTypeModel model)
 {
     return(new EnumValueSerializerDescriptor(
                context.CreateTypeName($"{model.Name}ValueSerializer"),
                context.Namespace,
                model.Type.Name,
                context.GetFullTypeName((EnumType)model.Type),
                CreateEnumElementDescriptors(model)));
 }
Exemple #4
0
        public override IType NormalizeTypeDeclaration(IType type)
        {
            if (type == null)
            {
                return(null);
            }

            if (type is ITypeModel)
            {
                return(type);
            }
            if (_visited.ContainsKey(type))
            {
                return(_visited[type]);
            }

            if (type is PrimaryType)
            {
                _visited[type] = new PrimaryTypeModel(type as PrimaryType);
                return(_visited[type]);
            }
            if (type is SequenceType)
            {
                SequenceTypeModel model = new SequenceTypeModel(type as SequenceType);
                _visited[type] = model;
                return(NormalizeSequenceType(model));
            }
            if (type is DictionaryType)
            {
                DictionaryTypeModel model = new DictionaryTypeModel(type as DictionaryType);
                _visited[type] = model;
                return(NormalizeDictionaryType(model));
            }
            if (type is CompositeType)
            {
                CompositeTypeModel model = NewCompositeTypeModel(type as CompositeType);
                _visited[type] = model;
                return(NormalizeCompositeType(model));
            }
            if (type is EnumType)
            {
                EnumTypeModel model = NewEnumTypeModel(type as EnumType);
                _visited[type] = model;
                return(NormalizeEnumType(model));
            }


            throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture,
                                                          "Type {0} is not supported.", type.GetType()));
        }
Exemple #5
0
        private static IReadOnlyList <EnumElementDescriptor> CreateEnumElementDescriptors(
            EnumTypeModel model)
        {
            var elements = new List <EnumElementDescriptor>();

            foreach (EnumValueModel value in model.Values)
            {
                elements.Add(new EnumElementDescriptor(
                                 value.Name,
                                 value.Value.Name,
                                 value.UnderlyingValue is { } s
                        ? (long?)long.Parse(s, CultureInfo.InvariantCulture)
                        : null));
            }

            return(elements);
        }
    /// <summary>
    /// Tries to create a type model based on the given type.
    /// </summary>
    public bool TryCreateTypeModel(TypeModelContainer container, Type type, [NotNullWhen(true)] out ITypeModel?typeModel)
    {
        if (type == typeof(string))
        {
            typeModel = new StringTypeModel(container);
            return(true);
        }

        if (type.IsArray)
        {
            if (typeof(IFlatBufferUnion).IsAssignableFrom(type.GetElementType()))
            {
                typeModel = new ArrayVectorOfUnionTypeModel(type, container);
            }
            else
            {
                typeModel = new ArrayVectorTypeModel(type, container);
            }

            return(true);
        }

        if (type.IsGenericType)
        {
            var genericDef = type.GetGenericTypeDefinition();
            if (genericDef == typeof(IList <>) || genericDef == typeof(IReadOnlyList <>))
            {
                if (typeof(IFlatBufferUnion).IsAssignableFrom(type.GetGenericArguments()[0]))
                {
                    typeModel = new ListVectorOfUnionTypeModel(type, container);
                }
                else
                {
                    typeModel = new ListVectorTypeModel(type, container);
                }

                return(true);
            }

            if (genericDef == typeof(Memory <>) || genericDef == typeof(ReadOnlyMemory <>))
            {
                typeModel = new MemoryVectorTypeModel(type, container);
                return(true);
            }

            if (genericDef == typeof(IIndexedVector <,>))
            {
                typeModel = new IndexedVectorTypeModel(type, container);
                return(true);
            }
        }

        if (typeof(IFlatBufferUnion).IsAssignableFrom(type))
        {
            typeModel = new UnionTypeModel(type, container);
            return(true);
        }

        if (type.IsEnum)
        {
            typeModel = new EnumTypeModel(type, container);
            return(true);
        }

        var underlyingType = Nullable.GetUnderlyingType(type);

        if (underlyingType is not null)
        {
            typeModel = new NullableTypeModel(container, type);
            return(true);
        }

        var tableAttribute  = type.GetCustomAttribute <FlatBufferTableAttribute>();
        var structAttribute = type.GetCustomAttribute <FlatBufferStructAttribute>();

        if (tableAttribute is not null && structAttribute is not null)
        {
            throw new InvalidFlatBufferDefinitionException($"Type '{CSharpHelpers.GetCompilableTypeName(type)}' is declared as both [FlatBufferTable] and [FlatBufferStruct].");
        }
Exemple #7
0
        private void ImplementEnumGetMaxSizeMethod(EnumTypeModel enumModel)
        {
            string body = $"return {enumModel.MaxInlineSize};";

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