summaryrefslogtreecommitdiff
path: root/src/type/root.zig
blob: 128f3d917034f088bab0fe939344e706c2048419 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const std = @import("std");
const c = @import("c");

const zpy = @import("../root.zig");

pub const Type = extern struct {
    const Self = @This();

    data: c.PyObject,

    pub const Watcher = @import("Watcher.zig");

    pub const Description = extern struct {
        name: [*:0]const u8,
        doc: ?[*:0]const u8 = null,
    };

    pub fn new(description: Description, comptime fields: []const Description) !*Self {
        var c_desc: c.PyStructSequence_Desc = .{
            .name = @ptrCast(description.name),
            .doc = @ptrCast(description.doc),
            .fields = @ptrCast(@constCast(fields)),
            .n_in_sequence = @intCast(fields.len),
        };

        return @ptrCast(c.PyStructSequence_NewType(@ptrCast(&c_desc)) orelse return error.Exception);
    }

    pub fn getDict(self: *Self) *zpy.Dict {
        return @ptrCast(c.PyType_GetDict(@ptrCast(self)));
    }

    pub fn modified(self: *Self) void {
        c.PyType_Modified(@ptrCast(self));
    }

    pub fn isGc(self: *Self) bool {
        return c.PyType_IS_GC(@ptrCast(self)) != 0;
    }

    pub fn isSubtype(self: *Self, target: *Self) bool {
        return c.PyType_IsSubtype(@ptrCast(self), @ptrCast(target)) != 0;
    }

    pub fn ready(self: *Self) !void {
        if (c.PyType_Ready(@ptrCast(self)) == -1) {
            return error.Exception;
        }
    }

    pub fn getName(self: *Self) *zpy.Str {
        return @ptrCast(c.Pytype_GetName(@ptrCast(self)));
    }

    pub fn asObject(self: *Self) *zpy.Object {
        return @ptrCast(self);
    }
};