GLib/GObject+C as the lingua franca?

koos vriezen koos.vriezen at gmail.com
Sun Jul 27 19:49:22 BST 2008


2008/7/27 nf2 <nf2 at scheinwelt.at>:

> 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...

But you also need to add a copy constructor then and think about
either implicit sharing the GFile object or copying ... GFile doesn't
seem to have ref counting only g_file_dup, so for sharing you must do
your own ref counting.
But if you can easily track the file objects, why not simply store the
pointers in the C++ containers.


> -------------------- C Code ------------------------
>
> GError * err = NULL;
> GFile * gFile = g_file_new_for_uri("ftp://user@server/file");
>      GFileInfo* gFileInfo = g_file_query_info( gFile,
>               "*",
>               G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
>               NULL,
>               &err);

g_file_new_for_uri doesn't have an GError argument.

> if (err)
> {
>     printf("error %s\n", err->message);
>     g_error_free(err);
>
>     g_object_unref (gFile);
>     return;
> }
>
> ----------------- 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,
>               "*",
>               G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
>               NULL,
>               err.pp());

You could even add a 'operator GError *()' to the GOError class,
instead of the pp()

> if (err.hasError())
> {
>     cout << "error " << err.message();
>     return;
> }

Note that not all gfoo's use g_object_unref, eg. GFile uses
g_file_delete. Makes it rather hard to templete this.

I think you should look where exactly this saves code.

Koos




More information about the kde-core-devel mailing list