DefaultStrategy.h 4.2 KB

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