Sign up

How to use ObjectG

Hey all,

Noob to Gtk programming in general so please bear with me. I'm trying to write D bindings for the VIPS library which uses the gobject library internally.

I wanted to subclass ObjectG for objects specific to the library, but I couldn't quite figure out when to pass in true for ObjectG class's constructor (the ownedRef parameter). Am I correct in guessing that we're supposed to pass in true when the gobject already has a linked ObjectG object to it?

Another question is if I am having the GObject generated by a third party and I am responsible for unreffing it, do I have to unref it myself or does the ObjectG class handle everything? It looks like the ObjectG class increments the refCount by 2 already, so I'm guessing I have to unref the GObject manually.

Thanks in advance!

Re: How to use ObjectG

On 21-01-2019 22:47, yes9111 wrote:

Hey all,

Noob to Gtk programming in general so please bear with me. I'm trying to write D bindings for the VIPS library which uses the gobject library internally.

Since libvips uses/supports goject introspection you could use gir-to-d
to generate the binding:

girtod -i /usr/share/gir-1.0/Vips-8.0.gir

This will generate a libvips binding that depends on GlibD or GtkD (in
the future GtkD will depend on GlibD but that still a work in progress).

gir-to-d: https://github.com/gtkd-developers/gir-to-d
GlibD: https://github.com/gtkd-developers/GlibD

I wanted to subclass ObjectG for objects specific to the library, but I couldn't quite figure out when to pass in true for ObjectG class's constructor (the ownedRef parameter). Am I correct in guessing that we're supposed to pass in true when the gobject already has a linked ObjectG object to it?

ObjectG does the reference counting for you, with GTK some functions
will pass the ownership of the Object to the caller, so you don't have
to ref the object yourself. In these cases you would pass true to
ObjectG to tell it to not add an extra reference to the object.

One of those cases is vips_image_new
(http://libvips.github.io/libvips/API/current/VipsImage.html#vips-image-new),
since it's annotated with transfer full in the documentation.

Another question is if I am having the GObject generated by a third party and I am responsible for unreffing it, do I have to unref it myself or does the ObjectG class handle everything? It looks like the ObjectG class increments the refCount by 2 already, so I'm guessing I have to unref the GObject manually.

Thanks in advance!

Unless there is a bug somewhere you shouldn't have to unref the object
manually.

ObjectG will add a toggle reference to the object, and if we already
owned the object (ownedRef = true) it will remove the regular reference.
So it should 1 or 0 references in the constructor, and it will remove 1
reference in the destructor.

Re: How to use ObjectG

On Tue, 22 Jan 2019 19:39:40 +0100, Mike Wey wrote:

On 21-01-2019 22:47, yes9111 wrote:

Hey all,

Noob to Gtk programming in general so please bear with me. I'm trying to write D bindings for the VIPS library which uses the gobject library internally.

Since libvips uses/supports goject introspection you could use gir-to-d
to generate the binding:

girtod -i /usr/share/gir-1.0/Vips-8.0.gir

This will generate a libvips binding that depends on GlibD or GtkD (in
the future GtkD will depend on GlibD but that still a work in progress).

gir-to-d: https://github.com/gtkd-developers/gir-to-d
GlibD: https://github.com/gtkd-developers/GlibD

I wanted to subclass ObjectG for objects specific to the library, but I couldn't quite figure out when to pass in true for ObjectG class's constructor (the ownedRef parameter). Am I correct in guessing that we're supposed to pass in true when the gobject already has a linked ObjectG object to it?

ObjectG does the reference counting for you, with GTK some functions
will pass the ownership of the Object to the caller, so you don't have
to ref the object yourself. In these cases you would pass true to
ObjectG to tell it to not add an extra reference to the object.

One of those cases is vips_image_new
(http://libvips.github.io/libvips/API/current/VipsImage.html#vips-image-new),
since it's annotated with transfer full in the documentation.

Another question is if I am having the GObject generated by a third party and I am responsible for unreffing it, do I have to unref it myself or does the ObjectG class handle everything? It looks like the ObjectG class increments the refCount by 2 already, so I'm guessing I have to unref the GObject manually.

Thanks in advance!

Unless there is a bug somewhere you shouldn't have to unref the object
manually.

ObjectG will add a toggle reference to the object, and if we already
owned the object (ownedRef = true) it will remove the regular reference.
So it should 1 or 0 references in the constructor, and it will remove 1
reference in the destructor.

Awesome, well that saves a lot of work for me :)
Thanks a ton for the response, it cleared up a lot.

Re: How to use ObjectG

On Tue, 22 Jan 2019 19:39:40 +0100, Mike Wey wrote:

On 21-01-2019 22:47, yes9111 wrote:

Hey all,

Noob to Gtk programming in general so please bear with me. I'm trying to write D bindings for the VIPS library which uses the gobject library internally.

Since libvips uses/supports goject introspection you could use gir-to-d
to generate the binding:

girtod -i /usr/share/gir-1.0/Vips-8.0.gir

This will generate a libvips binding that depends on GlibD or GtkD (in
the future GtkD will depend on GlibD but that still a work in progress).

gir-to-d: https://github.com/gtkd-developers/gir-to-d
GlibD: https://github.com/gtkd-developers/GlibD

I wanted to subclass ObjectG for objects specific to the library, but I couldn't quite figure out when to pass in true for ObjectG class's constructor (the ownedRef parameter). Am I correct in guessing that we're supposed to pass in true when the gobject already has a linked ObjectG object to it?

ObjectG does the reference counting for you, with GTK some functions
will pass the ownership of the Object to the caller, so you don't have
to ref the object yourself. In these cases you would pass true to
ObjectG to tell it to not add an extra reference to the object.

One of those cases is vips_image_new
(http://libvips.github.io/libvips/API/current/VipsImage.html#vips-image-new),
since it's annotated with transfer full in the documentation.

Another question is if I am having the GObject generated by a third party and I am responsible for unreffing it, do I have to unref it myself or does the ObjectG class handle everything? It looks like the ObjectG class increments the refCount by 2 already, so I'm guessing I have to unref the GObject manually.

Thanks in advance!

Unless there is a bug somewhere you shouldn't have to unref the object
manually.

ObjectG will add a toggle reference to the object, and if we already
owned the object (ownedRef = true) it will remove the regular reference.
So it should 1 or 0 references in the constructor, and it will remove 1
reference in the destructor.

Awesome, well that saves a lot of work for me :)
Thanks a ton for the response, it cleared up a lot.

Re: How to use ObjectG

On Tue, 22 Jan 2019 19:39:40 +0100, Mike Wey wrote:

Since libvips uses/supports goject introspection you could use gir-to-d
to generate the binding:

girtod -i /usr/share/gir-1.0/Vips-8.0.gir

This will generate a libvips binding that depends on GlibD or GtkD (in
the future GtkD will depend on GlibD but that still a work in progress).

Sorry to triple post - this seems to work smoothly except that it seems to generate
a single class with the name ObjectVips,vips. Is this the expected behavior?

Other than that, the tool seems very handy. Thanks again.

Re: How to use ObjectG

On 23-01-2019 03:46, yes9111 wrote:

Sorry to triple post - this seems to work smoothly except that it seems to generate
a single class with the name ObjectVips,vips. Is this the expected behavior?

Other than that, the tool seems very handy. Thanks again.

That is a bug in gir-to-d, it should be fixed now:
https://github.com/gtkd-developers/gir-to-d/commit/76323099bf62f972fe32d25a6d182d854e055043