gobject中,基本的gtype, 以及继承,派生关系;
gobject 类初始化,对象初始化,接口初始化,方法调用,信号发射等;需要仔细的观察;
所以我们需要函数完成2个功能:
1. 函数调用层级关系显示(类初始化,对象初始化,接口初始化...)
2. gtype/gobject父子关系,显示
参考gobject-query.c, 修改一下:
gtype.h:
void g_type_get_node_tree(void); void g_type_enter_func(const char *name); void g_type_leave_func(const char *name); #define G_ENTER_FUNC g_type_enter_func(__FUNCTION__) #define G_LEAVE_FUNC g_type_leave_func(__FUNCTION__)
gtype.c:
static GType static_gtype_root = G_TYPE_OBJECT; static void _g_type_show_nodes (GType type, GType sibling, const gchar *indent) { static gchar *indent_inc = NULL; static guint spacing = 1; static gboolean recursion = TRUE; #define O_SPACE " " #define O_ESPACE "" #define O_BRANCH "+" #define O_VLINE "|" #define O_LLEAF "`" #define O_KEY_FILL "_" GType *children; guint i; if (!type) return; if (!indent_inc) { indent_inc = g_new (gchar, strlen (O_SPACE) + 1); *indent_inc = 0; strcpy (indent_inc, O_SPACE); } children = g_type_children (type, NULL); if (type != static_gtype_root) for (i = 0; i < spacing; i++) g_printf("%s%s\n", indent, O_VLINE); g_printf("%s%s%s%s", indent, sibling ? O_BRANCH : (type != static_gtype_root ? O_LLEAF : O_SPACE), O_ESPACE, g_type_name (type)); for (i = strlen (g_type_name (type)); i <= strlen (indent_inc); i++) g_printf("%s", O_KEY_FILL); g_printf("\n"); if (children && recursion) { gchar *new_indent; GType *child; if (sibling) new_indent = g_strconcat (indent, O_VLINE, indent_inc, NULL); else new_indent = g_strconcat (indent, O_SPACE, indent_inc, NULL); for (child = children; *child; child++) _g_type_show_nodes (child[0], child[1], new_indent); g_free (new_indent); } g_free (children); } void g_type_get_node_tree(void) { gint i; static_gtype_root = ~0; for (i = 0; i <= G_TYPE_FUNDAMENTAL_MAX; i += G_TYPE_MAKE_FUNDAMENTAL (1)) { const gchar *name = g_type_name (i); if (name) _g_type_show_nodes (i, 0, ""); } } static guint g_static_depth = 0; void g_type_enter_func(const char *name) { guint i; const char *s; if (g_static_depth == 1) g_printf(" \n"); for (i = 0; i < g_static_depth; i++) g_printf("+"); g_printf("Entering %s\n", name); g_static_depth++; } void g_type_leave_func(const char *name) { const char *s; guint i; if (g_static_depth != 0) g_static_depth--; if (g_static_depth < 0) g_static_depth = 0; for (i = 0; i < g_static_depth; i++) g_printf("-"); g_printf("Leaving %s\n", name); }
修改gobject/testgobject.c
可以看到的结果:
Entering main +Entering test_object_get_type ++Entering test_iface_get_type --Leaving test_iface_get_type -Leaving test_object_get_type +Entering iface_base_init -Leaving iface_base_init +Entering iface_base_init -Leaving iface_base_init +Entering test_object_class_init ++Entering test_iface_get_type --Leaving test_iface_get_type -Leaving test_object_class_init +Entering test_object_test_iface_init ++Entering test_iface_get_type --Leaving test_iface_get_type -Leaving test_object_test_iface_init +Entering derived_object_get_type ++Entering test_object_get_type --Leaving test_object_get_type ++Entering test_iface_get_type --Leaving test_iface_get_type -Leaving derived_object_get_type +Entering iface_base_init -Leaving iface_base_init +Entering derived_object_class_init -Leaving derived_object_class_init +Entering derived_object_test_iface_init ++Entering test_iface_get_type --Leaving test_iface_get_type -Leaving derived_object_test_iface_init +Entering test_object_init ++Entering test_object_get_type --Leaving test_object_get_type -Leaving test_object_init +Entering derived_object_init ++Entering derived_object_get_type --Leaving derived_object_get_type ++Entering test_object_get_type --Leaving test_object_get_type ++Entering test_object_get_type --Leaving test_object_get_type -Leaving derived_object_init +Entering test_object_check_private_init ++Entering test_object_get_type --Leaving test_object_get_type private data during initialization: 54321 == 54321 -Leaving test_object_check_private_init | `void | `GInterface | +GTypePlugin | `TestIface | `gchar | `guchar | `gboolean | `gint | `guint | `glong | `gulong | `gint64 | `guint64 | `GEnum | `GFlags | `gfloat | `gdouble | `gchararray | `gpointer | `GType | `GBoxed | `GValueArray | `GParam | +GParamChar | +GParamUChar | +GParamBoolean | +GParamInt | +GParamUInt | +GParamLong | +GParamULong | +GParamInt64 | +GParamUInt64 | +GParamUnichar | +GParamEnum | +GParamFlags | +GParamFloat | +GParamDouble | +GParamString | +GParamParam | +GParamBoxed | +GParamPointer | +GParamValueArray | +GParamObject | +GParamOverride | +GParamGType | `GParamVariant | `GObject | `TestObject | `DerivedObject | `GVariant | `FooShadow1 | `FooShadow2 +Entering test_object_get_type -Leaving test_object_get_type +Entering test_object_init ++Entering test_object_get_type --Leaving test_object_get_type -Leaving test_object_init MAIN: emit test-signal: TestObject-Message: ::test_signal default_handler called +Entering test_object_test_signal ++Entering test_iface_get_type --Leaving test_iface_get_type -Leaving test_object_test_signal +Entering test_signal_accumulator -Leaving test_signal_accumulator TestObject-Message: ::test_signal default_handler called +Entering test_object_test_signal ++Entering test_iface_get_type --Leaving test_iface_get_type -Leaving test_object_test_signal +Entering test_signal_accumulator -Leaving test_signal_accumulator TestObject-Message: ::test_signal default_handler called +Entering test_object_test_signal ++Entering test_iface_get_type --Leaving test_iface_get_type -Leaving test_object_test_signal TestObject-Message: signal return: "<default_handler><default_handler>" MAIN: call iface print-string on test and derived object: +Entering iface_print_string ++Entering test_iface_get_type --Leaving test_iface_get_type ++Entering test_iface_get_type --Leaving test_iface_get_type ++Entering print_foo Iface-FOO: "iface-string-from-test-type" from 0x9370a00 --Leaving print_foo -Leaving iface_print_string +Entering iface_print_string ++Entering test_iface_get_type --Leaving test_iface_get_type ++Entering test_iface_get_type --Leaving test_iface_get_type ++Entering print_bar +++Entering test_iface_get_type ---Leaving test_iface_get_type Iface-BAR: "iface-string-from-derived-type" from 0x936aaf0 chaining: +++Entering test_iface_get_type ---Leaving test_iface_get_type +++Entering print_foo Iface-FOO: "iface-string-from-derived-type" from 0x936aaf0 ---Leaving print_foo --Leaving print_bar -Leaving iface_print_string +Entering test_object_get_type -Leaving test_object_get_type private data after initialization: 54321 == 54321 Leaving main
2015年10月10日 13:02
我也正在研究gstreamer和glib,看到你的文章,有很多问题我也或自然开朗,请加我QQ:317579780,微信号也是这个,请版主作者加我,一起学习,一下进步。