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
16
17#ifndef Foundation_ActiveRunnable_INCLUDED
18#define Foundation_ActiveRunnable_INCLUDED
19
20
21#include "Poco/Foundation.h"
22#include "Poco/ActiveResult.h"
23#include "Poco/Runnable.h"
24#include "Poco/RefCountedObject.h"
25#include "Poco/AutoPtr.h"
26#include "Poco/Exception.h"
27
28
29namespace Poco {
30
31
32class ActiveRunnableBase: public Runnable, public RefCountedObject
33 /// The base class for all ActiveRunnable instantiations.
34{
35public:
36 typedef AutoPtr<ActiveRunnableBase> Ptr;
37};
38
39
40template <class ResultType, class ArgType, class OwnerType>
41class ActiveRunnable: public ActiveRunnableBase
42 /// This class is used by ActiveMethod.
43 /// See the ActiveMethod class for more information.
44{
45public:
46 typedef ResultType (OwnerType::*Callback)(const ArgType&);
47 typedef ActiveResult<ResultType> ActiveResultType;
48
49 ActiveRunnable(OwnerType* pOwner, Callback method, const ArgType& arg, const ActiveResultType& result):
50 _pOwner(pOwner),
51 _method(method),
52 _arg(arg),
53 _result(result)
54 {
55 poco_check_ptr (pOwner);
56 }
57
58 void run()
59 {
60 ActiveRunnableBase::Ptr guard(this, false); // ensure automatic release when done
61 try
62 {
63 _result.data(new ResultType((_pOwner->*_method)(_arg)));
64 }
65 catch (Exception& e)
66 {
67 _result.error(e);
68 }
69 catch (std::exception& e)
70 {
71 _result.error(e.what());
72 }
73 catch (...)
74 {
75 _result.error("unknown exception");
76 }
77 _result.notify();
78 }
79
80private:
81 OwnerType* _pOwner;
82 Callback _method;
83 ArgType _arg;
84 ActiveResultType _result;
85};
86
87
88template <class ArgType, class OwnerType>
89class ActiveRunnable<void, ArgType, OwnerType>: public ActiveRunnableBase
90 /// This class is used by ActiveMethod.
91 /// See the ActiveMethod class for more information.
92{
93public:
94 typedef void (OwnerType::*Callback)(const ArgType&);
95 typedef ActiveResult<void> ActiveResultType;
96
97 ActiveRunnable(OwnerType* pOwner, Callback method, const ArgType& arg, const ActiveResultType& result):
98 _pOwner(pOwner),
99 _method(method),
100 _arg(arg),
101 _result(result)
102 {
103 poco_check_ptr (pOwner);
104 }
105
106 void run()
107 {
108 ActiveRunnableBase::Ptr guard(this, false); // ensure automatic release when done
109 try
110 {
111 (_pOwner->*_method)(_arg);
112 }
113 catch (Exception& e)
114 {
115 _result.error(e);
116 }
117 catch (std::exception& e)
118 {
119 _result.error(e.what());
120 }
121 catch (...)
122 {
123 _result.error("unknown exception");
124 }
125 _result.notify();
126 }
127
128private:
129 OwnerType* _pOwner;
130 Callback _method;
131 ArgType _arg;
132 ActiveResultType _result;
133};
134
135
136template <class ResultType, class OwnerType>
137class ActiveRunnable<ResultType, void, OwnerType>: public ActiveRunnableBase
138 /// This class is used by ActiveMethod.
139 /// See the ActiveMethod class for more information.
140{
141public:
142 typedef ResultType (OwnerType::*Callback)();
143 typedef ActiveResult<ResultType> ActiveResultType;
144
145 ActiveRunnable(OwnerType* pOwner, Callback method, const ActiveResultType& result):
146 _pOwner(pOwner),
147 _method(method),
148 _result(result)
149 {
150 poco_check_ptr (pOwner);
151 }
152
153 void run()
154 {
155 ActiveRunnableBase::Ptr guard(this, false); // ensure automatic release when done
156 try
157 {
158 _result.data(new ResultType((_pOwner->*_method)()));
159 }
160 catch (Exception& e)
161 {
162 _result.error(e);
163 }
164 catch (std::exception& e)
165 {
166 _result.error(e.what());
167 }
168 catch (...)
169 {
170 _result.error("unknown exception");
171 }
172 _result.notify();
173 }
174
175private:
176 OwnerType* _pOwner;
177 Callback _method;
178 ActiveResultType _result;
179};
180
181
182template <class OwnerType>
183class ActiveRunnable<void, void, OwnerType>: public ActiveRunnableBase
184 /// This class is used by ActiveMethod.
185 /// See the ActiveMethod class for more information.
186{
187public:
188 typedef void (OwnerType::*Callback)();
189 typedef ActiveResult<void> ActiveResultType;
190
191 ActiveRunnable(OwnerType* pOwner, Callback method, const ActiveResultType& result):
192 _pOwner(pOwner),
193 _method(method),
194 _result(result)
195 {
196 poco_check_ptr (pOwner);
197 }
198
199 void run()
200 {
201 ActiveRunnableBase::Ptr guard(this, false); // ensure automatic release when done
202 try
203 {
204 (_pOwner->*_method)();
205 }
206 catch (Exception& e)
207 {
208 _result.error(e);
209 }
210 catch (std::exception& e)
211 {
212 _result.error(e.what());
213 }
214 catch (...)
215 {
216 _result.error("unknown exception");
217 }
218 _result.notify();
219 }
220
221private:
222 OwnerType* _pOwner;
223 Callback _method;
224 ActiveResultType _result;
225};
226
227
228} // namespace Poco
229
230
231#endif // Foundation_ActiveRunnable_INCLUDED
232