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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use proc_macro2::{Ident, TokenStream as HelperTokenStream};
use quote::{format_ident, quote};
use std::collections::HashMap;
use syn::ItemStruct;
fn get_iclass_factory_interface_ident() -> Ident {
format_ident!("IClassFactory")
}
pub fn get_class_factory_base_interface_idents() -> Vec<Ident> {
vec![get_iclass_factory_interface_ident()]
}
pub fn get_class_factory_aggr_map() -> HashMap<Ident, Vec<Ident>> {
HashMap::new()
}
pub fn generate(struct_item: &ItemStruct) -> HelperTokenStream {
let base_interface_idents = get_class_factory_base_interface_idents();
let aggr_map = get_class_factory_aggr_map();
let struct_ident = &struct_item.ident;
let class_factory_ident = crate::utils::class_factory_ident(&struct_ident);
let struct_definition = gen_class_factory_struct_definition(&class_factory_ident);
let lock_server = gen_lock_server();
let iunknown_impl = gen_iunknown_impl(&base_interface_idents, &aggr_map, &class_factory_ident);
let class_factory_impl = gen_class_factory_impl(&base_interface_idents, &class_factory_ident);
quote! {
#struct_definition
impl com::interfaces::iclass_factory::IClassFactory for #class_factory_ident {
unsafe fn create_instance(
&self,
aggr: *mut *const <dyn com::interfaces::iunknown::IUnknown as com::ComInterface>::VTable,
riid: winapi::shared::guiddef::REFIID,
ppv: *mut *mut winapi::ctypes::c_void,
) -> winapi::shared::winerror::HRESULT {
use com::interfaces::iunknown::IUnknown;
if aggr != std::ptr::null_mut() {
return winapi::shared::winerror::CLASS_E_NOAGGREGATION;
}
let mut instance = #struct_ident::new();
instance.add_ref();
let hr = instance.query_interface(riid, ppv);
instance.release();
core::mem::forget(instance);
hr
}
#lock_server
}
#iunknown_impl
#class_factory_impl
}
}
pub fn gen_class_factory_struct_definition(class_factory_ident: &Ident) -> HelperTokenStream {
let ref_count_field = super::com_struct::gen_ref_count_field();
let interface_ident = get_iclass_factory_interface_ident();
let vptr_field_ident = crate::utils::vptr_field_ident(&interface_ident);
quote! {
#[repr(C)]
pub struct #class_factory_ident {
#vptr_field_ident: *const <dyn com::interfaces::iclass_factory::IClassFactory as com::ComInterface>::VTable,
#ref_count_field
}
}
}
pub fn gen_lock_server() -> HelperTokenStream {
quote! {
fn lock_server(&self, _increment: winapi::shared::minwindef::BOOL) -> winapi::shared::winerror::HRESULT {
winapi::shared::winerror::S_OK
}
}
}
pub fn gen_iunknown_impl(
base_interface_idents: &[Ident],
aggr_map: &HashMap<Ident, Vec<Ident>>,
class_factory_ident: &Ident,
) -> HelperTokenStream {
let query_interface = gen_query_interface();
let add_ref = super::iunknown_impl::gen_add_ref();
let release = gen_release(&base_interface_idents, &aggr_map, class_factory_ident);
quote! {
impl com::interfaces::iunknown::IUnknown for #class_factory_ident {
#query_interface
#add_ref
#release
}
}
}
pub fn gen_release(
base_interface_idents: &[Ident],
aggr_map: &HashMap<Ident, Vec<Ident>>,
struct_ident: &Ident,
) -> HelperTokenStream {
let ref_count_ident = crate::utils::ref_count_ident();
let release_decrement = super::iunknown_impl::gen_release_decrement(&ref_count_ident);
let release_assign_new_count_to_var = super::iunknown_impl::gen_release_assign_new_count_to_var(
&ref_count_ident,
&ref_count_ident,
);
let release_new_count_var_zero_check =
super::iunknown_impl::gen_new_count_var_zero_check(&ref_count_ident);
let release_drops =
super::iunknown_impl::gen_release_drops(base_interface_idents, aggr_map, struct_ident);
quote! {
unsafe fn release(&self) -> u32 {
use com::interfaces::iclass_factory::IClassFactory;
#release_decrement
#release_assign_new_count_to_var
if #release_new_count_var_zero_check {
#release_drops
}
#ref_count_ident
}
}
}
fn gen_query_interface() -> HelperTokenStream {
let vptr_field_ident = crate::utils::vptr_field_ident(&get_iclass_factory_interface_ident());
quote! {
unsafe fn query_interface(&self, riid: *const winapi::shared::guiddef::IID, ppv: *mut *mut winapi::ctypes::c_void) -> winapi::shared::winerror::HRESULT {
use com::interfaces::iunknown::IUnknown;
let riid = &*riid;
if winapi::shared::guiddef::IsEqualGUID(riid, &<dyn com::interfaces::iunknown::IUnknown as com::ComInterface>::IID) | winapi::shared::guiddef::IsEqualGUID(riid, &<dyn com::interfaces::iclass_factory::IClassFactory as com::ComInterface>::IID) {
*ppv = &self.#vptr_field_ident as *const _ as *mut winapi::ctypes::c_void;
self.add_ref();
winapi::shared::winerror::NOERROR
} else {
*ppv = std::ptr::null_mut::<winapi::ctypes::c_void>();
winapi::shared::winerror::E_NOINTERFACE
}
}
}
}
pub fn gen_class_factory_impl(
base_interface_idents: &[Ident],
class_factory_ident: &Ident,
) -> HelperTokenStream {
let ref_count_field = super::com_struct_impl::gen_allocate_ref_count_field();
let base_fields = super::com_struct_impl::gen_allocate_base_fields(base_interface_idents);
let base_inits =
super::com_struct_impl::gen_allocate_base_inits(class_factory_ident, base_interface_idents);
quote! {
impl #class_factory_ident {
pub(crate) fn new() -> Box<#class_factory_ident> {
use com::interfaces::iclass_factory::IClassFactory;
#base_inits
let out = #class_factory_ident {
#base_fields
#ref_count_field
};
Box::new(out)
}
}
}
}