FunctionPriorityDelegate.h 9.3 KB

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