-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory.zig
116 lines (97 loc) · 3.29 KB
/
factory.zig
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const std = @import("std");
const com = @import("com.zig");
const windows = std.os.windows;
const windows_extra = @import("windows_extra.zig");
const main = @import("dllmain.zig");
pub const WatchedClassFactory = extern struct {
const Self = @This();
const VTable = extern struct {
unknown: com.IUnknown.VTable(Self),
class_factory: com.IClassFactory.VTable(Self),
};
const CreateFn = *const fn (
riid: ?*const windows.GUID,
ppvObject: ?*?*anyopaque,
) callconv(windows.WINAPI) windows.HRESULT;
vtable: *const Self.VTable,
create_fn: CreateFn,
ref: u32,
pub fn QueryInterface(
self: *Self,
riid: ?*const windows.GUID,
ppvObject: ?*?*anyopaque,
) callconv(windows.WINAPI) windows.HRESULT {
if (!com.IsEqualIID(riid.?, com.IClassFactory.IID)) {
ppvObject.?.* = null;
return windows.E_NOINTERFACE;
}
ppvObject.?.* = self;
_ = self.vtable.unknown.AddRef(self);
return windows.S_OK;
}
pub fn AddRef(self: *Self) callconv(windows.WINAPI) u32 {
self.ref += 1;
return self.ref;
}
pub fn Release(self: *Self) callconv(windows.WINAPI) u32 {
self.ref -= 1;
if (self.ref == 0) {
main.global_allocator.destroy(self);
_ = @atomicRmw(windows.LONG, &main.obj_count, .Sub, 1, .monotonic);
return 0;
}
return self.ref;
}
pub fn CreateInstance(
self: *Self,
pUnkOuter: ?*com.IUnknown,
riid: ?*const windows.GUID,
ppvObject: ?*?*anyopaque,
) callconv(windows.WINAPI) windows.HRESULT {
if (pUnkOuter != null) {
return windows_extra.CLASS_E_NOAGGREGATION;
}
return self.create_fn(riid, ppvObject);
}
pub fn LockServer(
self: *Self,
fLock: windows.BOOL,
) callconv(windows.WINAPI) windows.HRESULT {
_ = self;
if (fLock != 0) {
_ = @atomicRmw(windows.LONG, &main.lock_count, .Add, 1, .monotonic);
} else {
_ = @atomicRmw(windows.LONG, &main.lock_count, .Sub, 1, .monotonic);
}
return windows.S_OK;
}
pub fn create(
allocator: std.mem.Allocator,
create_fn: CreateFn,
riid: ?*const windows.GUID,
ppvObject: ?*?*anyopaque,
) std.mem.Allocator.Error!void {
var obj = try allocator.create(WatchedClassFactory);
obj.vtable = &WatchedClassFactory.vtable_impl;
obj.create_fn = create_fn;
obj.ref = 1;
const result = obj.vtable.unknown.QueryInterface(obj, riid, ppvObject);
// since we set everything up before this call, any error is a failure on our part
std.debug.assert(result == windows.S_OK);
// Release to decrement reference count after it was incremented in the
// QueryInterface call
_ = obj.vtable.unknown.Release(obj);
_ = @atomicRmw(windows.LONG, &main.obj_count, .Add, 1, .monotonic);
}
pub const vtable_impl: Self.VTable = .{
.unknown = .{
.QueryInterface = QueryInterface,
.AddRef = AddRef,
.Release = Release,
},
.class_factory = .{
.CreateInstance = CreateInstance,
.LockServer = LockServer,
},
};
};