removed component contexts

master
Mike 5 years ago
parent ed23befd26
commit 5bf1754dfb

@ -32,7 +32,6 @@ pub fn ComponentStorage(comptime CompT: type, comptime EntityT: type, comptime D
.instances = undefined, .instances = undefined,
.safe_deinit = struct { .safe_deinit = struct {
fn deinit(self: *Self) void { 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) if (!is_empty_struct)
self.instances.deinit(); 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 // since we are stored as a pointer, we need to catpure this
store.safe_deinit = struct { store.safe_deinit = struct {
fn deinit(self: *Self) void { 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) if (!is_empty_struct)
self.instances.deinit(); 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 // 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 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. // 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.safe_deinit(self);
self.set.deinit(); self.set.deinit();

@ -30,8 +30,7 @@ pub const Registry = struct {
typemap: TypeMap, typemap: TypeMap,
handles: EntityHandles, handles: EntityHandles,
components: std.AutoHashMap(u8, usize), components: std.AutoHashMap(u8, usize),
component_contexts: std.AutoHashMap(u8, usize), contexts: std.AutoHashMap(u8, usize),
context: usize = 0,
allocator: *std.mem.Allocator, allocator: *std.mem.Allocator,
pub fn init(allocator: *std.mem.Allocator) Registry { pub fn init(allocator: *std.mem.Allocator) Registry {
@ -39,7 +38,7 @@ pub const Registry = struct {
.typemap = TypeMap.init(allocator), .typemap = TypeMap.init(allocator),
.handles = EntityHandles.init(allocator), .handles = EntityHandles.init(allocator),
.components = std.AutoHashMap(u8, usize).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, .allocator = allocator,
}; };
} }
@ -53,7 +52,7 @@ pub const Registry = struct {
} }
self.components.deinit(); self.components.deinit();
self.component_contexts.deinit(); self.contexts.deinit();
self.typemap.deinit(); self.typemap.deinit();
self.handles.deinit(); self.handles.deinit();
} }
@ -198,40 +197,28 @@ pub const Registry = struct {
/// Binds an object to the context of the registry /// Binds an object to the context of the registry
pub fn setContext(self: *Registry, context: var) void { pub fn setContext(self: *Registry, context: var) void {
std.debug.assert(@typeInfo(@TypeOf(context)) == .Pointer); 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 var type_id: u8 = undefined;
pub fn getContext(self: *Registry, comptime T: type) ?*T { _ = self.typemap.getOrPut(@typeInfo(@TypeOf(context)).Pointer.child, &type_id);
return if (self.context > 0) @intToPtr(*T, self.context) else null; _ = self.contexts.put(type_id, @ptrToInt(context)) catch unreachable;
} }
/// Binds an object to the context of the Component type /// Unsets a context variable if it exists
pub fn setComponentContext(self: *Registry, comptime Component: type, context: var) void { pub fn unsetContext(self: *Registry, comptime T: type) void {
std.debug.assert(@typeInfo(@TypeOf(context)) == .Pointer); std.debug.assert(@typeInfo(T) != .Pointer);
var type_id: u8 = undefined; var type_id: u8 = undefined;
_ = self.typemap.getOrPut(Component, &type_id); _ = self.typemap.getOrPut(T, &type_id);
_ = self.component_contexts.put(type_id, @ptrToInt(context)) catch unreachable; _ = self.contexts.put(type_id, 0) catch unreachable;
} }
/// Unsets a context variable associated with a Component type if it exists /// Returns a pointer to an object in the context of the registry
pub fn unsetComponentContext(self: *Registry, comptime Component: type) void { pub fn getContext(self: *Registry, comptime T: type) ?*T {
var type_id: u8 = undefined; std.debug.assert(@typeInfo(T) != .Pointer);
_ = 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 Component type
pub fn getComponentContext(self: *Registry, comptime Component: type, comptime T: type) ?*T {
var type_id: u8 = undefined; var type_id: u8 = undefined;
_ = self.typemap.getOrPut(Component, &type_id); _ = self.typemap.getOrPut(T, &type_id);
return if (self.component_contexts.get(type_id)) |ptr| return if (self.contexts.get(type_id)) |ptr|
return if (ptr.value > 0) @intToPtr(*T, ptr.value) else null return if (ptr.value > 0) @intToPtr(*T, ptr.value) else null
else else
null; null;
@ -272,7 +259,7 @@ test "context get/set/unset" {
ctx = reg.getContext(Position); ctx = reg.getContext(Position);
std.testing.expectEqual(ctx.?, &pos); std.testing.expectEqual(ctx.?, &pos);
reg.unsetContext(); reg.unsetContext(Position);
ctx = reg.getContext(Position); ctx = reg.getContext(Position);
std.testing.expectEqual(ctx, null); std.testing.expectEqual(ctx, null);
} }
@ -292,15 +279,15 @@ test "component context get/set/unset" {
var reg = Registry.init(std.testing.allocator); var reg = Registry.init(std.testing.allocator);
defer reg.deinit(); defer reg.deinit();
var ctx = reg.getComponentContext(Position, SomeType); var ctx = reg.getContext(SomeType);
std.testing.expectEqual(ctx, null); std.testing.expectEqual(ctx, null);
var pos = SomeType{ .dummy = 0 }; var pos = SomeType{ .dummy = 0 };
reg.setComponentContext(Position, &pos); reg.setContext(&pos);
ctx = reg.getComponentContext(Position, SomeType); ctx = reg.getContext(SomeType);
std.testing.expectEqual(ctx.?, &pos); std.testing.expectEqual(ctx.?, &pos);
reg.unsetComponentContext(Position); reg.unsetContext(SomeType);
ctx = reg.getComponentContext(Position, SomeType); ctx = reg.getContext(SomeType);
std.testing.expectEqual(ctx, null); std.testing.expectEqual(ctx, null);
} }

Loading…
Cancel
Save