FunctionPriorityDelegate.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // FunctionPriorityDelegate.h
  3. //
  4. // $Id: //poco/svn/Foundation/include/Poco/FunctionPriorityDelegate.h#2 $
  5. //
  6. // Library: Foundation
  7. // Package: Events
  8. // Module: FunctionPriorityDelegate
  9. //
  10. // Implementation of the FunctionPriorityDelegate template.
  11. //
  12. // Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // Permission is hereby granted, free of charge, to any person or organization
  16. // obtaining a copy of the software and accompanying documentation covered by
  17. // this license (the "Software") to use, reproduce, display, distribute,
  18. // execute, and transmit the Software, and to prepare derivative works of the
  19. // Software, and to permit third-parties to whom the Software is furnished to
  20. // do so, all subject to the following:
  21. //
  22. // The copyright notices in the Software and this entire statement, including
  23. // the above license grant, this restriction and the following disclaimer,
  24. // must be included in all copies of the Software, in whole or in part, and
  25. // all derivative works of the Software, unless such copies or derivative
  26. // works are solely in the form of machine-executable object code generated by
  27. // a source language processor.
  28. //
  29. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  32. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  33. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  34. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  35. // DEALINGS IN THE SOFTWARE.
  36. //
  37. #ifndef Foundation_FunctionPriorityDelegate_INCLUDED
  38. #define Foundation_FunctionPriorityDelegate_INCLUDED
  39. #include "Poco/Foundation.h"
  40. #include "Poco/AbstractPriorityDelegate.h"
  41. namespace Poco {
  42. template <class TArgs, bool useSender = true, bool senderIsConst = true>
  43. class FunctionPriorityDelegate: public AbstractPriorityDelegate<TArgs>
  44. /// Wraps a C style function (or a C++ static fucntion) to be used as
  45. /// a priority delegate
  46. {
  47. public:
  48. typedef void (*NotifyMethod)(const void*, TArgs&);
  49. FunctionPriorityDelegate(NotifyMethod method, int prio):
  50. AbstractPriorityDelegate<TArgs>(*reinterpret_cast<void**>(&method), prio),
  51. _receiverMethod(method)
  52. {
  53. }
  54. FunctionPriorityDelegate(const FunctionPriorityDelegate& delegate):
  55. AbstractPriorityDelegate<TArgs>(delegate._pTarget, delegate._priority),
  56. _receiverMethod(delegate._receiverMethod)
  57. {
  58. }
  59. FunctionPriorityDelegate& operator = (const FunctionPriorityDelegate& delegate)
  60. {
  61. if (&delegate != this)
  62. {
  63. this->_pTarget = delegate._pTarget;
  64. this->_receiverMethod = delegate._receiverMethod;
  65. this->_priority = delegate._priority;
  66. }
  67. return *this;
  68. }
  69. ~FunctionPriorityDelegate()
  70. {
  71. }
  72. bool notify(const void* sender, TArgs& arguments)
  73. {
  74. (*_receiverMethod)(sender, arguments);
  75. return true; // per default the delegate never expires
  76. }
  77. AbstractPriorityDelegate<TArgs>* clone() const
  78. {
  79. return new FunctionPriorityDelegate(*this);
  80. }
  81. protected:
  82. NotifyMethod _receiverMethod;
  83. private:
  84. FunctionPriorityDelegate();
  85. };
  86. template <class TArgs>
  87. class FunctionPriorityDelegate<TArgs, true, false>: public AbstractPriorityDelegate<TArgs>
  88. {
  89. public:
  90. typedef void (*NotifyMethod)(void*, TArgs&);
  91. FunctionPriorityDelegate(NotifyMethod method, int prio):
  92. AbstractPriorityDelegate<TArgs>(*reinterpret_cast<void**>(&method), prio),
  93. _receiverMethod(method)
  94. {
  95. }
  96. FunctionPriorityDelegate(const FunctionPriorityDelegate& delegate):
  97. AbstractPriorityDelegate<TArgs>(delegate._pTarget, delegate._priority),
  98. _receiverMethod(delegate._receiverMethod)
  99. {
  100. }
  101. FunctionPriorityDelegate& operator = (const FunctionPriorityDelegate& delegate)
  102. {
  103. if (&delegate != this)
  104. {
  105. this->_pTarget = delegate._pTarget;
  106. this->_receiverMethod = delegate._receiverMethod;
  107. this->_priority = delegate._priority;
  108. }
  109. return *this;
  110. }
  111. ~FunctionPriorityDelegate()
  112. {
  113. }
  114. bool notify(const void* sender, TArgs& arguments)
  115. {
  116. (*_receiverMethod)(const_cast<void*>(sender), arguments);
  117. return true; // per default the delegate never expires
  118. }
  119. AbstractPriorityDelegate<TArgs>* clone() const
  120. {
  121. return new FunctionPriorityDelegate(*this);
  122. }
  123. protected:
  124. NotifyMethod _receiverMethod;
  125. private:
  126. FunctionPriorityDelegate();
  127. };
  128. template <class TArgs>
  129. class FunctionPriorityDelegate<TArgs, false>: public AbstractPriorityDelegate<TArgs>
  130. {
  131. public:
  132. typedef void (*NotifyMethod)(TArgs&);
  133. FunctionPriorityDelegate(NotifyMethod method, int prio):
  134. AbstractPriorityDelegate<TArgs>(*reinterpret_cast<void**>(&method), prio),
  135. _receiverMethod(method)
  136. {
  137. }
  138. FunctionPriorityDelegate(const FunctionPriorityDelegate& delegate):
  139. AbstractPriorityDelegate<TArgs>(delegate._pTarget, delegate._priority),
  140. _receiverMethod(delegate._receiverMethod)
  141. {
  142. }
  143. FunctionPriorityDelegate& operator = (const FunctionPriorityDelegate& delegate)
  144. {
  145. if (&delegate != this)
  146. {
  147. this->_pTarget = delegate._pTarget;
  148. this->_receiverMethod = delegate._receiverMethod;
  149. this->_priority = delegate._priority;
  150. }
  151. return *this;
  152. }
  153. ~FunctionPriorityDelegate()
  154. {
  155. }
  156. bool notify(const void* sender, TArgs& arguments)
  157. {
  158. (*_receiverMethod)(arguments);
  159. return true; // per default the delegate never expires
  160. }
  161. AbstractPriorityDelegate<TArgs>* clone() const
  162. {
  163. return new FunctionPriorityDelegate(*this);
  164. }
  165. protected:
  166. NotifyMethod _receiverMethod;
  167. private:
  168. FunctionPriorityDelegate();
  169. };
  170. } // namespace Poco
  171. #endif