28 template <
typename TRet,
typename...
Args>
50 virtual std::type_index
GetType()
const = 0;
100 } _state = STATE_NONE;
123 *
this = std::move(
other);
134 if (
this == &
other) {
138 switch (
other._state) {
144 std::unique_ptr<TCallable>
cloned(
other._GetSingle()->Clone());
145 _Reset(STATE_SINGLE);
146 _GetSingle() = std::move(
cloned);
152 _GetList() = std::move(
copied);
164 if (
this == &
other) {
168 _Reset(
other._state);
170 switch (
other._state) {
175 _GetSingle() = std::move(
other._GetSingle());
180 _GetList() = std::move(
other._GetList());
207 return _GetList().size();
221 return _state == STATE_NONE;
246 if (
owned ==
nullptr) {
252 _Reset(STATE_SINGLE);
253 _GetSingle() = std::move(
owned);
260 std::shared_ptr<TCallable>
current(std::move(_GetSingle()));
261 list.emplace_back(std::move(
current));
262 list.emplace_back(std::move(
incoming));
264 _GetList() = std::move(list);
268 std::shared_ptr<TCallable>
sp(std::move(
owned));
269 _GetList().emplace_back(std::move(
sp));
291 auto &list = _GetList();
292 if (index >= list.size()) {
295 list.erase(list.begin() + index);
318 return index == 0 ? _GetSingle().get() :
nullptr;
321 auto &list = _GetList();
322 return (index < list.size()) ? list[index].get() :
nullptr;
345 return *
reinterpret_cast<TSinglePtr *
>(_data._single);
353 return *
reinterpret_cast<TSharedList *
>(_data._list);
359 void _Reset() noexcept
366 _GetSingle().~TSinglePtr();
371 _GetList().~TSharedList();
381 void _Reset(uint8_t state)
noexcept
388 _state = STATE_SINGLE;
405 template <
typename TRet,
typename... Args>
411 template <
typename T,
typename =
void>
415 template <
typename T>
420 template <
typename T,
typename =
void>
424 template <
typename T>
427 typename std::
enable_if< std::is_standard_layout<T>::value, void>::type> : std::true_type {
430 template <
typename T>
433 alignas(
T)
mutable uint8_t _storage[
sizeof(
T)];
436 _CallableWrapperImpl(
const T &value)
438 memset(_storage, 0,
sizeof(_storage));
439 new (_storage)
T(value);
441 _CallableWrapperImpl(
T &&value)
443 memset(_storage, 0,
sizeof(_storage));
444 new (_storage)
T(std::move(value));
446 virtual ~_CallableWrapperImpl()
451 T &GetValue()
const noexcept
453 return *
reinterpret_cast<T *
>(_storage);
455 TRet Invoke(
Args...
args)
const override
457 return GetValue()(std::forward<Args>(
args)...);
461 return new _CallableWrapperImpl(GetValue());
463 virtual std::type_index GetType()
const override
469 return EqualsImpl(
other);
471 template <
typename U = T>
473 ->
typename std::enable_if<_IsEqualityComparable<U>::value,
bool>::type
475 if (
this == &
other) {
478 if (GetType() !=
other.GetType()) {
484 template <
typename U = T>
488 if (
this == &
other) {
491 if (GetType() !=
other.GetType()) {
497 template <
typename U = T>
501 return this == &
other;
507 _CallableWrapperImpl(
const _CallableWrapperImpl &) =
delete;
508 _CallableWrapperImpl(_CallableWrapperImpl &&) =
delete;
509 _CallableWrapperImpl &operator=(
const _CallableWrapperImpl &) =
delete;
510 _CallableWrapperImpl &operator=(_CallableWrapperImpl &&) =
delete;
513 template <
typename T>
516 template <
typename T>
520 TRet (
T::*func)(
Args...);
523 _MemberFuncWrapper(
T &obj, TRet (
T::*func)(
Args...))
524 : obj(&obj), func(func)
527 TRet Invoke(
Args...
args)
const override
529 return (obj->*func)(std::forward<Args>(
args)...);
533 return new _MemberFuncWrapper(*obj, func);
535 virtual std::type_index GetType()
const override
541 if (
this == &
other) {
544 if (GetType() !=
other.GetType()) {
553 _MemberFuncWrapper(
const _MemberFuncWrapper &) =
delete;
554 _MemberFuncWrapper(_MemberFuncWrapper &&) =
delete;
555 _MemberFuncWrapper &operator=(
const _MemberFuncWrapper &) =
delete;
556 _MemberFuncWrapper &operator=(_MemberFuncWrapper &&) =
delete;
559 template <
typename T>
563 TRet (
T::*func)(
Args...)
const;
566 _ConstMemberFuncWrapper(
const T &obj, TRet (
T::*func)(
Args...)
const)
567 : obj(&obj), func(func)
570 TRet Invoke(
Args...
args)
const override
572 return (obj->*func)(std::forward<Args>(
args)...);
576 return new _ConstMemberFuncWrapper(*obj, func);
578 virtual std::type_index GetType()
const override
584 if (
this == &
other) {
587 if (GetType() !=
other.GetType()) {
596 _ConstMemberFuncWrapper(
const _ConstMemberFuncWrapper &) =
delete;
597 _ConstMemberFuncWrapper(_ConstMemberFuncWrapper &&) =
delete;
598 _ConstMemberFuncWrapper &operator=(
const _ConstMemberFuncWrapper &) =
delete;
599 _ConstMemberFuncWrapper &operator=(_ConstMemberFuncWrapper &&) =
delete;
644 template <
typename T>
653 template <
typename T>
664 for (
size_t i = 0;
i <
other._data.Count(); ++
i) {
673 : _data(std::move(
other._data))
682 if (
this == &
other) {
688 for (
size_t i = 0;
i <
other._data.Count(); ++
i) {
691 _data = std::move(
copied);
700 if (
this != &
other) {
701 _data = std::move(
other._data);
719 if (
callable.GetType() == GetType()) {
723 }
else if (
delegate._data.Count() == 1) {
736 if (func !=
nullptr) {
744 template <
typename T>
746 ->
typename std::enable_if<!std::is_base_of<_ICallable, T>::value,
void>::type
754 template <
typename T>
763 template <
typename T>
789 if (
callable.GetType() == GetType()) {
793 }
else if (
delegate._data.Count() == 1) {
807 if (func ==
nullptr) {
818 template <
typename T>
820 ->
typename std::enable_if<!std::is_base_of<_ICallable, T>::value,
bool>::type
830 template <
typename T>
841 template <
typename T>
855 return _InvokeImpl(std::forward<Args>(
args)...);
865 return Equals(
other);
875 return !Equals(
other);
929 template <
typename T>
931 ->
typename std::enable_if<!std::is_base_of<_ICallable, T>::value,
Delegate &>::type
961 template <
typename T>
963 ->
typename std::enable_if<!std::is_base_of<_ICallable, T>::value,
Delegate &>::type
977 return _InvokeImpl(std::forward<Args>(
args)...);
993 virtual std::type_index
GetType()
const override
1005 if (
this == &
other) {
1008 if (GetType() !=
other.GetType()) {
1015 for (
size_t i = _data.
Count();
i > 0; --
i) {
1030 template <
typename U = TRet>
1032 ->
typename std::enable_if<!std::is_void<U>::value, std::vector<U>>::type
1037 _ThrowEmptyDelegateError();
1038 }
else if (
count == 1) {
1039 results.emplace_back(_data[0]->Invoke(std::forward<Args>(
args)...));
1043 for (
size_t i = 0;
i + 1 <
count; ++
i) {
1046 results.emplace_back(list[
count - 1]->Invoke(std::forward<Args>(
args)...));
1055 bool _Remove(
const _ICallable &
callable)
1057 for (
size_t i = _data.
Count();
i > 0; --
i) {
1068 [[noreturn]]
void _ThrowEmptyDelegateError()
const
1070 throw std::runtime_error(
"Delegate is empty");
1078 inline TRet _InvokeImpl(Args... args)
const
1080 size_t count = _data.
Count();
1082 _ThrowEmptyDelegateError();
1083 }
else if (count == 1) {
1084 return _data[0]->Invoke(std::forward<Args>(args)...);
1087 for (
size_t i = 0; i + 1 < count; ++i)
1088 list[i]->Invoke(args...);
1089 return list[count - 1]->Invoke(std::forward<Args>(args)...);
1100 template <
typename TRet,
typename... Args>
1103 return d ==
nullptr;
1110 template <
typename TRet,
typename... Args>
1113 return d !=
nullptr;
1121 template <
typename... Args>
1127 template <
typename T>
1135 template <
typename...>
1141 template <
typename Last>
1144 using TArgsTuple = std::tuple<>;
1150 template <
typename First,
typename...
Rest>
1159 template <
typename TArgsTuple>
1165 template <
typename...
Args>
1167 template <
typename TRet>
1174 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:279
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:334
void Clear() noexcept
清空当前存储的可调用对象
Definition Delegate.h:227
TCallable * GetAt(size_t index) const noexcept
获取指定索引处的可调用对象
Definition Delegate.h:314
~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:407
Delegate(const T &obj, TRet(T::*func)(Args...) const)
构造函数,接受一个常量成员函数指针
Definition Delegate.h:654
void Add(TRet(*func)(Args...))
添加一个函数指针到委托中
Definition Delegate.h:734
void Add(const T &obj, TRet(T::*func)(Args...) const)
添加一个常量成员函数指针到委托中
Definition Delegate.h:764
Delegate & operator+=(TRet(*func)(Args...))
添加一个函数指针到委托中
Definition Delegate.h:919
Delegate(const Delegate &other)
拷贝构造函数
Definition Delegate.h:662
Delegate(std::nullptr_t=nullptr)
默认构造函数
Definition Delegate.h:612
auto InvokeAll(Args... args) const -> typename std::enable_if<!std::is_void< U >::value, std::vector< U > >::type
调用所有存储的可调用对象,并返回它们的结果
Definition Delegate.h:1031
Delegate & operator-=(TRet(*func)(Args...))
移除一个函数指针
Definition Delegate.h:951
Delegate(T &obj, TRet(T::*func)(Args...))
构造函数,接受一个成员函数指针
Definition Delegate.h:645
Delegate & operator=(Delegate &&other) noexcept
移动赋值运算符
Definition Delegate.h:698
virtual std::type_index GetType() const override
获取当前委托的类型信息
Definition Delegate.h:993
Delegate & operator=(const Delegate &other)
拷贝赋值运算符
Definition Delegate.h:680
Delegate(const T &callable)
构造函数,接受一个可调用对象
Definition Delegate.h:636
bool operator!=(const Delegate &other) const
判断当前委托是否不等于另一个委托
Definition Delegate.h:873
bool Remove(const ICallable< TRet(Args...)> &callable)
移除一个可调用对象
Definition Delegate.h:787
bool operator==(std::nullptr_t) const noexcept
判断当前委托是否等于nullptr
Definition Delegate.h:882
bool Remove(TRet(*func)(Args...))
移除一个函数指针
Definition Delegate.h:805
auto operator-=(const T &callable) -> typename std::enable_if<!std::is_base_of< _ICallable, T >::value, Delegate & >::type
移除一个可调用对象
Definition Delegate.h:962
virtual ICallable< TRet(Args...)> * Clone() const override
克隆当前委托
Definition Delegate.h:984
bool operator==(const Delegate &other) const
判断当前委托是否等于另一个委托
Definition Delegate.h:863
void Clear()
清空委托中的所有可调用对象
Definition Delegate.h:772
Delegate & operator+=(const ICallable< TRet(Args...)> &callable)
添加一个可调用对象到委托中
Definition Delegate.h:909
Delegate(Delegate &&other) noexcept
移动构造函数
Definition Delegate.h:672
auto operator+=(const T &callable) -> typename std::enable_if<!std::is_base_of< _ICallable, T >::value, Delegate & >::type
添加一个可调用对象到委托中
Definition Delegate.h:930
auto Remove(const T &callable) -> typename std::enable_if<!std::is_base_of< _ICallable, T >::value, bool >::type
移除一个可调用对象
Definition Delegate.h:819
virtual TRet Invoke(Args... args) const override
调用委托,执行所有存储的可调用对象
Definition Delegate.h:975
bool operator!=(std::nullptr_t) const noexcept
判断当前委托是否不等于nullptr
Definition Delegate.h:891
void Add(const ICallable< TRet(Args...)> &callable)
添加一个可调用对象到委托中
Definition Delegate.h:717
Delegate(TRet(*func)(Args...))
构造函数,接受一个函数指针
Definition Delegate.h:627
virtual bool Equals(const ICallable< TRet(Args...)> &other) const override
判断当前委托是否与另一个可调用对象相等
Definition Delegate.h:1003
bool Remove(const T &obj, TRet(T::*func)(Args...) const)
移除一个常量成员函数指针
Definition Delegate.h:842
Delegate & operator-=(const ICallable< TRet(Args...)> &callable)
移除一个可调用对象
Definition Delegate.h:941
auto Add(const T &callable) -> typename std::enable_if<!std::is_base_of< _ICallable, T >::value, void >::type
添加一个可调用对象到委托中
Definition Delegate.h:745
Delegate(const ICallable< TRet(Args...)> &callable)
构造函数,接受一个可调用对象
Definition Delegate.h:619
bool Remove(T &obj, TRet(T::*func)(Args...))
移除一个成员函数指针
Definition Delegate.h:831
void Add(T &obj, TRet(T::*func)(Args...))
添加一个成员函数指针到委托中
Definition Delegate.h:755
TRet operator()(Args... args) const
调用委托,执行所有存储的可调用对象
Definition Delegate.h:853
值转换器接口
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:1175
bool operator==(std::nullptr_t, const Delegate< TRet(Args...)> &d) noexcept
比较委托和nullptr
Definition Delegate.h:1101
bool operator!=(std::nullptr_t, const Delegate< TRet(Args...)> &d) noexcept
比较委托和nullptr
Definition Delegate.h:1111
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:1136
_FuncTypeHelper模板,用于根据参数元组生成对应的Func类型
Definition Delegate.h:1160