forked from mirror/vulkan-zig
Basic setup for examples
This commit is contained in:
@@ -1,67 +0,0 @@
|
||||
const std = @import("std");
|
||||
const testing = std.testing;
|
||||
|
||||
fn FlagTraits(comptime Self: type) type {
|
||||
return struct {
|
||||
pub fn init(flags: var) Self {
|
||||
var self: u32 = 0;
|
||||
|
||||
inline for (std.meta.fields(@TypeOf(flags))) |field| {
|
||||
self |= @enumToInt(@as(Self, @field(flags, field.name)));
|
||||
}
|
||||
|
||||
return @intToEnum(Self, self);
|
||||
}
|
||||
|
||||
pub fn merge(lhs: Self, rhs: Self) Self {
|
||||
return @intToEnum(Self, @enumToInt(lhs) | @enumToInt(rhs));
|
||||
}
|
||||
|
||||
pub fn intersect(lhs: Self, rhs: Self) Self {
|
||||
return @intToEnum(Self, @enumToInt(lhs) & @enumToInt(rhs));
|
||||
}
|
||||
|
||||
pub fn subtract(lhs: Self, rhs: Self) Self {
|
||||
return @intToEnum(Self, @enumToInt(lhs) & @enumToInt(rhs.complement()));
|
||||
}
|
||||
|
||||
pub fn complement(self: Self) Self {
|
||||
const all = comptime blk: {
|
||||
var flags: u32 = 0;
|
||||
for (std.meta.fields(Self)) |field| {
|
||||
flags |= field.value;
|
||||
}
|
||||
break :blk flags;
|
||||
};
|
||||
|
||||
return @intToEnum(Self, ~@enumToInt(self) & all);
|
||||
}
|
||||
|
||||
pub fn contains(lhs: Self, rhs: Self) bool {
|
||||
return @enumToInt(lhs) & @enumToInt(rhs) == @enumToInt(rhs);
|
||||
}
|
||||
|
||||
pub fn format(
|
||||
self: Self,
|
||||
fmt: []const u8,
|
||||
options: std.fmt.FormatOptions,
|
||||
out_stream: var
|
||||
) @TypeOf(out_stream).Error!void {
|
||||
const bits = @enumToInt(self);
|
||||
var first = true;
|
||||
inline for (std.meta.fields(Self)) |field| {
|
||||
if (@popCount(u32, field.value) != 1) continue;
|
||||
|
||||
if (bits & field.value == field.value) {
|
||||
if (first) {
|
||||
first = false;
|
||||
} else {
|
||||
try std.fmt.formatBuf(" | ", options, out_stream);
|
||||
}
|
||||
|
||||
try std.fmt.formatBuf(field.name, options, out_stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -193,11 +193,8 @@ pub const Generator = struct {
|
||||
}
|
||||
};
|
||||
|
||||
pub fn generate(allocator: *Allocator, xml_reader: var, writer: var) !void {
|
||||
const source = try xml_reader.readAllAlloc(allocator, std.math.maxInt(usize));
|
||||
defer allocator.free(source);
|
||||
|
||||
const spec = try xml.parse(allocator, source);
|
||||
pub fn generate(allocator: *Allocator, spec_xml: []const u8, writer: var) !void {
|
||||
const spec = try xml.parse(allocator, spec_xml);
|
||||
defer spec.deinit();
|
||||
|
||||
var gen = try Generator.init(allocator, spec.root);
|
||||
@@ -207,3 +204,8 @@ pub fn generate(allocator: *Allocator, xml_reader: var, writer: var) !void {
|
||||
try gen.resolveDeclarations();
|
||||
try gen.render(writer);
|
||||
}
|
||||
|
||||
test "main" {
|
||||
_ = @import("xml.zig");
|
||||
_ = @import("registry/c-parse.zig");
|
||||
}
|
||||
|
||||
@@ -120,13 +120,11 @@ pub fn main() !void {
|
||||
const file = try std.fs.cwd().openFileZ(std.os.argv[1], .{});
|
||||
defer file.close();
|
||||
|
||||
const spec = try file.reader().readAllAlloc(allocator, std.math.maxInt(usize));
|
||||
defer allocator.free(spec);
|
||||
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
try vkgen.generate(&prof_alloc.allocator, file.reader(), stdout);
|
||||
try vkgen.generate(allocator, spec, stdout);
|
||||
|
||||
std.debug.print("Total memory usage: {} KiB\n", .{@divTrunc(prof_alloc.max_usage, 1024)});
|
||||
}
|
||||
|
||||
test "main" {
|
||||
_ = @import("xml.zig");
|
||||
_ = @import("registry/c-parse.zig");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user