| 1 | // |
| 2 | // StdFunctionDelegate.h |
| 3 | // |
| 4 | // Library: Foundation |
| 5 | // Package: Events |
| 6 | // Module: StdFunctionDelegate |
| 7 | // |
| 8 | // Implementation of the StdFunctionDelegate template. |
| 9 | // |
| 10 | // Copyright (c) 2006-2011, Applied Informatics Software Engineering GmbH. |
| 11 | // and Contributors. |
| 12 | // |
| 13 | // SPDX-License-Identifier: BSL-1.0 |
| 14 | // |
| 15 | |
| 16 | |
| 17 | #ifndef Foundation_StdFunctionDelegate_INCLUDED |
| 18 | #define Foundation_StdFunctionDelegate_INCLUDED |
| 19 | |
| 20 | |
| 21 | #include "Poco/Foundation.h" |
| 22 | #include "Poco/AbstractDelegate.h" |
| 23 | #include "Poco/Mutex.h" |
| 24 | #include <functional> |
| 25 | #include <atomic> |
| 26 | |
| 27 | |
| 28 | namespace Poco { |
| 29 | |
| 30 | |
| 31 | template <class TArgs, bool hasSender = true, bool senderIsConst = true> |
| 32 | class StdFunctionDelegate: public Poco::AbstractDelegate<TArgs> |
| 33 | /// Wraps a std::function or lambda |
| 34 | /// for use as a Delegate. |
| 35 | { |
| 36 | public: |
| 37 | typedef std::function<void (const void*, TArgs&)> NotifyMethod; |
| 38 | |
| 39 | StdFunctionDelegate() = delete; |
| 40 | |
| 41 | StdFunctionDelegate(NotifyMethod method): |
| 42 | _receiverMethod(method), _id(++id_generator) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | StdFunctionDelegate(const StdFunctionDelegate& delegate): |
| 47 | Poco::AbstractDelegate<TArgs>(delegate), |
| 48 | _receiverMethod(delegate._receiverMethod), |
| 49 | _id(delegate._id) |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | ~StdFunctionDelegate() |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | StdFunctionDelegate& operator = (const StdFunctionDelegate& delegate) |
| 58 | { |
| 59 | if (&delegate != this) |
| 60 | { |
| 61 | this->_pTarget = delegate._pTarget; |
| 62 | this->_receiverMethod = delegate._receiverMethod; |
| 63 | this->_id = delegate._id; |
| 64 | } |
| 65 | return *this; |
| 66 | } |
| 67 | |
| 68 | bool notify(const void* sender, TArgs& arguments) |
| 69 | { |
| 70 | Poco::Mutex::ScopedLock lock(_mutex); |
| 71 | if (_receiverMethod) |
| 72 | { |
| 73 | _receiverMethod(sender, arguments); |
| 74 | return true; |
| 75 | } |
| 76 | else return false; |
| 77 | } |
| 78 | |
| 79 | |
| 80 | bool equals(const Poco::AbstractDelegate<TArgs>& other) const |
| 81 | { |
| 82 | const StdFunctionDelegate* pOtherDelegate = dynamic_cast<const StdFunctionDelegate*>(other.unwrap()); |
| 83 | return pOtherDelegate && _id == pOtherDelegate->_id; |
| 84 | } |
| 85 | |
| 86 | Poco::AbstractDelegate<TArgs>* clone() const |
| 87 | { |
| 88 | return new StdFunctionDelegate(*this); |
| 89 | } |
| 90 | |
| 91 | void disable() |
| 92 | { |
| 93 | Poco::Mutex::ScopedLock lock(_mutex); |
| 94 | _receiverMethod = nullptr; |
| 95 | } |
| 96 | |
| 97 | protected: |
| 98 | NotifyMethod _receiverMethod; |
| 99 | Poco::Mutex _mutex; |
| 100 | int _id; |
| 101 | |
| 102 | private: |
| 103 | static std::atomic_int id_generator; |
| 104 | }; |
| 105 | |
| 106 | |
| 107 | template <class TArgs, bool hasSender, bool senderIsConst> |
| 108 | std::atomic_int StdFunctionDelegate<TArgs, hasSender, senderIsConst>::id_generator; |
| 109 | |
| 110 | |
| 111 | } // namespace Poco |
| 112 | |
| 113 | |
| 114 | #endif // Foundation_StdFunctionDelegate_INCLUDED |
| 115 | |