use std::ptr;
use ffi;
use gobject_ffi;
#[macro_export]
macro_rules! glib_floating_reference_guard {
($obj:ident) => {
let _guard = $crate::subclass::guard::FloatingReferenceGuard::new($obj as *mut _);
};
}
pub struct FloatingReferenceGuard(ptr::NonNull<gobject_ffi::GObject>);
impl FloatingReferenceGuard {
#[doc(hidden)]
pub unsafe fn new(obj: *mut gobject_ffi::GObject) -> Option<FloatingReferenceGuard> {
assert!(!obj.is_null());
if gobject_ffi::g_object_is_floating(obj) != ffi::GFALSE {
gobject_ffi::g_object_ref_sink(obj);
Some(FloatingReferenceGuard(ptr::NonNull::new_unchecked(obj)))
} else {
None
}
}
}
impl Drop for FloatingReferenceGuard {
fn drop(&mut self) {
unsafe {
gobject_ffi::g_object_force_floating(self.0.as_ptr());
}
}
}