glib/gobject/type_module.rs
1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use crate::{
4 enums::{EnumValues, FlagsValues},
5 gobject_ffi,
6 prelude::*,
7 translate::*,
8 InterfaceInfo, TypeFlags, TypeInfo, TypePlugin,
9};
10
11crate::wrapper! {
12 /// `GTypeModule` provides a simple implementation of the `GTypePlugin`
13 /// interface.
14 ///
15 /// The model of `GTypeModule` is a dynamically loaded module which
16 /// implements some number of types and interface implementations.
17 ///
18 /// When the module is loaded, it registers its types and interfaces
19 /// using [method[`Object`][crate::Object].register_type] and
20 /// [method[`Object`][crate::Object].add_interface].
21 /// As long as any instances of these types and interface implementations
22 /// are in use, the module is kept loaded. When the types and interfaces
23 /// are gone, the module may be unloaded. If the types and interfaces
24 /// become used again, the module will be reloaded. Note that the last
25 /// reference cannot be released from within the module code, since that
26 /// would lead to the caller's code being unloaded before ``g_object_unref()``
27 /// returns to it.
28 ///
29 /// Keeping track of whether the module should be loaded or not is done by
30 /// using a use count - it starts at zero, and whenever it is greater than
31 /// zero, the module is loaded. The use count is maintained internally by
32 /// the type system, but also can be explicitly controlled by
33 /// [method[`Object`][crate::Object].use] and [method[`Object`][crate::Object].unuse].
34 /// Typically, when loading a module for the first type, `[`TypeModuleExtManual::use_()`][crate::prelude::TypeModuleExtManual::use_()]`
35 /// will be used to load it so that it can initialize its types. At some later
36 /// point, when the module no longer needs to be loaded except for the type
37 /// implementations it contains, `[`TypeModuleExtManual::unuse()`][crate::prelude::TypeModuleExtManual::unuse()]` is called.
38 ///
39 /// `GTypeModule` does not actually provide any implementation of module
40 /// loading and unloading. To create a particular module type you must
41 /// derive from `GTypeModule` and implement the load and unload functions
42 /// in `GTypeModuleClass`.
43 ///
44 /// This is an Abstract Base Class, you cannot instantiate it.
45 ///
46 /// # Implements
47 ///
48 /// [`ObjectExt`][trait@crate::prelude::ObjectExt], [`TypePluginExt`][trait@crate::prelude::TypePluginExt]
49 #[doc(alias = "GTypeModule")]
50 pub struct TypeModule(Object<gobject_ffi::GTypeModule, gobject_ffi::GTypeModuleClass>) @implements TypePlugin;
51
52 match fn {
53 type_ => || gobject_ffi::g_type_module_get_type(),
54 }
55}
56
57impl TypeModule {
58 pub const NONE: Option<&'static TypeModule> = None;
59}
60
61mod sealed {
62 pub trait Sealed {}
63 impl<T: super::IsA<super::TypeModule>> Sealed for T {}
64}
65
66pub trait TypeModuleExt: IsA<TypeModule> + sealed::Sealed + 'static {
67 #[doc(alias = "g_type_module_add_interface")]
68 fn add_interface(
69 &self,
70 instance_type: crate::types::Type,
71 interface_type: crate::types::Type,
72 interface_info: &InterfaceInfo,
73 ) {
74 unsafe {
75 gobject_ffi::g_type_module_add_interface(
76 self.as_ref().to_glib_none().0,
77 instance_type.into_glib(),
78 interface_type.into_glib(),
79 interface_info.as_ptr(),
80 );
81 }
82 }
83
84 #[doc(alias = "g_type_module_register_enum")]
85 fn register_enum(
86 &self,
87 name: &str,
88 const_static_values: &'static EnumValues,
89 ) -> crate::types::Type {
90 unsafe {
91 from_glib(gobject_ffi::g_type_module_register_enum(
92 self.as_ref().to_glib_none().0,
93 name.to_glib_none().0,
94 const_static_values.to_glib_none().0,
95 ))
96 }
97 }
98
99 #[doc(alias = "g_type_module_register_flags")]
100 fn register_flags(
101 &self,
102 name: &str,
103 const_static_values: &'static FlagsValues,
104 ) -> crate::types::Type {
105 unsafe {
106 from_glib(gobject_ffi::g_type_module_register_flags(
107 self.as_ref().to_glib_none().0,
108 name.to_glib_none().0,
109 const_static_values.to_glib_none().0,
110 ))
111 }
112 }
113
114 #[doc(alias = "g_type_module_register_type")]
115 fn register_type(
116 &self,
117 parent_type: crate::types::Type,
118 type_name: &str,
119 type_info: &TypeInfo,
120 flags: TypeFlags,
121 ) -> crate::types::Type {
122 unsafe {
123 from_glib(gobject_ffi::g_type_module_register_type(
124 self.as_ref().to_glib_none().0,
125 parent_type.into_glib(),
126 type_name.to_glib_none().0,
127 type_info.as_ptr(),
128 flags.into_glib(),
129 ))
130 }
131 }
132
133 #[doc(alias = "g_type_module_set_name")]
134 fn set_name(&self, name: &str) {
135 unsafe {
136 gobject_ffi::g_type_module_set_name(
137 self.as_ref().to_glib_none().0,
138 name.to_glib_none().0,
139 );
140 }
141 }
142
143 #[doc(alias = "g_type_module_unuse")]
144 fn unuse(&self) {
145 unsafe {
146 gobject_ffi::g_type_module_unuse(self.as_ref().to_glib_none().0);
147 }
148 }
149
150 #[doc(alias = "g_type_module_use")]
151 #[doc(alias = "use")]
152 fn use_(&self) -> bool {
153 unsafe {
154 from_glib(gobject_ffi::g_type_module_use(
155 self.as_ref().to_glib_none().0,
156 ))
157 }
158 }
159}
160
161impl<O: IsA<TypeModule>> TypeModuleExt for O {}