https://github.com/libsigcplusplus/libsigcplusplus
cpp
#include <iostream>
/*
在sigslot.h的420,将:
//typedef sender_set::const_iterator const_iterator;
改为:
//typedef typename sender_set::const_iterator const_iterator;
#include <sigslot.h>
using namespace sigslot; //必须加上sigslot的命名空间
//在用vs调试时还需要将sigslot.h中很多的自定义模板结构类型前加typename
const int TTRUE = 1;
const int TFALSE = 0;
class Switch
{
public:
signal0<> Clicked;
//这里的信号是不带参数的,signaln表示带几个参数
};
class Light : public has_slots<>
{
public:
Light(bool state) { b_state = state; Displaystate(); }
void ToggleState() { b_state = !b_state; Displaystate(); } //作为消息的响应
void TurnOn() { b_state = TTRUE; Displaystate(); }
void TurnOff() { b_state = TFALSE; Displaystate(); }
void Displaystate() { std::cout << "The state is " << b_state << std::endl; }
private:
bool b_state;
};
void main()
{
Switch sw1, sw2, all_on, all_off;
Light lp1(TTRUE), lp2(TFALSE);
sw1.Clicked.connect(&lp1, &Light::ToggleState); //绑定
sw2.Clicked.connect(&lp2, &Light::ToggleState);
all_on.Clicked.connect(&lp1, &Light::TurnOn);
all_on.Clicked.connect(&lp2, &Light::TurnOn);
all_off.Clicked.connect(&lp1, &Light::TurnOff);
all_off.Clicked.connect(&lp2, &Light::TurnOff);
sw1.Clicked();
sw2.Clicked();
all_on.Clicked();
all_off.Clicked();
sw1.Clicked.disconnect(&lp1);
sw2.Clicked.disconnect(&lp2);
all_on.Clicked.disconnect(&lp1);
all_on.Clicked.disconnect(&lp2);
all_off.Clicked.disconnect(&lp1);
all_off.Clicked.disconnect(&lp2);
}
*/
#include <sigc++/sigc++.h>
class AlienDetector
{
public:
void run() { signal_detected.emit("test"); }
sigc::signal<void(std::string)> signal_detected; // changed
};
void warn_people(std::string where)
{
std::cout << "There are aliens in " << where << "!" << std::endl;
}
int main_callback()
{
AlienDetector mydetector;
auto target = mydetector.signal_detected.connect(sigc::ptr_fun(warn_people)); // return sigc::connection
mydetector.run();
target.disconnect();
return 0;
}