当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
1 Star 1 Fork 52

捡捡/giveda_gobject_cpp11
暂停

forked from 类似Qt/信号槽 v11 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.cpp 2.61 KB
一键复制 编辑 原始数据 按行查看 历史
明心 提交于 2019-01-16 17:30 . 增加object destroy信号
/*
* Copyright (C) 2019 明心 <imleizhang@qq.com>
* All rights reserved.
*
* This program is an open-source software; and it is distributed in the hope
* that it will be useful, but WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE.
* This program is not a free software; so you can not redistribute it and/or
* modify it without my authorization. If you only need use it for personal
* study purpose(no redistribution, and without any commercial behavior),
* you should accept and follow the GNU AGPL v3 license, otherwise there
* will be your's credit and legal risks. And if you need use it for any
* commercial purpose, you should first get commercial authorization from
* me, otherwise there will be your's credit and legal risks.
*
*/
#include <gObject.h>
#include <string>
/**
* @mainpage 基于c++11实现的信号和槽
* @author 明心(imleizhang\@qq.com)
* @version 1.0.0
* @date 2018-12-27
*/
/**
* @class Receiver
* @brief Receiver类负责接收信号;并进行业务处理。
*/
class Receiver : public GObject
{
public:
Receiver ( )
{}
public slots:
/**
* @brief 接收sender发射的selected信号、并进行业务处理
*
* @param const string& selected信号传递过来的参数
* @param int selected信号传递过来的参数
* @return void
*/
void slotSelected( const string& str, int idx)
{
printf("param=[%s, %d]\n", str.c_str(), idx );
}
};
/**
* @class Sender
* @brief Sender类负责定义信号;并负责在需要时,发射信号。
*/
class Sender : public GObject
{
public:
/**
* 定义一个名称为selected的信号;该信号接收两个参数,参数类型分别为const string&和int
*/
GSignal<const string&, int> selected;
public:
/**
* @brief 发射信号
*
* @return void
*/
void notify()
{
string a("giveda.com");
selected(a, 3);
a = "Hello, Giveda!";
selected.emit(a, 6);
}
};
int main(int /*argc*/, char** /*argv*/)
{
Sender *s = new Sender;
s->notify();
printf("before connect\n");
Receiver *r = new Receiver;
GObject::connect(s, s->selected, r, &Receiver::slotSelected);
printf("after connect\n");
s->notify();
GObject::disconnect(s, s->selected, r, &Receiver::slotSelected);
printf("after disconnect\n");
s->notify();
printf("re-connected, but delete receiver\n");
GObject::connect(s, s->selected, r, &Receiver::slotSelected);
delete r;
s->notify();
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/jian_le_ge_u_pan/giveda_gobject_cpp11.git
git@gitee.com:jian_le_ge_u_pan/giveda_gobject_cpp11.git
jian_le_ge_u_pan
giveda_gobject_cpp11
giveda_gobject_cpp11
master

搜索帮助