Re: Instance private data



On 13 Feb 2003, Owen Taylor wrote:

> >	I've attached a patch that implements g_type_get_private_offset(). The
> > only worry I can see about this is that it might encourage people to
> > start poking in the private structures - but I don't think we've had any
> > cases of people doing that with the 'priv' pointers ....
>
> I think it's a reasonable addition ... one thing though is that you
> are relying on only one private structure being added for each type
> (that is, that the private data offset can be found from the private
> data offset of the parent), something that my patch didn't require.
>
> I've added a check that g_type_add_private() is called no more than
> once to the patch.
>
> > 	Oh, the patch adds some tests of this to testgobject was well, which
> > may be useful even if you don't want get_private_offset().
>
> Cool, thanks.
>
> > >  d) As long as people are required to call g_type_add_private() in their
> > >     class_init(), it should work fine for dynamically loaded
> > >     types. My patch doesn't try to catch calling g_type_add_private()
> > >     at the wrong time, but I really don't imagine that people will
> > >     try to call it elsewhere.
> >
> > 	Hmm ... my initial foolish instinct was to do it in get_type() - so
> > maybe a check might be worthwhile ...
>
> Yeah, probably partially caused by the name 'g_type_add_private' being
> similar to g_type_add_interface(). Don't know a better name though.
>
> I've added a check for g_type_add_private() being called prior to
> class_init() time as well.
>
> Tim - if I move the docs to the template files, is this OK to commit?

not quite, at least, there's a malicious assert and some cosmetics that should
be changed, but before that, i have API concerns. allowing private struct
size to be added only once per type, and only from class_init() seems
reasonable to me, too. given that, the complexity can be reduced to gtype.c
i think, simply by storing private_base in the type node as well, so
we get:

/* using g_class as argument is more convenient and conveys class_init()
 * time like g_type_class_peek_parent()
 */
void g_type_class_add_private (gpointer g_class,
                               guint16  priv_size);

/* return ((guint8*) instance) + instance_size + priv_base(priv_type), where
 * priv_base for priv_type is the sum of priv_size from its ancestors
 */
gpointer g_type_instance_get_private (GTypeInstance *instance,
                                      GType          priv_type);

/* macro to be used the same way as G_TYPE_INSTANCE_GET_CLASS() */
#define G_TYPE_INSTANCE_GET_PRIVATE(obj,type,ctype) ((ctype*) g_type_instance_get_private ((obj), type))

results in example usage:
gtk_window_class_init (GtkWindowClass *class)
{
  g_type_class_add_private (class, sizeof (GtkWindowPrivate));
}
#define GTK_WINDOW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE (obj, GTK_TYPE_WINDOW, GtkWindowPrivate))

or am i missing something in your patch?

>
> Regards,
>                                             Owen
>

---
ciaoTJ




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