1 | // |
---|
2 | // PriorityDelegate.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/PriorityDelegate.h#2 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Events |
---|
8 | // Module: PriorityDelegate |
---|
9 | // |
---|
10 | // Implementation of the PriorityDelegate 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 | |
---|
38 | |
---|
39 | #ifndef Foundation_PriorityDelegate_INCLUDED |
---|
40 | #define Foundation_PriorityDelegate_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include "Poco/AbstractPriorityDelegate.h" |
---|
45 | #include "Poco/PriorityExpire.h" |
---|
46 | #include "Poco/FunctionPriorityDelegate.h" |
---|
47 | |
---|
48 | |
---|
49 | namespace Poco { |
---|
50 | |
---|
51 | |
---|
52 | template <class TObj, class TArgs, bool useSender = true> |
---|
53 | class PriorityDelegate: public AbstractPriorityDelegate<TArgs> |
---|
54 | { |
---|
55 | public: |
---|
56 | typedef void (TObj::*NotifyMethod)(const void*, TArgs&); |
---|
57 | |
---|
58 | PriorityDelegate(TObj* obj, NotifyMethod method, int prio): |
---|
59 | AbstractPriorityDelegate<TArgs>(obj, prio), |
---|
60 | _receiverObject(obj), |
---|
61 | _receiverMethod(method) |
---|
62 | { |
---|
63 | } |
---|
64 | |
---|
65 | PriorityDelegate(const PriorityDelegate& delegate): |
---|
66 | AbstractPriorityDelegate<TArgs>(delegate._pTarget, delegate._priority), |
---|
67 | _receiverObject(delegate._receiverObject), |
---|
68 | _receiverMethod(delegate._receiverMethod) |
---|
69 | { |
---|
70 | } |
---|
71 | |
---|
72 | PriorityDelegate& operator = (const PriorityDelegate& delegate) |
---|
73 | { |
---|
74 | if (&delegate != this) |
---|
75 | { |
---|
76 | this->_pTarget = delegate._pTarget; |
---|
77 | this->_receiverObject = delegate._receiverObject; |
---|
78 | this->_receiverMethod = delegate._receiverMethod; |
---|
79 | this->_priority = delegate._priority; |
---|
80 | } |
---|
81 | return *this; |
---|
82 | } |
---|
83 | |
---|
84 | ~PriorityDelegate() |
---|
85 | { |
---|
86 | } |
---|
87 | |
---|
88 | bool notify(const void* sender, TArgs& arguments) |
---|
89 | { |
---|
90 | (_receiverObject->*_receiverMethod)(sender, arguments); |
---|
91 | return true; // per default the delegate never expires |
---|
92 | } |
---|
93 | |
---|
94 | AbstractPriorityDelegate<TArgs>* clone() const |
---|
95 | { |
---|
96 | return new PriorityDelegate(*this); |
---|
97 | } |
---|
98 | |
---|
99 | protected: |
---|
100 | TObj* _receiverObject; |
---|
101 | NotifyMethod _receiverMethod; |
---|
102 | |
---|
103 | private: |
---|
104 | PriorityDelegate(); |
---|
105 | }; |
---|
106 | |
---|
107 | |
---|
108 | |
---|
109 | template <class TObj, class TArgs> |
---|
110 | class PriorityDelegate<TObj, TArgs, false>: public AbstractPriorityDelegate<TArgs> |
---|
111 | { |
---|
112 | public: |
---|
113 | typedef void (TObj::*NotifyMethod)(TArgs&); |
---|
114 | |
---|
115 | PriorityDelegate(TObj* obj, NotifyMethod method, int prio): |
---|
116 | AbstractPriorityDelegate<TArgs>(obj, prio), |
---|
117 | _receiverObject(obj), |
---|
118 | _receiverMethod(method) |
---|
119 | { |
---|
120 | } |
---|
121 | |
---|
122 | PriorityDelegate(const PriorityDelegate& delegate): |
---|
123 | AbstractPriorityDelegate<TArgs>(delegate._pTarget, delegate._priority), |
---|
124 | _receiverObject(delegate._receiverObject), |
---|
125 | _receiverMethod(delegate._receiverMethod) |
---|
126 | { |
---|
127 | } |
---|
128 | |
---|
129 | PriorityDelegate& operator = (const PriorityDelegate& delegate) |
---|
130 | { |
---|
131 | if (&delegate != this) |
---|
132 | { |
---|
133 | this->_pTarget = delegate._pTarget; |
---|
134 | this->_receiverObject = delegate._receiverObject; |
---|
135 | this->_receiverMethod = delegate._receiverMethod; |
---|
136 | this->_priority = delegate._priority; |
---|
137 | } |
---|
138 | return *this; |
---|
139 | } |
---|
140 | |
---|
141 | ~PriorityDelegate() |
---|
142 | { |
---|
143 | } |
---|
144 | |
---|
145 | bool notify(const void* sender, TArgs& arguments) |
---|
146 | { |
---|
147 | (_receiverObject->*_receiverMethod)(arguments); |
---|
148 | return true; // per default the delegate never expires |
---|
149 | } |
---|
150 | |
---|
151 | AbstractPriorityDelegate<TArgs>* clone() const |
---|
152 | { |
---|
153 | return new PriorityDelegate(*this); |
---|
154 | } |
---|
155 | |
---|
156 | protected: |
---|
157 | TObj* _receiverObject; |
---|
158 | NotifyMethod _receiverMethod; |
---|
159 | |
---|
160 | private: |
---|
161 | PriorityDelegate(); |
---|
162 | }; |
---|
163 | |
---|
164 | |
---|
165 | template <class TObj, class TArgs> |
---|
166 | static PriorityDelegate<TObj, TArgs, true> priorityDelegate(TObj* pObj, void (TObj::*NotifyMethod)(const void*, TArgs&), int prio) |
---|
167 | { |
---|
168 | return PriorityDelegate<TObj, TArgs, true>(pObj, NotifyMethod, prio); |
---|
169 | } |
---|
170 | |
---|
171 | |
---|
172 | template <class TObj, class TArgs> |
---|
173 | static PriorityDelegate<TObj, TArgs, false> priorityDelegate(TObj* pObj, void (TObj::*NotifyMethod)(TArgs&), int prio) |
---|
174 | { |
---|
175 | return PriorityDelegate<TObj, TArgs, false>(pObj, NotifyMethod, prio); |
---|
176 | } |
---|
177 | |
---|
178 | |
---|
179 | template <class TObj, class TArgs> |
---|
180 | static PriorityExpire<TArgs> priorityDelegate(TObj* pObj, void (TObj::*NotifyMethod)(const void*, TArgs&), int prio, Timestamp::TimeDiff expireMilliSec) |
---|
181 | { |
---|
182 | return PriorityExpire<TArgs>(PriorityDelegate<TObj, TArgs, true>(pObj, NotifyMethod, prio), expireMilliSec); |
---|
183 | } |
---|
184 | |
---|
185 | |
---|
186 | template <class TObj, class TArgs> |
---|
187 | static PriorityExpire<TArgs> priorityDelegate(TObj* pObj, void (TObj::*NotifyMethod)(TArgs&), int prio, Timestamp::TimeDiff expireMilliSec) |
---|
188 | { |
---|
189 | return PriorityExpire<TArgs>(PriorityDelegate<TObj, TArgs, false>(pObj, NotifyMethod, prio), expireMilliSec); |
---|
190 | } |
---|
191 | |
---|
192 | |
---|
193 | template <class TArgs> |
---|
194 | static PriorityExpire<TArgs> priorityDelegate(void (*NotifyMethod)(const void*, TArgs&), int prio, Timestamp::TimeDiff expireMilliSec) |
---|
195 | { |
---|
196 | return PriorityExpire<TArgs>(FunctionPriorityDelegate<TArgs, true, true>(NotifyMethod, prio), expireMilliSec); |
---|
197 | } |
---|
198 | |
---|
199 | |
---|
200 | template <class TArgs> |
---|
201 | static PriorityExpire<TArgs> priorityDelegate(void (*NotifyMethod)(void*, TArgs&), int prio, Timestamp::TimeDiff expireMilliSec) |
---|
202 | { |
---|
203 | return PriorityExpire<TArgs>(FunctionPriorityDelegate<TArgs, true, false>(NotifyMethod, prio), expireMilliSec); |
---|
204 | } |
---|
205 | |
---|
206 | |
---|
207 | template <class TArgs> |
---|
208 | static PriorityExpire<TArgs> priorityDelegate(void (*NotifyMethod)(TArgs&), int prio, Timestamp::TimeDiff expireMilliSec) |
---|
209 | { |
---|
210 | return PriorityExpire<TArgs>(FunctionPriorityDelegate<TArgs, false>(NotifyMethod, prio), expireMilliSec); |
---|
211 | } |
---|
212 | |
---|
213 | |
---|
214 | template <class TArgs> |
---|
215 | static FunctionPriorityDelegate<TArgs, true, true> priorityDelegate(void (*NotifyMethod)(const void*, TArgs&), int prio) |
---|
216 | { |
---|
217 | return FunctionPriorityDelegate<TArgs, true, true>(NotifyMethod, prio); |
---|
218 | } |
---|
219 | |
---|
220 | |
---|
221 | template <class TArgs> |
---|
222 | static FunctionPriorityDelegate<TArgs, true, false> priorityDelegate(void (*NotifyMethod)(void*, TArgs&), int prio) |
---|
223 | { |
---|
224 | return FunctionPriorityDelegate<TArgs, true, false>(NotifyMethod, prio); |
---|
225 | } |
---|
226 | |
---|
227 | |
---|
228 | template <class TArgs> |
---|
229 | static FunctionPriorityDelegate<TArgs, false> priorityDelegate(void (*NotifyMethod)(TArgs&), int prio) |
---|
230 | { |
---|
231 | return FunctionPriorityDelegate<TArgs, false>(NotifyMethod, prio); |
---|
232 | } |
---|
233 | |
---|
234 | |
---|
235 | } // namespace Poco |
---|
236 | |
---|
237 | |
---|
238 | #endif |
---|