Refactor aggr_co_class com_struct to re-use gen methods from co_class

This commit is contained in:
adrianwithah 2019-09-12 18:50:40 +01:00
Родитель 3853335570
Коммит 4eb76077b5
4 изменённых файлов: 45 добавлений и 36 удалений

Просмотреть файл

@ -14,36 +14,24 @@ pub fn generate(
let struct_ident = &struct_item.ident; let struct_ident = &struct_item.ident;
let vis = &struct_item.vis; let vis = &struct_item.vis;
let bases_interface_idents = base_interface_idents.iter().map(|base| { let base_fields = co_class::com_struct::gen_base_fields(base_interface_idents);
let field_ident = macro_utils::vptr_field_ident(&base); let ref_count_field = co_class::com_struct::gen_ref_count_field();
quote!(#field_ident: <dyn #base as com::ComInterface>::VPtr) let user_fields = co_class::com_struct::gen_user_fields(struct_item);
}); let aggregate_fields = co_class::com_struct::gen_aggregate_fields(aggr_map);
let ref_count_ident = macro_utils::ref_count_ident();
let non_delegating_iunknown_field_ident = macro_utils::non_delegating_iunknown_field_ident(); let non_delegating_iunknown_field_ident = macro_utils::non_delegating_iunknown_field_ident();
let iunknown_to_use_field_ident = macro_utils::iunknown_to_use_field_ident(); let iunknown_to_use_field_ident = macro_utils::iunknown_to_use_field_ident();
let fields = match &struct_item.fields {
Fields::Named(f) => &f.named,
_ => panic!("Found non Named fields in struct."),
};
let aggregates = aggr_map.iter().map(|(aggr_field_ident, _)| {
quote!(
#aggr_field_ident: *mut <dyn com::IUnknown as com::ComInterface>::VPtr
)
});
quote!( quote!(
#[repr(C)] #[repr(C)]
#vis struct #struct_ident { #vis struct #struct_ident {
#(#bases_interface_idents,)* #base_fields
#non_delegating_iunknown_field_ident: <dyn com::IUnknown as com::ComInterface>::VPtr, #non_delegating_iunknown_field_ident: <dyn com::IUnknown as com::ComInterface>::VPtr,
// Non-reference counted interface pointer to outer IUnknown. // Non-reference counted interface pointer to outer IUnknown.
#iunknown_to_use_field_ident: *mut <dyn com::IUnknown as com::ComInterface>::VPtr, #iunknown_to_use_field_ident: *mut <dyn com::IUnknown as com::ComInterface>::VPtr,
#ref_count_ident: u32, #ref_count_field
#(#aggregates,)* #aggregate_fields
#fields #user_fields
} }
) )
} }

Просмотреть файл

@ -18,31 +18,50 @@ pub fn generate(
let struct_ident = &struct_item.ident; let struct_ident = &struct_item.ident;
let vis = &struct_item.vis; let vis = &struct_item.vis;
let base_fields = gen_base_fields(base_interface_idents);
let ref_count_field = gen_ref_count_field();
let user_fields = gen_user_fields(struct_item);
let aggregate_fields = gen_aggregate_fields(aggr_map);
quote!(
#[repr(C)]
#vis struct #struct_ident {
#base_fields
#ref_count_field
#aggregate_fields
#user_fields
}
)
}
pub fn gen_base_fields(base_interface_idents: &[Ident]) -> HelperTokenStream {
let bases_interface_idents = base_interface_idents.iter().map(|base| { let bases_interface_idents = base_interface_idents.iter().map(|base| {
let field_ident = macro_utils::vptr_field_ident(&base); let field_ident = macro_utils::vptr_field_ident(&base);
quote!(#field_ident: <dyn #base as com::ComInterface>::VPtr) quote!(#field_ident: <dyn #base as com::ComInterface>::VPtr)
}); });
quote!(#(#bases_interface_idents,)*)
}
pub fn gen_ref_count_field() -> HelperTokenStream {
let ref_count_ident = macro_utils::ref_count_ident(); let ref_count_ident = macro_utils::ref_count_ident();
quote!(#ref_count_ident: u32,)
}
let fields = match &struct_item.fields { pub fn gen_aggregate_fields(aggr_map: &HashMap<Ident, Vec<Ident>>) -> HelperTokenStream {
Fields::Named(f) => &f.named,
_ => panic!("Found non Named fields in struct."),
};
let aggregates = aggr_map.iter().map(|(aggr_field_ident, _)| { let aggregates = aggr_map.iter().map(|(aggr_field_ident, _)| {
quote!( quote!(
#aggr_field_ident: *mut <dyn com::IUnknown as com::ComInterface>::VPtr #aggr_field_ident: *mut <dyn com::IUnknown as com::ComInterface>::VPtr
) )
}); });
quote!( quote!(#(#aggregates,)*)
#[repr(C)] }
#vis struct #struct_ident {
#(#bases_interface_idents,)* pub fn gen_user_fields(struct_item: &ItemStruct) -> HelperTokenStream {
#ref_count_ident: u32, let fields = match &struct_item.fields {
#(#aggregates,)* Fields::Named(f) => &f.named,
#fields _ => panic!("Found non Named fields in struct."),
} };
)
quote!(#fields)
} }

Просмотреть файл

@ -46,7 +46,9 @@ pub fn gen_allocate_fn(
quote!( quote!(
fn allocate(#allocate_parameters) -> Box<#struct_ident> { fn allocate(#allocate_parameters) -> Box<#struct_ident> {
println!("Allocating new VTable for {}", stringify!(#struct_ident)); println!("Allocating new VTable for {}", stringify!(#struct_ident));
#base_inits #base_inits
let out = #struct_ident { let out = #struct_ident {
#base_fields #base_fields
#ref_count_field #ref_count_field

Просмотреть файл

@ -5,9 +5,9 @@ use syn::{AttributeArgs, ItemStruct};
use std::iter::FromIterator; use std::iter::FromIterator;
pub mod class_factory; pub mod class_factory;
mod com_struct; pub mod com_struct;
pub mod com_struct_impl; pub mod com_struct_impl;
mod drop_impl; pub mod drop_impl;
pub mod iunknown_impl; pub mod iunknown_impl;
// Macro expansion entry point. // Macro expansion entry point.