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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
#pragma once
#include <map>
#include <list>
#include <set>
#include <initializer_list>
#include "object.hpp"
#include "task.hpp"
#include "types.hpp"
#include <functional>
#include <memory>
#include "priority.hpp"
#include "labelled.hpp"
namespace flat
{
namespace core
{
class signal : virtual public labelled, virtual public prioritized
{
public:
object * sender;
package m_package;
priority_t prior;
signal( object * sender,
const std::string& id = "",
void * data = 0,
priority_t prior = priority_t::none);
/* Alias to flat::core::channel::emit() */
bool emit(const std::string& channel) const;
struct package
{
package(void *data) : data(data) {}
template<class T>
T * get() {
return dynamic_cast<T>(data);
}
void * data;
};
};
/* Channel class */
class channel : virtual public labelled
{
/* Post processing signal stacking */
queue<signal> stack;
/* Listeners list */
std::list<std::weak_ptr<listener>> listeners;
/* Synchronous task checking for signals */
task::ptr checker;
/* Channel mapping */
static std::map<std::string, std::weak_ptr<channel>> channels;
public:
channel(const std::string& id = "", priority_t task_priority = priority_t::none);
~channel();
void start();
bool map();
void emit(const signal&);
bool connect(listener::ptr l);
void disconnect(listener::ptr l);
static ptr find(const std::string&);
static ptr create(const string& id, priority_t prior);
void check_and_call();
typedef shared_ptr<channel> ptr;
};
/* Listener class */
class listener
{
std::list<std::string> filters;
bool check_in_filters(const std::string&) const;
callback_t m_callback;
public:
listener( callback_t m_callback,
const std::initializer_list<std::string>& filters = {});
~listener();
void add_filter(const std::string&);
void remove_filter(const std::string&);
bool connect(const std::string&);
bool disconnect(const std::string&);
void invoke(const signal&);
/* Allow to safely bind a functor */
template<class T>
static ptr create( const (T::*method(const object*, void*))& fct,
T* obj,
const std::initializer_list<std::string>& filters = {})
{
return new listener(std::bind(fct, ptr), filters);
}
typedef std::function<void(const object*, signal::package)> callback_t;
typedef shared_ptr<listener> ptr;
};
}
}
|