|
| 1 | +Type Modules |
| 2 | +============ |
| 3 | + |
| 4 | +This example shows you how to implement a GLib.TypeModule based plugin in Vala. It also shows the usage of static construct/destruct block. |
| 5 | + |
| 6 | +.. code-block:: vala |
| 7 | +
|
| 8 | + // plugin.vala |
| 9 | + public class MyClass : Object |
| 10 | + { |
| 11 | + static construct |
| 12 | + { |
| 13 | + message("MyClass init"); |
| 14 | + } |
| 15 | +
|
| 16 | + static ~MyClass() |
| 17 | + { |
| 18 | + message("MyClass deinit"); |
| 19 | + } |
| 20 | + } |
| 21 | +
|
| 22 | + [ModuleInit] |
| 23 | + Type plugin_init(GLib.TypeModule type_modul) |
| 24 | + { |
| 25 | + return typeof(MyClass); |
| 26 | + } |
| 27 | +
|
| 28 | +
|
| 29 | +.. code-block:: vala |
| 30 | +
|
| 31 | + // loader.vala |
| 32 | + class MyModule : TypeModule |
| 33 | + { |
| 34 | + [CCode (has_target = false)] |
| 35 | + private delegate Type PluginInitFunc(TypeModule module); |
| 36 | +
|
| 37 | + private GLib.Module module = null; |
| 38 | +
|
| 39 | + private string name = null; |
| 40 | +
|
| 41 | + public MyModule(string name) |
| 42 | + { |
| 43 | + this.name = name; |
| 44 | + } |
| 45 | +
|
| 46 | + public override bool load() |
| 47 | + { |
| 48 | + string path = Module.build_path(null, name); |
| 49 | + module = Module.open(path, GLib.ModuleFlags.BIND_LAZY); |
| 50 | + if(null == module) { |
| 51 | + error("Module not found"); |
| 52 | + } |
| 53 | +
|
| 54 | + void * plugin_init = null; |
| 55 | + if(! module.symbol("plugin_init", out plugin_init)) { |
| 56 | + error("No such symbol"); |
| 57 | + } |
| 58 | +
|
| 59 | + ((PluginInitFunc) plugin_init)(this); |
| 60 | +
|
| 61 | + return true; |
| 62 | + } |
| 63 | +
|
| 64 | + public override void unload() |
| 65 | + { |
| 66 | + module = null; |
| 67 | +
|
| 68 | + message("Library unloaded"); |
| 69 | + } |
| 70 | + } |
| 71 | +
|
| 72 | + // Never unref instance of GTypeModule |
| 73 | + // http://www.lanedo.com/~mitch/module-system-talk-guadec-2006/Module-System-Talk-Guadec-2006.pdf |
| 74 | + static TypeModule module = null; |
| 75 | +
|
| 76 | + int main() |
| 77 | + { |
| 78 | + module = new MyModule("plugin"); |
| 79 | + module.load(); |
| 80 | +
|
| 81 | + var o = GLib.Object.new(Type.from_name("MyClass")); |
| 82 | +
|
| 83 | + // free last instance, plugin unload |
| 84 | + o = null; |
| 85 | +
|
| 86 | + return 0; |
| 87 | + } |
| 88 | +
|
| 89 | +**Build**: |
| 90 | + |
| 91 | +.. code-block:: console |
| 92 | +
|
| 93 | + $ valac -o loader loader.vala --pkg=gmodule-2.0 |
| 94 | + $ valac --ccode plugin.vala |
| 95 | + $ gcc -fPIC -shared -o libplugin.so plugin.c $(pkg-config --libs --cflags gobject-2.0 gmodule-2.0) |
| 96 | +
|
| 97 | +**Run**: |
| 98 | + |
| 99 | +.. code-block:: console |
| 100 | +
|
| 101 | + $ LD_LIBRARY_PATH=$PWD ./loader |
| 102 | +
|
| 103 | +Which should output something like this: |
| 104 | + |
| 105 | +.. code-block:: output |
| 106 | +
|
| 107 | + ** Message: plugin.vala:5: MyClass init |
| 108 | + ** Message: plugin.vala:10: MyClass deinit |
| 109 | + ** Message: loader.vala:37: Library unloaded |
0 commit comments