-
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add glib::subclass::{register_module_type,register_module_interface}
Signed-off-by: fbrouille <[email protected]>
- Loading branch information
Showing
5 changed files
with
514 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
// Take a look at the license at the top of the repository in the LICENSE file. | ||
|
||
use crate::{subclass::prelude::*, translate::*, Cast, IsA, TypeModule}; | ||
|
||
pub trait TypeModuleObserver<T: IsA<TypeModule>> { | ||
fn on_load(type_module: &T) -> bool; | ||
fn on_unload(type_module: &T); | ||
} | ||
|
||
pub trait TypeModuleImpl: ObjectImpl + TypeModuleImplExt { | ||
// rustdoc-stripper-ignore-next | ||
/// Loads the module, registers one or more types using | ||
/// [`register_module_type`] and registers one or more interfaces using | ||
/// [`register_module_interface`] (see [`TypeModule`]). | ||
/// | ||
/// [`register_module_type`]: ../types/fn.register_module_type.html | ||
/// [`register_module_interface`]: ../interface/fn.register_module_interface.html | ||
/// [`TypeModule`]: ../../gobject/auto/type_module/struct.TypeModule.html | ||
fn load(&self) -> bool; | ||
// rustdoc-stripper-ignore-next | ||
/// Unloads the module (see [`TypeModuleExt::unuse`]). | ||
/// | ||
/// [`TypeModuleExt::unuse`]: ../../gobject/auto/type_module/trait.TypeModuleExt.html#method.unuse | ||
fn unload(&self); | ||
} | ||
|
||
pub trait TypeModuleImplExt: ObjectSubclass { | ||
fn parent_load(&self) -> bool; | ||
fn parent_unload(&self); | ||
} | ||
|
||
impl<T: TypeModuleImpl> TypeModuleImplExt for T { | ||
fn parent_load(&self) -> bool { | ||
unsafe { | ||
let data = T::type_data(); | ||
let parent_class = data.as_ref().parent_class() as *const gobject_ffi::GTypeModuleClass; | ||
|
||
let f = (*parent_class) | ||
.load | ||
.expect("No parent class implementation for \"load\""); | ||
|
||
from_glib(f(self | ||
.obj() | ||
.unsafe_cast_ref::<TypeModule>() | ||
.to_glib_none() | ||
.0)) | ||
} | ||
} | ||
|
||
fn parent_unload(&self) { | ||
unsafe { | ||
let data = T::type_data(); | ||
let parent_class = data.as_ref().parent_class() as *const gobject_ffi::GTypeModuleClass; | ||
|
||
let f = (*parent_class) | ||
.unload | ||
.expect("No parent class implementation for \"unload\""); | ||
|
||
f(self.obj().unsafe_cast_ref::<TypeModule>().to_glib_none().0); | ||
} | ||
} | ||
} | ||
|
||
unsafe impl<T: TypeModuleImpl> IsSubclassable<T> for TypeModule { | ||
fn class_init(class: &mut crate::Class<Self>) { | ||
Self::parent_class_init::<T>(class); | ||
|
||
let klass = class.as_mut(); | ||
klass.load = Some(load::<T>); | ||
klass.unload = Some(unload::<T>); | ||
} | ||
} | ||
|
||
unsafe extern "C" fn load<T: TypeModuleImpl>( | ||
type_module: *mut gobject_ffi::GTypeModule, | ||
) -> ffi::gboolean { | ||
let instance = &*(type_module as *mut T::Instance); | ||
let imp = instance.imp(); | ||
|
||
let res = imp.load(); | ||
|
||
if res { | ||
unsafe { | ||
gobject_ffi::g_object_ref(type_module as _); | ||
} | ||
} | ||
|
||
res.into_glib() | ||
} | ||
|
||
unsafe extern "C" fn unload<T: TypeModuleImpl>(type_module: *mut gobject_ffi::GTypeModule) { | ||
let instance = &*(type_module as *mut T::Instance); | ||
let imp = instance.imp(); | ||
|
||
imp.unload(); | ||
|
||
unsafe { | ||
gobject_ffi::g_object_unref(type_module as _); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::prelude::*; | ||
|
||
mod imp { | ||
use super::*; | ||
|
||
use crate as glib; | ||
|
||
#[derive(Default)] | ||
pub struct SimpleTypeModule; | ||
|
||
#[glib::object_subclass] | ||
impl ObjectSubclass for SimpleTypeModule { | ||
const NAME: &'static str = "SimpleTypeModule"; | ||
type Type = super::SimpleTypeModule; | ||
type ParentType = TypeModule; | ||
} | ||
|
||
impl ObjectImpl for SimpleTypeModule {} | ||
|
||
impl TypeModuleImpl for SimpleTypeModule { | ||
fn load(&self) -> bool { | ||
true | ||
} | ||
|
||
fn unload(&self) {} | ||
} | ||
} | ||
|
||
crate::wrapper! { | ||
pub struct SimpleTypeModule(ObjectSubclass<imp::SimpleTypeModule>) | ||
@extends crate::TypeModule; | ||
} | ||
|
||
#[test] | ||
fn test_simple_type_module() { | ||
let simple_type_module = crate::Object::new::<SimpleTypeModule>(); | ||
// simulate the glib type system to load the module | ||
assert!(simple_type_module.use_()); | ||
simple_type_module.unuse(); | ||
} | ||
} |
Oops, something went wrong.