A GObject may emit signals to show that something have happened. Signals can be connected to many "handlers" which are called when the signals are emitted.
Full code:
/* A subclass of GObject with a property. */
#include <stdio.h>
#include <glib-object.h>
typedef struct {
GObject something_as_boilerplate;
char *name;
} myinstance_t;
typedef struct {
GObjectClass something_as_boilerplate;
} myclass_t;
GType get_my_typeid();
void my_instance_init_func(myinstance_t *instance, gpointer data) {
instance->name = "AnInstance";
}
void my_class_init_func(myclass_t* klass, gpointer data) {
int signal_id = g_signal_new(
"my-signal",
get_my_typeid(),
G_SIGNAL_RUN_LAST,
0,
NULL,
NULL,
g_cclosure_marshal_VOID__STRING,
G_TYPE_NONE,
1,
G_TYPE_STRING,
NULL
);
}
GType get_my_typeid() {
static my_type_id = 0;
if(my_type_id==0) {
GTypeInfo my_type_info = {
sizeof(myclass_t), //class_size;
NULL, //base_init;
NULL, //base_finalize;
/* classed types, instantiated types */
(GClassInitFunc)my_class_init_func, //class_init;
NULL, //class_finalize;
NULL, //class_data;
/* instantiated types */
sizeof(myinstance_t),//instance_size;
0, //n_preallocs;
(GInstanceInitFunc)my_instance_init_func, //instance_init;
/* value handling */
NULL, //value_table;
};
my_type_id = g_type_register_static(
G_TYPE_OBJECT,
"MyClass",
&my_type_info,
0
);
}
return my_type_id;
}
void hello(GObject *sender, char* name, gpointer data) {
printf("Hello world, [%s]!\n",name);
}
void goodbye(GObject *sender, char* name, gpointer data) {
myinstance_t *instance = G_TYPE_CHECK_INSTANCE_CAST(
sender,get_my_typeid(),myinstance_t);
printf("Goodbye world, [%s]! Message from [%s]. data is [%d]\n",
name, instance->name, *(int*)data);
}
int main() {
g_type_init();
printf("Type id: %d\n",get_my_typeid());
printf("Type name: %s\n",g_type_name(get_my_typeid()));
GValue *val = g_new0(GValue,1);
// the first object
myinstance_t *instance = (myinstance_t*)g_object_new(get_my_typeid(),NULL);
int tmpvar = 42;
g_signal_connect(instance,"my-signal",G_CALLBACK(hello),NULL);
g_signal_connect(instance,"my-signal",G_CALLBACK(goodbye),&tmpvar);
g_signal_emit_by_name(instance,"my-signal","cloverprince");
g_object_unref(instance);
g_free(val);
return 0;
}
A signal is created using g_signal_new.
void my_class_init_func(myclass_t* klass, gpointer data) {
int signal_id = g_signal_new(
"my-signal",
get_my_typeid(),
G_SIGNAL_RUN_LAST,
0,
NULL,
NULL,
g_cclosure_marshal_VOID__STRING,
G_TYPE_NONE,
1,
G_TYPE_STRING,
NULL
);
}
You need a name for it. It should be a combination of lower-case letters and hyphens ('-'). You also need to specify the class it associtated to. Then you tell it the type of the parameters as well as the return value of the signal handler.
A GSignalCMarshaller is required. It is specific to each combination of parameter types and return-value type. There are many such marshaller functions provided by default. If there is not one for yours, you can create one with the glib-genmarshal command-line utility.
Signals are connected to functions with g_signal_connect and emitted with g_signal_emit or g_signal_emit_by_name.
Note that the first parameter of each signal handler function is always a GObject pointer pointing to the object which emitted the signal. The last parameter is the custom data provided in g_signal_connedt. The parameters in between are those specified in g_signal_new.
分享到:
相关推荐
利用本软件,c++开发者可以在不依赖c++编译器之外的任何工具的前提下,实现c++中类与类之间的解耦合(class A对象与class B对象之间的解耦合)。 本软件支持c++11并向后兼容(支持c++11及其后续版本)。 开发环境为...
本示例用于演示GObject创建新类,类的继承与重载。代码实现了shape和square类,继承关系为: square -> shape ->gobject 其中,square重载了shape类的info接口。
The GObject base class Object instanciation Object memory management Reference count Weak References Reference counts and cycles Object properties Accessing multiple properties at once The ...
UltiSnips C 片段有助于编写 GObject 代码 安装 这些片段使用 UltiSnips ( ) 所以你应该先安装和配置它。 我使用 Vundle ( ) 来管理 vim 插件,但这不是强制性的。 如果你使用 Vundle,你可以添加到你的 ~/.vimrc ...
crystal-gobject:Crystal的gobject-introspection
Gobject 离线API手册
Gobject 资料,全面介绍Gobject 资料,全面介绍Gobject 资料,全面介绍Gobject 资料,全面介绍Gobject 资料,全面介绍Gobject 资料,全面介绍Gobject 资料,全面介绍
Groonga GObject 描述 Groonga的GObject包装器。 去做... 安装 Debian GNU / Linux 。 运行以下命令: % sudo apt-get install -V -y gir1.2-groonga libgroonga-gobject0 的Ubuntu 。 运行以下命令: % ...
gtkforphp gobject扩展为GObject功能提供了语言绑定。 有关该库的文档和信息可以在找到 现在,这只不过是一个占位符。 您可以发送评论,补丁,问题 这仍然是实验性的扩展。 安装/配置 此扩展需要gtkforphp / glib...
对于学习glib很有用的资源啊,gobject很有用,多多支持~
码-对象反省码插件,用于基于GObject-Intropection构建库文档。要求Ruby / GObject-Introspection尝试一下 git clone ...
离线安装包,亲测可用
gir_ffi, 在运行时使用 FFI,为基于GObject的库自动生成绑定 GirFFI由 Matijs van Zuijlen描述使用GObject内省存储库的GNOME的ruby 绑定。状态 特性为基于任何gobject的库创建绑定。在运行时生成绑定。为选定方法...
本示例演示Gobject类的创建、类属性设置、实例接口重载、类接口重载。带Makefile,可直接编译运行。
avahi-gobject-0.6.25-11.el6.i686.rpm是centos工具包。
离线安装包,亲测可用
液化天然气LGI是基于gobject内省的动态Lua绑定到基于GObject的库。 它允许直接从Lua使用基于GObject的库。 已获得许可的许可,请参见LICENSE文件全文。 该项目的主页位于。 LGI经过测试,并与标准Lua 5.1,Lua 5.2,...
离线安装包,亲测可用