~
[scx.git] / include / Signal.hpp
blob5177845db4dd969521727b76e465d1918e0535ef
1 #ifndef SCX_SIGNAL_HPP
2 #define SCX_SIGNAL_HPP
4 #include <vector>
5 #include "Function.hpp"
7 namespace scx {
9 /* Signal class, not support copy now!! */
10 template<typename signature>
11 class Signal;
13 #define SCX_SIGNAL_COPY_SIGNAL_COMMON \
14 public: \
15 ~Signal() \
16 { \
17 for (size_t i = 0; i < m_Slots.size(); ++i) { \
18 delete m_Slots[i]; \
19 } \
20 } \
22 template<typename fn_t, typename pv_t> \
23 const Function_t* Connect(fn_t fn, pv_t pv) \
24 { \
25 Function_t* pslot = new Function_t(fn, pv); \
26 m_Slots.push_back(pslot); \
27 return pslot; \
28 } \
30 template<typename fn_t> \
31 const Function_t* Connect(fn_t fn) \
32 { \
33 Function_t* pslot = new Function_t(fn); \
34 m_Slots.push_back(pslot); \
35 return pslot; \
36 } \
38 void Connect(Signal* sig) \
39 { \
40 m_Signals.push_back(sig); \
41 } \
43 void Disconnect(const Function_t* fn) \
44 { \
45 for (size_t i = 0; i < m_Slots.size(); ++i) { \
46 if (m_Slots[i] == fn) { \
47 delete fn; \
48 m_Slots.erase(m_Slots.begin() + i); \
49 break; \
50 } \
51 } \
52 } \
54 void Disconnect(const Signal* sig) \
55 { \
56 for (size_t i = 0; i < m_Signals.size(); ++i) { \
57 if (m_Signals[i] == sig) { \
58 m_Signals.erase(m_Signals.begin() + i); \
59 break; \
60 } \
61 } \
62 } \
64 template<typename recv_t> \
65 void DisconnectReceiver(recv_t* recv) \
66 { \
67 for (int i = m_Slots.size()-1; i >= 0; --i) { \
68 if ((m_Slots[i]->GetReceiver()) == recv) { \
69 delete m_Slots[i]; \
70 m_Slots.erase(m_Slots.begin() + i); \
71 } \
72 } \
73 } \
75 void Reserve(const size_t size) \
76 { \
77 m_Slots.Reserve(size); \
78 m_Signals.Reserve(size); \
79 } \
81 private: \
82 std::vector<Function_t*> m_Slots; \
83 std::vector<Signal*> m_Signals
85 template<typename ret_t, typename arg_t>
86 class Signal<ret_t (arg_t)>
88 typedef Function<ret_t (arg_t)> Function_t;
90 SCX_SIGNAL_COPY_SIGNAL_COMMON;
92 public:
93 void operator()(arg_t arg) const
95 for (size_t i = 0; i < m_Slots.size(); ++i) {
96 (*m_Slots[i])(arg);
99 for (size_t i = 0; i < m_Signals.size(); ++i) {
100 (*m_Signals[i])(arg);
105 template<typename ret_t>
106 class Signal<ret_t (void)>
108 typedef Function<ret_t (void)> Function_t;
110 SCX_SIGNAL_COPY_SIGNAL_COMMON;
112 public:
113 void operator()(void) const
115 for (size_t i = 0; i < m_Slots.size(); ++i) {
116 (*m_Slots[i])();
119 for (size_t i = 0; i < m_Signals.size(); ++i) {
120 (*m_Signals[i])();
125 template<typename ret_t,
126 typename arg1_t,
127 typename arg2_t>
128 class Signal<ret_t (arg1_t, arg2_t)>
130 typedef Function<ret_t (arg1_t, arg2_t)> Function_t;
132 SCX_SIGNAL_COPY_SIGNAL_COMMON;
134 public:
135 void operator()(arg1_t arg1, arg2_t arg2) const
137 for (size_t i = 0; i < m_Slots.size(); ++i) {
138 (*m_Slots[i])(arg1, arg2);
141 for (size_t i = 0; i < m_Signals.size(); ++i) {
142 (*m_Signals[i])(arg1, arg2);
147 template<typename ret_t,
148 typename arg1_t,
149 typename arg2_t,
150 typename arg3_t>
151 class Signal<ret_t (arg1_t, arg2_t, arg3_t)>
153 typedef Function<ret_t (arg1_t, arg2_t, arg3_t)> Function_t;
155 SCX_SIGNAL_COPY_SIGNAL_COMMON;
157 public:
158 void operator()(arg1_t arg1, arg2_t arg2, arg3_t arg3) const
160 for (size_t i = 0; i < m_Slots.size(); ++i) {
161 (*m_Slots[i])(arg1, arg2, arg3);
164 for (size_t i = 0; i < m_Signals.size(); ++i) {
165 (*m_Signals[i])(arg1, arg2, arg3);
170 template<typename ret_t,
171 typename arg1_t,
172 typename arg2_t,
173 typename arg3_t,
174 typename arg4_t>
175 class Signal<ret_t (arg1_t, arg2_t, arg3_t, arg4_t)>
177 typedef Function<ret_t (arg1_t, arg2_t, arg3_t, arg4_t)> Function_t;
179 SCX_SIGNAL_COPY_SIGNAL_COMMON;
181 public:
182 void operator()(arg1_t arg1, arg2_t arg2, arg3_t arg3, arg4_t arg4) const
184 for (size_t i = 0; i < m_Slots.size(); ++i) {
185 (*m_Slots[i])(arg1, arg2, arg3, arg4);
188 for (size_t i = 0; i < m_Signals.size(); ++i) {
189 (*m_Signals[i])(arg1, arg2, arg3, arg4);
194 template<typename ret_t,
195 typename arg1_t,
196 typename arg2_t,
197 typename arg3_t,
198 typename arg4_t,
199 typename arg5_t>
200 class Signal<ret_t (arg1_t, arg2_t, arg3_t, arg4_t, arg5_t)>
202 typedef Function<ret_t (arg1_t, arg2_t, arg3_t, arg4_t, arg5_t)> Function_t;
204 SCX_SIGNAL_COPY_SIGNAL_COMMON;
206 public:
207 void operator()(arg1_t arg1, arg2_t arg2, arg3_t arg3, arg4_t arg4, arg5_t arg5) const
209 for (size_t i = 0; i < m_Slots.size(); ++i) {
210 (*m_Slots[i])(arg1, arg2, arg3, arg4, arg5);
213 for (size_t i = 0; i < m_Signals.size(); ++i) {
214 (*m_Signals[i])(arg1, arg2, arg3, arg4, arg5);
219 #undef SCX_SIGNAL_COPY_SIGNAL_COMMON
223 #endif