SimpleWindow
载入中...
搜索中...
未找到
Delegate.h
1#pragma once
2
3#include <cstdint>
4#include <cstdlib>
5#include <cstring>
6#include <memory>
7#include <stdexcept>
8#include <tuple>
9#include <type_traits>
10#include <typeindex>
11#include <vector>
12
13namespace sw
14{
15 // ICallable接口声明
16 template <typename>
17 struct ICallable;
18
19 // Delegate类声明
20 template <typename>
21 class Delegate;
22
23 /*================================================================================*/
24
28 template <typename TRet, typename... Args>
29 struct ICallable<TRet(Args...)> {
33 virtual ~ICallable() = default;
34
40 virtual TRet Invoke(Args... args) const = 0;
41
45 virtual ICallable *Clone() const = 0;
46
50 virtual std::type_index GetType() const = 0;
51
57 virtual bool Equals(const ICallable &other) const = 0;
58 };
59
60 /*================================================================================*/
61
65 template <typename T>
67 {
68 public:
73
77 using TSinglePtr = std::unique_ptr<TCallable>;
78
82 using TSharedList = std::vector<std::shared_ptr<TCallable>>;
83
84 private:
88 mutable union {
89 alignas(TSinglePtr) uint8_t _single[sizeof(TSinglePtr)];
90 alignas(TSharedList) uint8_t _list[sizeof(TSharedList)];
91 } _data = {};
92
96 enum : uint8_t {
97 STATE_NONE, // 未存储任何可调用对象
98 STATE_SINGLE, // 储存了一个可调用对象
99 STATE_LIST, // 储存了多个可调用对象
100 } _state = STATE_NONE;
101
102 public:
107 {
108 }
109
114 {
115 *this = other;
116 }
117
121 CallableList(CallableList &&other) noexcept
122 {
123 *this = std::move(other);
124 }
125
130 {
131 if (this == &other) {
132 return *this;
133 }
134
135 _Reset(other._state);
136
137 switch (other._state) {
138 case STATE_NONE: {
139 break;
140 }
141 case STATE_SINGLE: {
142 _GetSingle().reset(other._GetSingle()->Clone());
143 break;
144 }
145 case STATE_LIST: {
146 _GetList() = other._GetList();
147 break;
148 }
149 }
150 return *this;
151 }
152
157 {
158 if (this == &other) {
159 return *this;
160 }
161
162 _Reset(other._state);
163
164 switch (other._state) {
165 case STATE_NONE: {
166 break;
167 }
168 case STATE_SINGLE: {
169 _GetSingle() = std::move(other._GetSingle());
170 other._Reset();
171 break;
172 }
173 case STATE_LIST: {
174 _GetList() = std::move(other._GetList());
175 other._Reset();
176 break;
177 }
178 }
179 return *this;
180 }
181
186 {
187 _Reset();
188 }
189
194 size_t Count() const noexcept
195 {
196 switch (_state) {
197 case STATE_SINGLE: {
198 return 1;
199 }
200 case STATE_LIST: {
201 return _GetList().size();
202 }
203 default: {
204 return 0;
205 }
206 }
207 }
208
213 bool IsEmpty() const noexcept
214 {
215 return _state == STATE_NONE;
216 }
217
221 void Clear() noexcept
222 {
223 _Reset();
224 }
225
230 void Add(TCallable *callable)
231 {
232 if (callable == nullptr) {
233 return;
234 }
235
236 switch (_state) {
237 case STATE_NONE: {
238 _Reset(STATE_SINGLE);
239 _GetSingle().reset(callable);
240 break;
241 }
242 case STATE_SINGLE: {
243 TSharedList list;
244 list.emplace_back(_GetSingle().release());
245 list.emplace_back(callable);
246 _Reset(STATE_LIST);
247 _GetList() = std::move(list);
248 break;
249 }
250 case STATE_LIST: {
251 _GetList().emplace_back(callable);
252 break;
253 }
254 }
255 }
256
261 bool RemoveAt(size_t index) noexcept
262 {
263 switch (_state) {
264 case STATE_SINGLE: {
265 if (index != 0) {
266 return false;
267 } else {
268 _Reset();
269 return true;
270 }
271 }
272 case STATE_LIST: {
273 auto &list = _GetList();
274 if (index >= list.size()) {
275 return false;
276 }
277 list.erase(list.begin() + index);
278 if (list.empty()) {
279 _Reset();
280 }
281 // else if (list.size() == 1) {
282 // auto ptr = list.front()->Clone();
283 // _Reset(STATE_SINGLE);
284 // _GetSingle().reset(ptr);
285 // }
286 return true;
287 }
288 default: {
289 return false;
290 }
291 }
292 }
293
298 TCallable *GetAt(size_t index) const noexcept
299 {
300 switch (_state) {
301 case STATE_SINGLE: {
302 return index == 0 ? _GetSingle().get() : nullptr;
303 }
304 case STATE_LIST: {
305 auto &list = _GetList();
306 return (index < list.size()) ? list[index].get() : nullptr;
307 }
308 default: {
309 return nullptr;
310 }
311 }
312 }
313
318 TCallable *operator[](size_t index) const noexcept
319 {
320 return GetAt(index);
321 }
322
323 private:
327 constexpr TSinglePtr &_GetSingle() const noexcept
328 {
329 return *reinterpret_cast<TSinglePtr *>(_data._single);
330 }
331
335 constexpr TSharedList &_GetList() const noexcept
336 {
337 return *reinterpret_cast<TSharedList *>(_data._list);
338 }
339
343 void _Reset() noexcept
344 {
345 switch (_state) {
346 case STATE_NONE: {
347 break;
348 }
349 case STATE_SINGLE: {
350 _GetSingle().~TSinglePtr();
351 _state = STATE_NONE;
352 break;
353 }
354 case STATE_LIST: {
355 _GetList().~TSharedList();
356 _state = STATE_NONE;
357 break;
358 }
359 }
360 }
361
365 void _Reset(uint8_t state) noexcept
366 {
367 _Reset();
368
369 switch (state) {
370 case STATE_SINGLE: {
371 new (_data._single) TSinglePtr();
372 _state = STATE_SINGLE;
373 break;
374 }
375 case STATE_LIST: {
376 new (_data._list) TSharedList();
377 _state = STATE_LIST;
378 break;
379 }
380 }
381 }
382 };
383
384 /*================================================================================*/
385
389 template <typename TRet, typename... Args>
390 class Delegate<TRet(Args...)> final : public ICallable<TRet(Args...)>
391 {
392 private:
393 using _ICallable = ICallable<TRet(Args...)>;
394
395 template <typename T, typename = void>
396 struct _IsEqualityComparable : std::false_type {
397 };
398
399 template <typename T>
400 struct _IsEqualityComparable<
401 T, decltype(void(std::declval<T>() == std::declval<T>()))> : std::true_type {
402 };
403
404 template <typename T, typename = void>
405 struct _IsMemcmpSafe : std::false_type {
406 };
407
408 template <typename T>
409 struct _IsMemcmpSafe<
410 T,
411 typename std::enable_if</*std::is_trivial<T>::value &&*/ std::is_standard_layout<T>::value, void>::type> : std::true_type {
412 };
413
414 template <typename T>
415 class _CallableWrapperImpl final : public _ICallable
416 {
417 alignas(T) mutable uint8_t _storage[sizeof(T)];
418
419 public:
420 _CallableWrapperImpl(const T &value)
421 {
422 memset(_storage, 0, sizeof(_storage));
423 new (_storage) T(value);
424 }
425 _CallableWrapperImpl(T &&value)
426 {
427 memset(_storage, 0, sizeof(_storage));
428 new (_storage) T(std::move(value));
429 }
430 virtual ~_CallableWrapperImpl()
431 {
432 GetValue().~T();
433 // memset(_storage, 0, sizeof(_storage));
434 }
435 T &GetValue() const noexcept
436 {
437 return *reinterpret_cast<T *>(_storage);
438 }
439 TRet Invoke(Args... args) const override
440 {
441 return GetValue()(std::forward<Args>(args)...);
442 }
443 _ICallable *Clone() const override
444 {
445 return new _CallableWrapperImpl(GetValue());
446 }
447 virtual std::type_index GetType() const override
448 {
449 return typeid(T);
450 }
451 bool Equals(const _ICallable &other) const override
452 {
453 return EqualsImpl(other);
454 }
455 template <typename U = T>
456 typename std::enable_if<_IsEqualityComparable<U>::value, bool>::type
457 EqualsImpl(const _ICallable &other) const
458 {
459 if (this == &other) {
460 return true;
461 }
462 if (GetType() != other.GetType()) {
463 return false;
464 }
465 const auto &otherWrapper = static_cast<const _CallableWrapperImpl &>(other);
466 return GetValue() == otherWrapper.GetValue();
467 }
468 template <typename U = T>
469 typename std::enable_if<!_IsEqualityComparable<U>::value && _IsMemcmpSafe<U>::value, bool>::type
470 EqualsImpl(const _ICallable &other) const
471 {
472 if (this == &other) {
473 return true;
474 }
475 if (GetType() != other.GetType()) {
476 return false;
477 }
478 const auto &otherWrapper = static_cast<const _CallableWrapperImpl &>(other);
479 return memcmp(_storage, otherWrapper._storage, sizeof(_storage)) == 0;
480 }
481 template <typename U = T>
482 typename std::enable_if<!_IsEqualityComparable<U>::value && !_IsMemcmpSafe<U>::value, bool>::type
483 EqualsImpl(const _ICallable &other) const
484 {
485 return this == &other;
486 }
487 };
488
489 template <typename T>
490 using _CallableWrapper = _CallableWrapperImpl<typename std::decay<T>::type>;
491
492 template <typename T>
493 class _MemberFuncWrapper final : public _ICallable
494 {
495 T *obj;
496 TRet (T::*func)(Args...);
497
498 public:
499 _MemberFuncWrapper(T &obj, TRet (T::*func)(Args...))
500 : obj(&obj), func(func)
501 {
502 }
503 TRet Invoke(Args... args) const override
504 {
505 return (obj->*func)(std::forward<Args>(args)...);
506 }
507 _ICallable *Clone() const override
508 {
509 return new _MemberFuncWrapper(*obj, func);
510 }
511 virtual std::type_index GetType() const override
512 {
513 return typeid(func);
514 }
515 bool Equals(const _ICallable &other) const override
516 {
517 if (this == &other) {
518 return true;
519 }
520 if (GetType() != other.GetType()) {
521 return false;
522 }
523 const auto &otherWrapper = static_cast<const _MemberFuncWrapper &>(other);
524 return obj == otherWrapper.obj && func == otherWrapper.func;
525 }
526 };
527
528 template <typename T>
529 class _ConstMemberFuncWrapper final : public _ICallable
530 {
531 const T *obj;
532 TRet (T::*func)(Args...) const;
533
534 public:
535 _ConstMemberFuncWrapper(const T &obj, TRet (T::*func)(Args...) const)
536 : obj(&obj), func(func)
537 {
538 }
539 TRet Invoke(Args... args) const override
540 {
541 return (obj->*func)(std::forward<Args>(args)...);
542 }
543 _ICallable *Clone() const override
544 {
545 return new _ConstMemberFuncWrapper(*obj, func);
546 }
547 virtual std::type_index GetType() const override
548 {
549 return typeid(func);
550 }
551 bool Equals(const _ICallable &other) const override
552 {
553 if (this == &other) {
554 return true;
555 }
556 if (GetType() != other.GetType()) {
557 return false;
558 }
559 const auto &otherWrapper = static_cast<const _ConstMemberFuncWrapper &>(other);
560 return obj == otherWrapper.obj && func == otherWrapper.func;
561 }
562 };
563
564 private:
568 CallableList<TRet(Args...)> _data;
569
570 public:
574 Delegate(std::nullptr_t = nullptr)
575 {
576 }
577
581 Delegate(const ICallable<TRet(Args...)> &callable)
582 {
583 Add(callable);
584 }
585
589 Delegate(TRet (*func)(Args...))
590 {
591 Add(func);
592 }
593
597 template <typename T, typename std::enable_if<!std::is_base_of<_ICallable, T>::value, int>::type = 0>
598 Delegate(const T &callable)
599 {
600 Add(callable);
601 }
602
606 template <typename T>
607 Delegate(T &obj, TRet (T::*func)(Args...))
608 {
609 Add(obj, func);
610 }
611
615 template <typename T>
616 Delegate(const T &obj, TRet (T::*func)(Args...) const)
617 {
618 Add(obj, func);
619 }
620
624 Delegate(const Delegate &other)
625 {
626 for (size_t i = 0; i < other._data.Count(); ++i) {
627 _data.Add(other._data[i]->Clone());
628 }
629 }
630
634 Delegate(Delegate &&other) noexcept
635 : _data(std::move(other._data))
636 {
637 }
638
643 {
644 if (this == &other) {
645 return *this;
646 }
647 _data.Clear();
648 for (size_t i = 0; i < other._data.Count(); ++i) {
649 _data.Add(other._data[i]->Clone());
650 }
651 return *this;
652 }
653
657 Delegate &operator=(Delegate &&other) noexcept
658 {
659 if (this != &other) {
660 _data = std::move(other._data);
661 }
662 return *this;
663 }
664
668 void Add(const ICallable<TRet(Args...)> &callable)
669 {
670 // 当添加的可调用对象与当前委托类型相同时(针对单播委托进行优化):
671 // - 若委托内容为空,则直接返回
672 // - 若委托内容只有一个元素,则克隆该元素并添加到当前委托中
673 // - 否则,直接添加该可调用对象的克隆
674 if (callable.GetType() == GetType()) {
675 auto &delegate = static_cast<const Delegate &>(callable);
676 if (delegate._data.IsEmpty()) {
677 return;
678 } else if (delegate._data.Count() == 1) {
679 _data.Add(delegate._data[0]->Clone());
680 return;
681 }
682 }
683 _data.Add(callable.Clone());
684 }
685
689 void Add(TRet (*func)(Args...))
690 {
691 if (func != nullptr) {
692 _data.Add(new _CallableWrapper<decltype(func)>(func));
693 }
694 }
695
699 template <typename T>
700 typename std::enable_if<!std::is_base_of<_ICallable, T>::value, void>::type
701 Add(const T &callable)
702 {
703 _data.Add(new _CallableWrapper<T>(callable));
704 }
705
709 template <typename T>
710 void Add(T &obj, TRet (T::*func)(Args...))
711 {
712 _data.Add(new _MemberFuncWrapper<T>(obj, func));
713 }
714
718 template <typename T>
719 void Add(const T &obj, TRet (T::*func)(Args...) const)
720 {
721 _data.Add(new _ConstMemberFuncWrapper<T>(obj, func));
722 }
723
727 void Clear()
728 {
729 _data.Clear();
730 }
731
737 bool Remove(const ICallable<TRet(Args...)> &callable)
738 {
739 // 当移除的可调用对象与当前委托类型相同时(与Add逻辑相对应):
740 // - 若委托内容为空,则直接返回false
741 // - 若委托内容只有一个元素,则尝试移除该元素
742 // - 否则,直接调用_Remove函数进行移除
743 if (callable.GetType() == GetType()) {
744 auto &delegate = static_cast<const Delegate &>(callable);
745 if (delegate._data.IsEmpty()) {
746 return false;
747 } else if (delegate._data.Count() == 1) {
748 return _Remove(*delegate._data[0]);
749 }
750 }
751 return _Remove(callable);
752 }
753
759 bool Remove(TRet (*func)(Args...))
760 {
761 if (func == nullptr) {
762 return false;
763 }
764 return _Remove(_CallableWrapper<decltype(func)>(func));
765 }
766
772 template <typename T>
773 typename std::enable_if<!std::is_base_of<_ICallable, T>::value, bool>::type
774 Remove(const T &callable)
775 {
776 return _Remove(_CallableWrapper<T>(callable));
777 }
778
784 template <typename T>
785 bool Remove(T &obj, TRet (T::*func)(Args...))
786 {
787 return _Remove(_MemberFuncWrapper<T>(obj, func));
788 }
789
795 template <typename T>
796 bool Remove(const T &obj, TRet (T::*func)(Args...) const)
797 {
798 return _Remove(_ConstMemberFuncWrapper<T>(obj, func));
799 }
800
807 TRet operator()(Args... args) const
808 {
809 return _InvokeImpl(std::forward<Args>(args)...);
810 }
811
817 bool operator==(const Delegate &other) const
818 {
819 return Equals(other);
820 }
821
827 bool operator!=(const Delegate &other) const
828 {
829 return !Equals(other);
830 }
831
836 bool operator==(std::nullptr_t) const noexcept
837 {
838 return _data.IsEmpty();
839 }
840
845 bool operator!=(std::nullptr_t) const noexcept
846 {
847 return !_data.IsEmpty();
848 }
849
854 operator bool() const noexcept
855 {
856 return !_data.IsEmpty();
857 }
858
863 Delegate &operator+=(const ICallable<TRet(Args...)> &callable)
864 {
865 Add(callable);
866 return *this;
867 }
868
873 Delegate &operator+=(TRet (*func)(Args...))
874 {
875 Add(func);
876 return *this;
877 }
878
883 template <typename T>
884 typename std::enable_if<!std::is_base_of<_ICallable, T>::value, Delegate &>::type
885 operator+=(const T &callable)
886 {
887 Add(callable);
888 return *this;
889 }
890
895 Delegate &operator-=(const ICallable<TRet(Args...)> &callable)
896 {
897 Remove(callable);
898 return *this;
899 }
900
905 Delegate &operator-=(TRet (*func)(Args...))
906 {
907 Remove(func);
908 return *this;
909 }
910
915 template <typename T>
916 typename std::enable_if<!std::is_base_of<_ICallable, T>::value, Delegate &>::type
917 operator-=(const T &callable)
918 {
919 Remove(callable);
920 return *this;
921 }
922
929 virtual TRet Invoke(Args... args) const override
930 {
931 return _InvokeImpl(std::forward<Args>(args)...);
932 }
933
938 virtual ICallable<TRet(Args...)> *Clone() const override
939 {
940 return new Delegate(*this);
941 }
942
947 virtual std::type_index GetType() const override
948 {
949 return typeid(Delegate<TRet(Args...)>);
950 }
951
957 virtual bool Equals(const ICallable<TRet(Args...)> &other) const override
958 {
959 if (this == &other) {
960 return true;
961 }
962 if (GetType() != other.GetType()) {
963 return false;
964 }
965 const auto &otherDelegate = static_cast<const Delegate &>(other);
966 if (_data.Count() != otherDelegate._data.Count()) {
967 return false;
968 }
969 for (size_t i = _data.Count(); i > 0; --i) {
970 if (!_data[i - 1]->Equals(*otherDelegate._data[i - 1])) {
971 return false;
972 }
973 }
974 return true;
975 }
976
982 template <typename U = TRet>
983 typename std::enable_if<!std::is_void<U>::value, std::vector<U>>::type
984 InvokeAll(Args... args) const
985 {
986 std::vector<U> results;
987 size_t count = _data.Count();
988 if (count == 0) {
989 _ThrowEmptyDelegateError();
990 } else if (count == 1) {
991 results.emplace_back(_data[0]->Invoke(std::forward<Args>(args)...));
992 } else {
993 auto list = _data;
994 results.reserve(count = list.Count());
995 for (size_t i = 0; i < count; ++i) {
996 results.emplace_back(list[i]->Invoke(std::forward<Args>(args)...));
997 }
998 }
999 return results;
1000 }
1001
1002 private:
1006 bool _Remove(const _ICallable &callable)
1007 {
1008 for (size_t i = _data.Count(); i > 0; --i) {
1009 if (_data[i - 1]->Equals(callable)) {
1010 return _data.RemoveAt(i - 1);
1011 }
1012 }
1013 return false;
1014 }
1015
1019 [[noreturn]] void _ThrowEmptyDelegateError() const
1020 {
1021 throw std::runtime_error("Delegate is empty");
1022 }
1023
1027 inline TRet _InvokeImpl(Args... args) const
1028 {
1029 size_t count = _data.Count();
1030 if (count == 0) {
1031 _ThrowEmptyDelegateError();
1032 } else if (count == 1) {
1033 return _data[0]->Invoke(std::forward<Args>(args)...);
1034 } else {
1035 auto list = _data;
1036 for (size_t i = 0; i < count - 1; ++i)
1037 list[i]->Invoke(std::forward<Args>(args)...);
1038 return list[count - 1]->Invoke(std::forward<Args>(args)...);
1039 }
1040 }
1041 };
1042
1043 /*================================================================================*/
1044
1049 template <typename TRet, typename... Args>
1050 inline bool operator==(std::nullptr_t, const Delegate<TRet(Args...)> &d) noexcept
1051 {
1052 return d == nullptr;
1053 }
1054
1059 template <typename TRet, typename... Args>
1060 inline bool operator!=(std::nullptr_t, const Delegate<TRet(Args...)> &d) noexcept
1061 {
1062 return d != nullptr;
1063 }
1064
1065 /*================================================================================*/
1066
1070 template <typename... Args>
1071 using Action = Delegate<void(Args...)>;
1072
1076 template <typename T>
1077 using Predicate = Delegate<bool(T)>;
1078
1079 /*================================================================================*/
1080
1084 template <typename...>
1086
1090 template <typename Last>
1092 using TRet = Last;
1093 using TArgsTuple = std::tuple<>;
1094 };
1095
1099 template <typename First, typename... Rest>
1100 struct _FuncTraits<First, Rest...> {
1101 using TRet = typename _FuncTraits<Rest...>::TRet;
1102 using TArgsTuple = decltype(std::tuple_cat(std::declval<std::tuple<First>>(), std::declval<typename _FuncTraits<Rest...>::TArgsTuple>()));
1103 };
1104
1108 template <typename TArgsTuple>
1110
1114 template <typename... Args>
1115 struct _FuncTypeHelper<std::tuple<Args...>> {
1116 template <typename TRet>
1117 using TFunc = Delegate<TRet(Args...)>;
1118 };
1119
1123 template <typename... Types>
1124 using Func = typename _FuncTypeHelper<typename _FuncTraits<Types...>::TArgsTuple>::template TFunc<typename _FuncTraits<Types...>::TRet>;
1125}
用于存储和管理多个可调用对象的列表,针对单个可调用对象的情况进行优化
Definition Delegate.h:67
CallableList(CallableList &&other) noexcept
移动构造函数
Definition Delegate.h:121
CallableList(const CallableList &other)
拷贝构造函数
Definition Delegate.h:113
CallableList()
默认构造函数
Definition Delegate.h:106
bool RemoveAt(size_t index) noexcept
移除指定索引处的可调用对象
Definition Delegate.h:261
std::unique_ptr< TCallable > TSinglePtr
智能指针类型别名,用于存储可调用对象的唯一指针
Definition Delegate.h:77
CallableList & operator=(const CallableList &other)
拷贝赋值运算
Definition Delegate.h:129
CallableList & operator=(CallableList &&other) noexcept
移动赋值运算
Definition Delegate.h:156
TCallable * operator[](size_t index) const noexcept
获取指定索引处的可调用对象
Definition Delegate.h:318
void Clear() noexcept
清空当前存储的可调用对象
Definition Delegate.h:221
TCallable * GetAt(size_t index) const noexcept
获取指定索引处的可调用对象
Definition Delegate.h:298
~CallableList()
析构函数
Definition Delegate.h:185
std::vector< std::shared_ptr< TCallable > > TSharedList
列表类型别名,用于存储多个可调用对象的智能指针
Definition Delegate.h:82
size_t Count() const noexcept
获取当前存储的可调用对象数量
Definition Delegate.h:194
bool IsEmpty() const noexcept
判断当前存储的可调用对象是否为空
Definition Delegate.h:213
void Add(TCallable *callable)
添加一个可调用对象到列表中
Definition Delegate.h:230
委托类,类似于C::中的委托,支持存储和调用任意可调用对象
Definition Delegate.h:391
Delegate(const T &obj, TRet(T::*func)(Args...) const)
构造函数,接受一个常量成员函数指针
Definition Delegate.h:616
void Add(TRet(*func)(Args...))
添加一个函数指针到委托中
Definition Delegate.h:689
void Add(const T &obj, TRet(T::*func)(Args...) const)
添加一个常量成员函数指针到委托中
Definition Delegate.h:719
Delegate & operator+=(TRet(*func)(Args...))
添加一个函数指针到委托中
Definition Delegate.h:873
Delegate(const Delegate &other)
拷贝构造函数
Definition Delegate.h:624
Delegate(std::nullptr_t=nullptr)
默认构造函数
Definition Delegate.h:574
Delegate & operator-=(TRet(*func)(Args...))
移除一个函数指针
Definition Delegate.h:905
Delegate(T &obj, TRet(T::*func)(Args...))
构造函数,接受一个成员函数指针
Definition Delegate.h:607
Delegate & operator=(Delegate &&other) noexcept
移动赋值运算符
Definition Delegate.h:657
virtual std::type_index GetType() const override
获取当前委托的类型信息
Definition Delegate.h:947
Delegate & operator=(const Delegate &other)
拷贝赋值运算符
Definition Delegate.h:642
std::enable_if<!std::is_base_of< _ICallable, T >::value, Delegate & >::type operator-=(const T &callable)
移除一个可调用对象
Definition Delegate.h:917
std::enable_if<!std::is_void< U >::value, std::vector< U > >::type InvokeAll(Args... args) const
调用所有存储的可调用对象,并返回它们的结果
Definition Delegate.h:984
Delegate(const T &callable)
构造函数,接受一个可调用对象
Definition Delegate.h:598
bool operator!=(const Delegate &other) const
判断当前委托是否不等于另一个委托
Definition Delegate.h:827
bool Remove(const ICallable< TRet(Args...)> &callable)
移除一个可调用对象
Definition Delegate.h:737
bool operator==(std::nullptr_t) const noexcept
判断当前委托是否等于nullptr
Definition Delegate.h:836
bool Remove(TRet(*func)(Args...))
移除一个函数指针
Definition Delegate.h:759
virtual ICallable< TRet(Args...)> * Clone() const override
克隆当前委托
Definition Delegate.h:938
bool operator==(const Delegate &other) const
判断当前委托是否等于另一个委托
Definition Delegate.h:817
void Clear()
清空委托中的所有可调用对象
Definition Delegate.h:727
std::enable_if<!std::is_base_of< _ICallable, T >::value, Delegate & >::type operator+=(const T &callable)
添加一个可调用对象到委托中
Definition Delegate.h:885
Delegate & operator+=(const ICallable< TRet(Args...)> &callable)
添加一个可调用对象到委托中
Definition Delegate.h:863
Delegate(Delegate &&other) noexcept
移动构造函数
Definition Delegate.h:634
std::enable_if<!std::is_base_of< _ICallable, T >::value, bool >::type Remove(const T &callable)
移除一个可调用对象
Definition Delegate.h:774
virtual TRet Invoke(Args... args) const override
调用委托,执行所有存储的可调用对象
Definition Delegate.h:929
bool operator!=(std::nullptr_t) const noexcept
判断当前委托是否不等于nullptr
Definition Delegate.h:845
void Add(const ICallable< TRet(Args...)> &callable)
添加一个可调用对象到委托中
Definition Delegate.h:668
Delegate(TRet(*func)(Args...))
构造函数,接受一个函数指针
Definition Delegate.h:589
virtual bool Equals(const ICallable< TRet(Args...)> &other) const override
判断当前委托是否与另一个可调用对象相等
Definition Delegate.h:957
bool Remove(const T &obj, TRet(T::*func)(Args...) const)
移除一个常量成员函数指针
Definition Delegate.h:796
Delegate & operator-=(const ICallable< TRet(Args...)> &callable)
移除一个可调用对象
Definition Delegate.h:895
Delegate(const ICallable< TRet(Args...)> &callable)
构造函数,接受一个可调用对象
Definition Delegate.h:581
bool Remove(T &obj, TRet(T::*func)(Args...))
移除一个成员函数指针
Definition Delegate.h:785
std::enable_if<!std::is_base_of< _ICallable, T >::value, void >::type Add(const T &callable)
添加一个可调用对象到委托中
Definition Delegate.h:701
void Add(T &obj, TRet(T::*func)(Args...))
添加一个成员函数指针到委托中
Definition Delegate.h:710
TRet operator()(Args... args) const
调用委托,执行所有存储的可调用对象
Definition Delegate.h:807
Definition Delegate.h:21
ICallable接口,用于表示可调用对象的接口
Definition Delegate.h:29
virtual std::type_index GetType() const =0
获取当前可调用对象的类型信息
virtual ICallable * Clone() const =0
克隆当前可调用对象
virtual bool Equals(const ICallable &other) const =0
判断当前可调用对象是否与另一个可调用对象相等
virtual TRet Invoke(Args... args) const =0
调用函数
virtual ~ICallable()=default
析构函数
Definition Delegate.h:17
_FuncTraits模板,用于提取函数类型的返回值和参数类型
Definition Delegate.h:1085
_FuncTypeHelper模板,用于根据参数元组生成对应的Func类型
Definition Delegate.h:1109