Delegate.h 9.9 KB

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