GLib/GObject+C as the lingua franca?

nf2 nf2 at
Sun Jul 27 19:26:36 BST 2008

koos vriezen wrote:
> 2008/7/27 nf2 <nf2 at>:
>> Why? I think a lot of things that are tiring with plain C - like strings,
>> containers and garbage collection might be a bit easier with a bit of C++.
>> For instance a simple GObjectWrapper<T> which automatically
>> g_object_un_ref's in the destructor - and g_object_refs on copy might help.
>> Or a C++ wrapper for GError (that you don't need g_error_free()s all over
>> the place)... If someone wants to write a GObject based library in C++,
>> perhaps it's worth looking for an appropriate (but still simplistic) style.
> I don't think there is that much to gain, whether you do
> glib_type_new/g_object_unref vs. new GFoo/delete gfoo. Only you can
> create the object on the stack, which saves you the delete call (not
> for QObject's though)
> Of course this very much depends on the use case. Eg. I maintain a
> program both for KDE as for Maemo, and being able to apply patches
> easy, I did do some C++ wrapping on simple types as QString and KUrl
> (the GUI is completely different anyhow).
> A common code pattern might be a class candidate, often a function
> will do as well. But doing heavy wrapping in the beginning of the
> coding often shows unwanted frictions later, turning to hacks and the
> code gets uglier and uglier.
> Wrapping toolkits to others is just a pain, Luckily we can use C
> directly like glib, expat, libjpeg you-name-it.
> Stick with the toolkit API.

I thought of really lightweight wrapping. Very minimalistic. See the 
example below. Mainly that you can put GObjects into C++ containers and 
for garbage collection...

I believe heavy wrapping - like gtkmm is more interesting for library 
"end-users" like application developers, but not here.


-------------------- C Code ------------------------

GError * err = NULL;
GFile * gFile = g_file_new_for_uri("ftp://user@server/file");
GFileInfo* gFileInfo = g_file_query_info( gFile,

if (err)
      printf("error %s\n", err->message);

      g_object_unref (gFile);

----------------- C++ Code -------------------

GOError err;
GO<GFile> gFile = g_file_new_for_uri("ftp://user@server/file");
GO<GFileInfo> gFileInfo = g_file_query_info( gFile.p,

if (err.hasError())
      cout << "error " << err.message();

More information about the kde-core-devel mailing list