Generic COM call
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn comFindReturnType(comptime vTableType: type, comptime name: []const u8) type { | |
for (@typeInfo(vTableType).Struct.fields) |field| { | |
if (std.mem.eql(u8, field.name, name)) { | |
const funcType = @typeInfo(@typeInfo(field.field_type).Optional.child); | |
return funcType.Fn.return_type.?; | |
} | |
} | |
return void; | |
} | |
fn comFindVtableType(comptime parentType: type) type { | |
switch (@typeInfo(parentType)) { | |
.Struct => |structInfo| { | |
for (structInfo.fields) |field| { | |
if (std.mem.eql(u8, field.name, "lpVtbl")) { | |
return @typeInfo(field.field_type).Pointer.child; | |
} | |
} | |
}, | |
.Pointer => |pointerInfo| { | |
for (@typeInfo(pointerInfo.child).Struct.fields) |field| { | |
if (std.mem.eql(u8, field.name, "lpVtbl")) { | |
return @typeInfo(field.field_type).Pointer.child; | |
} | |
} | |
}, | |
else => {}, | |
} | |
return void; | |
} | |
fn comCall(comptime name: []const u8, args: var) comFindReturnType(comFindVtableType(@TypeOf(args[0])), name) { | |
if (@field(args[0].lpVtbl[0], name)) |func| { | |
return @call(.{}, func, args); | |
} | |
return undefined; | |
} | |
fn cleanupRenderTarget() void { | |
if (dxContext.mainRenderTargetView) |mainRenderTargetView| { | |
_ = comCall("Release", .{mainRenderTargetView}); | |
dxContext.mainRenderTargetView = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment