DefaultStrategy.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //
  2. // DefaultStrategy.h
  3. //
  4. // Library: Foundation
  5. // Package: Events
  6. // Module: DefaultStrategy
  7. //
  8. // Implementation of the DefaultStrategy 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_DefaultStrategy_INCLUDED
  16. #define Foundation_DefaultStrategy_INCLUDED
  17. #include "Poco/NotificationStrategy.h"
  18. #include "Poco/SharedPtr.h"
  19. #include <vector>
  20. namespace Poco {
  21. template <class TArgs, class TDelegate>
  22. class DefaultStrategy: public NotificationStrategy<TArgs, TDelegate>
  23. /// Default notification strategy.
  24. ///
  25. /// Internally, a std::vector<> is used to store
  26. /// delegate objects. Delegates are invoked in the
  27. /// order in which they have been registered.
  28. {
  29. public:
  30. typedef TDelegate* DelegateHandle;
  31. typedef SharedPtr<TDelegate> DelegatePtr;
  32. typedef std::vector<DelegatePtr> Delegates;
  33. typedef typename Delegates::iterator Iterator;
  34. public:
  35. DefaultStrategy()
  36. {
  37. }
  38. DefaultStrategy(const DefaultStrategy& s):
  39. _delegates(s._delegates)
  40. {
  41. }
  42. ~DefaultStrategy()
  43. {
  44. }
  45. void notify(const void* sender, TArgs& arguments)
  46. {
  47. for (Iterator it = _delegates.begin(); it != _delegates.end(); ++it)
  48. {
  49. (*it)->notify(sender, arguments);
  50. }
  51. }
  52. DelegateHandle add(const TDelegate& delegate)
  53. {
  54. DelegatePtr pDelegate(static_cast<TDelegate*>(delegate.clone()));
  55. _delegates.push_back(pDelegate);
  56. return pDelegate.get();
  57. }
  58. void remove(const TDelegate& delegate)
  59. {
  60. for (Iterator it = _delegates.begin(); it != _delegates.end(); ++it)
  61. {
  62. if (delegate.equals(**it))
  63. {
  64. (*it)->disable();
  65. _delegates.erase(it);
  66. return;
  67. }
  68. }
  69. }
  70. void remove(DelegateHandle delegateHandle)
  71. {
  72. for (Iterator it = _delegates.begin(); it != _delegates.end(); ++it)
  73. {
  74. if (*it == delegateHandle)
  75. {
  76. (*it)->disable();
  77. _delegates.erase(it);
  78. return;
  79. }
  80. }
  81. }
  82. DefaultStrategy& operator = (const DefaultStrategy& s)
  83. {
  84. if (this != &s)
  85. {
  86. _delegates = s._delegates;
  87. }
  88. return *this;
  89. }
  90. void clear()
  91. {
  92. for (Iterator it = _delegates.begin(); it != _delegates.end(); ++it)
  93. {
  94. (*it)->disable();
  95. }
  96. _delegates.clear();
  97. }
  98. bool empty() const
  99. {
  100. return _delegates.empty();
  101. }
  102. protected:
  103. Delegates _delegates;
  104. };
  105. template <class TDelegate>
  106. class DefaultStrategy<void,TDelegate>: public NotificationStrategy<void, TDelegate>
  107. /// Default notification strategy.
  108. ///
  109. /// Internally, a std::vector<> is used to store
  110. /// delegate objects. Delegates are invoked in the
  111. /// order in which they have been registered.
  112. {
  113. public:
  114. typedef TDelegate* DelegateHandle;
  115. typedef SharedPtr<TDelegate> DelegatePtr;
  116. typedef std::vector<DelegatePtr> Delegates;
  117. typedef typename Delegates::iterator Iterator;
  118. public:
  119. DefaultStrategy()
  120. {
  121. }
  122. DefaultStrategy(const DefaultStrategy& s):
  123. _delegates(s._delegates)
  124. {
  125. }
  126. ~DefaultStrategy()
  127. {
  128. }
  129. void notify(const void* sender)
  130. {
  131. for (Iterator it = _delegates.begin(); it != _delegates.end(); ++it)
  132. {
  133. (*it)->notify(sender);
  134. }
  135. }
  136. DelegateHandle add(const TDelegate& delegate)
  137. {
  138. DelegatePtr pDelegate(static_cast<TDelegate*>(delegate.clone()));
  139. _delegates.push_back(pDelegate);
  140. return pDelegate.get();
  141. }
  142. void remove(const TDelegate& delegate)
  143. {
  144. for (Iterator it = _delegates.begin(); it != _delegates.end(); ++it)
  145. {
  146. if (delegate.equals(**it))
  147. {
  148. (*it)->disable();
  149. _delegates.erase(it);
  150. return;
  151. }
  152. }
  153. }
  154. void remove(DelegateHandle delegateHandle)
  155. {
  156. for (Iterator it = _delegates.begin(); it != _delegates.end(); ++it)
  157. {
  158. if (*it == delegateHandle)
  159. {
  160. (*it)->disable();
  161. _delegates.erase(it);
  162. return;
  163. }
  164. }
  165. }
  166. DefaultStrategy& operator = (const DefaultStrategy& s)
  167. {
  168. if (this != &s)
  169. {
  170. _delegates = s._delegates;
  171. }
  172. return *this;
  173. }
  174. void clear()
  175. {
  176. for (Iterator it = _delegates.begin(); it != _delegates.end(); ++it)
  177. {
  178. (*it)->disable();
  179. }
  180. _delegates.clear();
  181. }
  182. bool empty() const
  183. {
  184. return _delegates.empty();
  185. }
  186. protected:
  187. Delegates _delegates;
  188. };
  189. } // namespace Poco
  190. #endif // Foundation_DefaultStrategy_INCLUDED