glib/subclass/
type_plugin.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use crate::enums::{EnumValues, FlagsValues};
4use crate::{
5    ffi, gobject_ffi, prelude::*, subclass::prelude::*, translate::*, Interface, InterfaceInfo,
6    Type, TypeFlags, TypeInfo, TypePlugin, TypeValueTable,
7};
8
9pub trait TypePluginImpl: ObjectImpl + TypePluginImplExt {
10    fn use_plugin(&self) {
11        self.parent_use_plugin();
12    }
13
14    fn unuse_plugin(&self) {
15        self.parent_unuse_plugin();
16    }
17
18    fn complete_type_info(&self, type_: Type) -> (TypeInfo, TypeValueTable) {
19        self.parent_complete_type_info(type_)
20    }
21
22    fn complete_interface_info(&self, instance_type: Type, interface_type: Type) -> InterfaceInfo {
23        self.parent_complete_interface_info(instance_type, interface_type)
24    }
25}
26
27pub trait TypePluginImplExt: ObjectSubclass {
28    fn parent_use_plugin(&self);
29    fn parent_unuse_plugin(&self);
30    fn parent_complete_type_info(&self, type_: Type) -> (TypeInfo, TypeValueTable);
31    fn parent_complete_interface_info(
32        &self,
33        instance_type: Type,
34        interface_type: Type,
35    ) -> InterfaceInfo;
36}
37
38impl<T: TypePluginImpl> TypePluginImplExt for T {
39    fn parent_use_plugin(&self) {
40        unsafe {
41            let type_data = Self::type_data();
42            let parent_iface = type_data.as_ref().parent_interface::<TypePlugin>()
43                as *const gobject_ffi::GTypePluginClass;
44
45            let f = (*parent_iface)
46                .use_plugin
47                .expect("no parent \"use_plugin\" implementation");
48
49            f(self.obj().unsafe_cast_ref::<TypePlugin>().to_glib_none().0)
50        }
51    }
52
53    fn parent_unuse_plugin(&self) {
54        unsafe {
55            let type_data = Self::type_data();
56            let parent_iface = type_data.as_ref().parent_interface::<TypePlugin>()
57                as *const gobject_ffi::GTypePluginClass;
58
59            let f = (*parent_iface)
60                .unuse_plugin
61                .expect("no parent \"unuse_plugin\" implementation");
62
63            f(self.obj().unsafe_cast_ref::<TypePlugin>().to_glib_none().0)
64        }
65    }
66
67    fn parent_complete_type_info(&self, type_: Type) -> (TypeInfo, TypeValueTable) {
68        unsafe {
69            let type_data = Self::type_data();
70            let parent_iface = type_data.as_ref().parent_interface::<TypePlugin>()
71                as *const gobject_ffi::GTypePluginClass;
72
73            let f = (*parent_iface)
74                .complete_type_info
75                .expect("no parent \"complete_type_info\" implementation");
76
77            let info = TypeInfo::default();
78            let value_table = TypeValueTable::default();
79            f(
80                self.obj().unsafe_cast_ref::<TypePlugin>().to_glib_none().0,
81                type_.into_glib(),
82                info.as_ptr(),
83                value_table.as_ptr(),
84            );
85
86            (info, value_table)
87        }
88    }
89
90    fn parent_complete_interface_info(
91        &self,
92        instance_type: Type,
93        interface_type: Type,
94    ) -> InterfaceInfo {
95        let info = InterfaceInfo::default();
96        unsafe {
97            let type_data = Self::type_data();
98            let parent_iface = type_data.as_ref().parent_interface::<TypePlugin>()
99                as *const gobject_ffi::GTypePluginClass;
100
101            let f = (*parent_iface)
102                .complete_interface_info
103                .expect("no parent \"complete_interface_info\" implementation");
104
105            f(
106                self.obj().unsafe_cast_ref::<TypePlugin>().to_glib_none().0,
107                instance_type.into_glib(),
108                interface_type.into_glib(),
109                info.as_ptr(),
110            )
111        }
112        info
113    }
114}
115
116unsafe impl<T: TypePluginImpl> IsImplementable<T> for TypePlugin {
117    fn interface_init(iface: &mut Interface<Self>) {
118        let iface = iface.as_mut();
119
120        iface.use_plugin = Some(use_plugin::<T>);
121        iface.unuse_plugin = Some(unuse_plugin::<T>);
122        iface.complete_type_info = Some(complete_type_info::<T>);
123        iface.complete_interface_info = Some(complete_interface_info::<T>);
124    }
125}
126
127unsafe extern "C" fn use_plugin<T: TypePluginImpl>(type_plugin: *mut gobject_ffi::GTypePlugin) {
128    let instance = &*(type_plugin as *mut T::Instance);
129    let imp = instance.imp();
130
131    imp.use_plugin();
132}
133
134unsafe extern "C" fn unuse_plugin<T: TypePluginImpl>(type_plugin: *mut gobject_ffi::GTypePlugin) {
135    let instance = &*(type_plugin as *mut T::Instance);
136    let imp = instance.imp();
137
138    imp.unuse_plugin();
139}
140
141unsafe extern "C" fn complete_type_info<T: TypePluginImpl>(
142    type_plugin: *mut gobject_ffi::GTypePlugin,
143    gtype: ffi::GType,
144    info_ptr: *mut gobject_ffi::GTypeInfo,
145    value_table_ptr: *mut gobject_ffi::GTypeValueTable,
146) {
147    assert!(!info_ptr.is_null());
148    assert!(!value_table_ptr.is_null());
149    let instance = &*(type_plugin as *mut T::Instance);
150    let imp = instance.imp();
151    let type_ = Type::from_glib(gtype);
152    let info = TypeInfo::from_glib_ptr_borrow_mut(info_ptr);
153    let value_table = TypeValueTable::from_glib_ptr_borrow_mut(value_table_ptr);
154
155    let (info_, value_table_) = imp.complete_type_info(type_);
156
157    *info = info_;
158    *value_table = value_table_;
159}
160
161unsafe extern "C" fn complete_interface_info<T: TypePluginImpl>(
162    type_plugin: *mut gobject_ffi::GTypePlugin,
163    instance_gtype: ffi::GType,
164    interface_gtype: ffi::GType,
165    info_ptr: *mut gobject_ffi::GInterfaceInfo,
166) {
167    assert!(!info_ptr.is_null());
168    let instance = &*(type_plugin as *mut T::Instance);
169    let imp = instance.imp();
170    let instance_type = Type::from_glib(instance_gtype);
171    let interface_type = Type::from_glib(interface_gtype);
172    let info = InterfaceInfo::from_glib_ptr_borrow_mut(info_ptr);
173
174    let info_ = imp.complete_interface_info(instance_type, interface_type);
175    *info = info_;
176}
177
178pub trait TypePluginRegisterImpl: ObjectImpl + TypePluginImpl {
179    fn add_dynamic_interface(
180        &self,
181        _instance_type: Type,
182        _interface_type: Type,
183        _interface_info: &InterfaceInfo,
184    ) {
185        unimplemented!()
186    }
187    fn register_dynamic_enum(
188        &self,
189        _name: &str,
190        _const_static_values: &'static EnumValues,
191    ) -> Type {
192        unimplemented!()
193    }
194    fn register_dynamic_flags(
195        &self,
196        _name: &str,
197        _const_static_values: &'static FlagsValues,
198    ) -> Type {
199        unimplemented!()
200    }
201    fn register_dynamic_type(
202        &self,
203        _parent_type: Type,
204        _type_name: &str,
205        _type_info: &TypeInfo,
206        _flags: TypeFlags,
207    ) -> Type {
208        unimplemented!()
209    }
210}
211
212#[cfg(test)]
213mod tests {
214    use crate::{self as glib, prelude::TypePluginExt};
215
216    use super::*;
217
218    mod imp {
219        use super::*;
220
221        #[derive(Default)]
222        pub struct SimplePlugin {
223            type_info: std::cell::Cell<Option<TypeInfo>>,
224        }
225
226        #[crate::object_subclass]
227        impl ObjectSubclass for SimplePlugin {
228            const NAME: &'static str = "SimplePlugin";
229            type Type = super::SimplePlugin;
230            type Interfaces = (TypePlugin,);
231        }
232
233        impl ObjectImpl for SimplePlugin {}
234
235        impl TypePluginImpl for SimplePlugin {
236            fn use_plugin(&self) {
237                // registers types on implementation load
238                SimplePluginType::on_implementation_load(self.obj().as_ref());
239            }
240
241            fn unuse_plugin(&self) {
242                // unregisters types on implementation unload
243                SimplePluginType::on_implementation_unload(self.obj().as_ref());
244            }
245
246            fn complete_type_info(&self, _type_: Type) -> (TypeInfo, TypeValueTable) {
247                assert!(self.type_info.get().is_some());
248                // returns type info
249                (self.type_info.get().unwrap(), TypeValueTable::default())
250            }
251        }
252
253        impl TypePluginRegisterImpl for SimplePlugin {
254            fn register_dynamic_type(
255                &self,
256                parent_type: Type,
257                type_name: &str,
258                type_info: &TypeInfo,
259                flags: TypeFlags,
260            ) -> Type {
261                let type_ = Type::from_name(type_name).unwrap_or_else(|| {
262                    Type::register_dynamic(
263                        parent_type,
264                        type_name,
265                        self.obj().upcast_ref::<TypePlugin>(),
266                        flags,
267                    )
268                });
269                if type_.is_valid() {
270                    // save type info
271                    self.type_info.set(Some(*type_info));
272                }
273                type_
274            }
275        }
276
277        #[derive(Default)]
278        pub struct SimplePluginType;
279
280        #[crate::object_subclass]
281        #[object_subclass_dynamic(plugin_type = super::SimplePlugin)]
282        impl ObjectSubclass for SimplePluginType {
283            const NAME: &'static str = "SimplePluginType";
284            type Type = super::SimplePluginType;
285        }
286
287        impl ObjectImpl for SimplePluginType {}
288    }
289
290    crate::wrapper! {
291        pub struct SimplePlugin(ObjectSubclass<imp::SimplePlugin>)
292        @implements TypePlugin;
293    }
294
295    crate::wrapper! {
296        pub struct SimplePluginType(ObjectSubclass<imp::SimplePluginType>);
297    }
298
299    #[test]
300    fn test_plugin() {
301        assert!(!imp::SimplePluginType::type_().is_valid());
302        let simple_plugin = crate::Object::new::<SimplePlugin>();
303        // simulates the GLib type system to use the plugin.
304        TypePluginExt::use_(&simple_plugin);
305        assert!(imp::SimplePluginType::type_().is_valid());
306        TypePluginExt::unuse(&simple_plugin);
307    }
308}