-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDelegate.h
846 lines (750 loc) · 27.3 KB
/
Delegate.h
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
#pragma once
#include <vector>
#include <algorithm>
#include <tuple>
#include <iostream>
#include <functional>
namespace dw
{
template <typename ReturnType, typename... Params>
class SimpleDelegateBase
{
protected:
/**
* @brief Type defining a pointer to the function with the same arguments as Delegate's
*/
typedef ReturnType (*FunctionType)(Params...);
//using FunctionType = std::function<ReturnType(Params...)>;
/**
* @brief **std::vector** of functions that are subscribed to this delegate.
*/
std::vector<FunctionType> subscribers;
SimpleDelegateBase() = default;
};
template <typename... Params>
class SimpleDelegate : public SimpleDelegateBase<void, Params...>
{
public:
using Parent = SimpleDelegateBase<void, Params...>;
using Parent::subscribers;
using typename Parent::FunctionType;
/**
* @brief Invoke all subscribed functions.
*
* @param params: Arguments of each subscribed function.
*/
void operator()(Params... params)
{
for (auto &&i : subscribers)
{
i(params...);
}
}
/**
* @brief Subscribe function to this delegate.
*
* @param rhs: Function to subscribe.
* @returns Reference to the delegate instance.
*/
SimpleDelegate &operator+=(const FunctionType &rhs)
{
this->subscribers.push_back(rhs);
return *this;
}
/**
* @brief Unsubscribe choosen function from this delegate.
*
* @param rhs: Function to unsubscribe from this delegate.
* @returns Reference to the delegate instance.
*/
SimpleDelegate &operator-=(const FunctionType &rhs)
{
subscribers.erase(std::remove(subscribers.begin(), subscribers.end(), rhs), subscribers.end());
return *this;
}
};
template <typename ReturnType, typename... Params>
class DelegateBase : public SimpleDelegateBase<ReturnType, Params...>
{
template <typename... T>
struct FunctionParams
{
size_t index = -1;
std::tuple<T...> parameters;
};
protected:
using Parent = SimpleDelegateBase<ReturnType, Params...>;
using Parent::subscribers;
using typename Parent::FunctionType;
/**
* @brief Vector of parameters updated when each function is subscribed to this delegate using Subscribe() method.
*/
std::vector<FunctionParams<Params...>> parameters;
public:
const std::vector<FunctionType> &GetSubscribers() const { return this->subscribers; }
/**
* @brief Subscribe all functions (subscribers) from other delegate to this delegate.
*
* @param other: Other delegate reference.
*/
void Combine(const DelegateBase &other)
{
for (size_t i = 0; i < other.subscribers.size(); i++)
{
this->subscribers.push_back(other.subscribers[i]);
AttachParameters(other.parameters[i].parameters, std::index_sequence_for<Params...>());
}
}
/**
* @brief Subscribes single function and saves single parameters pack.
* @note
* @param function: Function to subscribe.
* @param params: Parameters pack for the function to subscribe.
* @retval None
*/
void Subscribe(const FunctionType &function, Params... params)
{
this->subscribers.push_back(function);
AttachParameters(std::tuple<Params...>(params...), std::index_sequence_for<Params...>());
}
/**
* @brief Subscribes multiple functions and saves single parameters pack.
* @note
* @param functions: Multiple functions to subscribe.
* @param params: Parameters pack for the functions to subscribe.
* @retval None
*/
void Subscribe(const std::initializer_list<FunctionType> &functions, Params... params)
{
for (auto &&d : functions)
{
this->subscribers.push_back(d);
AttachParameters(std::tuple<Params...>(params...), std::index_sequence_for<Params...>());
}
}
/**
* @brief Subscribes single function and save multiple parameters packs.
* @note
* @param function: Function to subscribe.
* @param params: Multiple parameter packs for the function to subscribe.
* @retval None
*/
void Subscribe(const FunctionType &function, std::vector<std::tuple<Params...>> params)
{
for (size_t i = 0; i < params.size(); i++)
{
this->subscribers.push_back(function);
AttachParameters(params[i], std::index_sequence_for<Params...>());
}
}
/**
* @brief Call all subscribed functions of this delegate that have parameters saved on subscription.
*/
void Invoke()
{
for (size_t i = 0; i < parameters.size(); i++)
{
HelperInvoke(parameters[i].parameters, parameters[i].index,
std::index_sequence_for<Params...>());
}
return;
}
/**
* @brief Remove *count* functions from the back or front.
* @note
* @param count: Count of functions to remove.
* @param fromBack: If *true* - removing will be performed from back of the subscribers vector. From front otherwise.
* @retval None
*/
void Remove(int count = 1, bool fromBack = true)
{
int adjustedCount = count < subscribers.size() ? count : subscribers.size() - 1;
if (!fromBack)
{
for (size_t i = 0; i < adjustedCount; ++i)
{
DetachParameters(0);
}
subscribers.erase(subscribers.begin(), subscribers.begin() + count);
return;
}
for (size_t i = 0; i < adjustedCount; ++i)
{
DetachParameters(parameters.size() - 1);
}
for (size_t i = 0; i < adjustedCount; i++)
{
subscribers.pop_back();
}
}
/**
* @brief Is not implemented yet.
* @note
* @param subscriber:
*/
void Remove(const FunctionType &subscriber)
{
std::cout << "Removing " << &subscriber << std::endl;
subscribers.erase(
std::remove_if(
subscribers.begin(),
subscribers.end(),
[subscriber](const FunctionType &x) { return &x == &subscriber; }),
subscribers.end());
for (auto i : subscribers)
{
std::cout << "Address = " << &i << std::endl;
}
}
/**
* @brief Remove all subscribers of this delegate appearing in the ***subscribers*** parameter.
* @note Is not implemented yet.
* @param subscribers: *std*::vector of functions that must be removed from the delegate.
*/
void Remove(const std::vector<FunctionType> &subscribers)
{
for (auto &s : subscribers)
{
this->subscribers.erase(
std::remove_if(
this->subscribers.begin(),
this->subscribers.end(),
[s](const FunctionType &x) { return &s == &x; }),
this->subscribers.end());
}
}
/**
* @brief Remove all subscribed functions from this delegate.
* @returns *None*
*/
void Clear()
{
this->subscribers.clear();
this->parameters.clear();
}
/**
* @brief Subscribe function to this delegate.
*
* @param rhs: Function to subscribe.
* @returns Reference to the delegate instance.
*/
DelegateBase &operator+=(const FunctionType &rhs)
{
this->subscribers.push_back(rhs);
return *this;
}
DelegateBase &operator+=(const std::initializer_list<FunctionType> &rhs)
{
for (auto x : rhs)
{
this->subscribers.push_back(x);
}
return *this;
}
/**
* @brief Unsubscribe choosen function from this delegate.
*
* @param rhs: Function to unsubscribe from this delegate.
* @returns Reference to the delegate instance.
*/
DelegateBase &operator-=(const FunctionType &rhs)
{
subscribers.erase(std::remove(subscribers.begin(), subscribers.end(), rhs), subscribers.end());
return *this;
}
DelegateBase &operator-=(const std::initializer_list<FunctionType> &rhs)
{
auto toRemove = [&](const FunctionType &func) -> bool {
return std::find(rhs.begin(), rhs.end(), func) != rhs.end();
};
subscribers.erase(std::remove_if(subscribers.begin(), subscribers.end(), toRemove), subscribers.end());
return *this;
}
DelegateBase &operator++()
{
if (subscribers.empty())
{
return *this;
}
FunctionType newDel = subscribers.front();
if (parameters.empty())
{
subscribers.insert(subscribers.begin(), newDel);
return *this;
}
subscribers.insert(subscribers.begin(), newDel);
AttachParameters(parameters.front().parameters, std::index_sequence_for<Params...>());
return *this;
}
DelegateBase &operator++(int)
{
if (subscribers.empty())
{
return *this;
}
FunctionType newDel = subscribers.back();
if (parameters.empty())
{
subscribers.push_back(newDel);
return *this;
}
subscribers.push_back(newDel);
AttachParameters(parameters.back().parameters, std::index_sequence_for<Params...>());
return *this;
}
DelegateBase &operator--()
{
if (subscribers.empty())
{
return *this;
}
if (parameters.empty())
{
subscribers.pop_back();
return *this;
}
int index = parameters.back().index;
parameters.pop_back();
subscribers.erase(subscribers.begin() + index);
return *this;
}
DelegateBase &operator--(int)
{
if (subscribers.empty())
{
return *this;
}
if (parameters.empty())
{
subscribers.pop_back();
return *this;
}
int index = parameters.back().index;
parameters.pop_back();
subscribers.erase(subscribers.begin() + index);
return *this;
}
/**
* @brief Signature of comparable delegates must match exactly.
* @param rhs: Other delegate.
* @returns true if count of subscribers of this delegate is less than other's.
*/
bool operator<(const DelegateBase &rhs)
{
return subscribers.size() < rhs.subscribers.size();
}
/**
* @brief Signature of comparable delegates must match exactly.
* @param rhs: Other delegate.
* @returns true if count of subscribers of this delegate is less or equals to other's.
*/
bool operator<=(const DelegateBase &rhs)
{
return subscribers.size() <= rhs.subscribers.size();
}
/**
* @brief Signature of comparable delegates must match exactly.
* @param rhs: Other delegate.
* @returns true if count of subscribers of this delegate is more than other's.
*/
bool operator>(const DelegateBase &rhs)
{
return subscribers.size() > rhs.subscribers.size();
}
/**
* @brief Signature of comparable delegates must match exactly.
* @param rhs: Other delegate.
* @returns true if count of subscribers of this delegate is more or equals to other's.
*/
bool operator>=(const DelegateBase &rhs)
{
return subscribers.size() >= rhs.subscribers.size();
}
/**
* @brief Signature of comparable delegates must match exactly.
* @param rhs: Other delegate.
* @returns true if subscribers of this delegate are equal to other's.
*/
bool operator==(const DelegateBase &rhs)
{
return subscribers == rhs.subscribers;
}
/**
* @brief Signature of comparable delegates must match exactly.
* @param rhs: Other delegate.
* @returns true if subscribers of this delegate are not-equal to other's.
*/
bool operator!=(const DelegateBase &rhs)
{
return subscribers != rhs.subscribers;
}
/**
* @brief Transfer all subscribers of other delegate to this delegate. Will clear subscribers from other delegate.
* @param rhs: Other delegate.
* @returns Pointer to this delegate.
*/
DelegateBase &operator<<(DelegateBase &rhs)
{
if (this != &rhs)
{
this->Combine(rhs);
rhs.Clear();
}
return *this;
}
/**
* @brief Transfer all subscribers of this delegate to other delegate. Will clear subscribers from this delegate.
* @param rhs: Other delegate.
* @returns Pointer to this delegate.
*/
DelegateBase &operator>>(DelegateBase &rhs)
{
if (this != &rhs)
{
rhs.Combine(*this);
this->Clear();
}
return *this;
}
void DebugPrintParametersIndices()
{
for (auto &p : parameters)
{
std::cout << p.index << std::endl;
}
}
protected:
template <size_t... Indices>
ReturnType HelperInvoke(const std::tuple<Params...> &tuple, int index, std::index_sequence<Indices...>)
{
return subscribers[index](std::get<Indices>(tuple)...);
}
private:
void AttachParameters(Params... params)
{
this->parameters.push_back(FunctionParams<Params...>{subscribers.size() - 1, params...});
}
template <size_t... Indices>
void AttachParameters(const std::tuple<Params...> &tuple, std::index_sequence<Indices...>)
{
this->parameters.push_back(FunctionParams<Params...>{subscribers.size() - 1, tuple});
}
void DetachParameters(const size_t index)
{
int erasedCount = 0;
// Removing all parameters with specified index
parameters.erase(
std::remove_if(
parameters.begin(),
parameters.end(),
[index, &erasedCount](const FunctionParams<Params...> &x) {
if (x.index == index)
{
erasedCount++;
return true;
}
return false;
}),
parameters.end());
if (index > 0)
{
return;
}
// Correcting indices of saved parameters
for (size_t i = 0; i < parameters.size(); ++i)
{
parameters[i].index -= erasedCount;
}
}
};
/**
* @brief Delegate is a class that encapsulates a function(s).
* @note
* @tparam Params: Any number of arguments of any type.
*/
template <typename... Params>
class Delegate : public DelegateBase<void, Params...>
{
public:
using Parent = DelegateBase<void, Params...>;
using Parent::Clear;
using Parent::Invoke;
using Parent::subscribers;
using typename Parent::FunctionType;
/**
* @brief Invoke all subscribed functions.
*
* @param params: Arguments of each subscribed function.
*/
void operator()(Params... params)
{
for (auto &&i : subscribers)
{
i(params...);
}
}
};
/**
* @brief Delegate with any return type specified.
*
* @tparam ReturnType Return type of the Delegate.
* @tparam Params Any number of arguments of any type.
*/
template <typename ReturnType, typename... Params>
class RetDelegate : public DelegateBase<ReturnType, Params...>
{
static_assert(!std::is_void<ReturnType>::value, "RetDelegate can't have void return type!");
public:
using Parent = DelegateBase<ReturnType, Params...>;
using Parent::Clear;
using Parent::HelperInvoke;
using Parent::parameters;
using Parent::subscribers;
using typename Parent::FunctionType;
/**
* @brief Call all subscribed functions of this delegate that have parameters saved on subscription.
* @note
* @returns Sum of results of each function invocation.
*/
ReturnType Invoke()
{
ReturnType result = ReturnType();
for (size_t i = 0; i < parameters.size(); i++)
{
result += HelperInvoke(parameters[i].parameters, i, std::index_sequence_for<Params...>());
}
return result;
}
/**
* @brief Invoke all functions subscribed to this delegate.
*
* @param params: Arguments of each subscribed function.
* @returns Sum of all subscribed functions results.
*/
ReturnType operator()(Params... params)
{
ReturnType sum = ReturnType();
for (auto &&i : subscribers)
{
sum += i(params...);
}
return sum;
}
};
/**
* @brief Delegate that holds the subscribed member functions.
* @note
* @tparam ReturnType Return type of the Delegate.
* @tparam ObjType Type of the member function owner class.
* @tparam Params Any number of arguments of any type.
*/
template <typename ReturnType, class ObjType, typename... Params>
class MemberDelegateBase
{
template <typename... T>
struct MemberFunctionParams
{
size_t index = -1;
ObjType *object;
std::tuple<T...> parameters;
};
public:
typedef ReturnType (ObjType::*MemberFunctionType)(Params...);
protected:
/**
* @brief **std::vector** of methods that are subscribed to this delegate.
*/
std::vector<MemberFunctionType> subscribers;
/**
* @brief Vector of parameters saved when each method is subscribed to this delegate.
*/
std::vector<MemberFunctionParams<Params...>> parameters;
MemberDelegateBase() = default;
public:
/**
* @brief Subscribe single method for a choosen object with the specified parameters.
* @note
* @param obj: Pointer to an object that holds the specified method that will be subscribed.
* @param delegate:
* @param params:
* @retval None
*/
void Subscribe(ObjType *obj, const MemberFunctionType &method, Params... params)
{
subscribers.push_back(method);
AttachParameters(obj, std::tuple<Params...>(params...), std::index_sequence_for<Params...>());
}
void Clear()
{
subscribers->clear();
parameters->clear();
}
/**
* @brief Subscribe method to this delegate.
*
* @param rhs: Method to subscribe.
* @returns Reference to the delegate instance.
*/
MemberDelegateBase &operator+=(const MemberFunctionType &rhs)
{
subscribers.push_back(rhs);
return *this;
}
/**
* @brief Subscribe multiple methods to this delegate.
* @note
* @param rhs: Methods to subscribe.
* @retval Reference to the delegate instance.
*/
MemberDelegateBase &operator+=(const std::initializer_list<MemberFunctionType> &rhs)
{
for (auto x : rhs)
{
this->subscribers.push_back(x);
}
return *this;
}
/**
* @brief Unsubscribe choosen method from this delegate.
*
* @param rhs: Method to unsubscribe from this delegate.
* @returns Reference to the delegate instance.
*/
MemberDelegateBase &operator-=(const MemberFunctionType &rhs)
{
subscribers.erase(std::remove(subscribers.begin(), subscribers.end(), rhs), subscribers.end());
return *this;
}
/**
* @brief Unsubscribe multiple methods from this delegate.
* @note
* @param rhs: Methods to unsubscribe from this delegate.
* @retval Reference to the delegate instance.
*/
MemberDelegateBase &operator-=(const std::initializer_list<MemberFunctionType> &rhs)
{
auto toRemove = [&](const MemberFunctionType &delegate) -> bool {
return std::find(rhs.begin(), rhs.end(), delegate) != rhs.end();
};
subscribers.erase(std::remove_if(subscribers.begin(), subscribers.end(), toRemove), subscribers.end());
return *this;
}
private:
void AttachParameters(ObjType *obj, Params... params)
{
this->parameters.push_back(MemberFunctionParams<Params...>{subscribers.size() - 1, obj, params...});
}
template <size_t... Indices>
void AttachParameters(ObjType *obj, const std::tuple<Params...> &tuple, std::index_sequence<Indices...>)
{
this->parameters.push_back(MemberFunctionParams<Params...>{subscribers.size() - 1, obj, tuple});
}
};
template <class ObjType, typename... Params>
class MemberDelegate : public MemberDelegateBase<void, ObjType, Params...>
{
public:
using Parent = MemberDelegateBase<void, ObjType, Params...>;
using Parent::parameters;
using Parent::subscribers;
using typename Parent::MemberFunctionType;
/**
* @brief Call all subscribed methods of this delegate that have parameters saved on subscription.
*/
void Invoke()
{
for (size_t i = 0; i < parameters.size(); i++)
{
HelperMemberInvoke(parameters[i].object, parameters[i].parameters, i, std::index_sequence_for<Params...>());
}
return;
}
/**
* @brief Calls subscribed methods with the specified parameters.
* @param obj: Pointer to an object that will call *all* subscribed methods of this delegate.
* @param params: Method parameters pack.
* @retval None
*/
void operator()(ObjType *obj, Params... params)
{
for (auto &&i : subscribers)
{
(obj->*i)(params...);
}
}
private:
template <size_t... Indices>
void HelperMemberInvoke(ObjType *obj, const std::tuple<Params...> &tuple, int index, std::index_sequence<Indices...>)
{
(obj->*subscribers[index])(std::get<Indices>(tuple)...);
}
};
template <typename ReturnType, class ObjType, typename... Params>
class RetMemberDelegate : public MemberDelegateBase<ReturnType, ObjType, Params...>
{
static_assert(!std::is_void<ReturnType>::value, "RetMemberDelegate can't have void return type!");
public:
using Parent = MemberDelegateBase<ReturnType, ObjType, Params...>;
using Parent::parameters;
using Parent::subscribers;
using typename Parent::MemberFunctionType;
/**
* @brief Call all subscribed methods of this delegate that have parameters saved on subscription.
*/
ReturnType Invoke()
{
ReturnType result = ReturnType();
for (size_t i = 0; i < parameters.size(); i++)
{
result += HelperMemberInvoke(parameters[i].object, parameters[i].parameters, i, std::index_sequence_for<Params...>());
}
return result;
}
/**
* @brief Calls subscribed methods with the specified parameters.
* @param obj: Pointer to an object that will call *all* subscribed methods of this delegate.
* @param params: Method parameters pack.
* @retval None
*/
ReturnType operator()(ObjType *obj, Params... params)
{
ReturnType result = ReturnType();
for (auto &&i : subscribers)
{
result += (obj->*i)(params...);
}
return result;
}
private:
template <size_t... Indices>
ReturnType HelperMemberInvoke(ObjType *obj, const std::tuple<Params...> &tuple, int index, std::index_sequence<Indices...>)
{
return (obj->*subscribers[index])(std::get<Indices>(tuple)...);
}
};
// template <typename... Params>
// class MasterDelegate : public Delegate<Params...>
// {
// template<typename... Args>
// struct MasterArgs
// {
// std::tuple<Args...> args;
// };
// //std::vector<std::any> masterArgs;
// public:
// using typename DelegateBase<void, Params...>::DelegateType;
// using DelegateBase<void, Params...>::subscribers;
// using DelegateBase<void, Params...>::Clear;
// using DelegateBase<void, Params...>::Invoke;
// template<typename T, typename Args>
// T InvokeAll(T type, Args... args)
// {
// return T();
// }
// template<typename Returns, typename... Args>
// Returns AddUnique(Args... args)
// {
// return Returns();
// }
// };
} // namespace dw