RunnableAdapter.h 1.5 KB

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