8 #ifndef BOOSTER_CALLBACK_H
9 #define BOOSTER_CALLBACK_H
11 #include <booster/backtrace.h>
13 #include <booster/intrusive_ptr.h>
14 #include <booster/refcounted.h>
17 template<
typename Type>
20 template<
typename Type>
36 #ifdef BOOSTER_DOXYGEN_DOCS
37 template<
typename Result,
typename ...Params>
101 operator bool()
const;
111 #define BOOSTER_CALLBACK \
112 template<typename Result BOOSTER_TEMPLATE_PARAMS > \
113 struct callable<Result(BOOSTER_TEMPLATE_TYPE_PARAMS)> :public refcounted\
115 virtual Result operator()(BOOSTER_TYPE_PARAMS) = 0; \
116 virtual ~callable(){} \
119 template<typename Result BOOSTER_TEMPLATE_PARAMS > \
120 class callback<Result(BOOSTER_TEMPLATE_TYPE_PARAMS)> \
123 typedef Result result_type; \
125 typedef callable<Result(BOOSTER_TEMPLATE_TYPE_PARAMS)> \
128 template<typename R,typename F> \
129 struct callable_impl : public callable_type { \
131 callable_impl(F f) : func(f){} \
132 virtual R operator()(BOOSTER_TYPE_PARAMS) \
133 { return func(BOOSTER_CALL_PARAMS); } \
136 template<typename F> \
137 struct callable_impl<void,F> : public callable_type { \
139 callable_impl(F f) : func(f){} \
140 virtual void operator()(BOOSTER_TYPE_PARAMS) \
141 { func(BOOSTER_CALL_PARAMS); } \
146 template<typename Call> \
147 callback(intrusive_ptr<Call> c) : call_ptr(c) \
150 template<typename Call> \
151 callback(std::auto_ptr<Call> ptr) : call_ptr(ptr.release()) \
154 template<typename Call> \
155 callback const &operator=(intrusive_ptr<Call> c) \
156 { call_ptr = c; return *this; } \
158 template<typename Call> \
159 callback const &operator=(std::auto_ptr<Call> c) \
160 { call_ptr = 0; call_ptr = c.release(); return *this; } \
162 template<typename F> \
163 callback(F func) : call_ptr(new callable_impl<Result,F>(func)) \
166 callback(callback const &other) : call_ptr(other.call_ptr) {} \
168 template<typename F> \
169 callback const &operator=(F func) \
171 call_ptr = new callable_impl<Result,F>(func); \
175 callback const &operator=(callback const &other) \
177 if(this != &other) { call_ptr=other.call_ptr; } \
181 Result operator()(BOOSTER_TYPE_PARAMS) const \
183 if(!call_ptr.get()) throw bad_callback_call(); \
184 return (*call_ptr)(BOOSTER_CALL_PARAMS); \
187 bool empty() const { return call_ptr.get()==0; } \
189 operator bool() const { return !empty(); } \
191 void swap(callback &other) { call_ptr.swap(other.call_ptr); } \
194 intrusive_ptr<callable_type> call_ptr; \
197 #define BOOSTER_TEMPLATE_PARAMS
198 #define BOOSTER_TEMPLATE_TYPE_PARAMS
199 #define BOOSTER_TYPE_PARAMS
200 #define BOOSTER_CALL_PARAMS
202 #undef BOOSTER_TEMPLATE_PARAMS
203 #undef BOOSTER_TEMPLATE_TYPE_PARAMS
204 #undef BOOSTER_TYPE_PARAMS
205 #undef BOOSTER_CALL_PARAMS
207 #define BOOSTER_TEMPLATE_PARAMS ,typename P1
208 #define BOOSTER_TEMPLATE_TYPE_PARAMS P1
209 #define BOOSTER_TYPE_PARAMS P1 a1
210 #define BOOSTER_CALL_PARAMS a1
212 #undef BOOSTER_TEMPLATE_PARAMS
213 #undef BOOSTER_TEMPLATE_TYPE_PARAMS
214 #undef BOOSTER_TYPE_PARAMS
215 #undef BOOSTER_CALL_PARAMS
217 #define BOOSTER_TEMPLATE_PARAMS ,typename P1,typename P2
218 #define BOOSTER_TEMPLATE_TYPE_PARAMS P1, P2
219 #define BOOSTER_TYPE_PARAMS P1 a1,P2 a2
220 #define BOOSTER_CALL_PARAMS a1,a2
222 #undef BOOSTER_TEMPLATE_PARAMS
223 #undef BOOSTER_TEMPLATE_TYPE_PARAMS
224 #undef BOOSTER_TYPE_PARAMS
225 #undef BOOSTER_CALL_PARAMS
227 #define BOOSTER_TEMPLATE_PARAMS ,typename P1,typename P2,typename P3
228 #define BOOSTER_TEMPLATE_TYPE_PARAMS P1, P2, P3
229 #define BOOSTER_TYPE_PARAMS P1 a1,P2 a2,P3 a3
230 #define BOOSTER_CALL_PARAMS a1,a2,a3
232 #undef BOOSTER_TEMPLATE_PARAMS
233 #undef BOOSTER_TEMPLATE_TYPE_PARAMS
234 #undef BOOSTER_TYPE_PARAMS
235 #undef BOOSTER_CALL_PARAMS
237 #define BOOSTER_TEMPLATE_PARAMS ,typename P1,typename P2,typename P3,typename P4
238 #define BOOSTER_TEMPLATE_TYPE_PARAMS P1, P2, P3, P4
239 #define BOOSTER_TYPE_PARAMS P1 a1,P2 a2,P3 a3,P4 a4
240 #define BOOSTER_CALL_PARAMS a1,a2,a3,a4
242 #undef BOOSTER_TEMPLATE_PARAMS
243 #undef BOOSTER_TEMPLATE_TYPE_PARAMS
244 #undef BOOSTER_TYPE_PARAMS
245 #undef BOOSTER_CALL_PARAMS
247 #define BOOSTER_TEMPLATE_PARAMS ,typename P1,typename P2,typename P3,typename P4,typename P5
248 #define BOOSTER_TEMPLATE_TYPE_PARAMS P1, P2, P3, P4, P5
249 #define BOOSTER_TYPE_PARAMS P1 a1,P2 a2,P3 a3,P4 a4,P5 a5
250 #define BOOSTER_CALL_PARAMS a1,a2,a3,a4,a5
252 #undef BOOSTER_TEMPLATE_PARAMS
253 #undef BOOSTER_TEMPLATE_TYPE_PARAMS
254 #undef BOOSTER_TYPE_PARAMS
255 #undef BOOSTER_CALL_PARAMS
257 #define BOOSTER_TEMPLATE_PARAMS ,typename P1,typename P2,typename P3,typename P4,typename P5,typename P6
258 #define BOOSTER_TEMPLATE_TYPE_PARAMS P1, P2, P3, P4, P5, P6
259 #define BOOSTER_TYPE_PARAMS P1 a1,P2 a2,P3 a3,P4 a4,P5 a5,P6 a6
260 #define BOOSTER_CALL_PARAMS a1,a2,a3,a4,a5,a6
262 #undef BOOSTER_TEMPLATE_PARAMS
263 #undef BOOSTER_TEMPLATE_TYPE_PARAMS
264 #undef BOOSTER_TYPE_PARAMS
265 #undef BOOSTER_CALL_PARAMS
267 #define BOOSTER_TEMPLATE_PARAMS ,typename P1,typename P2,typename P3,typename P4,typename P5,typename P6,typename P7
268 #define BOOSTER_TEMPLATE_TYPE_PARAMS P1, P2, P3, P4, P5, P6, P7
269 #define BOOSTER_TYPE_PARAMS P1 a1,P2 a2,P3 a3,P4 a4,P5 a5,P6 a6,P7 a7
270 #define BOOSTER_CALL_PARAMS a1,a2,a3,a4,a5,a6,a7
272 #undef BOOSTER_TEMPLATE_PARAMS
273 #undef BOOSTER_TEMPLATE_TYPE_PARAMS
274 #undef BOOSTER_TYPE_PARAMS
275 #undef BOOSTER_CALL_PARAMS
277 #define BOOSTER_TEMPLATE_PARAMS ,typename P1,typename P2,typename P3,typename P4,typename P5,typename P6,typename P7,typename P8
278 #define BOOSTER_TEMPLATE_TYPE_PARAMS P1, P2, P3, P4, P5, P6, P7, P8
279 #define BOOSTER_TYPE_PARAMS P1 a1,P2 a2,P3 a3,P4 a4,P5 a5,P6 a6,P7 a7,P8 a8
280 #define BOOSTER_CALL_PARAMS a1,a2,a3,a4,a5,a6,a7,a8
282 #undef BOOSTER_TEMPLATE_PARAMS
283 #undef BOOSTER_TEMPLATE_TYPE_PARAMS
284 #undef BOOSTER_TYPE_PARAMS
285 #undef BOOSTER_CALL_PARAMS
287 #undef BOOSTER_CALLBACK
this exception is thrown in case of calling unassigned/empty function
Definition: callback.h:27
Same as std::runtime_error but records stack trace.
Definition: backtrace.h:158
Result result_type
Definition: callback.h:61
Definition: callback.h:21
Definition: callback.h:18