blob: 21cacb99d995442f74664a049b19410e75e61d03 (
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
|
use super::*;
#[doc(hidden)]
#[macro_export]
macro_rules! com_call {
($vtbl:ty, $this:ident.$method:ident($($args:tt)*)) => {
((&**($this.as_raw() as *mut *mut $vtbl)).$method)($this.as_raw(), $($args)*)
}
}
#[repr(transparent)]
pub struct ComPtr(core::ptr::NonNull<core::ffi::c_void>);
impl ComPtr {
pub fn as_raw(&self) -> *mut core::ffi::c_void {
unsafe { core::mem::transmute_copy(self) }
}
pub fn cast(&self, iid: &GUID) -> Option<Self> {
let mut result = None;
unsafe {
com_call!(
IUnknown_Vtbl,
self.QueryInterface(iid, &mut result as *mut _ as _)
);
}
result
}
}
impl PartialEq for ComPtr {
fn eq(&self, other: &Self) -> bool {
self.cast(&IID_IUnknown).unwrap().0 == other.cast(&IID_IUnknown).unwrap().0
}
}
impl Eq for ComPtr {}
impl Clone for ComPtr {
fn clone(&self) -> Self {
unsafe {
com_call!(IUnknown_Vtbl, self.AddRef());
}
Self(self.0)
}
}
impl Drop for ComPtr {
fn drop(&mut self) {
unsafe {
com_call!(IUnknown_Vtbl, self.Release());
}
}
}
|