aboutsummaryrefslogtreecommitdiff
path: root/src/signals.h
blob: 722f9285020ae342dc12aeb474857fa95c6c4d5d (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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
#ifndef BU_OBJECT_SIGNALS_H
#define BU_OBJECT_SIGNALS_H

#include "bu/util.h"

namespace Bu
{
	//
	// 0 Parameters
	//

	template<typename ret>
	class _Slot0
	{
	public:
		_Slot0() { }
		virtual ~_Slot0() { }

		virtual ret operator()()=0;

		virtual _Slot0<ret> *clone() const=0;
	};

	template<typename cls, typename ret>
	class __Slot0 : public _Slot0<ret>
	{
	public:
		__Slot0( cls *pCls, ret (cls::*pFnc)() ) :
			pCls( pCls ), pFnc( pFnc ) { }
		virtual ~__Slot0() { }

		virtual ret operator()()
		{
			return (pCls->*pFnc)();
		}
		
		virtual _Slot0<ret> *clone() const
		{
			return new __Slot0<cls, ret>( pCls, pFnc );
		}
	
	private:
		cls *pCls;
		ret (cls::*pFnc)();
	};

	template<typename ret>
	class __Slot0F : public _Slot0<ret>
	{
	public:
		__Slot0F( ret (*pFnc)() ) :
			pFnc( pFnc ) { }
		virtual ~__Slot0F() { }

		virtual ret operator()()
		{
			return (*pFnc)();
		}
		
		virtual _Slot0<ret> *clone() const
		{
			return new __Slot0F<ret>( pFnc );
		}
	
	private:
		ret (*pFnc)();
	};
	
	template<typename ret>
	class Signal0
	{
	public:
		Signal0() : pCb( NULL ) { }
		Signal0( _Slot0<ret> *pCb ) : pCb( pCb ) { }
		Signal0( const Signal0<ret> &rSrc ) :
			pCb( rSrc.pCb->clone() ) { }

		virtual ~Signal0() { delete pCb; pCb = NULL; }

		ret operator()()
		{
			return (*pCb)();
		}

	private:
		_Slot0<ret> *pCb;
	};
	
	template<typename cls, typename ret>
	Signal0<ret> slot( cls *pCls, ret (cls::*pFnc)() )
	{
		return Signal0<ret>(
			new __Slot0<cls, ret>( pCls, pFnc )
			);
	}
	
	template<typename ret>
	Signal0<ret> slot( ret (*pFnc)() )
	{
		return Signal0<ret>(
			new __Slot0F<ret>( pFnc )
			);
	}

	//
	// 1 Parameter
	//

	template<typename ret, typename p1t>
	class _Slot1
	{
	public:
		_Slot1() { }
		virtual ~_Slot1() { }

		virtual ret operator()( p1t p1 )=0;

		virtual _Slot1<ret, p1t> *clone() const=0;
	};

	template<typename cls, typename ret, typename p1t>
	class __Slot1 : public _Slot1<ret, p1t>
	{
	public:
		__Slot1( cls *pCls, ret (cls::*pFnc)( p1t ) ) :
			pCls( pCls ), pFnc( pFnc ) { }
		virtual ~__Slot1() { }

		virtual ret operator()( p1t p1 )
		{
			return (pCls->*pFnc)( p1 );
		}
		
		virtual _Slot1<ret, p1t> *clone() const
		{
			return new __Slot1<cls, ret, p1t>( pCls, pFnc );
		}
	
	private:
		cls *pCls;
		ret (cls::*pFnc)( p1t );
	};

	template<typename ret, typename p1t>
	class __Slot1F : public _Slot1<ret, p1t>
	{
	public:
		__Slot1F( ret (*pFnc)( p1t ) ) :
			pFnc( pFnc ) { }
		virtual ~__Slot1F() { }

		virtual ret operator()( p1t p1 )
		{
			return (*pFnc)( p1 );
		}
		
		virtual _Slot1<ret, p1t> *clone() const
		{
			return new __Slot1F<ret, p1t>( pFnc );
		}
	
	private:
		ret (*pFnc)( p1t p1 );
	};
	
	template<typename ret, typename p1t>
	class Signal1
	{
	public:
		Signal1() : pCb( NULL ) { }
		Signal1( _Slot1<ret, p1t> *pCb ) : pCb( pCb ) { }
		Signal1( const Signal1<ret, p1t> &rSrc ) :
			pCb( rSrc.pCb->clone() ) { }

		virtual ~Signal1() { delete pCb; pCb = NULL; }

		ret operator()( p1t p1 )
		{
			return (*pCb)( p1 );
		}

	private:
		_Slot1<ret, p1t> *pCb;
	};
	
	template<typename cls, typename ret, typename p1t>
	Signal1<ret, p1t> slot( cls *pCls, ret (cls::*pFnc)( p1t ) )
	{
		return Signal1<ret, p1t>(
			new __Slot1<cls, ret, p1t>( pCls, pFnc )
			);
	}
	
	template<typename ret, typename p1t>
	Signal1<ret, p1t> slot( ret (*pFnc)( p1t ) )
	{
		return Signal1<ret, p1t>(
			new __Slot1F<ret, p1t>( pFnc )
			);
	}

	//
	// 2 Parameters
	//

	template<typename ret, typename p1t, typename p2t>
	class _Slot2
	{
	public:
		_Slot2() { }
		virtual ~_Slot2() { }

		virtual ret operator()( p1t p1, p2t p2 )=0;

		virtual _Slot2<ret, p1t, p2t> *clone() const=0;
	};

	template<typename cls, typename ret, typename p1t, typename p2t>
	class __Slot2 : public _Slot2<ret, p1t, p2t>
	{
	public:
		__Slot2( cls *pCls, ret (cls::*pFnc)( p1t, p2t ) ) :
			pCls( pCls ), pFnc( pFnc ) { }
		virtual ~__Slot2() { }

		virtual ret operator()( p1t p1, p2t p2 )
		{
			return (pCls->*pFnc)( p1, p2 );
		}
		
		virtual _Slot2<ret, p1t, p2t> *clone() const
		{
			return new __Slot2<cls, ret, p1t, p2t>( pCls, pFnc );
		}
	
	private:
		cls *pCls;
		ret (cls::*pFnc)( p1t, p2t );
	};

	template<typename ret, typename p1t, typename p2t>
	class __Slot2F : public _Slot2<ret, p1t, p2t>
	{
	public:
		__Slot2F( ret (*pFnc)( p1t, p2t ) ) :
			pFnc( pFnc ) { }
		virtual ~__Slot2F() { }

		virtual ret operator()( p1t p1, p2t p2 )
		{
			return (*pFnc)( p1, p2 );
		}
		
		virtual _Slot2<ret, p1t, p2t> *clone() const
		{
			return new __Slot2F<ret, p1t, p2t>( pFnc );
		}
	
	private:
		ret (*pFnc)( p1t, p2t );
	};
	
	template<typename ret, typename p1t, typename p2t>
	class Signal2
	{
	public:
		Signal2() : pCb( NULL ) { }
		Signal2( _Slot2<ret, p1t, p2t> *pCb ) : pCb( pCb ) { }
		Signal2( const Signal2<ret, p1t, p2t> &rSrc ) :
			pCb( rSrc.pCb->clone() ) { }

		virtual ~Signal2() { delete pCb; pCb = NULL; }

		ret operator()( p1t p1, p2t p2 )
		{
			return (*pCb)( p1, p2 );
		}

	private:
		_Slot2<ret, p1t, p2t> *pCb;
	};
	
	template<typename cls, typename ret, typename p1t, typename p2t>
	Signal2<ret, p1t, p2t> slot( cls *pCls, ret (cls::*pFnc)( p1t, p2t ) )
	{
		return Signal2<ret, p1t, p2t>(
			new __Slot2<cls, ret, p1t, p2t>( pCls, pFnc )
			);
	}
	
	template<typename ret, typename p1t, typename p2t>
	Signal2<ret, p1t, p2t> slot( ret (*pFnc)( p1t, p2t ) )
	{
		return Signal2<ret, p1t, p2t>(
			new __Slot2F<ret, p1t, p2t>( pFnc )
			);
	}

	//
	// 3 Parameters
	//

	template<typename ret, typename p1t, typename p2t, typename p3t>
	class _Slot3
	{
	public:
		_Slot3() { }
		virtual ~_Slot3() { }

		virtual ret operator()( p1t p1, p2t p2, p3t p3 )=0;

		virtual _Slot3<ret, p1t, p2t, p3t> *clone() const=0;
	};

	template<typename cls, typename ret, typename p1t, typename p2t, typename p3t>
	class __Slot3 : public _Slot3<ret, p1t, p2t, p3t>
	{
	public:
		__Slot3( cls *pCls, ret (cls::*pFnc)( p1t, p2t, p3t ) ) :
			pCls( pCls ), pFnc( pFnc ) { }
		virtual ~__Slot3() { }

		virtual ret operator()( p1t p1, p2t p2, p3t p3 )
		{
			return (pCls->*pFnc)( p1, p2, p3 );
		}
		
		virtual _Slot3<ret, p1t, p2t, p3t> *clone() const
		{
			return new __Slot3<cls, ret, p1t, p2t, p3t>( pCls, pFnc );
		}
	
	private:
		cls *pCls;
		ret (cls::*pFnc)( p1t, p2t, p3t );
	};

	template<typename ret, typename p1t, typename p2t, typename p3t>
	class __Slot3F : public _Slot3<ret, p1t, p2t, p3t>
	{
	public:
		__Slot3F( ret (*pFnc)( p1t, p2t, p3t ) ) :
			pFnc( pFnc ) { }
		virtual ~__Slot3F() { }

		virtual ret operator()( p1t p1, p2t p2, p3t p3 )
		{
			return (*pFnc)( p1, p2, p3 );
		}
		
		virtual _Slot3<ret, p1t, p2t, p3t> *clone() const
		{
			return new __Slot3F<ret, p1t, p2t, p3t>( pFnc );
		}
	
	private:
		ret (*pFnc)( p1t, p2t, p3t );
	};
	
	template<typename ret, typename p1t, typename p2t, typename p3t>
	class Signal3
	{
	public:
		Signal3() : pCb( NULL ) { }
		Signal3( _Slot3<ret, p1t, p2t, p3t> *pCb ) : pCb( pCb ) { }
		Signal3( const Signal3<ret, p1t, p2t, p3t> &rSrc ) :
			pCb( rSrc.pCb->clone() ) { }

		virtual ~Signal3() { delete pCb; pCb = NULL; }

		ret operator()( p1t p1, p2t p2, p3t p3 )
		{
			return (*pCb)( p1, p2, p3 );
		}

	private:
		_Slot3<ret, p1t, p2t, p3t> *pCb;
	};
	
	template<typename cls, typename ret, typename p1t, typename p2t, typename p3t>
	Signal3<ret, p1t, p2t, p3t> slot( cls *pCls, ret (cls::*pFnc)( p1t, p2t, p3t ) )
	{
		return Signal3<ret, p1t, p2t, p3t>(
			new __Slot3<cls, ret, p1t, p2t, p3t>( pCls, pFnc )
			);
	}
	
	template<typename ret, typename p1t, typename p2t, typename p3t>
	Signal3<ret, p1t, p2t, p3t> slot( ret (*pFnc)( p1t, p2t, p3t ) )
	{
		return Signal3<ret, p1t, p2t, p3t>(
			new __Slot3F<ret, p1t, p2t, p3t>( pFnc )
			);
	}

	//
	// 4 Parameters
	//

	template<typename ret, typename p1t, typename p2t, typename p3t, typename p4t>
	class _Slot4
	{
	public:
		_Slot4() { }
		virtual ~_Slot4() { }

		virtual ret operator()( p1t p1, p2t p2, p3t p3, p4t p4 )=0;

		virtual _Slot4<ret, p1t, p2t, p3t, p4t> *clone() const=0;
	};

	template<typename cls, typename ret, typename p1t, typename p2t, typename p3t, typename p4t>
	class __Slot4 : public _Slot4<ret, p1t, p2t, p3t, p4t>
	{
	public:
		__Slot4( cls *pCls, ret (cls::*pFnc)( p1t, p2t, p3t, p4t ) ) :
			pCls( pCls ), pFnc( pFnc ) { }
		virtual ~__Slot4() { }

		virtual ret operator()( p1t p1, p2t p2, p3t p3, p4t p4 )
		{
			return (pCls->*pFnc)( p1, p2, p3, p4 );
		}
		
		virtual _Slot4<ret, p1t, p2t, p3t, p4t> *clone() const
		{
			return new __Slot4<cls, ret, p1t, p2t, p3t, p4t>( pCls, pFnc );
		}
	
	private:
		cls *pCls;
		ret (cls::*pFnc)( p1t, p2t, p3t, p4t );
	};

	template<typename ret, typename p1t, typename p2t, typename p3t, typename p4t>
	class __Slot4F : public _Slot4<ret, p1t, p2t, p3t, p4t>
	{
	public:
		__Slot4F( ret (*pFnc)( p1t, p2t, p3t, p4t ) ) :
			pFnc( pFnc ) { }
		virtual ~__Slot4F() { }

		virtual ret operator()( p1t p1, p2t p2, p3t p3, p4t p4 )
		{
			return (*pFnc)( p1, p2, p3, p4 );
		}
		
		virtual _Slot4<ret, p1t, p2t, p3t, p4t> *clone() const
		{
			return new __Slot4F<ret, p1t, p2t, p3t, p4t>( pFnc );
		}
	
	private:
		ret (*pFnc)( p1t, p2t, p3t, p4t );
	};
	
	template<typename ret, typename p1t, typename p2t, typename p3t, typename p4t>
	class Signal4
	{
	public:
		Signal4() : pCb( NULL ) { }
		Signal4( _Slot4<ret, p1t, p2t, p3t, p4t> *pCb ) : pCb( pCb ) { }
		Signal4( const Signal4<ret, p1t, p2t, p3t, p4t> &rSrc ) :
			pCb( rSrc.pCb->clone() ) { }

		virtual ~Signal4() { delete pCb; pCb = NULL; }

		ret operator()( p1t p1, p2t p2, p3t p3, p4t p4 )
		{
			return (*pCb)( p1, p2, p3, p4 );
		}

	private:
		_Slot4<ret, p1t, p2t, p3t, p4t> *pCb;
	};
	
	template<typename cls, typename ret, typename p1t, typename p2t, typename p3t, typename p4t>
	Signal4<ret, p1t, p2t, p3t, p4t> slot( cls *pCls, ret (cls::*pFnc)( p1t, p2t, p3t, p4t ) )
	{
		return Signal4<ret, p1t, p2t, p3t, p4t>(
			new __Slot4<cls, ret, p1t, p2t, p3t, p4t>( pCls, pFnc )
			);
	}
	
	template<typename ret, typename p1t, typename p2t, typename p3t, typename p4t>
	Signal4<ret, p1t, p2t, p3t, p4t> slot( ret (*pFnc)( p1t, p2t, p3t, p4t ) )
	{
		return Signal4<ret, p1t, p2t, p3t, p4t>(
			new __Slot4F<ret, p1t, p2t, p3t, p4t>( pFnc )
			);
	}

	//
	// 5 Parameters
	//

	template<typename ret, typename p1t, typename p2t, typename p3t, typename p4t, typename p5t>
	class _Slot5
	{
	public:
		_Slot5() { }
		virtual ~_Slot5() { }

		virtual ret operator()( p1t p1, p2t p2, p3t p3, p4t p4, p5t p5 )=0;

		virtual _Slot5<ret, p1t, p2t, p3t, p4t, p5t> *clone() const=0;
	};

	template<typename cls, typename ret, typename p1t, typename p2t, typename p3t, typename p4t, typename p5t>
	class __Slot5 : public _Slot5<ret, p1t, p2t, p3t, p4t, p5t>
	{
	public:
		__Slot5( cls *pCls, ret (cls::*pFnc)( p1t, p2t, p3t, p4t, p5t ) ) :
			pCls( pCls ), pFnc( pFnc ) { }
		virtual ~__Slot5() { }

		virtual ret operator()( p1t p1, p2t p2, p3t p3, p4t p4, p5t p5 )
		{
			return (pCls->*pFnc)( p1, p2, p3, p4, p5 );
		}
		
		virtual _Slot5<ret, p1t, p2t, p3t, p4t, p5t> *clone() const
		{
			return new __Slot5<cls, ret, p1t, p2t, p3t, p4t, p5t>( pCls, pFnc );
		}
	
	private:
		cls *pCls;
		ret (cls::*pFnc)( p1t, p2t, p3t, p4t, p5t );
	};

	template<typename ret, typename p1t, typename p2t, typename p3t, typename p4t, typename p5t>
	class __Slot5F : public _Slot5<ret, p1t, p2t, p3t, p4t, p5t>
	{
	public:
		__Slot5F( ret (*pFnc)( p1t, p2t, p3t, p4t, p5t ) ) :
			pFnc( pFnc ) { }
		virtual ~__Slot5F() { }

		virtual ret operator()( p1t p1, p2t p2, p3t p3, p4t p4, p5t p5 )
		{
			return (*pFnc)( p1, p2, p3, p4, p5 );
		}
		
		virtual _Slot5<ret, p1t, p2t, p3t, p4t, p5t> *clone() const
		{
			return new __Slot5F<ret, p1t, p2t, p3t, p4t, p5t>( pFnc );
		}
	
	private:
		ret (*pFnc)( p1t, p2t, p3t, p4t, p5t );
	};
	
	template<typename ret, typename p1t, typename p2t, typename p3t, typename p4t, typename p5t>
	class Signal5
	{
	public:
		Signal5() : pCb( NULL ) { }
		Signal5( _Slot5<ret, p1t, p2t, p3t, p4t, p5t> *pCb ) : pCb( pCb ) { }
		Signal5( const Signal5<ret, p1t, p2t, p3t, p4t, p5t> &rSrc ) :
			pCb( rSrc.pCb->clone() ) { }

		virtual ~Signal5() { delete pCb; pCb = NULL; }

		ret operator()( p1t p1, p2t p2, p3t p3, p4t p4, p5t p5 )
		{
			return (*pCb)( p1, p2, p3, p4, p5 );
		}

	private:
		_Slot5<ret, p1t, p2t, p3t, p4t, p5t> *pCb;
	};
	
	template<typename cls, typename ret, typename p1t, typename p2t, typename p3t, typename p4t, typename p5t>
	Signal5<ret, p1t, p2t, p3t, p4t, p5t> slot( cls *pCls, ret (cls::*pFnc)( p1t, p2t, p3t, p4t, p5t ) )
	{
		return Signal5<ret, p1t, p2t, p3t, p4t, p5t>(
			new __Slot5<cls, ret, p1t, p2t, p3t, p4t, p5t>( pCls, pFnc )
			);
	}
	
	template<typename ret, typename p1t, typename p2t, typename p3t, typename p4t, typename p5t>
	Signal5<ret, p1t, p2t, p3t, p4t, p5t> slot( ret (*pFnc)( p1t, p2t, p3t, p4t, p5t ) )
	{
		return Signal5<ret, p1t, p2t, p3t, p4t, p5t>(
			new __Slot5F<ret, p1t, p2t, p3t, p4t, p5t>( pFnc )
			);
	}

};

#endif