From 5bf1754dfb6c0fb6602cbeb3afe2b394b1f03b9d Mon Sep 17 00:00:00 2001 From: Mike Date: Mon, 1 Jun 2020 18:39:00 -0700 Subject: [PATCH] removed component contexts --- zig-ecs/src/ecs/component_storage.zig | 3 -- zig-ecs/src/ecs/registry.zig | 57 +++++++++++---------------- 2 files changed, 22 insertions(+), 38 deletions(-) diff --git a/zig-ecs/src/ecs/component_storage.zig b/zig-ecs/src/ecs/component_storage.zig index 773d554..fe7ddaf 100644 --- a/zig-ecs/src/ecs/component_storage.zig +++ b/zig-ecs/src/ecs/component_storage.zig @@ -32,7 +32,6 @@ pub fn ComponentStorage(comptime CompT: type, comptime EntityT: type, comptime D .instances = undefined, .safe_deinit = struct { fn deinit(self: *Self) void { - std.debug.warn("------ (inner) T: {}, size: {}, is_empty_struct: {}\n", .{ @typeName(@TypeOf(self)), @sizeOf(CompT), is_empty_struct }); if (!is_empty_struct) self.instances.deinit(); } @@ -56,7 +55,6 @@ pub fn ComponentStorage(comptime CompT: type, comptime EntityT: type, comptime D // since we are stored as a pointer, we need to catpure this store.safe_deinit = struct { fn deinit(self: *Self) void { - std.debug.warn("------ (inner) T: {}, size: {}, is_empty_struct: {}\n", .{ @typeName(@TypeOf(self)), @sizeOf(CompT), is_empty_struct }); if (!is_empty_struct) self.instances.deinit(); } @@ -69,7 +67,6 @@ pub fn ComponentStorage(comptime CompT: type, comptime EntityT: type, comptime D // great care must be taken here. Due to how Registry keeps this struct as pointers anything touching a type // will be wrong since it has to cast to a random struct when deiniting. Because of all that, is_empty_struct // will allways be false here so we have to deinit the instances no matter what. - std.debug.warn("\n------ (deinit) T: {}, size: {}, is_empty_struct: {}\n", .{ @typeName(@TypeOf(self)), @sizeOf(CompT), is_empty_struct }); self.safe_deinit(self); self.set.deinit(); diff --git a/zig-ecs/src/ecs/registry.zig b/zig-ecs/src/ecs/registry.zig index 716d05d..3dd68c7 100644 --- a/zig-ecs/src/ecs/registry.zig +++ b/zig-ecs/src/ecs/registry.zig @@ -30,8 +30,7 @@ pub const Registry = struct { typemap: TypeMap, handles: EntityHandles, components: std.AutoHashMap(u8, usize), - component_contexts: std.AutoHashMap(u8, usize), - context: usize = 0, + contexts: std.AutoHashMap(u8, usize), allocator: *std.mem.Allocator, pub fn init(allocator: *std.mem.Allocator) Registry { @@ -39,7 +38,7 @@ pub const Registry = struct { .typemap = TypeMap.init(allocator), .handles = EntityHandles.init(allocator), .components = std.AutoHashMap(u8, usize).init(allocator), - .component_contexts = std.AutoHashMap(u8, usize).init(allocator), + .contexts = std.AutoHashMap(u8, usize).init(allocator), .allocator = allocator, }; } @@ -53,7 +52,7 @@ pub const Registry = struct { } self.components.deinit(); - self.component_contexts.deinit(); + self.contexts.deinit(); self.typemap.deinit(); self.handles.deinit(); } @@ -198,40 +197,28 @@ pub const Registry = struct { /// Binds an object to the context of the registry pub fn setContext(self: *Registry, context: var) void { std.debug.assert(@typeInfo(@TypeOf(context)) == .Pointer); - self.context = @ptrToInt(context); - } - - /// Unsets a context variable if it exists - pub fn unsetContext(self: *Registry) void { - self.context = 0; - } - /// Returns a pointer to an object in the context of the registry - pub fn getContext(self: *Registry, comptime T: type) ?*T { - return if (self.context > 0) @intToPtr(*T, self.context) else null; + var type_id: u8 = undefined; + _ = self.typemap.getOrPut(@typeInfo(@TypeOf(context)).Pointer.child, &type_id); + _ = self.contexts.put(type_id, @ptrToInt(context)) catch unreachable; } - /// Binds an object to the context of the Component type - pub fn setComponentContext(self: *Registry, comptime Component: type, context: var) void { - std.debug.assert(@typeInfo(@TypeOf(context)) == .Pointer); + /// Unsets a context variable if it exists + pub fn unsetContext(self: *Registry, comptime T: type) void { + std.debug.assert(@typeInfo(T) != .Pointer); var type_id: u8 = undefined; - _ = self.typemap.getOrPut(Component, &type_id); - _ = self.component_contexts.put(type_id, @ptrToInt(context)) catch unreachable; + _ = self.typemap.getOrPut(T, &type_id); + _ = self.contexts.put(type_id, 0) catch unreachable; } - /// Unsets a context variable associated with a Component type if it exists - pub fn unsetComponentContext(self: *Registry, comptime Component: type) void { - var type_id: u8 = undefined; - _ = self.typemap.getOrPut(Component, &type_id); - _ = self.component_contexts.put(type_id, 0) catch unreachable; - } + /// Returns a pointer to an object in the context of the registry + pub fn getContext(self: *Registry, comptime T: type) ?*T { + std.debug.assert(@typeInfo(T) != .Pointer); - /// Returns a pointer to an object in the context of the Component type - pub fn getComponentContext(self: *Registry, comptime Component: type, comptime T: type) ?*T { var type_id: u8 = undefined; - _ = self.typemap.getOrPut(Component, &type_id); - return if (self.component_contexts.get(type_id)) |ptr| + _ = self.typemap.getOrPut(T, &type_id); + return if (self.contexts.get(type_id)) |ptr| return if (ptr.value > 0) @intToPtr(*T, ptr.value) else null else null; @@ -272,7 +259,7 @@ test "context get/set/unset" { ctx = reg.getContext(Position); std.testing.expectEqual(ctx.?, &pos); - reg.unsetContext(); + reg.unsetContext(Position); ctx = reg.getContext(Position); std.testing.expectEqual(ctx, null); } @@ -292,15 +279,15 @@ test "component context get/set/unset" { var reg = Registry.init(std.testing.allocator); defer reg.deinit(); - var ctx = reg.getComponentContext(Position, SomeType); + var ctx = reg.getContext(SomeType); std.testing.expectEqual(ctx, null); var pos = SomeType{ .dummy = 0 }; - reg.setComponentContext(Position, &pos); - ctx = reg.getComponentContext(Position, SomeType); + reg.setContext(&pos); + ctx = reg.getContext(SomeType); std.testing.expectEqual(ctx.?, &pos); - reg.unsetComponentContext(Position); - ctx = reg.getComponentContext(Position, SomeType); + reg.unsetContext(SomeType); + ctx = reg.getContext(SomeType); std.testing.expectEqual(ctx, null); }