From deb1104944e35c966a1ec61e508c6cc987cfc4f6 Mon Sep 17 00:00:00 2001 From: copygirl Date: Tue, 5 Mar 2024 13:50:59 +0100 Subject: [PATCH] Split Context.lookup into lookup and lookupMut --- src/main.zig | 20 ++++++++++++++------ src/test/flecs/world.zig | 8 ++++---- src/world.zig | 4 ++-- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/main.zig b/src/main.zig index 2de85b9..dcc4ea6 100644 --- a/src/main.zig +++ b/src/main.zig @@ -42,13 +42,21 @@ pub fn Context(comptime ctx: anytype) type { pub const Pair = @import("./pair.zig").Pair(ctx); pub const World = @import("./world.zig").World(ctx); - /// Returns a pointer unique to this context and the provided type + /// Looks up an entity ID unique to this `Context` for the provided + /// type that has been registered previously, typically done by + /// calling functions such as `World.component(...)`. + pub fn lookup(comptime T: type) c.ecs_entity_t { + return lookupMut(T).*; + } + + /// Returns a pointer unique to this `Context` and the provided type /// that holds an entity ID. Useful for associating types to entities. - pub fn lookup(comptime T: type) *c.ecs_entity_t { + pub fn lookupMut(comptime T: type) *c.ecs_entity_t { _ = T; // Only necessary to create a unique type. - return &(struct { + const EntityHolder = struct { pub var id: c.ecs_entity_t = 0; - }).id; + }; + return &EntityHolder.id; } /// Converts the specified value to an `ecs_entity_t`. @@ -67,7 +75,7 @@ pub fn Context(comptime ctx: anytype) type { c.ecs_entity_t => value, Entity => value.raw, ?Entity => if (value) |v| v.raw else 0, - type => lookup(value).*, + type => lookup(value), else => @compileError("Value of type " ++ @typeName(@TypeOf(value)) ++ " can't be converted to Entity"), }; } @@ -105,7 +113,7 @@ pub fn Context(comptime ctx: anytype) type { // c.ecs_entity_t => value, Entity => value.raw, ?Entity => if (value) |v| v.raw else 0, - type => lookup(value).*, + type => lookup(value), else => @compileError("Value of type " ++ @typeName(T) ++ " can't be converted to Id"), }; } diff --git a/src/test/flecs/world.zig b/src/test/flecs/world.zig index 02d33cb..b731257 100644 --- a/src/test/flecs/world.zig +++ b/src/test/flecs/world.zig @@ -56,8 +56,8 @@ test "World_progress_w_0" { try expect.equal(null, ctx.param); try expect.equal(e1.raw, ctx.e[0]); - try expect.equal(Context.lookup(Position).*, ctx.c[0][0]); - try expect.equal(Context.lookup(Velocity).*, ctx.c[0][1]); + try expect.equal(Context.lookup(Position), ctx.c[0][0]); + try expect.equal(Context.lookup(Velocity), ctx.c[0][1]); try expect.equal(0, ctx.s[0][0]); try expect.equal(0, ctx.s[0][1]); @@ -93,8 +93,8 @@ test "World_progress_w_t" { try expect.equal(null, ctx.param); try expect.equal(e1.raw, ctx.e[0]); - try expect.equal(Context.lookup(Position).*, ctx.c[0][0]); - try expect.equal(Context.lookup(Velocity).*, ctx.c[0][1]); + try expect.equal(Context.lookup(Position), ctx.c[0][0]); + try expect.equal(Context.lookup(Velocity), ctx.c[0][1]); try expect.equal(0, ctx.s[0][0]); try expect.equal(0, ctx.s[0][1]); diff --git a/src/world.zig b/src/world.zig index 167ed2b..e5ba34d 100644 --- a/src/world.zig +++ b/src/world.zig @@ -98,7 +98,7 @@ pub fn World(comptime ctx: anytype) type { if (@sizeOf(T) > 0) @compileError("'" ++ @typeName(T) ++ "' must be a zero-sized type"); const name = meta.simpleTypeName(T); const result = try self.entity(.{ .name = name, .symbol = name }, .{}); - Context.lookup(T).* = result.raw; + Context.lookupMut(T).* = result.raw; return result; } @@ -114,7 +114,7 @@ pub fn World(comptime ctx: anytype) type { const result = c.ecs_component_init(self.raw, &desc); if (result == 0) return flecs.getLastErrorOrUnknown(); - Context.lookup(T).* = result; + Context.lookupMut(T).* = result; return Entity.fromRaw(self, result); }