static GType gst_signal_object_get_type (void); 其实还有一个宏: G_DEFINE_ABSTRACT_TYPE (GstObject,gst_object, G_TYPE_OBJECT); |
#define G_DEFINE_ABSTRACT_TYPE(TN, t_n, T_P) G_DEFINE_TYPE_EXTENDED (TN, t_n, T_P, G_TYPE_FLAG_ABSTRACT, {}) #define G_DEFINE_TYPE_EXTENDED(TN, t_n, T_P, _f_, _C_) _G_DEFINE_TYPE_EXTENDED_BEGIN (TN, t_n, T_P, _f_) {_C_;} _G_DEFINE_TYPE_EXTENDED_END() |
把宏展开:
#define _G_DEFINE_TYPE_EXTENDED_BEGIN(GstObject, gst_object, G_TYPE_OBJECT, flags) \ \ static void gst_object_init (GstObject *self); \ static void gst_object_class_init (GstObjectClass *klass); \ static gpointer gst_object _parent_class = NULL; \ static void gst_object_class_intern_init (gpointer klass) \ { \ gst_object_parent_class = g_type_class_peek_parent (klass); \ gst_object_class_init ((GstObject Class*) klass); \ } \ \ GType \ gst_object_get_type (void) \ { \ static volatile gsize g_define_type_id__volatile = 0; \ if (g_once_init_enter (&g_define_type_id__volatile)) \ { \ GType g_define_type_id = \ g_type_register_static_simple (G_TYPE_OBJECT, \ g_intern_static_string (“GstObject”), \ sizeof (GstObjectClass), \ (GClassInitFunc)gst_object_class_intern_init, \ sizeof (GstObject), \ (GInstanceInitFunc)gst_object_init, \ (GTypeFlags) flags); \ { /* custom code follows */ #define _G_DEFINE_TYPE_EXTENDED_END() \ /* following custom code */ \ } \ g_once_init_leave (&g_define_type_id__volatile, g_define_type_id); \ } \ return g_define_type_id__volatile; \ } /* closes gst_object_get_type() */ |
static gboolean init_post (GOptionContext * context, GOptionGroup * group, gpointer data, GError ** error) { //…. g_type_class_ref (gst_object_get_type ()); //这个是非常重要的,里面执行和很多的东西 这个函数会调用前面定义的函数。 |
GType g_type_register_static_simple (GType parent_type, const gchar *type_name, guint class_size, GClassInitFunc class_init, guint instance_size, GInstanceInitFunc instance_init, GTypeFlags flags) { GTypeInfo info;
/* Instances are not allowed to be larger than this. If you have a big * fixed-length array or something, point to it instead. */ g_return_val_if_fail (class_size <= G_MAXUINT16, G_TYPE_INVALID); g_return_val_if_fail (instance_size <= G_MAXUINT16, G_TYPE_INVALID);
info.class_size = class_size; info.base_init = NULL; info.base_finalize = NULL; info.class_init = class_init; info.class_finalize = NULL; info.class_data = NULL; info.instance_size = instance_size; info.n_preallocs = 0; info.instance_init = instance_init; info.value_table = NULL;
return g_type_register_static (parent_type, type_name, &info, flags); } |
//给新类型,分配一块内存; //和父亲建立联系; //把定义的新类型的类函数/实例化函数,等都复制过去 GType g_type_register_static (GType parent_type, const gchar *type_name, const GTypeInfo *info, GTypeFlags flags) { TypeNode *pnode, *node; GType type = 0;
g_return_val_if_type_system_uninitialized (0); g_return_val_if_fail (parent_type > 0, 0); g_return_val_if_fail (type_name != NULL, 0); g_return_val_if_fail (info != NULL, 0);
if (!check_type_name_I (type_name) || !check_derivation_I (parent_type, type_name)) return 0; if (info->class_finalize) { g_warning ("class finalizer specified for static type `%s'", type_name); return 0; }
pnode = lookup_type_node_I (parent_type); G_WRITE_LOCK (&type_rw_lock); type_data_ref_Wm (pnode); if (check_type_info_I (pnode, NODE_FUNDAMENTAL_TYPE (pnode), type_name, info)) { node = type_node_new_W (pnode, type_name, NULL); type_add_flags_W (node, flags); type = NODE_TYPE (node); type_data_make_W (node, info, check_value_table_I (type_name, info->value_table) ? info->value_table : NULL); } G_WRITE_UNLOCK (&type_rw_lock);
return type; } |
所以gst_object_get_type(),就是获取一个新类型,并且把定义的一些函数指针赋值过去。
下面看另外一个重要的函数:
g_type_class_ref
gpointer g_type_class_ref (GType type) { TypeNode *node; GType ptype; gboolean holds_ref; GTypeClass *pclass;
/* optimize for common code path */ node = lookup_type_node_I (type); if (!node || !node->is_classed) { g_warning ("cannot retrieve class for invalid (unclassed) type `%s'", type_descriptive_name_I (type)); return NULL; }
if (G_LIKELY (type_data_ref_U (node))) { if (G_LIKELY (g_atomic_int_get (&node->data->class.init_state) == INITIALIZED)) return node->data->class.class; holds_ref = TRUE; } else holds_ref = FALSE;
/* here, we either have node->data->class.class == NULL, or a recursive * call to g_type_class_ref() with a partly initialized class, or * node->data->class.init_state == INITIALIZED, because any * concurrently running initialization was guarded by class_init_rec_mutex. */ g_rec_mutex_lock (&class_init_rec_mutex); /* required locking order: 1) class_init_rec_mutex, 2) type_rw_lock */
/* we need an initialized parent class for initializing derived classes */ ptype = NODE_PARENT_TYPE (node); pclass = ptype ? g_type_class_ref (ptype) : NULL;//如果父亲的类没有初始化,先递归处理父亲,祖父等的事情
G_WRITE_LOCK (&type_rw_lock);
if (!holds_ref) type_data_ref_Wm (node);
if (!node->data->class.class) /* class uninitialized */ type_class_init_Wm (node, pclass);//这个地方会调用gst_object_class_init,包括可能递归调用父类的类初始化函数
G_WRITE_UNLOCK (&type_rw_lock);
if (pclass) g_type_class_unref (pclass);
g_rec_mutex_unlock (&class_init_rec_mutex);
return node->data->class.class; } |