summaryrefslogtreecommitdiff
path: root/test cases/vala/21 type module/plugin-module.vala
blob: 1c3aecad21ec6ce316c336050bfd2aa2b04235bf (plain)
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
public class Foo.PluginModule : TypeModule {

    [CCode (has_target = false)]
    private delegate Type PluginInit (TypeModule type_module);

    public string? directory { get; construct; default = null; }

    public string name { get; construct; }

    public string path { get; construct; }

    public Type plugin_type { get; private set; }

    private Module? module = null;

    public PluginModule (string? directory, string name) {
        Object (directory: directory, name: name);
    }

    construct {
        path = Module.build_path (directory, name);
    }

    public override bool load () {
        module = Module.open (path, ModuleFlags.BIND_LAZY);

        if (module == null) {
            critical (Module.error ());
            return false;
        }

        void* plugin_init;
        if (!module.symbol ("plugin_init", out plugin_init)){
            critical (Module.error ());
            return false;
        }

        if (plugin_init == null) {
            return false;
        }

        plugin_type = ((PluginInit) plugin_init) (this);

        if (!plugin_type.is_a (typeof (Plugin))) {
            return false;
        }

        return true;
    }

    public override void unload () {
        module = null;
    }
}