gobject
文章目录
@[toc] ()
GObject
- 创建两个结构体
typedef struct _PMDList PMDList;
struct _PMDList {
GObject parent_instance;
PMDListNode *head;
PMDListNode *tail;
};
typedef struct _PMDListClass PMDListClass;
struct _PMDListClass {
GObjectClass parent_class;
};
-
创建三个函数
2.1 **_get_type 函数
2.2 类初始化函数
2.3 对象初始化函数
- 初始化只是赋值,不是malloc
- get_type函数可以通过宏定义自动创建,如
G_DEFINE_TYPE (PMDList, pm_dlist, G_TYPE_OBJECT);其内部将
- 使用对象
3.1
g_type_init ();/ GObject 库的类型管理系统的初始化 /
3.2list = g_object_new (PM_TYPE_DLIST, NULL);//注意这里面两个函数,第一个参数是函数的返回值,内部会进行一次函数注册,并返回一个GType类型的对象。在glib的世界中,GType是通行证。
3.3g_object_unref (list);//手动给计数器-1
#ifndef PM_DLIST_H
#define PM_DLIST_H
#include <glib-object.h>
#define PM_TYPE_DLIST (pm_dlist_get_type ())
typedef struct _PMDListNode PMDListNode;
struct _PMDListNode {
PMDListNode *prev;
PMDListNode *next;
void *data;
};
typedef struct _PMDList PMDList;
struct _PMDList {
GObject parent_instance;
PMDListNode *head;
PMDListNode *tail;
};
typedef struct _PMDListClass PMDListClass;
struct _PMDListClass {
GObjectClass parent_class;
};
GType pm_dlist_get_type (void);
#endif
#include "pm-dlist.h"
G_DEFINE_TYPE (PMDList, pm_dlist, G_TYPE_OBJECT);
static
void pm_dlist_init (PMDList *self)
{
g_printf ("\tobject init\n");
self->head = NULL;
self->tail = NULL;
}
static
void pm_dlist_class_init (PMDListClass *klass)
{
g_printf ("class init\n");
}
#include "pm-dlist.h"
int main (void)
{
/* GObject 库的类型管理系统的初始化 */
g_type_init ();
int i;
PMDList *list;
/* 进行三次对象实例化 */
for (i = 0; i < 3; i++){
list = g_object_new (PM_TYPE_DLIST, NULL);
g_object_unref (list);
}
/* 检查实例是否为 GObject 对象 */
list = g_object_new (PM_TYPE_DLIST, NULL);
if (G_IS_OBJECT (list))
g_printf ("\t这个实例是一个 GObject 对象!\n");
return 0;
}
glib
gloop
The creation of a new source requires us to define at least 3 functions:
- prepare(): Called before all the file descriptors are polled. If the source can determine that it is ready here (without waiting for the results of the poll() call), it should return TRUE. It can also return a time-out value which should be the maximum time-out (in milliseconds) which should be passed to the poll() call. The actual time-out used will be -1 if all sources returned -1, or it will be the minimum of all the timeout_values returned which were >= 0
-
check(): Called after all the file descriptors are polled. The source should return TRUE if it is ready to be dispatched.
Time may have passed since the previous prepare function was called, so the source should be checked again.
- dispatch() Called to dispatch the event source after it has returned TRUE in either its prepare or check function. The dispatch function is passed in a callback function and data. The callback function may be NULL if the source was never connected to a callback using g_source_set_callback(). The dispatch function should call the callback function with user_data and the additional parameters that are needed for this type of event source.
- prepare 中的timeout测试发现是 prepare到check的时间,check完之后会立即进入prepare
- check与prepare只要一个返回TRUE,dispatch 函数就执行
#include <glib.h>
gboolean callback(gpointer data)
{
static int i = 0;
i++;
g_print ("timeout_callback called %d times\n",i);
if (10 == i)
{
g_main_loop_quit((GMainLoop*)data);
return FALSE;
}
return TRUE;
}
gboolean prepare(GSource *source,gint *timeout_)
{
*timeout_ = -1;
return TRUE;
}
gboolean check(GSource *source)
{
return TRUE;
}
gboolean dispatch(GSource *source,GSourceFunc callback,gpointer user_data)
{
if (callback(user_data))
return TRUE;
else
return FALSE;
}
int main()
{
GMainLoop *loop = NULL;
GMainContext *context;
GSource *source;
int id;
//create a variable of type GSourceFuncs
GSourceFuncs SourceFuncs =
{
prepare,
check,
dispatch,
NULL
};
//create a new source
source = g_source_new (&SourceFuncs, sizeof(GSource));
//create a context
context = g_main_context_new ();
//attach source to context
id = g_source_attach (source,context);
//create a main loop with context
loop = g_main_loop_new (context,FALSE);
//set the callback for this source
g_source_set_callback (source,callback,loop,NULL);
g_main_loop_run (loop);
g_main_loop_unref (loop);
return 0;
}
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论