summaryrefslogtreecommitdiff
path: root/plugingui/signal.cc
blob: 5ba145e9f9302a7daebe4f0a26fca50bcee41943 (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#include <iostream>
#include <functional>
#include <vector>
#include <map>
#include <set>
#include <type_traits>
#include <utility>

/////////////////////////////////////////
//
// Listener and Notifier classes:
//
/////////////////////////////////////////

class Listener;
class NotifierBase {
public:
	virtual void disconnect(Listener* object) {}
};

class Listener {
public:
	virtual ~Listener()
	{
		for (auto signal = signals.begin(); signal != signals.end(); ++signal) {
			(*signal)->disconnect(this);
		}
	}

	void registerNotifier(NotifierBase* signal)
	{
		signals.insert(signal);
	}

	void unregisterNotifier(NotifierBase* signal)
	{
		signals.erase(signal);
	}

	std::set<NotifierBase*> signals;
};

template<typename T, typename... Args>
class Notifier : public NotifierBase {
public:
	Notifier() {}
	~Notifier()
	{
		for (auto slot = slots.begin(); slot != slots.end(); ++slot) {
			(*slot).first->unregisterNotifier(this);
		}
	}

	void connect(Listener* object, std::function<void(T, Args...)> slot)
	{
		slots[object] = slot;
		if(object) {
			object->registerNotifier(this);
		}
	}

	void disconnect(Listener* object)
	{
		slots.erase(object);
	}

	void notify(T t, Args...args)
	{
		for (auto slot = slots.begin(); slot != slots.end(); ++slot) {
			(*slot).second(t, args...);
		}
	}

	std::map<Listener*, std::function<void(T, Args...)>> slots;
};

/////////////////////////////////////////
//
// Code which I don't understand but does something clever and produces a compile warning:
//
/////////////////////////////////////////

template<unsigned... Is> struct seq{};
template<unsigned I, unsigned... Is>
struct gen_seq : gen_seq<I-1, I-1, Is...>{};
template<unsigned... Is>
struct gen_seq<0, Is...> : seq<Is...>{};

template<unsigned I> struct placeholder{};

namespace std{
template<unsigned I>
struct is_placeholder< ::placeholder<I> > : integral_constant<int, I>{};
} // std::

namespace aux{
template<unsigned... Is, class F, class... Ts>
auto easy_bind(seq<Is...>, F&& f, Ts&&... vs)
	-> decltype(std::bind(std::forward<F>(f), std::forward<Ts>(vs)..., ::placeholder<1 + Is>()...))
{
    return std::bind(std::forward<F>(f), std::forward<Ts>(vs)..., ::placeholder<1 + Is>()...);
}
} // aux::

template<class R, class C, class... FArgs, class... Args>
auto mem_bind(R (C::*ptmf)(FArgs...), Args&&... vs)
	-> decltype(aux::easy_bind(gen_seq<(sizeof...(FArgs) + 1) - sizeof...(Args)>(), ptmf, std::forward<Args>(vs)...))
{
    // the +1s for 'this' argument
    static_assert(sizeof...(Args) <= sizeof...(FArgs) + 1, "too many arguments to mem_bind");
    return aux::easy_bind(gen_seq<(sizeof...(FArgs) + 1) - sizeof...(Args)>(), ptmf, std::forward<Args>(vs)...);
}

template<class T, class C, class... Args>
auto mem_bind(T C::*ptmd, Args&&... vs)
	-> decltype(aux::easy_bind(gen_seq<1 - sizeof...(Args)>(), ptmd, std::forward<Args>(vs)...))
{
    // just 'this' argument
    static_assert(sizeof...(Args) <= 1, "too many arguments to mem_bind");
    return aux::easy_bind(gen_seq<1 - sizeof...(Args)>(), ptmd, std::forward<Args>(vs)...);
}



/////////////////////////////////////////
//
//  Example:
//
/////////////////////////////////////////

class MyObject : public Listener {
public:
	void onMouseMoved(int x, int y)
	{
		std::cout << __PRETTY_FUNCTION__ << std::endl;
		std::cout << "Mouse moved: " << x << "," << y << std::endl;
	}
};

class MyObject2 : public Listener {
public:
	void mouseMoved(int x, int y)
	{
		std::cout << __PRETTY_FUNCTION__ << std::endl;
		std::cout << "Mouse moved: " << x << "," << y << std::endl;
	}
};

class Button {
public:
	Notifier<int, int> mouseMove; // arguments: (int x, int y)

	void emulateMouseMove(int x, int y)
	{
		mouseMove.notify(x, y);
	}
};


#define CONN(O, M) &O, mem_bind(&decltype(O)::M, O)
#define obj_connect(SRC, SIG, TAR, SLO) SRC.SIG.connect(&TAR, mem_bind(&decltype(TAR)::SLO, TAR))
#define fun_connect(SRC, SIG, SLO) SRC.SIG.connect(nullptr, SLO)

int main()
{
	Button btn;

	MyObject object;
	//btn.mouseMove.connect(&object, std::bind(&MyObject::onMouseMoved, std::ref(object), _1, _2 )); // 'Vanilla' interface
	//btn.mouseMove.connect(&object, std::bind(&decltype::onMouseMoved, std::ref(object), _1, _2 )); // Use decltype
	//btn.mouseMove.connect(&object, mem_bind(&decltype(object)::onMouseMoved, object)); // Use clever mem_bind construct
	btn.mouseMove.connect(CONN(object, onMouseMoved)); // Use convenience macro

	obj_connect(btn, mouseMove, object, onMouseMoved);
	fun_connect(btn, mouseMove, [](int x, int y) {
			std::cout << __PRETTY_FUNCTION__ << std::endl;
			std::cout << x << " x " << y << std::endl;
		} );

	MyObject2 object2;
	btn.mouseMove.connect(CONN(object2, mouseMoved));

	MyObject2* pobject2 = new MyObject2();
	btn.mouseMove.connect(pobject2, mem_bind(&decltype(std::remove_reference(*pobject2))::mouseMoved, std::ref(*pobject2)));


	// Now trigger the notification
	btn.emulateMouseMove(10,10);
}












































































#if 0

#include <iostream>
#include <functional>
#include <vector>
#include <map>
#include <set>
#include <stdio.h>

template<typename T>
class SignalBase {
public:
	virtual T emit(T a) { return a; } 
	T slot(SignalBase* o, T a) { return o->emit(a); }
};

template<typename T>
class Signal : public SignalBase<T> {
public:
	typedef SignalBase<T>* signal_ptr_t;
		
	static T do_emit(signal_ptr_t o1, signal_ptr_t o2, T arg)
	{
		std::cout << __PRETTY_FUNCTION__ << "\n";
		//	printf("%p %p\n", (void*)o1, (void*)o2);
		return o1->slot(o2, arg);
	}

	using bound_emit_t =
		decltype(std::bind(do_emit, signal_ptr_t(), signal_ptr_t(), std::placeholders::_1));

	using mem_t = decltype()


	void connect(Signal<T>& o)
	{	
		connection_map[&o].emplace_back(do_emit, this, &o, std::placeholders::_1);

		if (o.connectee_objects.find(this) == o.connectee_objects.end()) {
			o.connectee_objects.insert(this);
		}
	}

	void disconnect(Signal<T>& o)
	{
		if (connection_map.find(&o) != connection_map.end()) {
			connection_map[&o].clear();
			connection_map.erase(&o);
		}

		if (o.connectee_objects.find(this) != o.connectee_objects.end()) {
			connectee_objects.erase(this);
		}
	}

	void send(T a)
	{
		std::cout << "send: " << a << std::endl;
		for (auto& slot : connection_map) {
			for (auto& conn : slot.second) {
				std::cout << conn(a) << std::endl;
			}
		}
	} 

	virtual ~Signal()
	{
		// Iterate all objects to which this is connected and disconnect from them:
		for (auto& c : connectee_objects) {
			c->disconnect(*this);
		}

		// Iterate all objects connected to this and disconnect them:
		for (auto& kv : connection_map) {
			disconnect(*kv.first);
		}
	}
	
	std::map<Signal<T>*, std::vector<bound_emit_t>> connection_map;
	std::set<Signal<T>*> connectee_objects; // Signals to which this is connected
};

/////////////////////////////////////////////////////////////////////////////////////

template<typename T>
class Signal2 : public Signal<T> {
public:
	Signal2(T t) : t(t) {}
	virtual T emit(T a) override { return a + t;} 
	T t;
};

int main()
{

	std::cout << std::endl << "First:" << std::endl;
	{
		Signal<int> o1;

		{
			Signal2<int> o2(1);
			Signal2<int> o3(1);
			
			o1.connect(o1);
			o1.connect(o2);
			o1.connect(o3);
			
			o1.send(4);
			
			o1.disconnect(o2);
			
			o1.send(5);
		} // implicit o1.disconnect(o3) in o3 destructor.
		
		o1.send(6);
	}

	std::cout << std::endl << "Second:" << std::endl;
	{
		Signal<float>* o = new Signal<float>();
		Signal<float>& o1 = *o;

		{
			Signal2<float> o2(1);
			Signal2<float> o3(1);
			
			o1.connect(o1);
			o1.connect(o2);
			o1.connect(o3);
			
			o1.send(4.1);
			
			delete o;
			std::cout << std::endl << " -- post delete:" << std::endl;

			o1.send(4.2);

		} // implicit o1.disconnect(o3) in o3 destructor.
	}

	std::cout << std::endl << "Third:" << std::endl;
	{
		Signal<std::string> o1;

		{
			Signal2<std::string> o2("a");
			Signal2<std::string> o3("b");
			
			o1.connect(o1);
			o1.connect(o2);
			o1.connect(o3);
			
			o1.send("hello");
			
			o1.disconnect(o2);
			
			o1.send("world");
		} // implicit o1.disconnect(o3) in o3 destructor.
		
		o1.send("nisse");
	}

	return 0;
}

#endif

/////////////////////////////////////////////////////////
// What I would like(tm)
/////////////////////////////////////////////////////////
/*

class Foo {
public:
	Signal<int, float, std::string> changeSignal;

	void worker()
	{
		somethingChangedSignal.emit(42, 1.234, "hello");
	}
};

class Bar {
public:
	Bar(Foo &foo)
	{
		foo.changeSignal.connect(this, &Bar::notifyCallback);
	}

	void notifyCallback(int a, float b, std::string c)
	{
		// ...
	}
};

class Bas {
public:
	void iWannaKnow(int a, float b, std::string c)
	{
		// ...
	}
};

int main()
{
	Foo foo;
	Bar bar(foo);
	Bas bas;
	foo.changeSignal.connect(&bas, &Bas::iWannaKnow);
	foo.worker();
}

*/