Skip to content

Commit

Permalink
Merge pull request #1255 from bilelmoussaoui/bilelmoussaoui/objectext
Browse files Browse the repository at this point in the history
codgen: simplify properties getters/setters
  • Loading branch information
sdroege committed Nov 8, 2021
2 parents e0b1709 + 9b389a7 commit dd6854c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 46 deletions.
94 changes: 51 additions & 43 deletions src/codegen/property_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,51 @@ impl<'a> Builder<'a> {
}

fn chunks_for_get(&self) -> Vec<Chunk> {
let mut params = Vec::new();
if self.is_child_property {
// TODO: make use of safe bindings for child properties setter
self.child_property_setter()
} else {
let self_ = if self.in_trait {
"self.as_ref()"
} else {
"self"
};

vec![Chunk::Custom(format!(
"{}::property({}, \"{}\")",
use_glib_type(self.env, "ObjectExt"),
self_,
self.name
))]
}
}

let cast_target = if self.is_child_property {
use_gtk_type(self.env, "ffi::GtkContainer")
fn chunks_for_set(&self) -> Vec<Chunk> {
if self.is_child_property {
// TODO: make use of safe bindings for child properties getter
self.child_property_getter()
} else {
use_glib_type(self.env, "gobject_ffi::GObject")
};
let self_ = if self.in_trait {
"self.as_ref()"
} else {
"self"
};

vec![Chunk::Custom(format!(
"{}::set_property({},\"{}\", &{})",
use_glib_type(self.env, "ObjectExt"),
self_,
self.name,
self.var_name
))]
}
}

fn child_property_setter(&self) -> Vec<Chunk> {
let mut body = Vec::new();

let mut params = Vec::new();
let cast_target = use_gtk_type(self.env, "ffi::GtkContainer");
if self.in_trait {
params.push(Chunk::Custom(format!(
"self.to_glib_none().0 as *mut {}",
Expand All @@ -94,20 +132,16 @@ impl<'a> Builder<'a> {
cast_target
)));
}
params.push(Chunk::Custom("item.to_glib_none().0 as *mut _".into()));

if self.is_child_property {
params.push(Chunk::Custom("item.to_glib_none().0 as *mut _".into()));
}
params.push(Chunk::Custom(format!(
"b\"{}\\0\".as_ptr() as *const _",
self.name
)));
params.push(Chunk::Custom("value.to_glib_none_mut().0".into()));

let mut body = Vec::new();

let ffi_call = Chunk::FfiCall {
name: self.get_ffi_func(),
name: use_gtk_type(self.env, "ffi::gtk_container_child_get_property"),
params,
};

Expand All @@ -131,18 +165,14 @@ impl<'a> Builder<'a> {
"value.get().expect(\"Return Value for property `{}` getter\")",
self.name,
)));

vec![Chunk::Unsafe(body)]
}

fn chunks_for_set(&self) -> Vec<Chunk> {
let mut params = Vec::new();
fn child_property_getter(&self) -> Vec<Chunk> {
let mut body = Vec::new();

let cast_target = if self.is_child_property {
use_gtk_type(self.env, "ffi::GtkContainer")
} else {
use_glib_type(self.env, "gobject_ffi::GObject")
};
let mut params = Vec::new();
let cast_target = use_gtk_type(self.env, "ffi::GtkContainer");
if self.in_trait {
params.push(Chunk::Custom(format!(
"self.to_glib_none().0 as *mut {}",
Expand All @@ -154,10 +184,7 @@ impl<'a> Builder<'a> {
cast_target
)));
}

if self.is_child_property {
params.push(Chunk::Custom("item.to_glib_none().0 as *mut _".into()));
}
params.push(Chunk::Custom("item.to_glib_none().0 as *mut _".into()));
params.push(Chunk::Custom(format!(
"b\"{}\\0\".as_ptr() as *const _",
self.name
Expand All @@ -167,34 +194,15 @@ impl<'a> Builder<'a> {
self.var_name
)));

let mut body = Vec::new();

let ffi_call = Chunk::FfiCall {
name: self.set_ffi_func(),
name: use_gtk_type(self.env, "ffi::gtk_container_child_set_property"),
params,
};
body.push(Chunk::FfiCallConversion {
ret: analysis::return_value::Info::default(),
array_length_name: None,
call: Box::new(ffi_call),
});

vec![Chunk::Unsafe(body)]
}

fn get_ffi_func(&self) -> String {
if self.is_child_property {
use_gtk_type(self.env, "ffi::gtk_container_child_get_property")
} else {
use_glib_type(self.env, "gobject_ffi::g_object_get_property")
}
}

fn set_ffi_func(&self) -> String {
if self.is_child_property {
use_gtk_type(self.env, "ffi::gtk_container_child_set_property")
} else {
use_glib_type(self.env, "gobject_ffi::g_object_set_property")
}
}
}
4 changes: 1 addition & 3 deletions src/codegen/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::{
analysis,
chunk::Chunk,
env::Env,
nameutil::use_glib_type,
writer::{primitives::tabs, ToCode},
};
use std::io::{Result, Write};
Expand Down Expand Up @@ -134,14 +133,13 @@ pub fn generate(

writeln!(
w,
"{}let {} = unsafe {{ glib::Object::from_glib_borrow(self.as_ptr() as *mut {}).emit_by_name(\"{}\", &[{}]).unwrap() }};",
"{}let {} = self.emit_by_name(\"{}\", &[{}]);",
tabs(indent + 1),
if trampoline.ret.typ != Default::default() {
"res"
} else {
"_"
},
use_glib_type(env, "gobject_ffi::GObject"),
analysis.signal_name,
args,
)?;
Expand Down

0 comments on commit dd6854c

Please sign in to comment.