1//
2// FunctionPriorityDelegate.h
3//
4// Library: Foundation
5// Package: Events
6// Module: FunctionPriorityDelegate
7//
8// Implementation of the FunctionPriorityDelegate template.
9//
10// Copyright (c) 2006-2011, Applied Informatics Software Engineering GmbH.
11// and Contributors.
12//
13// SPDX-License-Identifier: BSL-1.0
14//
15
16
17#ifndef Foundation_FunctionPriorityDelegate_INCLUDED
18#define Foundation_FunctionPriorityDelegate_INCLUDED
19
20
21#include "Poco/Foundation.h"
22#include "Poco/AbstractPriorityDelegate.h"
23#include "Poco/Mutex.h"
24
25
26namespace Poco {
27
28
29template <class TArgs, bool useSender = true, bool senderIsConst = true>
30class FunctionPriorityDelegate: public AbstractPriorityDelegate<TArgs>
31 /// Wraps a freestanding function or static member function
32 /// for use as a PriorityDelegate.
33{
34public:
35 typedef void (*NotifyFunction)(const void*, TArgs&);
36
37 FunctionPriorityDelegate(NotifyFunction function, int prio):
38 AbstractPriorityDelegate<TArgs>(prio),
39 _function(function)
40 {
41 }
42
43 FunctionPriorityDelegate(const FunctionPriorityDelegate& delegate):
44 AbstractPriorityDelegate<TArgs>(delegate),
45 _function(delegate._function)
46 {
47 }
48
49 FunctionPriorityDelegate& operator = (const FunctionPriorityDelegate& delegate)
50 {
51 if (&delegate != this)
52 {
53 this->_function = delegate._function;
54 this->_priority = delegate._priority;
55 }
56 return *this;
57 }
58
59 ~FunctionPriorityDelegate()
60 {
61 }
62
63 bool notify(const void* sender, TArgs& arguments)
64 {
65 Mutex::ScopedLock lock(_mutex);
66 if (_function)
67 {
68 (*_function)(sender, arguments);
69 return true;
70 }
71 else return false;
72 }
73
74 bool equals(const AbstractDelegate<TArgs>& other) const
75 {
76 const FunctionPriorityDelegate* pOtherDelegate = dynamic_cast<const FunctionPriorityDelegate*>(other.unwrap());
77 return pOtherDelegate && this->priority() == pOtherDelegate->priority() && _function == pOtherDelegate->_function;
78 }
79
80 AbstractDelegate<TArgs>* clone() const
81 {
82 return new FunctionPriorityDelegate(*this);
83 }
84
85 void disable()
86 {
87 Mutex::ScopedLock lock(_mutex);
88 _function = 0;
89 }
90
91protected:
92 NotifyFunction _function;
93 Mutex _mutex;
94
95private:
96 FunctionPriorityDelegate();
97};
98
99
100template <class TArgs>
101class FunctionPriorityDelegate<TArgs, true, false>: public AbstractPriorityDelegate<TArgs>
102{
103public:
104 typedef void (*NotifyFunction)(void*, TArgs&);
105
106 FunctionPriorityDelegate(NotifyFunction function, int prio):
107 AbstractPriorityDelegate<TArgs>(prio),
108 _function(function)
109 {
110 }
111
112 FunctionPriorityDelegate(const FunctionPriorityDelegate& delegate):
113 AbstractPriorityDelegate<TArgs>(delegate),
114 _function(delegate._function)
115 {
116 }
117
118 FunctionPriorityDelegate& operator = (const FunctionPriorityDelegate& delegate)
119 {
120 if (&delegate != this)
121 {
122 this->_function = delegate._function;
123 this->_priority = delegate._priority;
124 }
125 return *this;
126 }
127
128 ~FunctionPriorityDelegate()
129 {
130 }
131
132 bool notify(const void* sender, TArgs& arguments)
133 {
134 Mutex::ScopedLock lock(_mutex);
135 if (_function)
136 {
137 (*_function)(const_cast<void*>(sender), arguments);
138 return true;
139 }
140 else return false;
141 }
142
143 bool equals(const AbstractDelegate<TArgs>& other) const
144 {
145 const FunctionPriorityDelegate* pOtherDelegate = dynamic_cast<const FunctionPriorityDelegate*>(other.unwrap());
146 return pOtherDelegate && this->priority() == pOtherDelegate->priority() && _function == pOtherDelegate->_function;
147 }
148
149 AbstractDelegate<TArgs>* clone() const
150 {
151 return new FunctionPriorityDelegate(*this);
152 }
153
154 void disable()
155 {
156 Mutex::ScopedLock lock(_mutex);
157 _function = 0;
158 }
159
160protected:
161 NotifyFunction _function;
162 Mutex _mutex;
163
164private:
165 FunctionPriorityDelegate();
166};
167
168
169template <class TArgs>
170class FunctionPriorityDelegate<TArgs, false>: public AbstractPriorityDelegate<TArgs>
171{
172public:
173 typedef void (*NotifyFunction)(TArgs&);
174
175 FunctionPriorityDelegate(NotifyFunction function, int prio):
176 AbstractPriorityDelegate<TArgs>(prio),
177 _function(function)
178 {
179 }
180
181 FunctionPriorityDelegate(const FunctionPriorityDelegate& delegate):
182 AbstractPriorityDelegate<TArgs>(delegate),
183 _function(delegate._function)
184 {
185 }
186
187 FunctionPriorityDelegate& operator = (const FunctionPriorityDelegate& delegate)
188 {
189 if (&delegate != this)
190 {
191 this->_function = delegate._function;
192 this->_priority = delegate._priority;
193 }
194 return *this;
195 }
196
197 ~FunctionPriorityDelegate()
198 {
199 }
200
201 bool notify(const void* sender, TArgs& arguments)
202 {
203 Mutex::ScopedLock lock(_mutex);
204 if (_function)
205 {
206 (*_function)(arguments);
207 return true;
208 }
209 else return false;
210 }
211
212 bool equals(const AbstractDelegate<TArgs>& other) const
213 {
214 const FunctionPriorityDelegate* pOtherDelegate = dynamic_cast<const FunctionPriorityDelegate*>(other.unwrap());
215 return pOtherDelegate && this->priority() == pOtherDelegate->priority() && _function == pOtherDelegate->_function;
216 }
217
218 AbstractDelegate<TArgs>* clone() const
219 {
220 return new FunctionPriorityDelegate(*this);
221 }
222
223 void disable()
224 {
225 Mutex::ScopedLock lock(_mutex);
226 _function = 0;
227 }
228
229protected:
230 NotifyFunction _function;
231 Mutex _mutex;
232
233private:
234 FunctionPriorityDelegate();
235};
236
237
238template <>
239class FunctionPriorityDelegate<void, true, true>: public AbstractPriorityDelegate<void>
240 /// Wraps a freestanding function or static member function
241 /// for use as a PriorityDelegate.
242{
243public:
244 typedef void (*NotifyFunction)(const void*);
245
246 FunctionPriorityDelegate(NotifyFunction function, int prio):
247 AbstractPriorityDelegate<void>(prio),
248 _function(function)
249 {
250 }
251
252 FunctionPriorityDelegate(const FunctionPriorityDelegate& delegate):
253 AbstractPriorityDelegate<void>(delegate),
254 _function(delegate._function)
255 {
256 }
257
258 FunctionPriorityDelegate& operator = (const FunctionPriorityDelegate& delegate)
259 {
260 if (&delegate != this)
261 {
262 this->_function = delegate._function;
263 this->_priority = delegate._priority;
264 }
265 return *this;
266 }
267
268 ~FunctionPriorityDelegate()
269 {
270 }
271
272 bool notify(const void* sender)
273 {
274 Mutex::ScopedLock lock(_mutex);
275 if (_function)
276 {
277 (*_function)(sender);
278 return true;
279 }
280 else return false;
281 }
282
283 bool equals(const AbstractDelegate<void>& other) const
284 {
285 const FunctionPriorityDelegate* pOtherDelegate = dynamic_cast<const FunctionPriorityDelegate*>(other.unwrap());
286 return pOtherDelegate && this->priority() == pOtherDelegate->priority() && _function == pOtherDelegate->_function;
287 }
288
289 AbstractDelegate<void>* clone() const
290 {
291 return new FunctionPriorityDelegate(*this);
292 }
293
294 void disable()
295 {
296 Mutex::ScopedLock lock(_mutex);
297 _function = 0;
298 }
299
300protected:
301 NotifyFunction _function;
302 Mutex _mutex;
303
304private:
305 FunctionPriorityDelegate();
306};
307
308
309template <>
310class FunctionPriorityDelegate<void, true, false>: public AbstractPriorityDelegate<void>
311{
312public:
313 typedef void (*NotifyFunction)(void*);
314
315 FunctionPriorityDelegate(NotifyFunction function, int prio):
316 AbstractPriorityDelegate<void>(prio),
317 _function(function)
318 {
319 }
320
321 FunctionPriorityDelegate(const FunctionPriorityDelegate& delegate):
322 AbstractPriorityDelegate<void>(delegate),
323 _function(delegate._function)
324 {
325 }
326
327 FunctionPriorityDelegate& operator = (const FunctionPriorityDelegate& delegate)
328 {
329 if (&delegate != this)
330 {
331 this->_function = delegate._function;
332 this->_priority = delegate._priority;
333 }
334 return *this;
335 }
336
337 ~FunctionPriorityDelegate()
338 {
339 }
340
341 bool notify(const void* sender)
342 {
343 Mutex::ScopedLock lock(_mutex);
344 if (_function)
345 {
346 (*_function)(const_cast<void*>(sender));
347 return true;
348 }
349 else return false;
350 }
351
352 bool equals(const AbstractDelegate<void>& other) const
353 {
354 const FunctionPriorityDelegate* pOtherDelegate = dynamic_cast<const FunctionPriorityDelegate*>(other.unwrap());
355 return pOtherDelegate && this->priority() == pOtherDelegate->priority() && _function == pOtherDelegate->_function;
356 }
357
358 AbstractDelegate<void>* clone() const
359 {
360 return new FunctionPriorityDelegate(*this);
361 }
362
363 void disable()
364 {
365 Mutex::ScopedLock lock(_mutex);
366 _function = 0;
367 }
368
369protected:
370 NotifyFunction _function;
371 Mutex _mutex;
372
373private:
374 FunctionPriorityDelegate();
375};
376
377
378template <>
379class FunctionPriorityDelegate<void, false>: public AbstractPriorityDelegate<void>
380{
381public:
382 typedef void (*NotifyFunction)();
383
384 FunctionPriorityDelegate(NotifyFunction function, int prio):
385 AbstractPriorityDelegate<void>(prio),
386 _function(function)
387 {
388 }
389
390 FunctionPriorityDelegate(const FunctionPriorityDelegate& delegate):
391 AbstractPriorityDelegate<void>(delegate),
392 _function(delegate._function)
393 {
394 }
395
396 FunctionPriorityDelegate& operator = (const FunctionPriorityDelegate& delegate)
397 {
398 if (&delegate != this)
399 {
400 this->_function = delegate._function;
401 this->_priority = delegate._priority;
402 }
403 return *this;
404 }
405
406 ~FunctionPriorityDelegate()
407 {
408 }
409
410 bool notify(const void* sender)
411 {
412 Mutex::ScopedLock lock(_mutex);
413 if (_function)
414 {
415 (*_function)();
416 return true;
417 }
418 else return false;
419 }
420
421 bool equals(const AbstractDelegate<void>& other) const
422 {
423 const FunctionPriorityDelegate* pOtherDelegate = dynamic_cast<const FunctionPriorityDelegate*>(other.unwrap());
424 return pOtherDelegate && this->priority() == pOtherDelegate->priority() && _function == pOtherDelegate->_function;
425 }
426
427 AbstractDelegate<void>* clone() const
428 {
429 return new FunctionPriorityDelegate(*this);
430 }
431
432 void disable()
433 {
434 Mutex::ScopedLock lock(_mutex);
435 _function = 0;
436 }
437
438protected:
439 NotifyFunction _function;
440 Mutex _mutex;
441
442private:
443 FunctionPriorityDelegate();
444};
445
446
447} // namespace Poco
448
449
450#endif // Foundation_FunctionPriorityDelegate_INCLUDED
451