marlersoft / zigwin32gen

Generates Complete Zig bindings for Win32. See https://github.com/marlersoft/zigwin32 for the bindings themselves.
108 stars 17 forks source link

Overrides? #3

Open marler8997 opened 3 years ago

marler8997 commented 3 years ago

daurnimator asked if zigwin32 had the ability to override types. He suggested the root module could do something like this:

pub const zigwin32_config = struct {
    pub const overrides = struct {
        pub const SOME_STRUCT = struct{....}
    };
};

To support this, whenever I generate a type, instead of this:

pub const SOME_STRUCT = struct {
    //...
};

I could do something like this:

pub const SOME_STRUCT = if (getOverride("SOME_STRUCT")) |o| o else struct {
    //...
};

Where getOverride would look something like:

pub fn getOverride(comptime sym: []const u8) ?type {
    const root = @import("root");
    if (@hasDecl(root, "zigwin32_config")) {
        if (@hasDecl(root.zigwin32_config, "override")) {
            if (@hasDecl(root.zigwin32_config.override, sym)) {
                return @field(root.zigwin32_config.override, sym);
            }
        }
    }
    return null;
}

Note that this mechanism could be leveraged to help with a solution to the std interop problem.

daurnimator commented 3 years ago
if (getOverride("SOME_STRUCT")) |o| o else struct {

getOverride("SOME_STRUCT") orelse struct {....

Note also that overrides would follow the same structures you have, e.g.

pub const zigwin32_config = struct {
    pub const overrides = struct {
        pub const ai = struct {
          pub const FOO = struct{....}
        }
        pub const otherthing = struct {
          pub const BAR = struct{....}
        }
        pub const core = struct {
          pub const HANDLE = std.os.windows.HANDLE;
        }
    };
};