117 lines
3.9 KiB
Zig
Raw Normal View History

2020-06-04 19:19:34 -07:00
const std = @import("std");
2020-06-05 14:17:25 -07:00
const ErasedPtr = @import("../ecs/utils.zig").ErasedPtr;
2020-06-05 14:15:17 -07:00
2020-06-05 19:15:21 -07:00
/// Simple cache for resources of a given type. If any resource has a deinit method it will be called when clear
/// or remove is called. Implementing a "loader" which is passed to "load" is a struct with one method:
/// - load(self: @This()) *T.
2020-06-05 14:15:17 -07:00
pub fn Cache(comptime T: type) type {
2020-06-04 19:19:34 -07:00
return struct {
2020-06-05 14:15:17 -07:00
const Self = @This();
safe_deinit: *const fn (*@This()) void,
2020-06-05 19:15:21 -07:00
resources: std.AutoHashMap(u32, *T),
2021-12-15 11:55:58 -07:00
allocator: ?std.mem.Allocator = null,
2020-06-05 14:15:17 -07:00
2021-12-15 11:55:58 -07:00
pub fn initPtr(allocator: std.mem.Allocator) *@This() {
2020-06-05 14:15:17 -07:00
var cache = allocator.create(@This()) catch unreachable;
cache.safe_deinit = struct {
fn deinit(self: *Self) void {
self.clear();
self.resources.deinit();
self.allocator.?.destroy(self);
}
}.deinit;
2020-06-05 19:15:21 -07:00
cache.resources = std.AutoHashMap(u32, *T).init(allocator);
2020-06-05 14:15:17 -07:00
cache.allocator = allocator;
return cache;
}
2021-12-15 11:55:58 -07:00
pub fn init(allocator: std.mem.Allocator) @This() {
2020-06-04 19:19:34 -07:00
return .{
2020-06-05 14:15:17 -07:00
.safe_deinit = struct {
fn deinit(self: *Self) void {
self.clear();
self.resources.deinit();
}
}.deinit,
2020-06-05 19:15:21 -07:00
.resources = std.AutoHashMap(u32, *T).init(allocator),
2020-06-04 19:19:34 -07:00
};
}
2020-06-04 23:26:34 -07:00
pub fn deinit(self: *@This()) void {
2020-06-05 14:15:17 -07:00
self.safe_deinit(self);
2020-06-04 19:19:34 -07:00
}
pub fn load(self: *@This(), id: u32, comptime loader: anytype) @typeInfo(@typeInfo(@TypeOf(@field(loader, "load"))).Pointer.child).Fn.return_type.? {
2020-08-05 22:37:17 +02:00
if (self.resources.get(id)) |resource| {
2020-06-04 19:19:34 -07:00
return resource;
}
var resource = loader.load(loader);
2020-06-04 19:19:34 -07:00
_ = self.resources.put(id, resource) catch unreachable;
return resource;
}
2020-06-05 19:15:21 -07:00
pub fn contains(self: *@This(), id: u32) bool {
2020-06-04 19:19:34 -07:00
return self.resources.contains(id);
}
2020-06-05 19:15:21 -07:00
pub fn remove(self: *@This(), id: u32) void {
2021-06-29 22:26:23 -06:00
if (self.resources.fetchRemove(id)) |kv| {
2020-06-04 19:19:34 -07:00
if (@hasDecl(T, "deinit")) {
@call(.always_inline, T.deinit, .{kv.value});
2020-06-04 19:19:34 -07:00
}
}
}
pub fn clear(self: *@This()) void {
// optionally deinit any resources that have a deinit method
if (@hasDecl(T, "deinit")) {
2020-12-04 12:48:15 -08:00
var iter = self.resources.iterator();
while (iter.next()) |kv| {
@call(.always_inline, T.deinit, .{kv.value_ptr.*});
2020-06-04 19:19:34 -07:00
}
}
2020-08-05 22:37:17 +02:00
self.resources.clearAndFree();
2020-06-04 19:19:34 -07:00
}
pub fn size(self: @This()) usize {
2020-12-04 12:48:15 -08:00
return self.resources.count();
2020-06-04 19:19:34 -07:00
}
};
}
test "cache" {
2020-06-05 19:15:21 -07:00
const utils = @import("../ecs/utils.zig");
2020-06-04 19:19:34 -07:00
const Thing = struct {
fart: i32,
pub fn deinit(self: *@This()) void {
std.testing.allocator.destroy(self);
}
};
2020-06-05 19:15:21 -07:00
const ThingLoadArgs = struct {
// Use actual field "load" as function pointer to avoid zig v0.10.0
// compiler error: "error: no field named 'load' in struct '...'"
load: *const fn (self: @This()) *Thing,
pub fn loadFn(self: @This()) *Thing {
_ = self;
2020-06-04 19:19:34 -07:00
return std.testing.allocator.create(Thing) catch unreachable;
}
};
2020-06-05 19:15:21 -07:00
var cache = Cache(Thing).init(std.testing.allocator);
2020-06-04 19:19:34 -07:00
defer cache.deinit();
_ = cache.load(utils.hashString("my/id"), ThingLoadArgs{ .load = ThingLoadArgs.loadFn });
_ = cache.load(utils.hashString("another/id"), ThingLoadArgs{ .load = ThingLoadArgs.loadFn });
2021-06-29 22:26:23 -06:00
try std.testing.expectEqual(cache.size(), 2);
2020-06-04 19:19:34 -07:00
2020-06-05 19:15:21 -07:00
cache.remove(utils.hashString("my/id"));
2021-06-29 22:26:23 -06:00
try std.testing.expectEqual(cache.size(), 1);
2020-06-04 19:19:34 -07:00
cache.clear();
2021-06-29 22:26:23 -06:00
try std.testing.expectEqual(cache.size(), 0);
2020-06-05 14:15:17 -07:00
}