Exemplo n.º 1
0
        public bool AddEnum(EnumObject enumObject)
        {
            if (enumObject.name == null || !enumObject.members.Any(m => m.value != null))
            {
                // Skip if we have no name or values to write
                return(false);
            }

            this.EnsureStarted();
            if (enumObject.flags)
            {
                this.writer.WriteLine(
                    $"    [Flags]");
            }

            string type          = enumObject.type ?? "uint";
            bool   forceUnsigned = enumObject.flags || (enumObject.type != null && (enumObject.type == "uint" || enumObject.type == "ulong" || enumObject.type == "ushort"));

            StringWriter enumBodyWriter = new StringWriter();

            foreach (var member in enumObject.members.Where(m => m.value != null))
            {
                var    currentType = type;
                string valueText   = ConstantWriter.FixIntValueText(forceUnsigned, ref currentType, member.value);

                enumBodyWriter.Write(
                    $@"        {member.name} = {valueText},
");
                // Make the type more specific if it's signed
                if (currentType != type && (type != "int" && type != "long"))
                {
                    type = currentType;
                }
            }

            if (enumObject.type != null)
            {
                type = enumObject.type;
            }

            string typePart = type != "int" ? $" : {type}" : string.Empty;

            this.writer.WriteLine(
                $@"    public enum {enumObject.name}{typePart}
    {{");

            this.writer.Write(enumBodyWriter.ToString());

            this.writer.WriteLine(
                $@"    }}
");

            return(true);
        }
Exemplo n.º 2
0
            private ConstantWriter GetConstantWriter(string originalNamespace, string name)
            {
                string foundNamespace = originalNamespace;

                if (this.requiredNamespaces.TryGetValue(name, out var newNamspace))
                {
                    foundNamespace = newNamspace;
                }

                if (!this.namespacesToConstantWriters.TryGetValue(foundNamespace, out var constantWriter))
                {
                    string partConstantsFile = Path.Combine(EmitterGeneratedDir, $@"{foundNamespace}.constants.cs");
                    if (File.Exists(partConstantsFile))
                    {
                        File.Delete(partConstantsFile);
                    }

                    constantWriter = new ConstantWriter(partConstantsFile, foundNamespace, this.constantsHeaderText, this.withAttributes);
                    this.namespacesToConstantWriters.Add(foundNamespace, constantWriter);
                }

                return(constantWriter);
            }
Exemplo n.º 3
0
            private ConstantWriter GetConstantWriter(string originalNamespace, string name)
            {
                string foundNamespace = originalNamespace;

                if (this.requiredNamespaces.TryGetValue(name, out var newNamspace))
                {
                    foundNamespace = newNamspace;
                }

                if (!this.namespacesToConstantWriters.TryGetValue(foundNamespace, out var constantWriter))
                {
                    string partConstantsFile = Path.Combine(repoRoot, $@"generation\emitter\generated\{foundNamespace}.constants.cs");
                    if (File.Exists(partConstantsFile))
                    {
                        File.Delete(partConstantsFile);
                    }

                    constantWriter = new ConstantWriter(partConstantsFile, foundNamespace);
                    this.namespacesToConstantWriters.Add(foundNamespace, constantWriter);
                }

                return(constantWriter);
            }