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
104
105
106
107
108
109
110
111
112
113
114
|
#include "core/signal.hpp"
#include "core/task.hpp"
#include "object.hpp"
#include "window.hpp"
#include "flatland.hpp"
#include "exceptions/forcequit.hpp"
#include <iostream>
using namespace std;
using namespace flat;
using namespace flat::core;
class sender : public object
{
const char * message;
channel::ptr chan;
public:
sender(const char * message, channel::ptr chan) : message(message), chan(chan)
{
}
void send()
{
signal msg(this, "", (void*)message);
chan->emit(msg);
}
};
void function_listener(const object*, core::signal::package msg)
{
cout << "Funzione: " << msg.get<const char>() << endl;
}
class c_listener
{
listener::ptr lis;
public:
c_listener(channel::ptr chan)
{
lis = chan->connect(&c_listener::method_listener, *this);
}
void method_listener(const object *o, signal::package msg)
{
cout << "Metodo" << msg.get<const char>() << endl;
}
};
/* Objects definition */
channel::ptr alpha;
sender * m_sender;
c_listener * m_listener;
listener::ptr f_listener;
int steps = 0;
void lifeloop()
{
if (!(steps % 10))
cout << "Step: " << steps << endl;
if (!(steps % 40))
m_sender->send();
if (++steps > 2000)
{
signal quit(0, "quit");
// quit request
flat::core_channel().emit(quit);
}
if (steps > 2100)
throw flat::ForceQuit("Too many steps");
}
int main()
{
FlatWindow win(600, 900, "Test 3");
flat_status status;
alpha = channel::create("alpha");
if (alpha == nullptr)
{
cout << "Could not create channel alpha" << endl;
return -1;
}
// create sender
m_sender = new sender("Ciao", alpha);
m_listener = new c_listener(alpha);
// Connect listener to alpha channel
f_listener = alpha->connect(&function_listener);
// bind counter task
task::ptr looptask = flat::main_job().delegate_task(lifeloop);
init_flatland(&win, status, 60);
delete m_sender;
delete m_listener;
return 0;
}
|