ActiveRunnable.h 4.6 KB

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