7#ifndef MYGUI_DELEGATE_H_
8#define MYGUI_DELEGATE_H_
28 virtual ~IDelegateUnlink() =
default;
31 m_baseDelegateUnlink(
this)
34 bool compare(IDelegateUnlink* _unlink)
const
36 return m_baseDelegateUnlink == _unlink->m_baseDelegateUnlink;
40 IDelegateUnlink* m_baseDelegateUnlink;
43 template<
typename... Args>
45 template<
typename... Args>
57 DelegateFunctionBase(
const DelegateFunctionBase&) =
delete;
58 DelegateFunctionBase& operator=(
const DelegateFunctionBase&) =
delete;
59 virtual ~DelegateFunctionBase() =
default;
61 bool compare(
const DelegateFunctionBase* _delegate)
const
63 return _delegate && _delegate->mObject == mObject &&
64 _delegate->mFunctionPointer.compare(mFunctionPointer);
66 bool compare(IDelegateUnlink* _unlink)
const
68 return mUnlink == _unlink;
72 DelegateFunctionBase(Any&& _identity,
const void* _object,
const IDelegateUnlink* _unlink) :
75 mFunctionPointer(std::move(_identity))
83 void release()
noexcept
85 if (--mReferences == 0)
89 size_t mReferences{1};
90 DelegateFunctionBase* mNextRetired{
nullptr};
91 const void* mOwner{
nullptr};
92 const IDelegateUnlink* mUnlink{
nullptr};
93 const void* mObject{
nullptr};
103 mDelegate(std::exchange(_other.mDelegate,
nullptr))
110 *
this = std::exchange(_other.mDelegate,
nullptr);
127 if (
auto* old = std::exchange(
mDelegate,
nullptr))
136 _delegate->release();
141 if (
auto* old = std::exchange(
mDelegate, _delegate))
153 ++mDelegate->mReferences;
159 mDelegate->release();
169 bool mDestroying{
false};
177 mState(std::exchange(_other.mState,
nullptr))
184 if (
auto* state = std::exchange(
mState,
nullptr))
187 state->closed =
true;
207 void clear(IDelegateUnlink* _unlink)
209 if (
auto* state =
mState; state && _unlink)
212 for (
size_t i = 0; i < state->callbacks.size(); ++i)
214 auto* callback = state->callbacks[i];
215 if (callback && callback->compare(_unlink))
234 state->add(_delegate);
245 std::unique_ptr<DelegateFunctionBase> token(_delegate->mOwner ?
nullptr : _delegate);
246 const auto index = state->find(_delegate);
247 if (index != state->callbacks.size())
248 state->remove(index);
250 else if (!_delegate->mOwner)
269 state->collectIfIdle();
271 state->add(_delegate);
286 size_t find(DelegateFunctionBase* _delegate)
const
288 for (
size_t i = 0; i <
callbacks.size(); ++i)
292 if (callback && callback->compare(_delegate) &&
callbacks[i] == callback)
298 void add(DelegateFunctionBase* _delegate)
307 const auto index =
find(_delegate);
308 if (_delegate->mOwner || index !=
callbacks.size())
317 _delegate->mOwner =
this;
323 auto* callback = std::exchange(
callbacks[_index],
nullptr);
334 for (
size_t i = 0; i <
callbacks.size(); ++i)
347 auto* removed = std::exchange(
retired,
nullptr);
352 auto* next = std::exchange(removed->mNextRetired,
nullptr);
367 ++mState->references;
374 mState->collectIfIdle();
375 if (--mState->references == 0)
386 bool mDestroying{
false};
392 template<
typename... Args>
404 mInvoke(
this, std::forward<Args>(args)...);
409 return DelegateFunctionBase::compare(_delegate);
414 return DelegateFunctionBase::compare(_unlink);
426 const IDelegateUnlink* _unlink) :
427 DelegateFunctionBase(std::move(_identity), _object, _unlink),
440 template<
typename Result,
typename Callable>
443 template<
typename Callable,
typename... Args>
447 Storage(Callable _function, Any _identity,
const void* _object,
const IDelegateUnlink* _unlink) :
448 Result(&
invoke, std::move(_identity), _object, _unlink),
455 std::invoke(
static_cast<Storage*
>(_self)->
function, std::forward<Args>(args)...);
461 template<
typename Result,
typename Callable>
465 const void* _object =
nullptr,
466 const IDelegateUnlink* _unlink =
nullptr)
471 template<
typename Result,
typename Callable>
472 static Result*
create(Callable _function, Any _identity,
const IDelegateUnlink* _object)
474 return create<Result>(std::move(_function), std::move(_identity), _object, _object);
483 template<
typename... Args>
488 std::function<void(Args...)>{},
494 template<
typename T,
typename... Args,
typename Owner>
498 [_object, _method](Args&&... args) { std::invoke(_method, _object, std::forward<Args>(args)...); },
502 template<
typename T,
typename... Args,
typename Owner>
505 void (Owner::*_method)(Args... args)
const)
508 [_object, _method](Args&&... args) { std::invoke(_method, _object, std::forward<Args>(args)...); },
516 template<
typename... Args>
518 const std::function<
void(Args...)>& _function,
526 template<
typename R,
typename C,
typename... Args>
535 template<
typename TLambda>
536 inline auto newDelegate(
const TLambda& _function, int64_t delegateId)
546 template<
typename... Args>
569 static_cast<IDelegate*
>(delegate)->invoke(std::forward<Args>(args)...);
574 template<
typename... Args>
606 if (!state || state->active == 0)
613 if (
auto* callback = state->callbacks[i])
614 static_cast<IDelegate*
>(callback)->invoke(args...);
615 }
while (++i < state->extent);
619#ifndef MYGUI_DONT_USE_OBSOLETE
621 template<
typename... Args>
623 template<
typename... Args>
625 template<
typename... Args>
627 template<
typename... Args>
629 template<
typename... Args>
631 template<
typename... Args>
635 template<
typename... Args>
637 template<
typename... Args>
639 template<
typename... Args>
641 template<
typename... Args>
643 template<
typename... Args>
645 template<
typename... Args>
#define MYGUI_EXCEPT(dest)
#define MYGUI_OBSOLETE(text)
void invoke(Args... args)
DelegateFunction & operator=(const DelegateFunction &)=delete
bool compare(DelegateFunction *_delegate) const
std::function< void(Args...)> Function
DelegateFunction(const DelegateFunction &)=delete
bool compare(IDelegateUnlink *_unlink) const
~DelegateFunction() override=default
void operator()(Args... args) const
DelegateFunction< Args... > IDelegate
Delegate(Delegate &&) noexcept=default
std::list< std::unique_ptr< IDelegate > > ListDelegate
void operator-=(IDelegate *_delegate)
DelegateFunction< Args... > IDelegate
MultiDelegate(MultiDelegate &&) noexcept=default
void operator()(Args... args) const
Invocation(const Invocation &)=delete
Invocation & operator=(const Invocation &)=delete
Invocation(DelegateFunctionBase *_delegate)
DelegateBase & operator=(DelegateFunctionBase *_delegate)
DelegateFunctionBase * mDelegate
DelegateBase & operator=(DelegateBase &&_other) noexcept
DelegateBase(DelegateBase &&_other) noexcept
Operation & operator=(const Operation &)=delete
Operation(const Operation &)=delete
void clear(IDelegateUnlink *_unlink)
MultiDelegateBase & operator=(DelegateFunctionBase *_delegate)
void operator+=(DelegateFunctionBase *_delegate)
MultiDelegateBase()=default
void operator-=(DelegateFunctionBase *_delegate)
MultiDelegateBase(MultiDelegateBase &&_other) noexcept
MultiDelegate< Args... > CMultiDelegate6
MultiDelegate< Args... > CMultiDelegate1
MultiDelegate< Args... > CMultiDelegate2
MultiDelegate< Args... > CMultiDelegate3
Delegate< Args... > CDelegate3
Delegate< Args... > CDelegate1
Delegate< Args... > CDelegate2
MultiDelegate<> CMultiDelegate0
MultiDelegate< Args... > CMultiDelegate4
Delegate< Args... > CDelegate5
Delegate< Args... > CDelegate6
Delegate< Args... > CDelegate4
MultiDelegate< Args... > CMultiDelegate5
delegates::DelegateFunction< Args... > * newDelegate(void(*_func)(Args... args))
MyGUI::delegates::DelegateFunction< Args... > type
static void invoke(Result *_self, Args &&... args)
DelegateFunction< Args... > Result
Storage(Callable _function, Any _identity, const void *_object, const IDelegateUnlink *_unlink)
static Result * create(Callable _function, Any _identity, const void *_object=nullptr, const IDelegateUnlink *_unlink=nullptr)
static Result * create(Callable _function, Any _identity, const IDelegateUnlink *_object)
DelegateFunctionBase * retired
void remove(size_t _index)
void add(DelegateFunctionBase *_delegate)
DelegateFunctionBase * retiredTail
size_t find(DelegateFunctionBase *_delegate) const
std::vector< DelegateFunctionBase * > callbacks