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