Re: built-in types

Adrian Feiguin wrote:

I wonder if somebody can shed some light on the way the BOXED data types
are generated. I understand that maketypes.awk is used, are there any
guidelines? Thank you!

For your own code, I wouldn't recommend copying the maketypes.awk stuff.

First of all, you should ask yourself if the type you are talking about fits the model of a boxed type. It should be safe to copy like a value (or support reference counting as a substitute for copying). If your type fits this model okay, then here is what you should do:

  1. implement copy and free functions.  The copy function takes one
     argument (the one to copy/ref), and returns the copy.  The free
     function takes one argument (the one to free/unref), and has no
     return value.
  2. Write a get_type() function for the type, something like this:

           static GType boxed_type;

           if (!boxed_type) {
               boxed_type = g_boxed_register_static("MyBoxed",
       my_boxed_copy, my_boxed_free);
           return boxed_type;

Then just put a prototype for the get_type function in your headers, and a macro to call it:

   #define MY_TYPE_BOXED (my_boxed_get_type())

You can then use MY_TYPE_BOXED as the typecode for the boxed type in signal and property definitions. You don't need to make the copy/free functions public -- they can be accessed through the GBoxed APIs:

   copy = g_boxed_copy(MY_TYPE_BOXED, original);
   g_boxed_free(MY_TYPE_BOXED, copy);

(Language bindings may use the above API to handle arbitrary boxed types).

If your type doesn't behave this way, it is also possible to register it as a subtype of G_TYPE_POINTER. However, you should make your type usable as a boxed or GObject if possible. Don't register as a pointer type just because you don't want the value to be copied during signal emission -- you can use the static scope flag for the signal argument in this case instead.


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