28 template <
typename TRet,
typename... Args>
40 virtual TRet
Invoke(Args... args)
const = 0;
50 virtual std::type_index
GetType()
const = 0;
100 } _state = STATE_NONE;
123 *
this = std::move(other);
131 if (
this == &other) {
135 _Reset(other._state);
137 switch (other._state) {
142 _GetSingle().reset(other._GetSingle()->Clone());
146 _GetList() = other._GetList();
158 if (
this == &other) {
162 _Reset(other._state);
164 switch (other._state) {
169 _GetSingle() = std::move(other._GetSingle());
174 _GetList() = std::move(other._GetList());
201 return _GetList().size();
215 return _state == STATE_NONE;
232 if (callable ==
nullptr) {
238 _Reset(STATE_SINGLE);
239 _GetSingle().reset(callable);
244 list.emplace_back(_GetSingle().release());
245 list.emplace_back(callable);
247 _GetList() = std::move(list);
251 _GetList().emplace_back(callable);
273 auto &list = _GetList();
274 if (index >= list.size()) {
277 list.erase(list.begin() + index);
302 return index == 0 ? _GetSingle().get() :
nullptr;
305 auto &list = _GetList();
306 return (index < list.size()) ? list[index].get() :
nullptr;
327 constexpr TSinglePtr &_GetSingle() const noexcept
329 return *
reinterpret_cast<TSinglePtr *
>(_data._single);
337 return *
reinterpret_cast<TSharedList *
>(_data._list);
343 void _Reset() noexcept
350 _GetSingle().~TSinglePtr();
355 _GetList().~TSharedList();
365 void _Reset(uint8_t state)
noexcept
372 _state = STATE_SINGLE;
389 template <
typename TRet,
typename... Args>
395 template <
typename T,
typename =
void>
396 struct _IsEqualityComparable : std::false_type {
399 template <
typename T>
400 struct _IsEqualityComparable<
401 T, decltype(void(std::declval<T>() == std::declval<T>()))> : std::true_type {
404 template <
typename T,
typename =
void>
405 struct _IsMemcmpSafe : std::false_type {
408 template <
typename T>
409 struct _IsMemcmpSafe<
411 typename std::enable_if< std::is_standard_layout<T>::value, void>::type> : std::true_type {
414 template <
typename T>
415 class _CallableWrapperImpl final :
public _ICallable
417 alignas(T)
mutable uint8_t _storage[
sizeof(T)];
420 _CallableWrapperImpl(
const T &value)
422 memset(_storage, 0,
sizeof(_storage));
423 new (_storage) T(value);
425 _CallableWrapperImpl(T &&value)
427 memset(_storage, 0,
sizeof(_storage));
428 new (_storage) T(std::move(value));
430 virtual ~_CallableWrapperImpl()
435 T &GetValue()
const noexcept
437 return *
reinterpret_cast<T *
>(_storage);
439 TRet Invoke(Args... args)
const override
441 return GetValue()(std::forward<Args>(args)...);
445 return new _CallableWrapperImpl(GetValue());
447 virtual std::type_index GetType()
const override
451 bool Equals(
const _ICallable &other)
const override
453 return EqualsImpl(other);
455 template <
typename U = T>
456 typename std::enable_if<_IsEqualityComparable<U>::value,
bool>::type
459 if (
this == &other) {
462 if (GetType() != other.
GetType()) {
465 const auto &otherWrapper =
static_cast<const _CallableWrapperImpl &
>(other);
466 return GetValue() == otherWrapper.GetValue();
468 template <
typename U = T>
469 typename std::enable_if<!_IsEqualityComparable<U>::value && _IsMemcmpSafe<U>::value,
bool>::type
472 if (
this == &other) {
475 if (GetType() != other.
GetType()) {
478 const auto &otherWrapper =
static_cast<const _CallableWrapperImpl &
>(other);
479 return memcmp(_storage, otherWrapper._storage,
sizeof(_storage)) == 0;
481 template <
typename U = T>
482 typename std::enable_if<!_IsEqualityComparable<U>::value && !_IsMemcmpSafe<U>::value,
bool>::type
485 return this == &other;
489 template <
typename T>
490 using _CallableWrapper = _CallableWrapperImpl<typename std::decay<T>::type>;
492 template <
typename T>
493 class _MemberFuncWrapper final :
public _ICallable
496 TRet (T::*func)(Args...);
499 _MemberFuncWrapper(T &obj, TRet (T::*func)(Args...))
500 : obj(&obj), func(func)
503 TRet Invoke(Args... args)
const override
505 return (obj->*func)(std::forward<Args>(args)...);
509 return new _MemberFuncWrapper(*obj, func);
511 virtual std::type_index GetType()
const override
515 bool Equals(
const _ICallable &other)
const override
517 if (
this == &other) {
520 if (GetType() != other.
GetType()) {
523 const auto &otherWrapper =
static_cast<const _MemberFuncWrapper &
>(other);
524 return obj == otherWrapper.obj && func == otherWrapper.func;
528 template <
typename T>
529 class _ConstMemberFuncWrapper final :
public _ICallable
532 TRet (T::*func)(Args...)
const;
535 _ConstMemberFuncWrapper(
const T &obj, TRet (T::*func)(Args...)
const)
536 : obj(&obj), func(func)
539 TRet Invoke(Args... args)
const override
541 return (obj->*func)(std::forward<Args>(args)...);
545 return new _ConstMemberFuncWrapper(*obj, func);
547 virtual std::type_index GetType()
const override
551 bool Equals(
const _ICallable &other)
const override
553 if (
this == &other) {
556 if (GetType() != other.
GetType()) {
559 const auto &otherWrapper =
static_cast<const _ConstMemberFuncWrapper &
>(other);
560 return obj == otherWrapper.obj && func == otherWrapper.func;
597 template <typename T, typename std::enable_if<!std::is_base_of<_ICallable, T>::value,
int>::type = 0>
606 template <
typename T>
615 template <
typename T>
616 Delegate(
const T &obj, TRet (T::*func)(Args...) const)
626 for (
size_t i = 0; i < other._data.Count(); ++i) {
627 _data.
Add(other._data[i]->Clone());
635 : _data(std::move(other._data))
644 if (
this == &other) {
648 for (
size_t i = 0; i < other._data.Count(); ++i) {
649 _data.
Add(other._data[i]->Clone());
659 if (
this != &other) {
660 _data = std::move(other._data);
674 if (callable.GetType() == GetType()) {
675 auto &delegate =
static_cast<const Delegate &
>(callable);
676 if (delegate._data.IsEmpty()) {
678 }
else if (delegate._data.Count() == 1) {
679 _data.
Add(delegate._data[0]->Clone());
683 _data.
Add(callable.Clone());
689 void Add(TRet (*func)(Args...))
691 if (func !=
nullptr) {
692 _data.
Add(
new _CallableWrapper<
decltype(func)>(func));
699 template <
typename T>
700 typename std::enable_if<!std::is_base_of<_ICallable, T>::value,
void>::type
703 _data.
Add(
new _CallableWrapper<T>(callable));
709 template <
typename T>
710 void Add(T &obj, TRet (T::*func)(Args...))
712 _data.
Add(
new _MemberFuncWrapper<T>(obj, func));
718 template <
typename T>
719 void Add(
const T &obj, TRet (T::*func)(Args...) const)
721 _data.
Add(
new _ConstMemberFuncWrapper<T>(obj, func));
743 if (callable.GetType() == GetType()) {
744 auto &delegate =
static_cast<const Delegate &
>(callable);
745 if (delegate._data.IsEmpty()) {
747 }
else if (delegate._data.Count() == 1) {
748 return _Remove(*delegate._data[0]);
751 return _Remove(callable);
761 if (func ==
nullptr) {
764 return _Remove(_CallableWrapper<
decltype(func)>(func));
772 template <
typename T>
773 typename std::enable_if<!std::is_base_of<_ICallable, T>::value,
bool>::type
776 return _Remove(_CallableWrapper<T>(callable));
784 template <
typename T>
785 bool Remove(T &obj, TRet (T::*func)(Args...))
787 return _Remove(_MemberFuncWrapper<T>(obj, func));
795 template <
typename T>
796 bool Remove(
const T &obj, TRet (T::*func)(Args...) const)
798 return _Remove(_ConstMemberFuncWrapper<T>(obj, func));
809 return _InvokeImpl(std::forward<Args>(args)...);
819 return Equals(other);
829 return !Equals(other);
854 operator bool() const noexcept
883 template <
typename T>
884 typename std::enable_if<!std::is_base_of<_ICallable, T>::value,
Delegate &>::type
915 template <
typename T>
916 typename std::enable_if<!std::is_base_of<_ICallable, T>::value,
Delegate &>::type
929 virtual TRet
Invoke(Args... args)
const override
931 return _InvokeImpl(std::forward<Args>(args)...);
947 virtual std::type_index
GetType()
const override
949 return typeid(
Delegate<TRet(Args...)>);
959 if (
this == &other) {
962 if (GetType() != other.GetType()) {
965 const auto &otherDelegate =
static_cast<const Delegate &
>(other);
966 if (_data.
Count() != otherDelegate._data.Count()) {
969 for (
size_t i = _data.
Count(); i > 0; --i) {
970 if (!_data[i - 1]->Equals(*otherDelegate._data[i - 1])) {
982 template <
typename U = TRet>
983 typename std::enable_if<!std::is_void<U>::value, std::vector<U>>::type
986 std::vector<U> results;
987 size_t count = _data.
Count();
989 _ThrowEmptyDelegateError();
990 }
else if (count == 1) {
991 results.emplace_back(_data[0]->Invoke(std::forward<Args>(args)...));
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)...));
1006 bool _Remove(
const _ICallable &callable)
1008 for (
size_t i = _data.
Count(); i > 0; --i) {
1009 if (_data[i - 1]->Equals(callable)) {
1019 [[noreturn]]
void _ThrowEmptyDelegateError()
const
1021 throw std::runtime_error(
"Delegate is empty");
1027 inline TRet _InvokeImpl(Args... args)
const
1029 size_t count = _data.
Count();
1031 _ThrowEmptyDelegateError();
1032 }
else if (count == 1) {
1033 return _data[0]->Invoke(std::forward<Args>(args)...);
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)...);
1049 template <
typename TRet,
typename... Args>
1050 inline bool operator==(std::nullptr_t,
const Delegate<TRet(Args...)> &d)
noexcept
1052 return d ==
nullptr;
1059 template <
typename TRet,
typename... Args>
1060 inline bool operator!=(std::nullptr_t,
const Delegate<TRet(Args...)> &d)
noexcept
1062 return d !=
nullptr;
1070 template <
typename... Args>
1071 using Action = Delegate<void(Args...)>;
1076 template <
typename T>
1077 using Predicate = Delegate<bool(T)>;
1084 template <
typename...>
1090 template <
typename Last>
1093 using TArgsTuple = std::tuple<>;
1099 template <
typename First,
typename... Rest>
1108 template <
typename TArgsTuple>
1114 template <
typename... Args>
1116 template <
typename TRet>
1123 template <
typename... Types>
用于存储和管理多个可调用对象的列表,针对单个可调用对象的情况进行优化
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
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
析构函数
_FuncTraits模板,用于提取函数类型的返回值和参数类型
Definition Delegate.h:1085
_FuncTypeHelper模板,用于根据参数元组生成对应的Func类型
Definition Delegate.h:1109