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