Remove hooks
This commit is contained in:
@@ -1,13 +1,11 @@
|
||||
const std = @import("std");
|
||||
const root = @import("root");
|
||||
const hooks = @import("nu/hooks.zig");
|
||||
|
||||
pub const Window = @import("nu/Window.zig");
|
||||
pub const Render = @import("nu/Render.zig");
|
||||
pub const ImGui = @import("nu/ImGui.zig");
|
||||
|
||||
pub const Bus = @import("nu/Bus.zig");
|
||||
pub const Hook = hooks.Hook;
|
||||
|
||||
const Config = struct {
|
||||
window: Window.Config = .{},
|
||||
@@ -112,5 +110,4 @@ const Graph = struct {
|
||||
};
|
||||
|
||||
test {
|
||||
std.testing.refAllDecls(hooks);
|
||||
}
|
@@ -6,7 +6,6 @@ const vk = @import("vk");
|
||||
const nu = @import("../nu.zig");
|
||||
const au = @import("Render/au.zig");
|
||||
|
||||
const Hook = @import("hooks.zig").Hook;
|
||||
const Render = @import("Render.zig");
|
||||
const Window = @import("Window.zig");
|
||||
|
||||
|
@@ -1,82 +0,0 @@
|
||||
const std = @import("std");
|
||||
|
||||
pub fn Hook(ftype: type) type {
|
||||
const F: std.builtin.Type.Fn = @typeInfo(ftype).Fn;
|
||||
const R: std.builtin.Type = @typeInfo(F.return_type.?);
|
||||
comptime if (R != .Void) @compileError("Hook signature must return void.");
|
||||
|
||||
return struct {
|
||||
const Self = @This();
|
||||
|
||||
handlers: std.AutoArrayHashMap(*const ftype, void),
|
||||
|
||||
pub fn init(alloc: std.mem.Allocator) Self {
|
||||
return Self{
|
||||
.handlers = std.AutoArrayHashMap(*const ftype, void).init(alloc),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Self) void {
|
||||
self.handlers.deinit();
|
||||
}
|
||||
|
||||
pub fn register(self: *Self, f: ftype) !void {
|
||||
try self.handlers.putNoClobber(f, {});
|
||||
}
|
||||
|
||||
pub fn unregister(self: *Self, f: ftype) void {
|
||||
_ = self.handlers.orderedRemove(f);
|
||||
}
|
||||
|
||||
pub fn invoke(self: Self, args: anytype) void {
|
||||
for (self.handlers.keys()) |handler| {
|
||||
@call(.auto, handler, args);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
test "void hooks" {
|
||||
var set_flags = Hook(fn (*usize) void).init(std.testing.allocator);
|
||||
defer set_flags.deinit();
|
||||
|
||||
const hooks = struct {
|
||||
pub fn set_one(f: *usize) void {
|
||||
f.* |= 0b01;
|
||||
}
|
||||
|
||||
pub fn set_two(f: *usize) void {
|
||||
f.* |= 0b10;
|
||||
}
|
||||
};
|
||||
|
||||
var flag: usize = undefined;
|
||||
|
||||
flag = 0;
|
||||
set_flags.invoke(.{&flag});
|
||||
try std.testing.expect(flag == 0b00);
|
||||
|
||||
try set_flags.register(hooks.set_one);
|
||||
|
||||
flag = 0;
|
||||
set_flags.invoke(.{&flag});
|
||||
try std.testing.expect(flag == 0b01);
|
||||
|
||||
try set_flags.register(hooks.set_two);
|
||||
|
||||
flag = 0;
|
||||
set_flags.invoke(.{&flag});
|
||||
try std.testing.expect(flag == 0b11);
|
||||
|
||||
set_flags.unregister(hooks.set_one);
|
||||
|
||||
flag = 0;
|
||||
set_flags.invoke(.{&flag});
|
||||
try std.testing.expect(flag == 0b10);
|
||||
|
||||
set_flags.unregister(hooks.set_two);
|
||||
|
||||
flag = 0;
|
||||
set_flags.invoke(.{&flag});
|
||||
try std.testing.expect(flag == 0b00);
|
||||
}
|
Reference in New Issue
Block a user