refactor: add support for delayed registering of command collections
This commit is contained in:
parent
64deb9cee6
commit
70cc191c4a
1 changed files with 17 additions and 1 deletions
|
|
@ -288,15 +288,31 @@ pub fn Collection(comptime Namespace: type) type {
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
||||||
pub fn init(self: *Self, targetPtr: *Target) !void {
|
pub fn init(self: *Self, targetPtr: *Target) !void {
|
||||||
|
if (cmds.len == 0)
|
||||||
|
@compileError("no commands found in type " ++ @typeName(Target) ++ " (did you mark them public?)");
|
||||||
|
inline for (cmds) |cmd|
|
||||||
|
@field(self.fields, cmd.name) = Closure(*Target).init(cmd.f, targetPtr, cmd.name, cmd.meta);
|
||||||
|
try self.register();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn init_unregistered(self: *Self, targetPtr: *Target) void {
|
||||||
if (cmds.len == 0)
|
if (cmds.len == 0)
|
||||||
@compileError("no commands found in type " ++ @typeName(Target) ++ " (did you mark them public?)");
|
@compileError("no commands found in type " ++ @typeName(Target) ++ " (did you mark them public?)");
|
||||||
inline for (cmds) |cmd| {
|
inline for (cmds) |cmd| {
|
||||||
@field(self.fields, cmd.name) = Closure(*Target).init(cmd.f, targetPtr, cmd.name, cmd.meta);
|
@field(self.fields, cmd.name) = Closure(*Target).init(cmd.f, targetPtr, cmd.name, cmd.meta);
|
||||||
try @field(self.fields, cmd.name).register();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *Self) void {
|
pub fn deinit(self: *Self) void {
|
||||||
|
self.unregister();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn register(self: *Self) !void {
|
||||||
|
inline for (cmds) |cmd|
|
||||||
|
try @field(self.fields, cmd.name).register();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn unregister(self: *Self) void {
|
||||||
inline for (cmds) |cmd|
|
inline for (cmds) |cmd|
|
||||||
Closure(*Target).unregister(&@field(self.fields, cmd.name));
|
Closure(*Target).unregister(&@field(self.fields, cmd.name));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue