Skip to content

Commit

Permalink
Change Action to Event, using Vector as inspiration.
Browse files Browse the repository at this point in the history
  • Loading branch information
Makosai committed Aug 23, 2024
1 parent 696d130 commit f3206b9
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 114 deletions.
120 changes: 119 additions & 1 deletion src/Events/BaseEvent.zig
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
12 changes: 6 additions & 6 deletions src/Transport/ThreadManager.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ const sustenet = @import("root").sustenet;

const RwLock = std.Thread.RwLock;
const ArrayList = std.ArrayList;
const Action = sustenet.utils.Extensions.Action();
const Event = sustenet.events.BaseEvent.Event();

const ThreadManager = @This();
var instance: ?ThreadManager = null;

main_pool_lock: RwLock = .{},
main_pool: ArrayList(Action),
main_pool_copied: ArrayList(Action),
main_pool: Event,
main_pool_copied: Event,
execute_action: bool = false,

/// Get the singleton instance.
pub fn getInstance(allocator: std.mem.Allocator) !ThreadManager {
if (instance == null) {
instance = ThreadManager{
.main_pool = ArrayList(Action).init(allocator),
.main_pool_copied = ArrayList(Action).init(allocator),
.main_pool = Event.init(allocator),
.main_pool_copied = Event.init(allocator),
.execute_action = false,
};
}
Expand All @@ -31,7 +31,7 @@ pub fn getInstance(allocator: std.mem.Allocator) !ThreadManager {
pub fn executeOnMainThread(
self: *ThreadManager,
/// The action to be executed on the main thread.
action: Action,
action: Event,
) void {
self.main_pool_lock.lock();
defer self.main_pool_lock.unlock();
Expand Down
107 changes: 0 additions & 107 deletions src/Utils/Extensions.zig
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

0 comments on commit f3206b9

Please sign in to comment.