Inheritence without knowing the parent at compile time



Hi,

After playing about with GObject a bit, I'm trying to work out how to
inherit at runtime.

Normally, type and class structs have the parent type and class structs
at their start, in order to make them the right size and give the
correct offset to the other members of the struct. ie.

struct _MyObject {
	ParentObject parent;

	gpointer myfield;
	... etc ...
};

However, I can't do this because I don't know what the parent is going
to be. So I'm omitting that from my structs and registering my types
with this sort of function:

GType
tirith_obi_python_new_type (const gchar *class, const gchar *super)
{
	guint type = 0, parent_type = 0;
	GTypeQuery query;
	
	parent_type = g_type_from_name (super);
	g_type_query (parent_type, &query);

	GTypeInfo obi_info =
	{
		query.class_size + sizeof (TirithObiPythonClass),
		(GBaseInitFunc) NULL,
		(GBaseFinalizeFunc) NULL,
		(GClassInitFunc) tirith_obi_python_class_init,
		(GClassFinalizeFunc) NULL,
		NULL,
		query.instance_size + sizeof (TirithObiPython),
		0,
		(GInstanceInitFunc) tirith_obi_python_init,
		NULL
	};

	type = g_type_register_static (g_type_from_name (super), class,
&obi_info, 0);

	return type;
}

note the sizeof() addition so the runtime system gets the correct size.
But now I expect my pointers are pointing to the wrong place when they
are passed to a function.

void
tirith_obi_python_init (TirithObiPython *obi)

In this function, obi will be pointing to the start the gobject which is
not the same point as the beginning of the TirithObiPython struct?
Presumably I have to do something along the lines of:

newobi = obi + sizeof (parent);

to get the correct position? But I don't know what the immediate parent
is. Is there a gobject function where I can find out?

Basically, I'd like to know if I'm on the right lines here, or if I'm
doing it totally wrong.

Thanks a lot for your help.

-- 
                 Merry Christmas,

                               Andrew

-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GS/M d--(-) s: a17 C++(+++) UL+ P++ L+++ E--- W+>++ N(-) o? K? w--(---) !O M V-
PS+ PE Y+ PGP+>++++ t@ 5-- X- R tv-@ b++++ DI+++ D>---- G- e- h! r--- y?
------END GEEK CODE BLOCK------




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