Use new opaque {} syntax

This commit is contained in:
Isaac Freund
2020-09-26 01:23:03 +02:00
parent dae8765366
commit bf0684066a
4 changed files with 109 additions and 151 deletions
+80 -92
View File
@@ -4,114 +4,105 @@ const os = std.os;
const common = @import("common.zig");
pub usingnamespace @import("test_data/client_proto.zig");
pub const Proxy = struct {
pub const Impl = @OpaqueType();
impl: *Impl,
extern fn wl_proxy_create(factory: *Impl, interface: *const common.Interface) *Impl;
pub fn create(factory: Proxy, interface: *const common.Interface) error{OutOfMemory}!Proxy {
return .{ .impl = wl_proxy_create(factory.impl, interface) orelse return error.OutOfMemory };
pub const Proxy = opaque {
extern fn wl_proxy_create(factory: *Proxy, interface: *const common.Interface) *Proxy;
pub fn create(factory: *Proxy, interface: *const common.Interface) error{OutOfMemory}!*Proxy {
return wl_proxy_create(factory.impl, interface) orelse error.OutOfMemory;
}
extern fn wl_proxy_destroy(proxy: *Impl) void;
pub fn destroy(proxy: Proxy) void {
wl_proxy_destroy(proxy.impl);
extern fn wl_proxy_destroy(proxy: *Proxy) void;
pub fn destroy(proxy: *Proxy) void {
wl_proxy_destroy(proxy);
}
extern fn wl_proxy_marshal_array(proxy: *Impl, opcode: u32, args: [*]common.Argument) void;
pub fn marshal(proxy: Proxy, opcode: u32, args: [*]common.Argument) void {
wl_proxy_marshal_array(proxy.impl, opcode, args);
extern fn wl_proxy_marshal_array(proxy: *Proxy, opcode: u32, args: [*]common.Argument) void;
pub fn marshal(proxy: *Proxy, opcode: u32, args: [*]common.Argument) void {
wl_proxy_marshal_array(proxy, opcode, args);
}
extern fn wl_proxy_marshal_array_constructor(
proxy: *Impl,
proxy: *Proxy,
opcode: u32,
args: [*]common.Argument,
interface: *const common.Interface,
) ?*Impl;
) ?*Proxy;
pub fn marshalConstructor(
proxy: Proxy,
proxy: *Proxy,
opcode: u32,
args: [*]common.Argument,
interface: *const common.Interface,
) error{OutOfMemory}!Proxy {
return Proxy{
.impl = wl_proxy_marshal_array_constructor(proxy.impl, opcode, args, interface) orelse
return error.OutOfMemory,
};
) error{OutOfMemory}!*Proxy {
return wl_proxy_marshal_array_constructor(proxy, opcode, args, interface) orelse
error.OutOfMemory;
}
extern fn wl_proxy_marshal_array_constructor_versioned(
proxy: *Impl,
proxy: *Proxy,
opcode: u32,
args: [*]common.Argument,
interface: *common.Interface,
version: u32,
) ?*Impl;
) ?*Proxy;
pub fn marshalConstructorVersioned(
proxy: Proxy,
proxy: *Proxy,
opcode: u32,
args: [*]common.Argument,
interface: *const common.Interface,
version: u32,
) error{OutOfMemory}!Proxy {
return .{
.impl = wl_proxy_marshal_array_constructor(proxy.impl, opcode, args, interface, version) orelse
return error.OutOfMemory,
};
) error{OutOfMemory}!*Proxy {
return wl_proxy_marshal_array_constructor(proxy, opcode, args, interface, version) orelse
error.OutOfMemory;
}
extern fn wl_proxy_add_listener(proxy: *Proxy.Impl, implementation: [*]fn () callconv(.C) void, data: ?*c_void) i32;
pub fn addListener(proxy: Proxy, implementation: [*]fn () callconv(.C) void, data: ?*c_void) error{AlreadyHasListener}!void {
if (wl_proxy_add_listener(proxy.impl, implementation, data) == -1) return error.AlreadyHasListener;
extern fn wl_proxy_add_listener(proxy: *Proxy, implementation: [*]fn () callconv(.C) void, data: ?*c_void) i32;
pub fn addListener(proxy: *Proxy, implementation: [*]fn () callconv(.C) void, data: ?*c_void) error{AlreadyHasListener}!void {
if (wl_proxy_add_listener(proxy, implementation, data) == -1) return error.AlreadyHasListener;
}
extern fn wl_proxy_set_user_data(proxy: *Impl, user_data: ?*c_void) void;
pub fn setUserData(proxy: Proxy, user_data: ?*c_void) void {
wl_proxy_set_user_data(proxy.impl, user_data);
extern fn wl_proxy_set_user_data(proxy: *Proxy, user_data: ?*c_void) void;
pub fn setUserData(proxy: *Proxy, user_data: ?*c_void) void {
wl_proxy_set_user_data(proxy, user_data);
}
extern fn wl_proxy_get_user_data(proxy: *Impl) ?*c_void;
pub fn getUserData(proxy: Proxy) ?*c_void {
return wl_proxy_get_user_data(proxy.impl);
extern fn wl_proxy_get_user_data(proxy: *Proxy) ?*c_void;
pub fn getUserData(proxy: *Proxy) ?*c_void {
return wl_proxy_get_user_data(proxy);
}
extern fn wl_proxy_get_version(proxy: *Impl) u32;
pub fn getVersion(proxy: Proxy) u32 {
return wl_proxy_get_version(proxy.impl);
extern fn wl_proxy_get_version(proxy: *Proxy) u32;
pub fn getVersion(proxy: *Proxy) u32 {
return wl_proxy_get_version(proxy);
}
extern fn wl_proxy_get_id(proxy: *Impl) u32;
pub fn getId(proxy: Proxy) u32 {
return wl_proxy_get_id(proxy.impl);
extern fn wl_proxy_get_id(proxy: *Proxy) u32;
pub fn getId(proxy: *Proxy) u32 {
return wl_proxy_get_id(proxy);
}
};
pub const display_functions = struct {
const Impl = Display.Impl;
extern fn wl_display_connect(name: ?[*:0]const u8) ?*Impl;
pub fn connect(name: ?[*:0]const u8) error{ConnectFailed}!Display {
return Display{ .impl = wl_display_connect(name) orelse return error.ConnectFailed };
extern fn wl_display_connect(name: ?[*:0]const u8) ?*Display;
pub fn connect(name: ?[*:0]const u8) error{ConnectFailed}!*Display {
return wl_display_connect(name) orelse return error.ConnectFailed;
}
extern fn wl_display_connect_to_fd(fd: c_int) ?*Impl;
pub fn connectToFd(fd: os.fd_t) error{ConnectFailed}!Display {
return .{ .impl = wl_display_connect_to_fd(fd) orelse return error.ConnectFailed };
extern fn wl_display_connect_to_fd(fd: c_int) ?*Display;
pub fn connectToFd(fd: os.fd_t) error{ConnectFailed}!*Display {
return wl_display_connect_to_fd(fd) orelse return error.ConnectFailed;
}
extern fn wl_display_disconnect(display: *Impl) void;
pub fn disconnect(self: Display) void {
wl_display_disconnect(self.impl);
extern fn wl_display_disconnect(display: *Display) void;
pub fn disconnect(display: *Display) void {
wl_display_disconnect(display);
}
extern fn wl_display_get_fd(display: *Impl) c_int;
pub fn getFd(self: Display) os.fd_t {
return wl_display_get_fd(self.impl);
extern fn wl_display_get_fd(display: *Display) c_int;
pub fn getFd(display: *Display) os.fd_t {
return wl_display_get_fd(display);
}
extern fn wl_display_dispatch(display: *Impl) c_int;
pub fn dispatch(self: Display) !void {
const rc = wl_display_dispatch(self.impl);
extern fn wl_display_dispatch(display: *Display) c_int;
pub fn dispatch(display: *Display) !void {
const rc = wl_display_dispatch(display);
return switch (os.errno(rc)) {
0 => @intCast(u31, rc),
// TODO
@@ -119,9 +110,9 @@ pub const display_functions = struct {
};
}
extern fn wl_display_dispatch_pending(display: *Impl) c_int;
pub fn dispatchPending(self: Display) !u31 {
const rc = wl_display_dispatch_pending(self.impl);
extern fn wl_display_dispatch_pending(display: *Display) c_int;
pub fn dispatchPending(display: *Display) !u31 {
const rc = wl_display_dispatch_pending(display);
return switch (os.errno(rc)) {
0 => @intCast(u31, rc),
// TODO
@@ -129,9 +120,9 @@ pub const display_functions = struct {
};
}
extern fn wl_display_dispatch_queue(display: *Impl, queue: *EventQueue.Impl) c_int;
pub fn dispatchQueue(self: Display, queue: EventQueue) !u31 {
const rc = wl_display_dispatch_queue(self.impl, queue.impl);
extern fn wl_display_dispatch_queue(display: *Display, queue: *EventQueue.Impl) c_int;
pub fn dispatchQueue(display: *Display, queue: EventQueue) !u31 {
const rc = wl_display_dispatch_queue(display, queue.impl);
return switch (os.errno(rc)) {
0 => @intCast(u31, rc),
// TODO
@@ -139,9 +130,9 @@ pub const display_functions = struct {
};
}
extern fn wl_display_dispatch_queue_pending(display: *Impl, queue: *EventQueue.Impl) c_int;
pub fn dispatchQueuePending(self: Display, queue: EventQueue) !u31 {
const rc = wl_display_dispatch_queue_pending(self.impl, queue.impl);
extern fn wl_display_dispatch_queue_pending(display: *Display, queue: *EventQueue.Impl) c_int;
pub fn dispatchQueuePending(display: *Display, queue: EventQueue) !u31 {
const rc = wl_display_dispatch_queue_pending(display, queue.impl);
return switch (os.errno(rc)) {
0 => @intCast(u31, rc),
// TODO
@@ -149,9 +140,9 @@ pub const display_functions = struct {
};
}
extern fn wl_display_roundtrip(display: *Impl) c_int;
pub fn roundtrip(self: Display) !u31 {
const rc = wl_display_roundtrip(self.impl);
extern fn wl_display_roundtrip(display: *Display) c_int;
pub fn roundtrip(display: *Display) !u31 {
const rc = wl_display_roundtrip(display);
return switch (os.errno(rc)) {
0 => @intCast(u31, rc),
// TODO
@@ -159,9 +150,9 @@ pub const display_functions = struct {
};
}
extern fn wl_display_roundtrip_queue(display: *Impl, queue: *EventQueue.Impl) c_int;
pub fn roundtripQueue(self: Display, queue: EventQueue) !u31 {
const rc = wl_display_roundtrip_queue(self.impl, queue.impl);
extern fn wl_display_roundtrip_queue(display: *Display, queue: *EventQueue.Impl) c_int;
pub fn roundtripQueue(display: *Display, queue: EventQueue) !u31 {
const rc = wl_display_roundtrip_queue(display, queue.impl);
return switch (os.errno(rc)) {
0 => @intCast(u31, rc),
// TODO
@@ -169,9 +160,9 @@ pub const display_functions = struct {
};
}
extern fn wl_display_flush(display: *Impl) c_int;
pub fn flush(self: Display) error{WouldBlock}!u31 {
const rc = wl_display_dispatch_queue_pending(self.impl, queue.impl);
extern fn wl_display_flush(display: *Display) c_int;
pub fn flush(display: *Display) error{WouldBlock}!u31 {
const rc = wl_display_dispatch_queue_pending(display, queue.impl);
return switch (os.errno(rc)) {
0 => @intCast(u31, rc),
os.EAGAIN => error.WouldBlock,
@@ -179,24 +170,21 @@ pub const display_functions = struct {
};
}
extern fn wl_display_create_queue(display: *Impl) *EventQueue.Impl;
pub fn createQueue(self: Display) error{OutOfMemory}!EventQueue {
return .{ .impl = wl_display_create_queue(self.impl) orelse return error.OutOfMemory };
extern fn wl_display_create_queue(display: *Display) *EventQueue.Impl;
pub fn createQueue(display: *Display) error{OutOfMemory}!EventQueue {
return .{ .impl = wl_display_create_queue(display) orelse return error.OutOfMemory };
}
// TODO: should we interpret this return value?
extern fn wl_display_get_error(display: *Impl) c_int;
pub fn getError(self: Display) i32 {
return wl_display_get_error(self.impl);
extern fn wl_display_get_error(display: *Display) c_int;
pub fn getError(display: *Display) i32 {
return wl_display_get_error(display);
}
};
pub const EventQueue = struct {
const Impl = @OpaqueType();
impl: *Impl,
extern fn wl_event_queue_destroy(queue: *Impl) void;
pub fn destroy(event_queue: EventQueue) void {
wl_event_queue_destroy(event_queue.impl);
pub const EventQueue = opaque {
extern fn wl_event_queue_destroy(queue: *EventQueue) void;
pub fn destroy(event_queue: *EventQueue) void {
wl_event_queue_destroy(event_queue);
}
};
+1 -1
View File
@@ -1,6 +1,6 @@
const std = @import("std");
pub const Object = @OpaqueType();
pub const Object = opaque{};
pub const Message = extern struct {
name: [*:0]const u8,
+2 -2
View File
@@ -12,11 +12,11 @@ pub fn main() !void {
_ = try display.roundtrip();
}
fn global(data: *u32, registry: *wl.Registry.Impl, name: u32, interface: [*:0]const u8, version: u32) callconv(.C) void {
fn global(data: *u32, registry: *wl.Registry, name: u32, interface: [*:0]const u8, version: u32) callconv(.C) void {
std.debug.warn("foo is {}\n", .{data.*});
std.debug.warn("interface is {}\n", .{interface});
}
fn global_remove(data: *u32, registry: *wl.Registry.Impl, name: u32) callconv(.C) void {
fn global_remove(data: *u32, registry: *wl.Registry, name: u32) callconv(.C) void {
// do nothing
}
+26 -56
View File
@@ -1,12 +1,9 @@
const common = @import("../common.zig");
const client = @import("../client.zig");
pub const Display = struct {
pub const Display = opaque {
pub usingnamespace client.display_functions;
pub const Impl = @OpaqueType();
impl: *Impl,
pub const interface = common.Interface{
.name = "wl_display",
.version = 1,
@@ -61,52 +58,38 @@ pub const Display = struct {
return extern struct {
@"error": fn (
data: T,
display: *Impl,
display: *Display,
object_id: ?*common.Object,
code: u32,
message: [*:0]const u8,
) callconv(.C) void,
delete_id: fn (
data: T,
display: *Impl,
display: *Display,
id: u32,
) callconv(.C) void,
};
}
pub fn toProxy(display: Display) client.Proxy {
return .{ .impl = @ptrCast(*client.Proxy.Impl, display.impl) };
pub fn addListener(display: *Display, comptime T: type, listener: Listener(T), data: T) !void {
const proxy = @ptrCast(*client.Proxy, display);
try proxy.addListener(@intToPtr([*]fn () callconv(.C) void, @ptrToInt(&listener)), data);
}
pub fn addListener(display: Display, comptime T: type, listener: Listener(T), data: T) !void {
try display.toProxy().addListener(@intToPtr([*]fn () callconv(.C) void, @ptrToInt(&listener)), data);
}
pub fn sync(display: Display) !Callback {
pub fn sync(display: *Display) !*Callback {
const proxy = @ptrCast(*client.Proxy, display);
var args = [_]common.Argument{.{ .o = null }};
return Callback{
.impl = @ptrCast(
*Callback.Impl,
try display.toProxy().marshalConstructor(opcodes.sync, &args, &Callback.interface),
),
};
return @ptrCast(*Callback, try proxy.marshalConstructor(opcodes.sync, &args, &Callback.interface));
}
pub fn getRegistry(display: Display) !Registry {
pub fn getRegistry(display: *Display) !*Registry {
const proxy = @ptrCast(*client.Proxy, display);
var args = [_]common.Argument{.{ .o = null }};
return Registry{
.impl = @ptrCast(
*Registry.Impl,
(try display.toProxy().marshalConstructor(opcodes.get_registry, &args, &Registry.interface)).impl,
),
};
return @ptrCast(*Registry, try proxy.marshalConstructor(opcodes.get_registry, &args, &Registry.interface));
}
};
pub const Registry = struct {
pub const Impl = @OpaqueType();
impl: *Impl,
pub const Registry = opaque {
pub const interface = common.Interface{
.name = "wl_registry",
.version = 1,
@@ -142,47 +125,37 @@ pub const Registry = struct {
return extern struct {
global: fn (
data: T,
registry: *Impl,
registry: *Registry,
name: u32,
interface: [*:0]const u8,
version: u32,
) callconv(.C) void,
global_remove: fn (
data: T,
registry: *Impl,
registry: *Registry,
name: u32,
) callconv(.C) void,
};
}
pub fn toProxy(registry: Registry) client.Proxy {
return .{ .impl = @ptrCast(*client.Proxy.Impl, registry.impl) };
pub fn addListener(registry: *Registry, comptime T: type, listener: Listener(T), data: T) !void {
const proxy = @ptrCast(*client.Proxy, registry);
try proxy.addListener(@intToPtr([*]fn () callconv(.C) void, @ptrToInt(&listener)), data);
}
pub fn addListener(registry: Registry, comptime T: type, listener: Listener(T), data: T) !void {
try registry.toProxy().addListener(@intToPtr([*]fn () callconv(.C) void, @ptrToInt(&listener)), data);
}
pub fn bind(registry: Registry, name: u32, comptime T: type, version: u32) !T {
pub fn bind(registry: *Registry, name: u32, comptime T: type, version: u32) !*T {
const proxy = @ptrCast(*client.Proxy, registry);
var args = [_]common.Argument{
.{ .u = name },
.{ .s = T.interface.name },
.{ .u = version },
.{ .o = null },
};
return T{
.impl = @ptrCast(
*T.Impl,
(try registry.toProxy().marshalConstructorVersioned(opcodes.bind, &args, T.interface, version)).impl,
),
};
return @ptrCast(*T, proxy.marshalConstructorVersioned(opcodes.bind, &args, T.interface, version));
}
};
pub const Callback = struct {
pub const Impl = @OpaqueType();
impl: *Impl,
pub const Callback = opaque {
pub const interface = common.Interface{
.name = "wl_callback",
.version = 1,
@@ -204,17 +177,14 @@ pub const Callback = struct {
return extern struct {
done: fn (
data: T,
callback: *Impl,
callback: *Callback,
callback_data: u32,
) callconv(.C) void,
};
}
pub fn toProxy(registry: Registry) client.Proxy {
return .{ .impl = @ptrCast(*client.Proxy.Impl, registry.impl) };
}
pub fn addListener(registry: Registry, comptime T: type, listener: Listener(T), data: T) !void {
try registry.toProxy().addListener(@intToPtr([*]fn () callconv(.C) void, @ptrToInt(&listener)), data);
pub fn addListener(callback: *Callback, comptime T: type, listener: Listener(T), data: T) !void {
const proxy = @ptrCast(*client.Proxy, callback);
try proxy.addListener(@intToPtr([*]fn () callconv(.C) void, @ptrToInt(&listener)), data);
}
};