// // FunctionPriorityDelegate.h // // Library: Foundation // Package: Events // Module: FunctionPriorityDelegate // // Implementation of the FunctionPriorityDelegate template. // // Copyright (c) 2006-2025, Applied Informatics Software Engineering GmbH. // and Contributors. // // SPDX-License-Identifier: BSL-1.0 // #ifndef Foundation_FunctionPriorityDelegate_INCLUDED #define Foundation_FunctionPriorityDelegate_INCLUDED #include "Poco/Foundation.h" #include "Poco/AbstractPriorityDelegate.h" #include "Poco/Mutex.h" #include namespace Poco { namespace Detail { /// Helper to select the correct function pointer type based on template parameters. template struct FunctionPriorityDelegateNotifyType; template struct FunctionPriorityDelegateNotifyType { using Type = void (*)(const void*, TArgs&); }; template struct FunctionPriorityDelegateNotifyType { using Type = void (*)(void*, TArgs&); }; template struct FunctionPriorityDelegateNotifyType { using Type = void (*)(TArgs&); }; template <> struct FunctionPriorityDelegateNotifyType { using Type = void (*)(const void*); }; template <> struct FunctionPriorityDelegateNotifyType { using Type = void (*)(void*); }; template struct FunctionPriorityDelegateNotifyType { using Type = void (*)(); }; } // namespace Detail template class FunctionPriorityDelegate: public AbstractPriorityDelegate /// Wraps a freestanding function or static member function /// for use as a PriorityDelegate. { public: using NotifyFunction = typename Detail::FunctionPriorityDelegateNotifyType::Type; FunctionPriorityDelegate(NotifyFunction function, int prio): AbstractPriorityDelegate(prio), _function(function) { } FunctionPriorityDelegate(const FunctionPriorityDelegate& delegate): AbstractPriorityDelegate(delegate), _function(delegate._function) { } FunctionPriorityDelegate& operator = (const FunctionPriorityDelegate& delegate) { if (&delegate != this) { this->_function = delegate._function; this->_priority = delegate._priority; } return *this; } ~FunctionPriorityDelegate() = default; FunctionPriorityDelegate() = delete; bool notify(const void* sender, TArgs& arguments) { Mutex::ScopedLock lock(_mutex); if (_function) { if constexpr (hasSender) { if constexpr (senderIsConst) { (*_function)(sender, arguments); } else { (*_function)(const_cast(sender), arguments); } } else { (*_function)(arguments); } return true; } else return false; } bool equals(const AbstractDelegate& other) const { const FunctionPriorityDelegate* pOtherDelegate = dynamic_cast(other.unwrap()); return pOtherDelegate && this->priority() == pOtherDelegate->priority() && _function == pOtherDelegate->_function; } AbstractDelegate* clone() const { return new FunctionPriorityDelegate(*this); } void disable() { Mutex::ScopedLock lock(_mutex); _function = nullptr; } protected: NotifyFunction _function; Mutex _mutex; }; template class FunctionPriorityDelegate: public AbstractPriorityDelegate /// Specialization for void arguments (no TArgs parameter). { public: using NotifyFunction = typename Detail::FunctionPriorityDelegateNotifyType::Type; FunctionPriorityDelegate(NotifyFunction function, int prio): AbstractPriorityDelegate(prio), _function(function) { } FunctionPriorityDelegate(const FunctionPriorityDelegate& delegate): AbstractPriorityDelegate(delegate), _function(delegate._function) { } FunctionPriorityDelegate& operator = (const FunctionPriorityDelegate& delegate) { if (&delegate != this) { this->_function = delegate._function; this->_priority = delegate._priority; } return *this; } ~FunctionPriorityDelegate() override = default; FunctionPriorityDelegate() = delete; bool notify(const void* sender) override { Mutex::ScopedLock lock(_mutex); if (_function) { if constexpr (hasSender) { if constexpr (senderIsConst) { (*_function)(sender); } else { (*_function)(const_cast(sender)); } } else { (*_function)(); } return true; } else return false; } bool equals(const AbstractDelegate& other) const override { const FunctionPriorityDelegate* pOtherDelegate = dynamic_cast(other.unwrap()); return pOtherDelegate && this->priority() == pOtherDelegate->priority() && _function == pOtherDelegate->_function; } AbstractDelegate* clone() const override { return new FunctionPriorityDelegate(*this); } void disable() override { Mutex::ScopedLock lock(_mutex); _function = nullptr; } protected: NotifyFunction _function; Mutex _mutex; }; } // namespace Poco #endif // Foundation_FunctionPriorityDelegate_INCLUDED