value: Allow automatic transforms to/from interfaces

Use the new g_type_interface_instantiable_prerequisite() to check
compatibility for transform functions.

In particular, this allows interfaces (in my case GDK_TYPE_PAINTABLE) to
be transformed to/from any GObject type (in my case G_TYPE_OBJECT) using
the transform function registered to tranform between any 2 objects
(g_value_object_transform_value() does a type check and uses NULL if the
types don't match).

And this in turn allows be to g_object_bind_property() a gobject-typed
generic property (GtkListItem::item) to a GtkImage::paintable.
This commit is contained in:
Benjamin Otte 2019-11-25 19:58:59 +01:00
parent e994d45352
commit 5899c61ed2

View File

@ -448,6 +448,15 @@ g_value_init_from_instance (GValue *value,
}
}
static GType
tranform_lookup_get_parent_type (GType type)
{
if (g_type_fundamental (type) == G_TYPE_INTERFACE)
return g_type_interface_instantiable_prerequisite (type);
return g_type_parent (type);
}
static GValueTransform
transform_func_lookup (GType src_type,
GType dest_type)
@ -470,11 +479,11 @@ transform_func_lookup (GType src_type,
g_type_value_table_peek (entry.src_type) == g_type_value_table_peek (src_type))
return e->func;
}
entry.dest_type = g_type_parent (entry.dest_type);
entry.dest_type = tranform_lookup_get_parent_type (entry.dest_type);
}
while (entry.dest_type);
entry.src_type = g_type_parent (entry.src_type);
entry.src_type = tranform_lookup_get_parent_type (entry.src_type);
}
while (entry.src_type);