Re: Glib / GObject newbie



this list is for development *of* gtk+ and gobject, not *with* them. your question is more appropriate for gtk-list or gtk-app-devel-list.

that said...


On Apr 25, 2005, at 6:36 AM, Laurent JALLET wrote:

that's what I want to implement using GObjects :
. a Camera containing a position ( => Vector3 : a GObject containing 3 gfloats) and other members


and I'd like to write
static void
camera_get_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
{
      Camera *self = (Camera *) object;

       switch (property_id) {
       case CAMERA_FOV: {
               g_value_set_float (value, self->priv->mFov);
       }
               break;

       case CAMERA_POSITION :
       // There is my problem
       // I don't know what to do
How can I transform a GValue into a GObject ?
And how do I use it ?

   g_value_set_object (value, self->priv->mPosition);

This takes a reference on the GObject and places the pointer into the GValue. When the GValue is unset, the reference will be released. Code that fetches the value with g_object_get (camera, "position", &position, NULL); should g_object_ref() the position for as long as it wants to keep it.

http://developer.gnome.org/doc/API/2.0/gobject/gobject-Standard- Parameter-and-Value-Types.html#g-value-set-object


Of course there's the same problem with the camera_set_property function.

http://developer.gnome.org/doc/API/2.0/gobject/gobject-Standard- Parameter-and-Value-Types.html#g-value-get-object

    /* if the new position is not the same as the old one, take it. */
    Vector3 * new_position = g_value_get_object (value);
    if (new_position != camera->priv->mPosition) {
        if (camera->priv->mPosition) {
            /* disconnect from old */
            g_object_unref (camera->priv->mPosition);
        }
        if (new_position) {
            /* connect to new */
            g_object_ref (new_position);
        }
        camera->priv->mPosition = new_position;
    }

it's usually best to add accessor methods like camera_get_position() and camera_set_position(), and have your get_property()/set_property() implementations call the accessors.


I don't really understand what GValue is and how I can use it.

there are plenty of examples in the gtk+ source. you don't need to be using or implementing gtk+ in order to learn from its source.


--
The door is locked. I tried to open it, but the lock is harder to pick than a broken nose.
  -- Sensei, on 'I, Ninja'




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