Re: Direct access to object's private variables



On Sat, 2007-07-28 at 23:32 +0200, Tomasz Jankowski wrote:
Hello!

I have an object based on GObject system. I added to this object it's
private structure using 'g_type_class_add_private ()'. Now need to access to
this private data very often, so it isn't good idea to use macro
SOME_OBJECT_GET_PRIVATE all the time. It's waste of CPU time in my
situation. Is there any pointer, which goes directly to this structure?
Something like object->private->my_variable ?

not by default, as adding a pointer to GObject would break ABI and would
probably collide with similarly named members of subclasses, not to tell
the waste of a sizeof(gpointer) even for classes without a private data
structure.

you have to add a pointer to the private data structure in your own
subclass:

  typedef struct _YourFoo        YourFoo;
  typedef struct _YourFooPrivate YourFooPrivate;
  typedef struct _YourFooClass   YourFooClass;

  struct _YourFoo
  {
    GObject parent_instance;
    YourFooPrivate *priv;
  };

and initialise it in the _init function:

  static void
  your_foo_init (YourFoo *foo)
  {
    YourFooPrivate *priv;

    foo->priv = priv = YOUR_FOO_GET_PRIVATE (foo);
  }

and then you can use foo->priv to access the private data structure from
any part of your program where YourFooPrivate is actually defined.

ciao,
 Emmanuele.

-- 
Emmanuele Bassi,
W: http://www.emmanuelebassi.net
B: http://log.emmanuelebassi.net




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