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