glib/gobject/
type_module.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::{
    enums::{EnumValues, FlagsValues},
    gobject_ffi,
    prelude::*,
    translate::*,
    InterfaceInfo, TypeFlags, TypeInfo, TypePlugin,
};

crate::wrapper! {
    /// `GTypeModule` provides a simple implementation of the `GTypePlugin`
    /// interface.
    ///
    /// The model of `GTypeModule` is a dynamically loaded module which
    /// implements some number of types and interface implementations.
    ///
    /// When the module is loaded, it registers its types and interfaces
    /// using [method[`Object`][crate::Object].register_type] and
    /// [method[`Object`][crate::Object].add_interface].
    /// As long as any instances of these types and interface implementations
    /// are in use, the module is kept loaded. When the types and interfaces
    /// are gone, the module may be unloaded. If the types and interfaces
    /// become used again, the module will be reloaded. Note that the last
    /// reference cannot be released from within the module code, since that
    /// would lead to the caller's code being unloaded before ``g_object_unref()``
    /// returns to it.
    ///
    /// Keeping track of whether the module should be loaded or not is done by
    /// using a use count - it starts at zero, and whenever it is greater than
    /// zero, the module is loaded. The use count is maintained internally by
    /// the type system, but also can be explicitly controlled by
    /// [method[`Object`][crate::Object].use] and [method[`Object`][crate::Object].unuse].
    /// Typically, when loading a module for the first type, `[`TypeModuleExtManual::use_()`][crate::prelude::TypeModuleExtManual::use_()]`
    /// will be used to load it so that it can initialize its types. At some later
    /// point, when the module no longer needs to be loaded except for the type
    /// implementations it contains, `[`TypeModuleExtManual::unuse()`][crate::prelude::TypeModuleExtManual::unuse()]` is called.
    ///
    /// `GTypeModule` does not actually provide any implementation of module
    /// loading and unloading. To create a particular module type you must
    /// derive from `GTypeModule` and implement the load and unload functions
    /// in `GTypeModuleClass`.
    ///
    /// This is an Abstract Base Class, you cannot instantiate it.
    ///
    /// # Implements
    ///
    /// [`ObjectExt`][trait@crate::prelude::ObjectExt], [`TypePluginExt`][trait@crate::prelude::TypePluginExt]
    #[doc(alias = "GTypeModule")]
    pub struct TypeModule(Object<gobject_ffi::GTypeModule, gobject_ffi::GTypeModuleClass>) @implements TypePlugin;

    match fn {
        type_ => || gobject_ffi::g_type_module_get_type(),
    }
}

impl TypeModule {
    pub const NONE: Option<&'static TypeModule> = None;
}

pub trait TypeModuleExt: IsA<TypeModule> + 'static {
    #[doc(alias = "g_type_module_add_interface")]
    fn add_interface(
        &self,
        instance_type: crate::types::Type,
        interface_type: crate::types::Type,
        interface_info: &InterfaceInfo,
    ) {
        unsafe {
            gobject_ffi::g_type_module_add_interface(
                self.as_ref().to_glib_none().0,
                instance_type.into_glib(),
                interface_type.into_glib(),
                interface_info.as_ptr(),
            );
        }
    }

    #[doc(alias = "g_type_module_register_enum")]
    fn register_enum(
        &self,
        name: &str,
        const_static_values: &'static EnumValues,
    ) -> crate::types::Type {
        unsafe {
            from_glib(gobject_ffi::g_type_module_register_enum(
                self.as_ref().to_glib_none().0,
                name.to_glib_none().0,
                const_static_values.to_glib_none().0,
            ))
        }
    }

    #[doc(alias = "g_type_module_register_flags")]
    fn register_flags(
        &self,
        name: &str,
        const_static_values: &'static FlagsValues,
    ) -> crate::types::Type {
        unsafe {
            from_glib(gobject_ffi::g_type_module_register_flags(
                self.as_ref().to_glib_none().0,
                name.to_glib_none().0,
                const_static_values.to_glib_none().0,
            ))
        }
    }

    #[doc(alias = "g_type_module_register_type")]
    fn register_type(
        &self,
        parent_type: crate::types::Type,
        type_name: &str,
        type_info: &TypeInfo,
        flags: TypeFlags,
    ) -> crate::types::Type {
        unsafe {
            from_glib(gobject_ffi::g_type_module_register_type(
                self.as_ref().to_glib_none().0,
                parent_type.into_glib(),
                type_name.to_glib_none().0,
                type_info.as_ptr(),
                flags.into_glib(),
            ))
        }
    }

    #[doc(alias = "g_type_module_set_name")]
    fn set_name(&self, name: &str) {
        unsafe {
            gobject_ffi::g_type_module_set_name(
                self.as_ref().to_glib_none().0,
                name.to_glib_none().0,
            );
        }
    }

    #[doc(alias = "g_type_module_unuse")]
    fn unuse(&self) {
        unsafe {
            gobject_ffi::g_type_module_unuse(self.as_ref().to_glib_none().0);
        }
    }

    #[doc(alias = "g_type_module_use")]
    #[doc(alias = "use")]
    fn use_(&self) -> bool {
        unsafe {
            from_glib(gobject_ffi::g_type_module_use(
                self.as_ref().to_glib_none().0,
            ))
        }
    }
}

impl<O: IsA<TypeModule>> TypeModuleExt for O {}