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
61pub trait TypeModuleExt: IsA<TypeModule> + 'static {
62 #[doc(alias = "g_type_module_add_interface")]
63 fn add_interface(
64 &self,
65 instance_type: crate::types::Type,
66 interface_type: crate::types::Type,
67 interface_info: &InterfaceInfo,
68 ) {
69 unsafe {
70 gobject_ffi::g_type_module_add_interface(
71 self.as_ref().to_glib_none().0,
72 instance_type.into_glib(),
73 interface_type.into_glib(),
74 interface_info.as_ptr(),
75 );
76 }
77 }
78
79 #[doc(alias = "g_type_module_register_enum")]
80 fn register_enum(
81 &self,
82 name: &str,
83 const_static_values: &'static EnumValues,
84 ) -> crate::types::Type {
85 unsafe {
86 from_glib(gobject_ffi::g_type_module_register_enum(
87 self.as_ref().to_glib_none().0,
88 name.to_glib_none().0,
89 const_static_values.to_glib_none().0,
90 ))
91 }
92 }
93
94 #[doc(alias = "g_type_module_register_flags")]
95 fn register_flags(
96 &self,
97 name: &str,
98 const_static_values: &'static FlagsValues,
99 ) -> crate::types::Type {
100 unsafe {
101 from_glib(gobject_ffi::g_type_module_register_flags(
102 self.as_ref().to_glib_none().0,
103 name.to_glib_none().0,
104 const_static_values.to_glib_none().0,
105 ))
106 }
107 }
108
109 #[doc(alias = "g_type_module_register_type")]
110 fn register_type(
111 &self,
112 parent_type: crate::types::Type,
113 type_name: &str,
114 type_info: &TypeInfo,
115 flags: TypeFlags,
116 ) -> crate::types::Type {
117 unsafe {
118 from_glib(gobject_ffi::g_type_module_register_type(
119 self.as_ref().to_glib_none().0,
120 parent_type.into_glib(),
121 type_name.to_glib_none().0,
122 type_info.as_ptr(),
123 flags.into_glib(),
124 ))
125 }
126 }
127
128 #[doc(alias = "g_type_module_set_name")]
129 fn set_name(&self, name: &str) {
130 unsafe {
131 gobject_ffi::g_type_module_set_name(
132 self.as_ref().to_glib_none().0,
133 name.to_glib_none().0,
134 );
135 }
136 }
137
138 #[doc(alias = "g_type_module_unuse")]
139 fn unuse(&self) {
140 unsafe {
141 gobject_ffi::g_type_module_unuse(self.as_ref().to_glib_none().0);
142 }
143 }
144
145 #[doc(alias = "g_type_module_use")]
146 #[doc(alias = "use")]
147 fn use_(&self) -> bool {
148 unsafe {
149 from_glib(gobject_ffi::g_type_module_use(
150 self.as_ref().to_glib_none().0,
151 ))
152 }
153 }
154}
155
156impl<O: IsA<TypeModule>> TypeModuleExt for O {}