Re: [Vala] Setting up a GLib main loop with custom Sources with Vala 0.11.7



Hello,
(See below for my attempts at improving your workarounds)

                      في ن، 28-03-2011 عند 06:23 -0500 ، كتب Jim Peters:
For reference, I have a Glib main loop with custom Source instances
working now, in case anyone else needs to do it.  With the 0.11.7
compiler I had to make a number of work-arounds for compiler bugs and
limitations, and I had to hack my glib VAPI file.  It is not ideal but
it works (although I haven't checked for leaks yet).  Hopefully in
later versions these hacks won't be necessary.

Probably this isn't the best way of solving these problems, but since
I know no better ...


- Change SourcePrepareFunc in the VAPI to use 'int*' instead of 'out
  int' for the timeout, and change the calling code accordingly:

        [CCode (has_target = false)]
        public delegate bool SourcePrepareFunc (Source source, int* timeout_);

  This works around bug 645838.  Using 'out int' would be better in
  the long run.

Another (better IMO) workaround is not to use a lambda expression and
use a method instead.

- Add a constructor for SourceFuncs in the VAPI, and implement
  g_source_funcs_new and g_source_funcs_free in C:
[...]
  Note that GLib expects SourceFuncs to be static data, i.e. it
  doesn't copy it or do any ownership stuff.  So the caller has to
  make sure it stays around as long as the Source remains active.

That's not right, we can't have an additional C code other than what
there is in glib. You can probably make it a struct instead: it will be
allocated/freed automatically (as it is a value type), and could then be
statically allocated.

- Pass a 'guessed' size value to the Source superclass constructor.
  The Source constructor wants to know the size of the object
  constructed.  The code 'sizeof(MySource)' gives the size of a
  pointer, not the size of the structure, so it doesn't seem possible
  to get this value in Vala.  So I pass 256 for now which is more than
  enough.

A workaround I see here (but that would still be a hack), is to have a
new constant named SIZE in GLib.Source defined as (untested):

        [CCode (cname="sizeof(GSource)"]
        public const int SIZE;

and use that in the constructor (assuming you don't need additional
fields in your source).

HTH,
Abderrahim




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]