-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change Action to Event, using Vector as inspiration.
- Loading branch information
Showing
3 changed files
with
125 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,121 @@ | ||
//! This is a collection of implementations for Action in Zig. | ||
//! Instead of being called Action, they're all Event. | ||
|
||
const std = @import("std"); | ||
|
||
pub const BaseEvent = struct {}; | ||
const ArrayList = std.ArrayList; | ||
|
||
//#region Action | ||
/// A Zig implmenetation of C#'s `Action`. | ||
pub fn Event() type { | ||
return struct { | ||
invokes: ArrayList(*const fn () void), | ||
|
||
const Self = @This(); | ||
|
||
pub inline fn init(allocator: std.mem.Allocator) Self { | ||
return Self{ .invokes = ArrayList(*const fn () void).init(allocator) }; | ||
} | ||
|
||
pub inline fn add(self: *Self, callable: *const fn () void) !void { | ||
try self.invokes.append(callable); | ||
} | ||
|
||
pub inline fn addSlice(self: *Self, callables: []const *const fn () void) !void { | ||
try self.invokes.appendSlice(callables); | ||
} | ||
|
||
pub inline fn clear(self: *Self) void { | ||
self.invokes.clearAndFree(); | ||
} | ||
|
||
pub inline fn invoke(self: *Self) void { | ||
for (self.invokes.items()) |callable| { | ||
callable(); | ||
} | ||
} | ||
|
||
pub inline fn deinit(self: *Self) void { | ||
self.invokes.clearAndFree(); | ||
self.invokes.deinit(); | ||
} | ||
}; | ||
} | ||
|
||
/// A Zig implmenetation of C#'s `Action<T>`. | ||
pub fn EventT1(comptime _T: type) type { | ||
return struct { | ||
invokes: ArrayList(*const fn (comptime T) void), | ||
|
||
pub const T = _T; | ||
|
||
const Self = @This(); | ||
|
||
pub inline fn init(allocator: std.mem.Allocator) void { | ||
Self{ .invokes = ArrayList(*const fn (comptime T) void).init(allocator) }; | ||
} | ||
|
||
pub inline fn add(self: *Self, callable: *const fn (comptime T) void) void { | ||
self.invokes.append(callable); | ||
} | ||
|
||
pub inline fn addSlice(self: *Self, callables: []const *const fn (comptime T) void) void { | ||
self.invokes.appendSlice(callables); | ||
} | ||
|
||
pub inline fn clear(self: *Self) void { | ||
self.invokes.clearAndFree(); | ||
} | ||
|
||
pub inline fn invoke(self: *Self, arg: T) void { | ||
for (self.invokes.items()) |callable| { | ||
callable(arg); | ||
} | ||
} | ||
|
||
pub inline fn deinit(self: *Self) void { | ||
self.invokes.clearAndFree(); | ||
self.invokes.deinit(); | ||
} | ||
}; | ||
} | ||
|
||
/// A Zig implmenetation of C#'s `Action<T1, T2>`. | ||
pub fn EventT2(comptime _T1: type, comptime _T2: type) type { | ||
return struct { | ||
invokes: ArrayList(*const fn (comptime T1, comptime T2) void), | ||
|
||
pub const T1 = _T1; | ||
pub const T2 = _T2; | ||
|
||
const Self = @This(); | ||
|
||
pub inline fn init(allocator: std.mem.Allocator) void { | ||
Self{ .invokes = ArrayList(*const fn (comptime T1, comptime T2) void).init(allocator) }; | ||
} | ||
|
||
pub inline fn add(self: *Self, callable: *const fn (comptime T1, comptime T2) void) void { | ||
self.invokes.append(callable); | ||
} | ||
|
||
pub inline fn addSlice(self: *Self, callables: []const *const fn (comptime T1, comptime T2) void) void { | ||
self.invokes.appendSlice(callables); | ||
} | ||
|
||
pub inline fn clear(self: *Self) void { | ||
self.invokes.clearAndFree(); | ||
} | ||
|
||
pub inline fn invoke(self: *Self, arg1: T1, arg2: T2) void { | ||
for (self.invokes.items()) |callable| { | ||
callable(arg1, arg2); | ||
} | ||
} | ||
|
||
pub inline fn deinit(self: *Self) void { | ||
self.invokes.clearAndFree(); | ||
self.invokes.deinit(); | ||
} | ||
}; | ||
} | ||
//#endregion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1 @@ | ||
//! Extensions for various things you might find useful from other languages. | ||
|
||
const std = @import("std"); | ||
|
||
const ArrayList = std.ArrayList; | ||
|
||
//#region Action | ||
/// A Zig implmenetation of C#'s `Action`. | ||
pub fn Action() type { | ||
return struct { | ||
invokes: ArrayList(*const fn () void), | ||
|
||
const Self = @This(); | ||
|
||
pub inline fn init(allocator: std.mem.Allocator) void { | ||
Self{ .invokes = ArrayList(*const fn () void).init(allocator) }; | ||
} | ||
|
||
pub inline fn add(self: *Self, callable: *const fn () void) void { | ||
self.invokes.append(callable); | ||
} | ||
|
||
pub inline fn clear(self: *Self) void { | ||
self.invokes.clearAndFree(); | ||
} | ||
|
||
pub inline fn invoke(self: *Self) void { | ||
for (self.invokes.items()) |callable| { | ||
callable(); | ||
} | ||
} | ||
|
||
pub inline fn deinit(self: *Self) void { | ||
self.invokes.clearAndFree(); | ||
self.invokes.deinit(); | ||
} | ||
}; | ||
} | ||
|
||
/// A Zig implmenetation of C#'s `Action<T>`. | ||
pub fn ActionT1(comptime _T: type) type { | ||
return struct { | ||
invokes: ArrayList(*const fn (comptime T) void), | ||
|
||
pub const T = _T; | ||
|
||
const Self = @This(); | ||
|
||
pub inline fn init(allocator: std.mem.Allocator) void { | ||
Self{ .invokes = ArrayList(*const fn (comptime T) void).init(allocator) }; | ||
} | ||
|
||
pub inline fn add(self: *Self, callable: *const fn (comptime T) void) void { | ||
self.invokes.append(callable); | ||
} | ||
|
||
pub inline fn clear(self: *Self) void { | ||
self.invokes.clearAndFree(); | ||
} | ||
|
||
pub inline fn invoke(self: *Self, arg: T) void { | ||
for (self.invokes.items()) |callable| { | ||
callable(arg); | ||
} | ||
} | ||
|
||
pub inline fn deinit(self: *Self) void { | ||
self.invokes.clearAndFree(); | ||
self.invokes.deinit(); | ||
} | ||
}; | ||
} | ||
|
||
/// A Zig implmenetation of C#'s `Action<T1, T2>`. | ||
pub fn ActionT2(comptime _T1: type, comptime _T2: type) type { | ||
return struct { | ||
invokes: ArrayList(*const fn (comptime T1, comptime T2) void), | ||
|
||
pub const T1 = _T1; | ||
pub const T2 = _T2; | ||
|
||
const Self = @This(); | ||
|
||
pub inline fn init(allocator: std.mem.Allocator) void { | ||
Self{ .invokes = ArrayList(*const fn (comptime T1, comptime T2) void).init(allocator) }; | ||
} | ||
|
||
pub inline fn add(self: *Self, callable: *const fn (comptime T1, comptime T2) void) void { | ||
self.invokes.append(callable); | ||
} | ||
|
||
pub inline fn clear(self: *Self) void { | ||
self.invokes.clearAndFree(); | ||
} | ||
|
||
pub inline fn invoke(self: *Self, arg1: T1, arg2: T2) void { | ||
for (self.invokes.items()) |callable| { | ||
callable(arg1, arg2); | ||
} | ||
} | ||
|
||
pub inline fn deinit(self: *Self) void { | ||
self.invokes.clearAndFree(); | ||
self.invokes.deinit(); | ||
} | ||
}; | ||
} | ||
//#endregion |