Hi,
في خ، 10-06-2010 عند 20:12 +0200 ، كتب JM:
> Inheriting from TypeModule gives me a warning:
>
> main.vala:10.5-10.26: warning: unable to chain up to private base
> constructor
> public PluginRegistrar (string name) {
>
> Looks like this isn't possible.
This is just a warning, you can work around it by adding Object(); at
the top of the constructor.
I've tried to make the example work, I don't like it but it's a start
(in particular I don't think making the "registrar" using generics is a
good idea. I don't like the name registrar either.)
Anyway, attached is a working version, I needed to patch gobject
bindings according to what I understood from the documentation.
to compile, I did :
# to compile the interface library
valac -C -H plugin-interface.h --library plugin-interface plugin-interface.vala
# to compile the main program
valac --pkg gmodule-2.0 -C main.vala plugin-interface.vapi
gcc -g -O0 $(pkg-config --cflags --libs gmodule-2.0 gobject-2.0) -I. main.c
plugin-interface.c -o main
# to compile the plugin
valac -C plugin.vala plugin-interface.vapi
gcc -g -O0 -shared -fPIC -I. $(pkg-config --cflags --libs glib-2.0) -o
libplugin.so plugin.c
The procedure is more complicated than that of the example because the
example was relying on buggy behaviour of valac (not registering types
in other files than the one containing [ModuleInit]).
If someone can polish them and put them in the wiki, it'd be great.
HTH,
Abderrahim
diff --git a/vapi/gobject-2.0.vapi b/vapi/gobject-2.0.vapi
index a3876d3..3b0d70f 100644
--- a/vapi/gobject-2.0.vapi
+++ b/vapi/gobject-2.0.vapi
@@ -98,15 +98,15 @@ namespace GLib {
public interface TypePlugin {
}
- [CCode (lower_case_csuffix = "type_module")]
- public class TypeModule : Object, TypePlugin {
+ [CCode (lower_case_csuffix = "type_module", unref_function = "")]
+ public abstract class TypeModule : Object, TypePlugin {
public bool use ();
public void unuse ();
public void set_name (string name);
[NoWrapper]
- public virtual bool load ();
+ public abstract bool load ();
[NoWrapper]
- public virtual void unload ();
+ public abstract void unload ();
}
[CCode (type_id = "G_TYPE_PARAM", ref_function = "g_param_spec_ref", unref_function = "g_param_spec_unref", param_spec_function = "g_param_spec_param", get_value_function = "g_value_get_param", set_value_function = "g_value_set_param", take_value_function = "g_value_take_param")]
public class PluginRegistrar<T> : TypeModule {
public string path { get; private set; }
private Type type;
private Module module;
private bool loaded = false;
private delegate Type RegisterPluginFunction (TypeModule m);
public PluginRegistrar (string name) {
assert (Module.supported ());
this.path = Module.build_path (Environment.get_variable ("PWD"), name);
}
public override bool load () {
stdout.printf ("Loading plugin with path: '%s'\n", path);
if (loaded)
return true;
module = Module.open (path, ModuleFlags.BIND_LAZY);
if (module == null) {
return false;
}
stdout.printf ("Loaded module: '%s'\n", module.name ());
void* function;
module.symbol ("register_plugin", out function);
RegisterPluginFunction register_plugin = (RegisterPluginFunction) function;
type = register_plugin (this);
stdout.printf ("Plugin type: %s\n\n", type.name ());
loaded = true;
return true;
}
public override void unload () {
stdout.printf ("Should be unloading plugin with path '%s'\n", path);
}
public T new_object () {
return Object.new (type);
}
}
void main () {
var registrar = new PluginRegistrar<TestPlugin> ("plugin");
registrar.load ();
var plugin = registrar.new_object ();
plugin.hello ();
}
class MyPlugin : Object, TestPlugin {
public void hello () {
stdout.printf ("Hello world!\n");
}
}
[ModuleInit]
public Type register_plugin (TypeModule module) {
// types are registered automatically
return typeof (MyPlugin);
}
public interface TestPlugin : Object {
public abstract void hello ();
}
_______________________________________________
vala-list mailing list
vala-list@gnome.org
http://mail.gnome.org/mailman/listinfo/vala-list