diff options
Diffstat (limited to 'src/object/root.zig')
| -rw-r--r-- | src/object/root.zig | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/object/root.zig b/src/object/root.zig new file mode 100644 index 0000000..1895d3f --- /dev/null +++ b/src/object/root.zig @@ -0,0 +1,76 @@ +const std = @import("std"); +const wayland = @import("../root.zig"); + +const Self = @This(); + +pub const interface = @import("interface.zig"); + +pub const Ref = struct { + id: u32, + + pub const @"null": @This() = .{ + .id = 0, + }; + + pub const display: @This() = .{ + .id = 1, + }; + + pub const registry: @This() = .{ + .id = 2, + }; +}; + +const VTable = struct { + on_event: *const fn ( + ptr: *anyopaque, + ctx: wayland.Context, + opcode: u16, + args: []const u8, + ) void, +}; + +ptr: ?*anyopaque, +vtable: VTable, +name: []const u8, + +pub inline fn on_event( + self: *Self, + ctx: wayland.Context, + opcode: u16, + args: []const u8, +) !void { + if (self.ptr) |ptr| { + self.vtable.on_event(ptr, ctx, opcode, args); + } else { + return error.UseAfterRelease; + } +} + +pub inline fn from_self(ptr: anytype) Self { + const T = @TypeOf(ptr.*); + + return .{ + .ptr = ptr, + .vtable = VTable{ + .on_event = T.Events.on_event, + }, + .name = @typeName(T), + }; +} + +pub fn disable(self: *Self) void { + self.ptr = null; +} + +pub fn format( + self: *const Self, + comptime fmt: []const u8, + options: std.fmt.FormatOptions, + writer: anytype, +) !void { + _ = fmt; + _ = options; + + try writer.print("Object<{s}> {{ {?} }}", .{self.name, self.ptr}); +} |