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
use prelude::*;
use super::common::*;
use idents;
use utils;
use model;
use builtin_model;
use std::iter::FromIterator;
extern crate quote;
pub fn expand_com_library(
arg_tokens: TokenStreamNightly,
) -> Result<TokenStreamNightly, model::ParseError>
{
let mut output = vec![];
let lib = model::ComLibrary::parse( &lib_name(), arg_tokens.into() )?;
let mut match_arms = vec![];
let mut creatable_classes = vec![];
for struct_path in lib.coclasses() {
let clsid_path = idents::clsid_path( struct_path );
match_arms.push( quote!(
self::#clsid_path =>
Ok( ::intercom::ComBox::new(
#struct_path::new()
) as ::intercom::RawComPtr )
) );
creatable_classes.push( quote!( #clsid_path ) );
}
for bti in builtin_model::builtin_intercom_types( lib.name() ) {
let clsid_tokens = utils::get_guid_tokens(
bti.class.clsid().as_ref().unwrap() );
let clsid_doc = format!( "Built-in {} class ID.", bti.class.name() );
let builtin_clsid = idents::clsid( bti.class.name() );
output.push( quote!(
#[allow(non_upper_case_globals)]
#[doc = #clsid_doc ]
pub const #builtin_clsid : ::intercom::CLSID = #clsid_tokens;
) );
let ctor = bti.ctor;
match_arms.push( quote!(
self::#builtin_clsid =>
Ok( ::intercom::ComBox::new( #ctor ) as ::intercom::RawComPtr )
) );
creatable_classes.push( quote!( #builtin_clsid ) );
}
let dll_get_class_object = get_dll_get_class_object_function( &match_arms );
output.push( dll_get_class_object );
let list_class_objects = get_intercom_list_class_objects_function( &creatable_classes );
output.push( list_class_objects );
Ok( TokenStream::from_iter( output.into_iter() ).into() )
}
fn get_dll_get_class_object_function(
match_arms: &[TokenStream]
) -> TokenStream
{
let calling_convetion = get_calling_convetion();
quote!(
#[no_mangle]
#[allow(non_snake_case)]
#[allow(dead_code)]
#[doc(hidden)]
pub unsafe extern #calling_convetion fn DllGetClassObject(
rclsid : ::intercom::REFCLSID,
riid : ::intercom::REFIID,
pout : *mut ::intercom::RawComPtr
) -> ::intercom::raw::HRESULT
{
let mut com_struct = ::intercom::ComStruct::new(
::intercom::ClassFactory::new( rclsid, | clsid | {
match *clsid {
#( #match_arms, )*
_ => Err( ::intercom::raw::E_NOINTERFACE ),
}
} ) );
::intercom::ComBox::query_interface(
com_struct.as_mut(),
riid,
pout );
::intercom::raw::S_OK
}
)
}
fn get_intercom_list_class_objects_function(
clsid_tokens: &[TokenStream]
) -> TokenStream
{
let calling_convetion = get_calling_convetion();
let token_count = clsid_tokens.len();
quote!(
#[no_mangle]
#[allow(non_snake_case)]
#[allow(dead_code)]
#[doc(hidden)]
pub unsafe extern #calling_convetion fn IntercomListClassObjects(
pcount: *mut usize,
pclsids: *mut *const ::intercom::CLSID,
) -> ::intercom::raw::HRESULT
{
if pcount.is_null() { return ::intercom::raw::E_POINTER; }
if pclsids.is_null() { return ::intercom::raw::E_POINTER; }
static AVAILABLE_CLASSES: [::intercom::CLSID; #token_count ] = [
#( #clsid_tokens, )*
];
*pcount = #token_count;
*pclsids = AVAILABLE_CLASSES.as_ptr();
::intercom::raw::S_OK
}
)
}