函数

GObjectClosurenew_simple

声明 [源]

GClosure*
g_closure_new_simple (
  guint sizeof_closure,
  gpointer data
)

描述 [源]

为给定大小分配一个结构,并将初始部分初始化为GClosure

此函数在实现新类型的闭包时非常有用。

typedef struct _MyClosure MyClosure;
struct _MyClosure
{
  GClosure closure;
  // extra data goes here
};

static void
my_closure_finalize (gpointer  notify_data,
                     GClosure *closure)
{
  MyClosure *my_closure = (MyClosure *)closure;

  // free extra data here
}

MyClosure *my_closure_new (gpointer data)
{
  GClosure *closure;
  MyClosure *my_closure;

  closure = g_closure_new_simple (sizeof (MyClosure), data);
  my_closure = (MyClosure *) closure;

  // initialize extra data here

  g_closure_add_finalize_notifier (closure, notify_data,
                                   my_closure_finalize);
  return my_closure;
}

参数

sizeof_closure

类型: guint

分配的结构大小,必须至少为sizeof (GClosure)

data

类型: gpointer

存储在新分配的GClosure字段data中的数据。

参数可以为NULL
数据由函数的调用者拥有。

返回值

类型: GClosure

对新GClosure的浮动引用。

数据由被调用函数拥有。