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