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
122 {
123 *this = std::move(other);
124 }
125
133 {
134 if (this == &other) {
135 return *this;
136 }
137
138 switch (other._state) {
139 case STATE_NONE: {
140 _Reset();
141 break;
142 }
143 case STATE_SINGLE: {
144 std::unique_ptr<TCallable> cloned(other._GetSingle()->Clone());
145 _Reset(STATE_SINGLE);
146 _GetSingle() = std::move(cloned);
147 break;
148 }
149 case STATE_LIST: {
150 TSharedList copied = other._GetList();
151 _Reset(STATE_LIST);
152 _GetList() = std::move(copied);
153 break;
154 }
155 }
156 return *this;
157 }
158
163 {
164 if (this == &other) {
165 return *this;
166 }
167
168 _Reset(other._state);
169
170 switch (other._state) {
171 case STATE_NONE: {
172 break;
173 }
174 case STATE_SINGLE: {
175 _GetSingle() = std::move(other._GetSingle());
176 other._Reset();
177 break;
178 }
179 case STATE_LIST: {
180 _GetList() = std::move(other._GetList());
181 other._Reset();
182 break;
183 }
184 }
185 return *this;
186 }
187
192 {
193 _Reset();
194 }
195
201 {
202 switch (_state) {
203 case STATE_SINGLE: {
204 return 1;
205 }
206 case STATE_LIST: {
207 return _GetList().size();
208 }
209 default: {
210 return 0;
211 }
212 }
213 }
214
220 {
221 return _state == STATE_NONE;
222 }
223
228 {
229 _Reset();
230 }
231
243 {
244 if (callable == nullptr) {
245 return;
246 }
247
248 switch (_state) {
249 case STATE_NONE: {
250 _Reset(STATE_SINGLE);
251 _GetSingle().reset(callable);
252 break;
253 }
254 case STATE_SINGLE: {
256 list.reserve(2);
257 list.emplace_back(_GetSingle().release());
258 list.emplace_back(callable);
259 _Reset(STATE_LIST);
260 _GetList() = std::move(list);
261 break;
262 }
263 case STATE_LIST: {
264 std::shared_ptr<TCallable> sp(callable);
265 _GetList().emplace_back(std::move(sp));
266 break;
267 }
268 }
269 }
270
275 bool RemoveAt(size_t index) noexcept
276 {
277 switch (_state) {
278 case STATE_SINGLE: {
279 if (index != 0) {
280 return false;
281 } else {
282 _Reset();
283 return true;
284 }
285 }
286 case STATE_LIST: {
287 auto &list = _GetList();
288 if (index >= list.size()) {
289 return false;
290 }
291 list.erase(list.begin() + index);
292 if (list.empty()) {
293 _Reset();
294 }
295 // 注:LIST 仅剩 1 元素时不降级回 SINGLE。shared_ptr 无法转移给
296 // unique_ptr,强行 Clone 反而带来额外开销,保留 LIST 单元素状态
297 // 在功能与性能上都可接受。
298 return true;
299 }
300 default: {
301 return false;
302 }
303 }
304 }
305
310 TCallable *GetAt(size_t index) const noexcept
311 {
312 switch (_state) {
313 case STATE_SINGLE: {
314 return index == 0 ? _GetSingle().get() : nullptr;
315 }
316 case STATE_LIST: {
317 auto &list = _GetList();
318 return (index < list.size()) ? list[index].get() : nullptr;
319 }
320 default: {
321 return nullptr;
322 }
323 }
324 }
325
330 TCallable *operator[](size_t index) const noexcept
331 {
332 return GetAt(index);
333 }
334
335 private:
339 constexpr TSinglePtr &_GetSingle() const noexcept
340 {
341 return *reinterpret_cast<TSinglePtr *>(_data._single);
342 }
343
347 constexpr TSharedList &_GetList() const noexcept
348 {
349 return *reinterpret_cast<TSharedList *>(_data._list);
350 }
351
355 void _Reset() noexcept
356 {
357 switch (_state) {
358 case STATE_NONE: {
359 break;
360 }
361 case STATE_SINGLE: {
362 _GetSingle().~TSinglePtr();
363 _state = STATE_NONE;
364 break;
365 }
366 case STATE_LIST: {
367 _GetList().~TSharedList();
368 _state = STATE_NONE;
369 break;
370 }
371 }
372 }
373
377 void _Reset(uint8_t state) noexcept
378 {
379 _Reset();
380
381 switch (state) {
382 case STATE_SINGLE: {
383 new (_data._single) TSinglePtr();
384 _state = STATE_SINGLE;
385 break;
386 }
387 case STATE_LIST: {
388 new (_data._list) TSharedList();
389 _state = STATE_LIST;
390 break;
391 }
392 }
393 }
394 };
395
396 /*================================================================================*/
397
401 template <typename TRet, typename... Args>
402 class Delegate<TRet(Args...)> final : public ICallable<TRet(Args...)>
403 {
404 private:
405 using _ICallable = ICallable<TRet(Args...)>;
406
407 template <typename T, typename = void>
408 struct _IsEqualityComparable : std::false_type {
409 };
410
411 template <typename T>
413 T, decltype(void(std::declval<T>() == std::declval<T>()))> : std::true_type {
414 };
415
416 template <typename T, typename = void>
417 struct _IsMemcmpSafe : std::false_type {
418 };
419
420 template <typename T>
421 struct _IsMemcmpSafe<
422 T,
423 typename std::enable_if</*std::is_trivial<T>::value &&*/ std::is_standard_layout<T>::value, void>::type> : std::true_type {
424 };
425
426 template <typename T>
427 class _CallableWrapperImpl final : public _ICallable
428 {
429 alignas(T) mutable uint8_t _storage[sizeof(T)];
430
431 public:
432 _CallableWrapperImpl(const T &value)
433 {
434 memset(_storage, 0, sizeof(_storage));
435 new (_storage) T(value);
436 }
437 _CallableWrapperImpl(T &&value)
438 {
439 memset(_storage, 0, sizeof(_storage));
440 new (_storage) T(std::move(value));
441 }
442 virtual ~_CallableWrapperImpl()
443 {
444 GetValue().~T();
445 // memset(_storage, 0, sizeof(_storage));
446 }
447 T &GetValue() const noexcept
448 {
449 return *reinterpret_cast<T *>(_storage);
450 }
451 TRet Invoke(Args... args) const override
452 {
453 return GetValue()(std::forward<Args>(args)...);
454 }
455 _ICallable *Clone() const override
456 {
457 return new _CallableWrapperImpl(GetValue());
458 }
459 virtual std::type_index GetType() const override
460 {
461 return typeid(T);
462 }
463 bool Equals(const _ICallable &other) const override
464 {
465 return EqualsImpl(other);
466 }
467 template <typename U = T>
468 auto EqualsImpl(const _ICallable &other) const
469 -> typename std::enable_if<_IsEqualityComparable<U>::value, bool>::type
470 {
471 if (this == &other) {
472 return true;
473 }
474 if (GetType() != other.GetType()) {
475 return false;
476 }
477 const auto &otherWrapper = static_cast<const _CallableWrapperImpl &>(other);
478 return GetValue() == otherWrapper.GetValue();
479 }
480 template <typename U = T>
481 auto EqualsImpl(const _ICallable &other) const
482 -> typename std::enable_if<!_IsEqualityComparable<U>::value && _IsMemcmpSafe<U>::value, bool>::type
483 {
484 if (this == &other) {
485 return true;
486 }
487 if (GetType() != other.GetType()) {
488 return false;
489 }
490 const auto &otherWrapper = static_cast<const _CallableWrapperImpl &>(other);
491 return memcmp(_storage, otherWrapper._storage, sizeof(_storage)) == 0;
492 }
493 template <typename U = T>
494 auto EqualsImpl(const _ICallable &other) const
495 -> typename std::enable_if<!_IsEqualityComparable<U>::value && !_IsMemcmpSafe<U>::value, bool>::type
496 {
497 return this == &other;
498 }
499
500 public:
501 // 禁用拷贝/移动:默认实现会按字节拷贝 _storage,不会调用 T 的构造函数,
502 // 对非平凡可拷贝类型会破坏不变式。需要克隆请走 Clone()。
503 _CallableWrapperImpl(const _CallableWrapperImpl &) = delete;
504 _CallableWrapperImpl(_CallableWrapperImpl &&) = delete;
505 _CallableWrapperImpl &operator=(const _CallableWrapperImpl &) = delete;
506 _CallableWrapperImpl &operator=(_CallableWrapperImpl &&) = delete;
507 };
508
509 template <typename T>
511
512 template <typename T>
513 class _MemberFuncWrapper final : public _ICallable
514 {
515 T *obj;
516 TRet (T::*func)(Args...);
517
518 public:
519 _MemberFuncWrapper(T &obj, TRet (T::*func)(Args...))
520 : obj(&obj), func(func)
521 {
522 }
523 TRet Invoke(Args... args) const override
524 {
525 return (obj->*func)(std::forward<Args>(args)...);
526 }
527 _ICallable *Clone() const override
528 {
529 return new _MemberFuncWrapper(*obj, func);
530 }
531 virtual std::type_index GetType() const override
532 {
533 return typeid(func);
534 }
535 bool Equals(const _ICallable &other) const override
536 {
537 if (this == &other) {
538 return true;
539 }
540 if (GetType() != other.GetType()) {
541 return false;
542 }
543 const auto &otherWrapper = static_cast<const _MemberFuncWrapper &>(other);
544 return obj == otherWrapper.obj && func == otherWrapper.func;
545 }
546
547 public:
548 // 禁用拷贝/移动:与 _CallableWrapperImpl 保持一致,需要克隆请走 Clone()。
549 _MemberFuncWrapper(const _MemberFuncWrapper &) = delete;
550 _MemberFuncWrapper(_MemberFuncWrapper &&) = delete;
551 _MemberFuncWrapper &operator=(const _MemberFuncWrapper &) = delete;
552 _MemberFuncWrapper &operator=(_MemberFuncWrapper &&) = delete;
553 };
554
555 template <typename T>
556 class _ConstMemberFuncWrapper final : public _ICallable
557 {
558 const T *obj;
559 TRet (T::*func)(Args...) const;
560
561 public:
562 _ConstMemberFuncWrapper(const T &obj, TRet (T::*func)(Args...) const)
563 : obj(&obj), func(func)
564 {
565 }
566 TRet Invoke(Args... args) const override
567 {
568 return (obj->*func)(std::forward<Args>(args)...);
569 }
570 _ICallable *Clone() const override
571 {
572 return new _ConstMemberFuncWrapper(*obj, func);
573 }
574 virtual std::type_index GetType() const override
575 {
576 return typeid(func);
577 }
578 bool Equals(const _ICallable &other) const override
579 {
580 if (this == &other) {
581 return true;
582 }
583 if (GetType() != other.GetType()) {
584 return false;
585 }
586 const auto &otherWrapper = static_cast<const _ConstMemberFuncWrapper &>(other);
587 return obj == otherWrapper.obj && func == otherWrapper.func;
588 }
589
590 public:
591 // 禁用拷贝/移动:与 _CallableWrapperImpl 保持一致,需要克隆请走 Clone()。
592 _ConstMemberFuncWrapper(const _ConstMemberFuncWrapper &) = delete;
593 _ConstMemberFuncWrapper(_ConstMemberFuncWrapper &&) = delete;
594 _ConstMemberFuncWrapper &operator=(const _ConstMemberFuncWrapper &) = delete;
595 _ConstMemberFuncWrapper &operator=(_ConstMemberFuncWrapper &&) = delete;
596 };
597
598 private:
602 CallableList<TRet(Args...)> _data;
603
604 public:
608 Delegate(std::nullptr_t = nullptr)
609 {
610 }
611
615 Delegate(const ICallable<TRet(Args...)> &callable)
616 {
617 Add(callable);
618 }
619
623 Delegate(TRet (*func)(Args...))
624 {
625 Add(func);
626 }
627
633 {
634 Add(callable);
635 }
636
640 template <typename T>
641 Delegate(T &obj, TRet (T::*func)(Args...))
642 {
643 Add(obj, func);
644 }
645
649 template <typename T>
650 Delegate(const T &obj, TRet (T::*func)(Args...) const)
651 {
652 Add(obj, func);
653 }
654
659 {
660 for (size_t i = 0; i < other._data.Count(); ++i) {
661 _data.Add(other._data[i]->Clone());
662 }
663 }
664
669 : _data(std::move(other._data))
670 {
671 }
672
677 {
678 if (this == &other) {
679 return *this;
680 }
681 _data.Clear();
682 for (size_t i = 0; i < other._data.Count(); ++i) {
683 _data.Add(other._data[i]->Clone());
684 }
685 return *this;
686 }
687
692 {
693 if (this != &other) {
694 _data = std::move(other._data);
695 }
696 return *this;
697 }
698
710 void Add(const ICallable<TRet(Args...)> &callable)
711 {
712 if (callable.GetType() == GetType()) {
713 auto &delegate = static_cast<const Delegate &>(callable);
714 if (delegate._data.IsEmpty()) {
715 return;
716 } else if (delegate._data.Count() == 1) {
717 _data.Add(delegate._data[0]->Clone());
718 return;
719 }
720 }
721 _data.Add(callable.Clone());
722 }
723
727 void Add(TRet (*func)(Args...))
728 {
729 if (func != nullptr) {
730 _data.Add(new _CallableWrapper<decltype(func)>(func));
731 }
732 }
733
737 template <typename T>
738 auto Add(const T &callable)
739 -> typename std::enable_if<!std::is_base_of<_ICallable, T>::value, void>::type
740 {
741 _data.Add(new _CallableWrapper<T>(callable));
742 }
743
747 template <typename T>
748 void Add(T &obj, TRet (T::*func)(Args...))
749 {
750 _data.Add(new _MemberFuncWrapper<T>(obj, func));
751 }
752
756 template <typename T>
757 void Add(const T &obj, TRet (T::*func)(Args...) const)
758 {
759 _data.Add(new _ConstMemberFuncWrapper<T>(obj, func));
760 }
761
765 void Clear()
766 {
767 _data.Clear();
768 }
769
780 bool Remove(const ICallable<TRet(Args...)> &callable)
781 {
782 if (callable.GetType() == GetType()) {
783 auto &delegate = static_cast<const Delegate &>(callable);
784 if (delegate._data.IsEmpty()) {
785 return false;
786 } else if (delegate._data.Count() == 1) {
787 return _Remove(*delegate._data[0]);
788 }
789 }
790 return _Remove(callable);
791 }
792
798 bool Remove(TRet (*func)(Args...))
799 {
800 if (func == nullptr) {
801 return false;
802 }
803 return _Remove(_CallableWrapper<decltype(func)>(func));
804 }
805
811 template <typename T>
812 auto Remove(const T &callable)
813 -> typename std::enable_if<!std::is_base_of<_ICallable, T>::value, bool>::type
814 {
815 return _Remove(_CallableWrapper<T>(callable));
816 }
817
823 template <typename T>
824 bool Remove(T &obj, TRet (T::*func)(Args...))
825 {
826 return _Remove(_MemberFuncWrapper<T>(obj, func));
827 }
828
834 template <typename T>
835 bool Remove(const T &obj, TRet (T::*func)(Args...) const)
836 {
837 return _Remove(_ConstMemberFuncWrapper<T>(obj, func));
838 }
839
846 TRet operator()(Args... args) const
847 {
848 return _InvokeImpl(std::forward<Args>(args)...);
849 }
850
856 bool operator==(const Delegate &other) const
857 {
858 return Equals(other);
859 }
860
866 bool operator!=(const Delegate &other) const
867 {
868 return !Equals(other);
869 }
870
875 bool operator==(std::nullptr_t) const noexcept
876 {
877 return _data.IsEmpty();
878 }
879
884 bool operator!=(std::nullptr_t) const noexcept
885 {
886 return !_data.IsEmpty();
887 }
888
893 explicit operator bool() const noexcept
894 {
895 return !_data.IsEmpty();
896 }
897
903 {
904 Add(callable);
905 return *this;
906 }
907
912 Delegate &operator+=(TRet (*func)(Args...))
913 {
914 Add(func);
915 return *this;
916 }
917
922 template <typename T>
923 auto operator+=(const T &callable)
924 -> typename std::enable_if<!std::is_base_of<_ICallable, T>::value, Delegate &>::type
925 {
926 Add(callable);
927 return *this;
928 }
929
935 {
936 Remove(callable);
937 return *this;
938 }
939
944 Delegate &operator-=(TRet (*func)(Args...))
945 {
946 Remove(func);
947 return *this;
948 }
949
954 template <typename T>
955 auto operator-=(const T &callable)
956 -> typename std::enable_if<!std::is_base_of<_ICallable, T>::value, Delegate &>::type
957 {
958 Remove(callable);
959 return *this;
960 }
961
968 virtual TRet Invoke(Args... args) const override
969 {
970 return _InvokeImpl(std::forward<Args>(args)...);
971 }
972
977 virtual ICallable<TRet(Args...)> *Clone() const override
978 {
979 return new Delegate(*this);
980 }
981
986 virtual std::type_index GetType() const override
987 {
988 return typeid(Delegate<TRet(Args...)>);
989 }
990
996 virtual bool Equals(const ICallable<TRet(Args...)> &other) const override
997 {
998 if (this == &other) {
999 return true;
1000 }
1001 if (GetType() != other.GetType()) {
1002 return false;
1003 }
1004 const auto &otherDelegate = static_cast<const Delegate &>(other);
1005 if (_data.Count() != otherDelegate._data.Count()) {
1006 return false;
1007 }
1008 for (size_t i = _data.Count(); i > 0; --i) {
1009 if (!_data[i - 1]->Equals(*otherDelegate._data[i - 1])) {
1010 return false;
1011 }
1012 }
1013 return true;
1014 }
1015
1023 template <typename U = TRet>
1024 auto InvokeAll(Args... args) const
1025 -> typename std::enable_if<!std::is_void<U>::value, std::vector<U>>::type
1026 {
1027 std::vector<U> results;
1028 size_t count = _data.Count();
1029 if (count == 0) {
1030 _ThrowEmptyDelegateError();
1031 } else if (count == 1) {
1032 results.emplace_back(_data[0]->Invoke(std::forward<Args>(args)...));
1033 } else {
1034 auto list = _data;
1035 results.reserve(count);
1036 for (size_t i = 0; i + 1 < count; ++i) {
1037 results.emplace_back(list[i]->Invoke(args...));
1038 }
1039 results.emplace_back(list[count - 1]->Invoke(std::forward<Args>(args)...));
1040 }
1041 return results;
1042 }
1043
1044 private:
1048 bool _Remove(const _ICallable &callable)
1049 {
1050 for (size_t i = _data.Count(); i > 0; --i) {
1051 if (_data[i - 1]->Equals(callable)) {
1052 return _data.RemoveAt(i - 1);
1053 }
1054 }
1055 return false;
1056 }
1057
1061 [[noreturn]] void _ThrowEmptyDelegateError() const
1062 {
1063 throw std::runtime_error("Delegate is empty");
1064 }
1065
1071 inline TRet _InvokeImpl(Args... args) const
1072 {
1073 size_t count = _data.Count();
1074 if (count == 0) {
1075 _ThrowEmptyDelegateError();
1076 } else if (count == 1) {
1077 return _data[0]->Invoke(std::forward<Args>(args)...);
1078 } else {
1079 auto list = _data;
1080 for (size_t i = 0; i + 1 < count; ++i)
1081 list[i]->Invoke(args...);
1082 return list[count - 1]->Invoke(std::forward<Args>(args)...);
1083 }
1084 }
1085 };
1086
1087 /*================================================================================*/
1088
1093 template <typename TRet, typename... Args>
1094 inline bool operator==(std::nullptr_t, const Delegate<TRet(Args...)> &d) noexcept
1095 {
1096 return d == nullptr;
1097 }
1098
1103 template <typename TRet, typename... Args>
1104 inline bool operator!=(std::nullptr_t, const Delegate<TRet(Args...)> &d) noexcept
1105 {
1106 return d != nullptr;
1107 }
1108
1109 /*================================================================================*/
1110
1114 template <typename... Args>
1115 using Action = Delegate<void(Args...)>;
1116
1120 template <typename T>
1122
1123 /*================================================================================*/
1124
1128 template <typename...>
1130
1134 template <typename Last>
1136 using TRet = Last;
1137 using TArgsTuple = std::tuple<>;
1138 };
1139
1143 template <typename First, typename... Rest>
1144 struct _FuncTraits<First, Rest...> {
1145 using TRet = typename _FuncTraits<Rest...>::TRet;
1146 using TArgsTuple = decltype(std::tuple_cat(std::declval<std::tuple<First>>(), std::declval<typename _FuncTraits<Rest...>::TArgsTuple>()));
1147 };
1148
1152 template <typename TArgsTuple>
1154
1158 template <typename... Args>
1159 struct _FuncTypeHelper<std::tuple<Args...>> {
1160 template <typename TRet>
1161 using TFunc = Delegate<TRet(Args...)>;
1162 };
1163
1167 template <typename... Types>
1168 using Func = typename _FuncTypeHelper<typename _FuncTraits<Types...>::TArgsTuple>::template TFunc<typename _FuncTraits<Types...>::TRet>;
1169}
用于存储和管理多个可调用对象的列表,针对单个可调用对象的情况进行优化
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:275
std::unique_ptr< TCallable > TSinglePtr
智能指针类型别名,用于存储可调用对象的唯一指针
Definition Delegate.h:77
CallableList & operator=(const CallableList &other)
拷贝赋值运算
Definition Delegate.h:132
CallableList & operator=(CallableList &&other) noexcept
移动赋值运算
Definition Delegate.h:162
TCallable * operator[](size_t index) const noexcept
获取指定索引处的可调用对象
Definition Delegate.h:330
void Clear() noexcept
清空当前存储的可调用对象
Definition Delegate.h:227
TCallable * GetAt(size_t index) const noexcept
获取指定索引处的可调用对象
Definition Delegate.h:310
~CallableList()
析构函数
Definition Delegate.h:191
std::vector< std::shared_ptr< TCallable > > TSharedList
列表类型别名,用于存储多个可调用对象的智能指针
Definition Delegate.h:82
size_t Count() const noexcept
获取当前存储的可调用对象数量
Definition Delegate.h:200
bool IsEmpty() const noexcept
判断当前存储的可调用对象是否为空
Definition Delegate.h:219
void Add(TCallable *callable)
添加一个可调用对象到列表中
Definition Delegate.h:242
委托类,类似于C::中的委托,支持存储和调用任意可调用对象
Definition Delegate.h:403
Delegate(const T &obj, TRet(T::*func)(Args...) const)
构造函数,接受一个常量成员函数指针
Definition Delegate.h:650
void Add(TRet(*func)(Args...))
添加一个函数指针到委托中
Definition Delegate.h:727
void Add(const T &obj, TRet(T::*func)(Args...) const)
添加一个常量成员函数指针到委托中
Definition Delegate.h:757
Delegate & operator+=(TRet(*func)(Args...))
添加一个函数指针到委托中
Definition Delegate.h:912
Delegate(const Delegate &other)
拷贝构造函数
Definition Delegate.h:658
Delegate(std::nullptr_t=nullptr)
默认构造函数
Definition Delegate.h:608
auto InvokeAll(Args... args) const -> typename std::enable_if<!std::is_void< U >::value, std::vector< U > >::type
调用所有存储的可调用对象,并返回它们的结果
Definition Delegate.h:1024
Delegate & operator-=(TRet(*func)(Args...))
移除一个函数指针
Definition Delegate.h:944
Delegate(T &obj, TRet(T::*func)(Args...))
构造函数,接受一个成员函数指针
Definition Delegate.h:641
Delegate & operator=(Delegate &&other) noexcept
移动赋值运算符
Definition Delegate.h:691
virtual std::type_index GetType() const override
获取当前委托的类型信息
Definition Delegate.h:986
Delegate & operator=(const Delegate &other)
拷贝赋值运算符
Definition Delegate.h:676
Delegate(const T &callable)
构造函数,接受一个可调用对象
Definition Delegate.h:632
bool operator!=(const Delegate &other) const
判断当前委托是否不等于另一个委托
Definition Delegate.h:866
bool Remove(const ICallable< TRet(Args...)> &callable)
移除一个可调用对象
Definition Delegate.h:780
bool operator==(std::nullptr_t) const noexcept
判断当前委托是否等于nullptr
Definition Delegate.h:875
bool Remove(TRet(*func)(Args...))
移除一个函数指针
Definition Delegate.h:798
auto operator-=(const T &callable) -> typename std::enable_if<!std::is_base_of< _ICallable, T >::value, Delegate & >::type
移除一个可调用对象
Definition Delegate.h:955
virtual ICallable< TRet(Args...)> * Clone() const override
克隆当前委托
Definition Delegate.h:977
bool operator==(const Delegate &other) const
判断当前委托是否等于另一个委托
Definition Delegate.h:856
void Clear()
清空委托中的所有可调用对象
Definition Delegate.h:765
Delegate & operator+=(const ICallable< TRet(Args...)> &callable)
添加一个可调用对象到委托中
Definition Delegate.h:902
Delegate(Delegate &&other) noexcept
移动构造函数
Definition Delegate.h:668
auto operator+=(const T &callable) -> typename std::enable_if<!std::is_base_of< _ICallable, T >::value, Delegate & >::type
添加一个可调用对象到委托中
Definition Delegate.h:923
auto Remove(const T &callable) -> typename std::enable_if<!std::is_base_of< _ICallable, T >::value, bool >::type
移除一个可调用对象
Definition Delegate.h:812
virtual TRet Invoke(Args... args) const override
调用委托,执行所有存储的可调用对象
Definition Delegate.h:968
bool operator!=(std::nullptr_t) const noexcept
判断当前委托是否不等于nullptr
Definition Delegate.h:884
void Add(const ICallable< TRet(Args...)> &callable)
添加一个可调用对象到委托中
Definition Delegate.h:710
Delegate(TRet(*func)(Args...))
构造函数,接受一个函数指针
Definition Delegate.h:623
virtual bool Equals(const ICallable< TRet(Args...)> &other) const override
判断当前委托是否与另一个可调用对象相等
Definition Delegate.h:996
bool Remove(const T &obj, TRet(T::*func)(Args...) const)
移除一个常量成员函数指针
Definition Delegate.h:835
Delegate & operator-=(const ICallable< TRet(Args...)> &callable)
移除一个可调用对象
Definition Delegate.h:934
auto Add(const T &callable) -> typename std::enable_if<!std::is_base_of< _ICallable, T >::value, void >::type
添加一个可调用对象到委托中
Definition Delegate.h:738
Delegate(const ICallable< TRet(Args...)> &callable)
构造函数,接受一个可调用对象
Definition Delegate.h:615
bool Remove(T &obj, TRet(T::*func)(Args...))
移除一个成员函数指针
Definition Delegate.h:824
void Add(T &obj, TRet(T::*func)(Args...))
添加一个成员函数指针到委托中
Definition Delegate.h:748
TRet operator()(Args... args) const
调用委托,执行所有存储的可调用对象
Definition Delegate.h:846
Definition Delegate.h:21
值转换器接口
Definition IValueConverter.h:14
SimpleWindow框架的顶级命名空间,所有公共类型、控件、枚举和工具函数均定义于此。
Definition Alignment.h:4
typename _FuncTypeHelper< typename _FuncTraits< Types... >::TArgsTuple >::template TFunc< typename _FuncTraits< Types... >::TRet > Func
Func类型别名,类似C::中的Func<T1, T2, ..., TResult>
Definition Delegate.h:1168
bool operator==(std::nullptr_t, const Delegate< TRet(Args...)> &d) noexcept
比较委托和nullptr
Definition Delegate.h:1094
bool operator!=(std::nullptr_t, const Delegate< TRet(Args...)> &d) noexcept
比较委托和nullptr
Definition Delegate.h:1104
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:1129
_FuncTypeHelper模板,用于根据参数元组生成对应的Func类型
Definition Delegate.h:1153