I Am a GObject May 18, 2008
This past week, I’ve been up close and personal with GObjects, deciding to finally learn how to use them. Till now, most of my hacking has been with Gtk+, and that too largely in Python, which is, well…, child’s play. Luckily, I had a lot of patience to work out the boiler plate code, and figure out it’s various idiosyncrasies, enough so to appreciate what it’s trying to do. Before I say anything further, kudos to Mathieu Lacage’s for his helpful tutorial, which has been included in the official GNOME docs. It very clearly explains the motivation behind the whole GObject concept, which, to be fully honest is required if you want to make sense of it. He also did well in introducing the naming conventions used by GNOME.
GObjects really gave me an insight into C++ and other OOP-ly languages, and I decided to come up with a mapping of C++ to GObject terms, that the Brain (of Pinky and the Brain fame) might find handy. Hopefully, it will appeal to non-fictional characters as well. (Note: I’ve excluded the constructor and destructor functions which are implemented in the boilerplate _constructor
and _finalize
functions).
If you’ve ever used GObjects, you’d see a load of boilerplate functions. I felt a bit uncomfortable about the various _init
functions even after reading the docs, so I stepped through with a debugger to see what happens where. Here’s another diagram.
A few notes:
I’ve found that putting the signal code into the
_class_init
function seems more logical, and well… works, however, in the tutorial, Mathieu puts these in the constructor.There’s an interesting loop that occurs between the
_constructor
and the_instance_init
functions. The tutorial explains:
Finally, at one point or another,
g_object_constructor
is invoked by the last constructor in the chain. This function allocates the object’s instance’ buffer throughg_type_create_instance
which means that theinstance_init
function is invoked at this point if one was registered. Afterinstance_init
returns, the object is fully initialized and should be ready to answer any user-request. Wheng_type_create_instance
returns,g_object_constructor
sets the construction properties (i.e. the properties which were given tog_object_new
) and returns to the user’s constructor which is then allowed to do useful instance initialization…
- Despite this, I can’t find a use for the
_instance_init
function. I’d be delighted to hear an explanation.