summaryrefslogtreecommitdiff
path: root/src/type/Watcher.zig
blob: 6369df59da4d11d7f20ff02c3e493f428fcbc40c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const std = @import("std");
const c = @import("c");
const zpy = @import("../root.zig");

const Self = @This();

id: c_int,

pub const Callback = fn (*zpy.Type) error{Exception}!void;

pub fn add(callback: Callback) !Self {
    const wrapper = struct {
        pub fn wrapper(t: [*c]c.PyObject) callconv(.c) c_int {
            return if (std.meta.isError(callback(@ptrCast(t)))) -1 else 0;
        }
    };

    const result = c.PyType_AddWatcher(&wrapper);

    if (result == -1) {
        return error.Exception;
    }

    return .{ .id = result };
}

pub fn clear(self: Self) !void {
    if (c.PyType_Clear(self.id) == -1) {
        return error.Exception;
    }
}

pub fn watch(self: Self, t: *zpy.Dict) !void {
    if (c.PyType_Watch(self.id, t) == -1) {
        return error.Exception;
    }
}

pub fn unwatch(self: Self, t: *zpy.Dict) !void {
    if (c.PyType_Unwatch(self.id, t) == -1) {
        return error.Exception;
    }
}