1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
#include <cstdio>
#include <string>
// force single threaded sigslot
#define SIGSLOT_PURE_ISO 1
#include "sigslot.h"
using sigslot::signal;
#define __signals
#define __slots
#define DBG(FMT,...) printf("%s: " FMT "\n", __PRETTY_FUNCTION__, ##__VA_ARGS__)
class Foo: public sigslot::has_slots<>, public sigslot::has_signals<Foo>
{
public:
Foo()
{
connect(signalIntEmitted, &Foo::handleIntEmission);
connect(signalSomethingHappened, &Foo::handleSomething);
// above is just some template-sugar for these calls
//connect(signalIntEmitted, this, &Foo::handleIntEmission);
//connect(signalSomethingHappened, this, &Foo::handleSomething);
}
void doStuff()
{
DBG("");
signalIntEmitted(42);
}
public __slots:
void handleIntEmission(int i)
{
DBG("i = %d", i);
signalSomethingHappened();
}
void handleSomething()
{
DBG("");
}
public __signals:
signal<int> signalIntEmitted;
signal<> signalSomethingHappened;
signal<std::string &> signalStr;
};
class Bar: public sigslot::has_slots<>
{
public:
Bar(Foo *foo) :
signalSig(foo->signalIntEmitted)
{
foo->signalIntEmitted.connect(this, &Bar::handleIntEmission);
foo->signalStr.connect(this, &Bar::handleStr);
}
public __slots:
void handleIntEmission(int i)
{
DBG("i = %d", i);
}
void handleStr(std::string &str)
{
DBG("str = %s", str.c_str());
str += ", World!";
}
public __signals:
signal<int> signalSig;
};
int main()
{
Foo foo;
Bar bar(&foo);
foo.doStuff();
std::string str = "Hello";
foo.signalStr(str);
DBG("str = %s", str.c_str());
printf("\n");
bar.signalSig.SIGSLOT_EMIT(1);
return 0;
}
|