-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
90 lines (71 loc) · 1.55 KB
/
main.cpp
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
#include <mw/signal.h>
#include <iostream>
enum class GameEvent {
GameOver,
Walk
};
class Zombie {
public:
Zombie() = default;
Zombie(Zombie&& zombie) noexcept
: gameEventUpdated{std::move(zombie.gameEventUpdated)}
, pointsUpdated{std::move(zombie.pointsUpdated)} {
}
Zombie& operator=(Zombie&& zombie) noexcept {
gameEventUpdated = std::move(zombie.gameEventUpdated);
pointsUpdated = std::move(zombie.pointsUpdated);
return *this;
}
mw::PublicSignal<Zombie, GameEvent> gameEventUpdated;
mw::PublicSignal<Zombie, int> pointsUpdated;
void walk() {
++x_;
gameEventUpdated(GameEvent::Walk);
if (x_ == 2) {
++points_;
pointsUpdated(points_);
}
if (x_ == 3) {
++points_;
pointsUpdated(points_);
}
if (x_ == 5) {
gameEventUpdated(GameEvent::GameOver);
}
}
private:
int x_ = 0;
int points_ = 0;
};
void example() {
bool gameOver = false;
Zombie zombie;
mw::signals::ScopedConnections connections;
connections += {
zombie.gameEventUpdated.connect([&](GameEvent gameEvent) {
switch (gameEvent) {
case GameEvent::GameOver:
gameOver = true;
std::cout << "Game Over\n";
break;
case GameEvent::Walk:
std::cout << "Walking\n";
break;
}
}),
zombie.pointsUpdated.connect([&](int points) {
std::cout << "Points updated: " << points << "\n";
})
};
while (!gameOver) {
zombie.walk();
}
}
int main(int argc, char** argv) {
std::cout << "Example PublicSignal Zombie\n";
example();
// Test move constructor.
Zombie zombie;
Zombie zombie2 = std::move(zombie);
return 0;
}