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