public static void WriteHandle(CsCodeWriter cw, HandleDefinition handle)
        {
            if (handle.Parent != null)
            {
                cw.WriteIndentation();
                cw.Write($"///<summary>");
                cw.Write($"A {(handle.Dispatchable ? "dispatchable" : "non-dispatchable")} handle owned by a {handle.Parent}.");
                cw.Write("</summary>");
                cw.Write(Environment.NewLine);
            }
            cw.WriteLine($"[DebuggerDisplay(\"{{DebuggerDisplay,nq}}\")]");
            using (cw.PushBlock($"public partial struct {handle.Name} : IEquatable<{handle.Name}>"))
            {
                string handleType = handle.Dispatchable ? "IntPtr" : "ulong";
                string nullValue  = handle.Dispatchable ? "IntPtr.Zero" : "0";

                cw.WriteLine($"public readonly {handleType} Handle;");

                cw.WriteLine($"public {handle.Name}({handleType} existingHandle) {{ Handle = existingHandle; }}");
                cw.WriteLine($"public static {handle.Name} Null => new {handle.Name}({nullValue});");
                cw.WriteLine($"public static implicit operator {handle.Name}({handleType} handle) => new {handle.Name}(handle);");
                cw.WriteLine($"public static bool operator ==({handle.Name} left, {handle.Name} right) => left.Handle == right.Handle;");
                cw.WriteLine($"public static bool operator !=({handle.Name} left, {handle.Name} right) => left.Handle != right.Handle;");
                cw.WriteLine($"public static bool operator ==({handle.Name} left, {handleType} right) => left.Handle == right;");
                cw.WriteLine($"public static bool operator !=({handle.Name} left, {handleType} right) => left.Handle != right;");
                cw.WriteLine($"public bool Equals({handle.Name} h) => Handle == h.Handle;");
                cw.WriteLine($"public override bool Equals(object o) => o is {handle.Name} h && Equals(h);");
                cw.WriteLine($"public override int GetHashCode() => Handle.GetHashCode();");
                cw.WriteLine($"private string DebuggerDisplay => string.Format(\"{handle.Name} [0x{{0}}]\", Handle.ToString(\"X\"));");
            }
        }
Example #2
0
        public static VulkanSpecification LoadFromXmlStream(Stream specFileStream)
        {
            var spec     = XDocument.Load(specFileStream);
            var registry = spec.Element("registry");
            var commands = registry.Element("commands");

            CommandDefinition[] commandDefinitions = commands.Elements("command")
                                                     .Select(commandx => CommandDefinition.CreateFromXml(commandx)).ToArray();

            ConstantDefinition[] constantDefinitions = registry.Elements("enums")
                                                       .Where(enumx => enumx.Attribute("name").Value == "API Constants")
                                                       .SelectMany(enumx => enumx.Elements("enum"))
                                                       .Select(enumxx => ConstantDefinition.CreateFromXml(enumxx)).ToArray();

            var types = registry.Elements("types");

            TypedefDefinition[] typedefDefinitions = types.Elements("type").Where(xe => xe.Value.Contains("typedef") && xe.HasCategoryAttribute("bitmask"))
                                                     .Select(xe2 => TypedefDefinition.CreateFromXml(xe2)).ToArray();

            EnumDefinition[] enumDefinitions = registry.Elements("enums")
                                               .Where(enumx => enumx.GetTypeAttributeOrNull() == "enum" || enumx.GetTypeAttributeOrNull() == "bitmask")
                                               .Select(enumx => EnumDefinition.CreateFromXml(enumx)).ToArray();

            StructureDefinition[] structures = types.Elements("type").Where(typex => typex.HasCategoryAttribute("struct"))
                                               .Select(typex => StructureDefinition.CreateFromXml(typex)).ToArray();

            StructureDefinition[] unions =
                types.Elements("type")
                .Where(typex => typex.HasCategoryAttribute("union"))
                .Select(typex => StructureDefinition.CreateFromXml(typex)).ToArray();

            HandleDefinition[] handles = types.Elements("type").Where(typex => typex.HasCategoryAttribute("handle"))
                                         .Select(typex => HandleDefinition.CreateFromXml(typex)).ToArray();

            string[] bitmaskTypes = types.Elements("type").Where(typex => typex.HasCategoryAttribute("bitmask"))
                                    .Select(typex => typex.GetNameElement()).ToArray();

            Dictionary <string, string> baseTypes = types.Elements("type").Where(typex => typex.HasCategoryAttribute("basetype"))
                                                    .ToDictionary(
                typex => typex.GetNameElement(),
                typex => typex.Element("type").Value);

            ExtensionDefinition[] extensions = registry.Element("extensions").Elements("extension")
                                               .Select(xe => ExtensionDefinition.CreateFromXml(xe)).ToArray();

            return(new VulkanSpecification(
                       commandDefinitions,
                       constantDefinitions,
                       typedefDefinitions,
                       enumDefinitions,
                       structures,
                       unions,
                       handles,
                       bitmaskTypes,
                       baseTypes,
                       extensions));
        }
Example #3
0
        public static void WriteHandle(CsCodeWriter cw, HandleDefinition handle)
        {
            if (handle.Parent != null)
            {
                cw.WriteIndentation();
                cw.Write($"///<summary>");
                cw.Write($"A {(handle.Dispatchable ? "dispatchable" : "non-dispatchable")} handle owned by a {handle.Parent}.");
                cw.Write("</summary>");
                cw.Write(Environment.NewLine);
            }
            using (cw.PushBlock("public partial struct " + handle.Name))
            {
                cw.WriteLine("public readonly IntPtr Handle;");

                cw.WriteLine($"public {handle.Name}(IntPtr existingHandle) {{ Handle = existingHandle; }}");
                cw.WriteLine($"public static implicit operator {handle.Name}(IntPtr handle) => new {handle.Name}(handle);");
                cw.WriteLine($"public static implicit operator IntPtr({handle.Name} handle) => handle.Handle;");
                //cw.WriteLine($"public static bool operator ==({handle.Name} left, IntPtr right) => left.Handle == right;");
                //cw.WriteLine($"public static bool operator !=({handle.Name} left, IntPtr right) => left.Handle != right;");
            }
        }