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
#![cfg(windows)]

extern crate winapi;

use std::sync::atomic::{AtomicUsize, Ordering};

#[repr(transparent)]
/// Wrapper for the C++ VTable member of a COM object.
///
/// When you're using `#[derive(ComImpl)]`, this should be the first member of your struct.
pub struct VTable<T> {
    pub ptr: *const T,
}

impl<T> VTable<T> {
    pub fn new(ptr: &'static T) -> Self {
        VTable { ptr }
    }
}

impl<T> std::fmt::Debug for VTable<T> {
    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
        fmt.debug_tuple("VTable").field(&self.ptr).finish()
    }
}

/// Trait that allows accessing the VTable for all of the COM interfaces your object
/// implements.
pub unsafe trait BuildVTable<T: 'static> {
    const VTBL: T;
    fn static_vtable() -> VTable<T>;
}

#[derive(Debug)]
/// Refcounter object for automatic COM Object implementations. Atomically keeps track of
/// the reference count so that the implementation of IUnknown can properly deallocate
/// the object when all reference counts are gone.
pub struct Refcount {
    count: AtomicUsize,
}

impl Default for Refcount {
    fn default() -> Self {
        Refcount {
            count: AtomicUsize::new(1),
        }
    }
}

impl Refcount {
    #[inline]
    /// `fetch_add(1, Acquire) + 1`
    pub unsafe fn add_ref(&self) -> u32 {
        self.count.fetch_add(1, Ordering::Acquire) as u32 + 1
    }

    #[inline]
    /// `fetch_sub(1, Release) - 1`
    pub unsafe fn release(&self) -> u32 {
        self.count.fetch_sub(1, Ordering::Release) as u32 - 1
    }
}