ActiveRunnable.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //
  2. // ActiveRunnable.h
  3. //
  4. // Library: Foundation
  5. // Package: Threading
  6. // Module: ActiveObjects
  7. //
  8. // Definition of the ActiveRunnable class.
  9. //
  10. // Copyright (c) 2004-2007, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Foundation_ActiveRunnable_INCLUDED
  16. #define Foundation_ActiveRunnable_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #include "Poco/ActiveResult.h"
  19. #include "Poco/Runnable.h"
  20. #include "Poco/RefCountedObject.h"
  21. #include "Poco/AutoPtr.h"
  22. #include "Poco/Exception.h"
  23. namespace Poco {
  24. class ActiveRunnableBase: public Runnable, public RefCountedObject
  25. /// The base class for all ActiveRunnable instantiations.
  26. {
  27. public:
  28. typedef AutoPtr<ActiveRunnableBase> Ptr;
  29. };
  30. template <class ResultType, class ArgType, class OwnerType>
  31. class ActiveRunnable: public ActiveRunnableBase
  32. /// This class is used by ActiveMethod.
  33. /// See the ActiveMethod class for more information.
  34. {
  35. public:
  36. typedef ResultType (OwnerType::*Callback)(const ArgType&);
  37. typedef ActiveResult<ResultType> ActiveResultType;
  38. ActiveRunnable(OwnerType* pOwner, Callback method, const ArgType& arg, const ActiveResultType& result):
  39. _pOwner(pOwner),
  40. _method(method),
  41. _arg(arg),
  42. _result(result)
  43. {
  44. poco_check_ptr (pOwner);
  45. }
  46. void run()
  47. {
  48. ActiveRunnableBase::Ptr guard(this, false); // ensure automatic release when done
  49. try
  50. {
  51. _result.data(new ResultType((_pOwner->*_method)(_arg)));
  52. }
  53. catch (Exception& e)
  54. {
  55. _result.error(e);
  56. }
  57. catch (std::exception& e)
  58. {
  59. _result.error(e.what());
  60. }
  61. catch (...)
  62. {
  63. _result.error("unknown exception");
  64. }
  65. _result.notify();
  66. }
  67. private:
  68. OwnerType* _pOwner;
  69. Callback _method;
  70. ArgType _arg;
  71. ActiveResultType _result;
  72. };
  73. template <class ArgType, class OwnerType>
  74. class ActiveRunnable<void, ArgType, OwnerType>: public ActiveRunnableBase
  75. /// This class is used by ActiveMethod.
  76. /// See the ActiveMethod class for more information.
  77. {
  78. public:
  79. typedef void (OwnerType::*Callback)(const ArgType&);
  80. typedef ActiveResult<void> ActiveResultType;
  81. ActiveRunnable(OwnerType* pOwner, Callback method, const ArgType& arg, const ActiveResultType& result):
  82. _pOwner(pOwner),
  83. _method(method),
  84. _arg(arg),
  85. _result(result)
  86. {
  87. poco_check_ptr (pOwner);
  88. }
  89. void run()
  90. {
  91. ActiveRunnableBase::Ptr guard(this, false); // ensure automatic release when done
  92. try
  93. {
  94. (_pOwner->*_method)(_arg);
  95. }
  96. catch (Exception& e)
  97. {
  98. _result.error(e);
  99. }
  100. catch (std::exception& e)
  101. {
  102. _result.error(e.what());
  103. }
  104. catch (...)
  105. {
  106. _result.error("unknown exception");
  107. }
  108. _result.notify();
  109. }
  110. private:
  111. OwnerType* _pOwner;
  112. Callback _method;
  113. ArgType _arg;
  114. ActiveResultType _result;
  115. };
  116. template <class ResultType, class OwnerType>
  117. class ActiveRunnable<ResultType, void, OwnerType>: public ActiveRunnableBase
  118. /// This class is used by ActiveMethod.
  119. /// See the ActiveMethod class for more information.
  120. {
  121. public:
  122. typedef ResultType (OwnerType::*Callback)();
  123. typedef ActiveResult<ResultType> ActiveResultType;
  124. ActiveRunnable(OwnerType* pOwner, Callback method, const ActiveResultType& result):
  125. _pOwner(pOwner),
  126. _method(method),
  127. _result(result)
  128. {
  129. poco_check_ptr (pOwner);
  130. }
  131. void run()
  132. {
  133. ActiveRunnableBase::Ptr guard(this, false); // ensure automatic release when done
  134. try
  135. {
  136. _result.data(new ResultType((_pOwner->*_method)()));
  137. }
  138. catch (Exception& e)
  139. {
  140. _result.error(e);
  141. }
  142. catch (std::exception& e)
  143. {
  144. _result.error(e.what());
  145. }
  146. catch (...)
  147. {
  148. _result.error("unknown exception");
  149. }
  150. _result.notify();
  151. }
  152. private:
  153. OwnerType* _pOwner;
  154. Callback _method;
  155. ActiveResultType _result;
  156. };
  157. template <class OwnerType>
  158. class ActiveRunnable<void, void, OwnerType>: public ActiveRunnableBase
  159. /// This class is used by ActiveMethod.
  160. /// See the ActiveMethod class for more information.
  161. {
  162. public:
  163. typedef void (OwnerType::*Callback)();
  164. typedef ActiveResult<void> ActiveResultType;
  165. ActiveRunnable(OwnerType* pOwner, Callback method, const ActiveResultType& result):
  166. _pOwner(pOwner),
  167. _method(method),
  168. _result(result)
  169. {
  170. poco_check_ptr (pOwner);
  171. }
  172. void run()
  173. {
  174. ActiveRunnableBase::Ptr guard(this, false); // ensure automatic release when done
  175. try
  176. {
  177. (_pOwner->*_method)();
  178. }
  179. catch (Exception& e)
  180. {
  181. _result.error(e);
  182. }
  183. catch (std::exception& e)
  184. {
  185. _result.error(e.what());
  186. }
  187. catch (...)
  188. {
  189. _result.error("unknown exception");
  190. }
  191. _result.notify();
  192. }
  193. private:
  194. OwnerType* _pOwner;
  195. Callback _method;
  196. ActiveResultType _result;
  197. };
  198. } // namespace Poco
  199. #endif // Foundation_ActiveRunnable_INCLUDED