Move Context to its own file

main
copygirl 3 months ago
parent ea93c6026a
commit 7f3e79781e
  1. 88
      src/context.zig
  2. 2
      src/entity.zig
  3. 2
      src/id.zig
  4. 2
      src/iter.zig
  5. 88
      src/main.zig
  6. 2
      src/pair.zig
  7. 6
      src/test/util.zig
  8. 2
      src/world.zig

@ -0,0 +1,88 @@
const c = @import("./c.zig");
const meta = @import("./meta.zig");
pub fn Context(comptime ctx: anytype) type {
return struct {
pub const Entity = @import("./entity.zig").Entity(ctx);
pub const Id = @import("./id.zig").Id(ctx);
pub const Iter = @import("./iter.zig").Iter(ctx);
pub const Pair = @import("./pair.zig").Pair(ctx);
pub const World = @import("./world.zig").World(ctx);
/// 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 lookupMut(comptime T: type) *c.ecs_entity_t {
_ = T; // Only necessary to create a unique type.
const EntityHolder = struct {
pub var id: c.ecs_entity_t = 0;
};
return &EntityHolder.id;
}
/// Converts the specified value to an `ecs_entity_t`.
///
/// If `null` is passed to this function, either directly or through an
/// optional type of one of the supported types, `0` is returned. It's up to
/// the caller to do any necessary testing for `0` values, and whether the
/// returned entity is valid or alive.
///
/// The following can be converted:
/// - `Entity` and `ecs_entity_t` - Self-explanatory.
/// - `type` - Looks up the entity associated with that type.
pub fn anyToEntity(value: anytype) c.ecs_entity_t {
return switch (@TypeOf(value)) {
@TypeOf(null) => 0,
c.ecs_entity_t => value,
Entity => value.raw,
?Entity => if (value) |v| v.raw else 0,
type => lookup(value),
else => @compileError("Value of type " ++ @typeName(@TypeOf(value)) ++ " can't be converted to Entity"),
};
}
/// Converts the specified value to an `ecs_id_t`.
///
/// If `null` is passed to this function, either directly or through an
/// optional type of one of the supported types, `0` is returned. It's up to
/// the caller to do any necessary testing for `0` values, and whether the
/// returned id is valid.
///
/// The following can be converted:
/// - `Id` and `ecs_id_it` - Self-explanatory.
/// - `Entity` and `ecs_entity_t` - Self-explanatory.
/// - `Pair` - Converts to the equivalent `Id`.
/// - `.{ relation, target }` - A `Pair`, converted using `anyToEntity()`.
/// - `type` - Looks up the entity associated with that type.
pub fn anyToId(value: anytype) c.ecs_id_t {
const T = @TypeOf(value);
if (comptime meta.isTuple(T)) {
if (@typeInfo(T).Struct.fields.len != 2)
@compileError("Value of type " ++ @typeName(T) ++ " must be a tuple with 2 elements, to be a Pair");
const relation = anyToEntity(value[0]);
const target = anyToEntity(value[1]);
return c.ecs_make_pair(relation, target);
}
return switch (T) {
@TypeOf(null) => 0,
c.ecs_id_t => value,
Id => value.raw,
?Id => if (value) |v| v.raw else 0,
Pair => value.raw,
?Pair => if (value) |v| v.raw else 0,
// Technically same type as `ecs_id_it`.
// c.ecs_entity_t => value,
Entity => value.raw,
?Entity => if (value) |v| v.raw else 0,
type => lookup(value),
else => @compileError("Value of type " ++ @typeName(T) ++ " can't be converted to Id"),
};
}
};
}

