Since GLib 2.36 SourceFuncs->check can be null
A minimal GSource example would be:
void main () {
var loop = new MainLoop ();
SourceFunc quit = () => {
loop.quit ();
return Source.REMOVE;
};
Source a = new QuitApplicationSource ();
a.set_callback ((owned)quit);
a.attach ();
loop.run ();
}
public class QuitApplicationSource:Source {
public override bool prepare (out int timeout) {
timeout = -1;
return true;
}
public override bool dispatch (SourceFunc? callback) {
return callback ();
}
}
At present this produces the error:
minimal_gsource.vala:17.1-17.41: error: `QuitApplicationSource' does not implement abstract method `GLib.Source.check'
public class QuitApplicationSource:Source {
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Compilation failed: 1 error(s), 0 warning(s)
and needs the following added to QuitApplicationSource
:
public override bool check () {
return false;
}
The GSourceFuncs documentation advises that check ()
since '2.36 this may be NULL, in which case the effect is as if the function always returns FALSE'.
One solution would be to ignore the NULL in SourceFuncs, but have a default implementation in glib-2.0.vapi
that is a virtual method that always returns false:
protected virtual bool check () { return false; }
That would save an author writing it out again in their own Vala code. The problem with that is the translation in codegen/valaccodemethodcallmodule.vala
of Source.check
to SourceFuncs doesn't pick up the default virtual method implementation at present. Also the use of NULL might be marginally more efficient in the MainContext.
An alternative would be to have the default implementation in the VAPI have an empty body then the methodcallmodule translates that to null. By having it in the VAPI Vala makes sure a check
method is present, but translates the default implementation to NULL in the generated C.
Another option is to remove check
from the VAPI and have methodcallmodule use NULL if there is no implementation.
This would be useful for custom GSources that do not check for events on a file descriptor, e.g. a message queue source.