FunctionDelegate.h 7.9 KB

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