@ -40,7 +40,7 @@ pub fn Entity(comptime ctx: anytype) type {
return struct {
const Self = @This();
const Context = flecszigble.Context(ctx);
const Context = @import("./context.zig").Context(ctx);
const World = Context.World;
const Id = Context.Id;

@ -16,7 +16,7 @@ pub fn Id(comptime ctx: anytype) type {
return struct {
const Self = @This();
const Context = flecszigble.Context(ctx);
const Context = @import("./context.zig").Context(ctx);
const World = Context.World;
const Entity = Context.Entity;
const Pair = Context.Pair;

@ -5,7 +5,7 @@ pub fn Iter(comptime ctx: anytype) type {
return struct {
const Self = @This();
const Context = flecszigble.Context(ctx);
const Context = @import("./context.zig").Context(ctx);
const World = Context.World;
const Entity = Context.Entity;
const Id = Context.Id;

@ -1,6 +1,5 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const meta = @import("./meta.zig");
// Meant for internal use, but exposed since flecs-zig-ble
// doesn't wrap nearly enough of Flecs' available features.
@ -10,6 +9,7 @@ pub const c = @import("./c.zig");
pub const FlecsError = errors.FlecsError;
pub const Path = @import("./path.zig");
pub usingnamespace @import("./context.zig");
pub usingnamespace @import("./entity.zig");
pub usingnamespace @import("./id.zig");
pub usingnamespace @import("./iter.zig");
@ -36,92 +36,6 @@ pub fn init(alloc: Allocator) void {
c.ecs_os_api.free_ = flecsFree;
}
pub fn Context(comptime ctx: anytype) type {
return struct {
pub const Entity = @import("./entity.zig").Entity(ctx);
pub const Id = @import("./id.zig").Id(ctx);
pub const Iter = @import("./iter.zig").Iter(ctx);
pub const Pair = @import("./pair.zig").Pair(ctx);
pub const World = @import("./world.zig").World(ctx);
/// 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 lookupMut(comptime T: type) *c.ecs_entity_t {
_ = T; // Only necessary to create a unique type.
const EntityHolder = struct {
pub var id: c.ecs_entity_t = 0;
};
return &EntityHolder.id;
}
/// Converts the specified value to an `ecs_entity_t`.
///
/// If `null` is passed to this function, either directly or through an
/// optional type of one of the supported types, `0` is returned. It's up to
/// the caller to do any necessary testing for `0` values, and whether the
/// returned entity is valid or alive.
///
/// The following can be converted:
/// - `Entity` and `ecs_entity_t` - Self-explanatory.
/// - `type` - Looks up the entity associated with that type.
pub fn anyToEntity(value: anytype) c.ecs_entity_t {
return switch (@TypeOf(value)) {
@TypeOf(null) => 0,
c.ecs_entity_t => value,
Entity => value.raw,
?Entity => if (value) |v| v.raw else 0,
type => lookup(value),
else => @compileError("Value of type " ++ @typeName(@TypeOf(value)) ++ " can't be converted to Entity"),
};
}
/// Converts the specified value to an `ecs_id_t`.
///
/// If `null` is passed to this function, either directly or through an
/// optional type of one of the supported types, `0` is returned. It's up to
/// the caller to do any necessary testing for `0` values, and whether the
/// returned id is valid.
///
/// The following can be converted:
/// - `Id` and `ecs_id_it` - Self-explanatory.
/// - `Entity` and `ecs_entity_t` - Self-explanatory.
/// - `Pair` - Converts to the equivalent `Id`.
/// - `.{ relation, target }` - A `Pair`, converted using `anyToEntity()`.
/// - `type` - Looks up the entity associated with that type.
pub fn anyToId(value: anytype) c.ecs_id_t {
const T = @TypeOf(value);
if (comptime meta.isTuple(T)) {
if (@typeInfo(T).Struct.fields.len != 2)
@compileError("Value of type " ++ @typeName(T) ++ " must be a tuple with 2 elements, to be a Pair");
const relation = anyToEntity(value[0]);
const target = anyToEntity(value[1]);
return c.ecs_make_pair(relation, target);
}
return switch (T) {
@TypeOf(null) => 0,
c.ecs_id_t => value,
Id => value.raw,
?Id => if (value) |v| v.raw else 0,
Pair => value.raw,
?Pair => if (value) |v| v.raw else 0,
// Technically same type as `ecs_id_it`.
// c.ecs_entity_t => value,
Entity => value.raw,
?Entity => if (value) |v| v.raw else 0,
type => lookup(value),
else => @compileError("Value of type " ++ @typeName(T) ++ " can't be converted to Id"),
};
}
};
}
fn flecsMalloc(size: i32) callconv(.C) ?*anyopaque {
return allocLengthEncodedSlice(size, null).ptr;
}

@ -24,7 +24,7 @@ pub fn Pair(comptime ctx: anytype) type {
return struct {
const Self = @This();
const Context = flecszigble.Context(ctx);
const Context = @import("./context.zig").Context(ctx);
const World = Context.World;
const Entity = Context.Entity;
const Id = Context.Id;

@ -6,10 +6,8 @@ const expectStrEql = std.testing.expectEqualStrings;
const flecszigble = @import("../main.zig");
const c = flecszigble.c;
const context = flecszigble.Context(void);
const Entity = context.Entity;
const Iter = context.Iter;
const Id = context.Id;
const Context = @import("../context.zig").Context(void);
const Iter = Context.Iter;
pub const MAX_SYS_COLUMNS = 20;
pub const MAX_ENTITIES = 256;

@ -11,7 +11,7 @@ pub fn World(comptime ctx: anytype) type {
return struct {
const Self = @This();
const Context = flecszigble.Context(ctx);
const Context = @import("./context.zig").Context(ctx);
const Entity = Context.Entity;
const Iter = Context.Iter;

Loading…
Cancel
Save