| 1 | // |
| 2 | // RunnableAdapter.h |
| 3 | // |
| 4 | // Library: Foundation |
| 5 | // Package: Threading |
| 6 | // Module: Thread |
| 7 | // |
| 8 | // Definition of the RunnableAdapter template class. |
| 9 | // |
| 10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
| 11 | // and Contributors. |
| 12 | // |
| 13 | // SPDX-License-Identifier: BSL-1.0 |
| 14 | // |
| 15 | |
| 16 | |
| 17 | #ifndef Foundation_RunnableAdapter_INCLUDED |
| 18 | #define Foundation_RunnableAdapter_INCLUDED |
| 19 | |
| 20 | |
| 21 | #include "Poco/Foundation.h" |
| 22 | #include "Poco/Runnable.h" |
| 23 | |
| 24 | |
| 25 | namespace Poco { |
| 26 | |
| 27 | |
| 28 | template <class C> |
| 29 | class RunnableAdapter: public Runnable |
| 30 | /// This adapter simplifies using ordinary methods as |
| 31 | /// targets for threads. |
| 32 | /// Usage: |
| 33 | /// RunnableAdapter<MyClass> ra(myObject, &MyObject::doSomething)); |
| 34 | /// Thread thr; |
| 35 | /// thr.Start(ra); |
| 36 | /// |
| 37 | /// For using a freestanding or static member function as a thread |
| 38 | /// target, please see the ThreadTarget class. |
| 39 | { |
| 40 | public: |
| 41 | typedef void (C::*Callback)(); |
| 42 | |
| 43 | RunnableAdapter(C& object, Callback method): _pObject(&object), _method(method) |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | RunnableAdapter(const RunnableAdapter& ra): _pObject(ra._pObject), _method(ra._method) |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | ~RunnableAdapter() |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | RunnableAdapter& operator = (const RunnableAdapter& ra) |
| 56 | { |
| 57 | _pObject = ra._pObject; |
| 58 | _method = ra._method; |
| 59 | return *this; |
| 60 | } |
| 61 | |
| 62 | void run() |
| 63 | { |
| 64 | (_pObject->*_method)(); |
| 65 | } |
| 66 | |
| 67 | private: |
| 68 | RunnableAdapter(); |
| 69 | |
| 70 | C* _pObject; |
| 71 | Callback _method; |
| 72 | }; |
| 73 | |
| 74 | |
| 75 | } // namespace Poco |
| 76 | |
| 77 | |
| 78 | #endif // Foundation_RunnableAdapter_INCLUDED |
| 79 | |