16 template<
class return_t,
class function_t>
21 virtual return_t invoke(
const function_t&
function) noexcept = 0;
24 template<
class return_t,
class function_t,
class invoke_function_t>
28 invoker(
const invoke_function_t& invoke) noexcept : m_InvokeFunction(invoke)
33 virtual return_t invoke(
const function_t&
function) noexcept
override;
36 const invoke_function_t& m_InvokeFunction;
39 template<
class return_t,
class function_t,
class invoke_function_t>
42 return m_InvokeFunction(
function);
45 template<
class derived_t,
class return_t,
class... args_t>
46 base_delegate<derived_t, return_t, args_t...>::base_delegate(base_delegate&& other) noexcept
48 *
this = std::move(other);
51 template<
class derived_t,
class return_t,
class... args_t>
52 typename base_delegate<derived_t, return_t, args_t...>::base_delegate& base_delegate<derived_t, return_t, args_t...>::
53 operator=(base_delegate&& other) noexcept
55 std::swap(m_optFunctions, other.m_optFunctions);
56 std::swap(m_WeakSubscriptions, other.m_WeakSubscriptions);
57 std::swap(m_pDelegateAliveMarker, other.m_pDelegateAliveMarker);
59 if (m_pDelegateAliveMarker)
60 *m_pDelegateAliveMarker =
this;
62 if (other.m_pDelegateAliveMarker)
63 *other.m_pDelegateAliveMarker = &other;
68 template<
class derived_t,
class return_t,
class... args_t>
69 template<
class... creation_args_t>
73 if constexpr (
requires(derived_t d) { d.add_weak(args...); })
75 if constexpr (
requires(derived_t d) { d.add_token(args...); })
83 template<
class derived_t,
class return_t,
class... args_t>
84 template<callable_c<return_t, args_t...> callable_t>
91 add_function(key, std::move(callable));
95 template<
class derived_t,
class return_t,
class... args_t>
96 template<
class object_t>
99 return_t (object_t::*pMethod)(args_t...),
104 [pMethod, &
object](args_t... args)
106 return (object.*pMethod)(std::forward<args_t>(args)...);
111 template<
class derived_t,
class return_t,
class... args_t>
112 template<callable_c<return_t, args_t...> callable_t>
121 template<
class derived_t,
class return_t,
class... args_t>
122 template<
class object_t>
125 return_t (object_t::*pMethod)(args_t...),
128 return add_destruction_callback(
131 [&
object, pMethod](args_t... args)
133 return (object.*pMethod)(std::forward<args_t>(args)...);
137 template<
class derived_t,
class return_t,
class... args_t>
138 template<
class object_t, callable_c<return_t, args_t...> callable_t>
140 std::weak_ptr<object_t> pWeakObject,
147 std::move(pWeakObject),
149 [callable_ = std::move(callable)](object_t*, args_t... args)
151 return callable_(std::forward<args_t>(args)...);
156 template<
class derived_t,
class return_t,
class... args_t>
157 template<
class object_t>
159 std::weak_ptr<object_t> pWeakObject,
160 return_t (object_t::*pMethod)(args_t...),
166 std::move(pWeakObject),
168 [pMethod](object_t* pObject, args_t... args)
170 return (pObject->*pMethod)(std::forward<args_t>(args)...);
175 template<
class derived_t,
class return_t,
class... args_t>
178 m_WeakSubscriptions.erase(token);
179 return remove_function(token);
182 template<
class derived_t,
class return_t,
class... args_t>
185 return m_optFunctions
187 [
this, token]<
class T>(T& value)
189 if constexpr (std::is_same_v<T, single_value_type>)
191 if (value.first == token)
193 m_optFunctions.reset();
201 else if constexpr (std::is_same_v<T, container_type>)
203 return value.erase(token) == 1;
214 template<
class derived_t,
class return_t,
class... args_t>
217 m_optFunctions.reset();
218 m_WeakSubscriptions.clear();
221 template<
class derived_t,
class return_t,
class... args_t>
224 for (
auto it = m_WeakSubscriptions.begin(); it != m_WeakSubscriptions.end();)
226 if (it->second.expired())
229 it = m_WeakSubscriptions.erase(it);
230 remove_function(token);
239 template<
class derived_t,
class return_t,
class... args_t>
246 []<
class T>(
const T& value) ->
size_t
248 if constexpr (std::is_same_v<T, single_value_type>)
252 else if constexpr (std::is_same_v<T, container_type>)
265 template<
class derived_t,
class return_t,
class... args_t>
271 template<
class derived_t,
class return_t,
class... args_t>
277 template<
class derived_t,
class return_t,
class... args_t>
278 template<
class invoke_single_t,
class invoke_multiple_t>
280 const invoke_single_t& invokeSingle,
281 const invoke_multiple_t& invokeMultiple) noexcept
286 if constexpr (std::is_void_v<return_t>)
292 #if (QX_CLANG || QX_APPLE_CLANG) && QX_LIBCPP
297 thread_local variant_type tempFunctions;
298 if (std::holds_alternative<single_value_type>(*m_optFunctions))
300 tempFunctions.template emplace<single_value_type>(std::get<single_value_type>(*m_optFunctions));
301 return invokeSingle(std::get<single_value_type>(tempFunctions).second);
305 const container_type& source = std::get<container_type>(*m_optFunctions);
306 if (!std::holds_alternative<container_type>(tempFunctions))
307 tempFunctions.template emplace<container_type>();
308 container_type& container = std::get<container_type>(tempFunctions);
309 bool sameKeys = source.size() == container.size();
312 auto sourceIt = source.begin();
313 auto targetIt = container.begin();
314 for (
size_t i = 0; i < source.size(); ++i, ++sourceIt, ++targetIt)
315 if (!(sourceIt->first == targetIt->first))
322 auto sourceIt = source.begin();
325 auto targetIt = container.begin();
326 for (
size_t i = 0; i < source.size(); ++i, ++sourceIt, ++targetIt)
327 targetIt->second = sourceIt->second;
332 for (
size_t i = 0; i < source.size(); ++i, ++sourceIt)
333 container.try_emplace(sourceIt->first, sourceIt->second);
339 if (container.size() == 1)
343 if constexpr (std::is_void_v<return_t>)
346 for (
auto it = container.begin(); index < container.size(); ++it, ++index)
351 else if constexpr (delegate_pipe_c<return_t>)
355 for (
auto it = container.begin(); index < container.size(); ++it, ++index)
356 result = result |
invoker->invoke(it->second);
364 for (
auto it = container.begin(); index < container.size(); ++it, ++index)
365 result =
invoker->invoke(it->second);
372 thread_local variant_type tempFunctions;
373 tempFunctions = *m_optFunctions;
375 if (std::holds_alternative<single_value_type>(tempFunctions))
377 const auto& [_,
function] = std::get<single_value_type>(tempFunctions);
378 return invokeSingle(
function);
382 const container_type& container = std::get<container_type>(tempFunctions);
387 if (container.size() == 1)
392 if constexpr (std::is_void_v<return_t>)
394 for (
const auto& [_,
function] : container)
399 else if constexpr (delegate_pipe_c<return_t>)
402 for (
const auto& [_,
function] : container)
403 result = result |
invoker->invoke(
function);
410 for (
const auto& [_,
function] : container)
411 result =
invoker->invoke(
function);
419 template<
class derived_t,
class return_t,
class... args_t>
420 template<
class object_t, callable_c<return_t, object_t*, args_t...> callable_t>
423 std::weak_ptr<object_t> pWeakObject,
424 callable_t callable) noexcept
426 m_WeakSubscriptions.emplace(key, pWeakObject);
429 [pWeakObject, callable_ = std::move(callable)](args_t... args)
431 if (std::shared_ptr<object_t> pObject = pWeakObject.lock())
432 return callable_(pObject.get(), std::forward<args_t>(args)...);
438 template<
class derived_t,
class return_t,
class... args_t>
441 function_type value) noexcept
443 add_function(key, std::move(value));
445 if (!m_pDelegateAliveMarker)
446 m_pDelegateAliveMarker = std::make_shared<base_delegate*>(
this);
448 return [key, pDelegateAliveMarker = std::weak_ptr(m_pDelegateAliveMarker)]()
450 if (
const auto pDelegate = pDelegateAliveMarker.lock())
451 (*pDelegate)->remove(key);
455 template<
class derived_t,
class return_t,
class... args_t>
456 void base_delegate<derived_t, return_t, args_t...>::add_function(
457 time_ordered_priority_key key,
458 function_type
function) noexcept
462 m_optFunctions = variant_type(single_value_type(key, std::move(
function)));
464 else if (std::holds_alternative<single_value_type>(*m_optFunctions))
466 single_value_type first = std::get<single_value_type>(std::move(*m_optFunctions));
467 container_type container;
468 container.try_emplace(first.first, std::move(first.second));
469 container.try_emplace(key, std::move(
function));
470 m_optFunctions = std::move(container);
474 std::get<container_type>(*m_optFunctions).try_emplace(key, std::move(
function));
480 template<
class return_t,
class... args_t>
481 requires(
sizeof...(args_t) > 0 && (!std::is_void_v<args_t> && ...))
482 return_t delegate<return_t(args_t...)>::execute(args_t... args) noexcept
484 return this->execute_internal(
485 [&args...](
const typename super_type::function_type&
function)
487 return function(std::forward<args_t>(args)...);
489 [&args...](
const typename super_type::function_type&
function)
491 return function(args...);
495 template<
class return_t>
496 return_t delegate<return_t(
void)>::execute() noexcept
498 return this->execute_internal(
499 [](
const typename super_type::function_type&
function)
503 [](
const typename super_type::function_type&
function)
Single or multicast delegate.
Class for RAII: functor passed in constructor will be called in destructor.
void clear_dead() noexcept
Remove weak subscriptions whose tracked objects have expired, without invoking callbacks.
delegate_token_type add_weak(std::weak_ptr< object_t > pWeakObject, callable_t callable, priority ePriority=priority::normal) noexcept
Add a callable that will be executed only if the appropriate weak object is valid.
void clear() noexcept
Clear all the callables in this delegate.
destruction_callback add_destruction_callback(callable_t callable, priority ePriority=priority::normal) noexcept
Add a callable that will be removed from the delegate when its destruction callback is destroyed.
bool empty() const noexcept
Check if this delegate is empty.
size_t size() const noexcept
Get the number of functions bound to this delegate.
static derived_t create_singlecast(creation_args_t... args) noexcept
Create a singlecast delegate.
bool remove(delegate_token_type token) noexcept
Remove a callable using its token.
delegate_token_type add_token(callable_t callable, priority ePriority=priority::normal) noexcept
Add a callable using a token with manual unsubscribing.
return_t execute_internal(const invoke_single_t &invokeSingle, const invoke_multiple_t &invokeMultiple) noexcept
Execute all callables.
bool is_singlecast() const noexcept
Check whether exactly one callable is bound to this delegate.
A class that can be used as a key in ordered containers so that items are ordered in descending order...
requires(same_variadic_args_v< args_t... >) const expr auto coalesce(args_t &&... args)
Coalesce function, C# a ?? b analogue.
priority
User may use the predefined values or the custom ones, for ex. "normal - 1", this type is supposed to...
#define QX_STATIC_ASSERT_NO_INSTANTIATION(Message)
This static assert will fail if block it placed in must not be instantiated.