00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef PDF_OBSERVER_H_
00028 #define PDF_OBSERVER_H_
00029
00030 #include "pdf_define.h"
00031
00032
00033 namespace pdf
00034 {
00035
00036 typedef struct
00037 {
00038 }empty;
00039
00040 typedef empty* UNUSABLE;
00041
00042
00043
00044 template <class R, class A1 = UNUSABLE,
00045 class A2 = UNUSABLE,
00046 class A3 = UNUSABLE,
00047 class A4 = UNUSABLE,
00048 class A5 = UNUSABLE>
00049 class IFunctor
00050 {
00051 public:
00052 typedef IFunctor<R, A1, A2, A3, A4, A5>* IFunctorPtr;
00053
00054 public:
00055 IFunctor(){}
00056 virtual ~IFunctor(){}
00057
00058 virtual R operator()() = 0;
00059 virtual R operator()(A1 arg1) = 0;
00060 virtual R operator()(A1 arg1, A2 arg2) = 0;
00061 virtual R operator()(A1 arg1, A2 arg2, A3 arg3) = 0;
00062 virtual R operator()(A1 arg1, A2 arg2, A3 arg3, A4 arg4) = 0;
00063 virtual R operator()(A1 arg1, A2 arg2, A3 arg3, A4 arg4, A5 arg5) = 0;
00064
00065
00066 virtual bool compare(IFunctorPtr p) = 0;
00067
00068 };
00069
00070 template <class T, class R, class A1=UNUSABLE,
00071 class A2=UNUSABLE,
00072 class A3=UNUSABLE,
00073 class A4=UNUSABLE,
00074 class A5=UNUSABLE>
00075 class Functor : public IFunctor<R, A1, A2, A3, A4, A5>
00076 {
00077
00078 typedef R (T::*Function0)();
00079 typedef R (T::*Function1)(A1);
00080 typedef R (T::*Function2)(A1, A2);
00081 typedef R (T::*Function3)(A1, A2, A3);
00082 typedef R (T::*Function4)(A1, A2, A3, A4);
00083 typedef R (T::*Function5)(A1, A2, A3, A4, A5);
00084
00085
00086 typedef Functor<T, R, A1, A2, A3, A4, A5>* FunctorPtr;
00087
00088 public:
00089 typedef IFunctor<R, A1, A2, A3, A4, A5>* IFunctorPtr;
00090
00091 public:
00092
00093 Functor(T *callee, Function0 func0)
00094 {
00095 instance = callee;
00096 functor.func0_ = func0;
00097 }
00098 Functor(T *callee, Function1 func1)
00099 {
00100 instance = callee;
00101 functor.func1_ = func1;
00102 }
00103 Functor(T *callee, Function2 func2)
00104 {
00105 instance = callee;
00106 functor.func2_ = func2;
00107 }
00108 Functor(T *callee, Function3 func3)
00109 {
00110 instance = callee;
00111 functor.func3_ = func3;
00112 }
00113 Functor(T *callee, Function4 func4)
00114 {
00115 instance = callee;
00116 functor.func4_ = func4;
00117 }
00118 Functor(T *callee, Function5 func5)
00119 {
00120 instance = callee;
00121 functor.func5_ = func5;
00122 }
00123
00124 ~Functor()
00125 {
00126 }
00127
00128
00129 R operator()()
00130 {
00131 return (instance->*(functor.func0_))();
00132 }
00133
00134 R operator()(A1 arg1)
00135 {
00136 return (instance->*(functor.func1_))(arg1);
00137 }
00138
00139 R operator()(A1 arg1, A2 arg2)
00140 {
00141 return (instance->*(functor.func2_))(arg1, arg2);
00142 }
00143
00144 R operator()(A1 arg1, A2 arg2, A3 arg3)
00145 {
00146 return (instance->*(functor.func3_))(arg1, arg2, arg3);
00147 }
00148
00149 R operator()(A1 arg1, A2 arg2, A3 arg3, A4 arg4)
00150 {
00151 return (instance->*(functor.func4_))(arg1, arg2, arg3, arg4);
00152 }
00153
00154 R operator()(A1 arg1, A2 arg2, A3 arg3, A4 arg4, A5 arg5)
00155 {
00156 return (instance->*(functor.func5_))(arg1, arg2, arg3, arg4, arg5);
00157 }
00158
00159
00160 bool compare(IFunctorPtr p)
00161 {
00162 return functor.func0_ == (FunctorPtr(p))->functor.func0_;
00163 }
00164
00165
00166 private:
00167 T *instance;
00168 union FUNCTOR
00169 {
00170 Function0 func0_;
00171 Function1 func1_;
00172 Function2 func2_;
00173 Function3 func3_;
00174 Function4 func4_;
00175 Function5 func5_;
00176 }functor;
00177 };
00178
00179
00180 template <class F>
00181 class ReceiversOperations
00182 {
00183 typedef typename std::vector<F> Receivers;
00184 typedef typename std::vector<F>::iterator ReceiversIter;
00185 public:
00186
00187 static void clear(Receivers& receivers)
00188 {
00189 ReceiversIter begin, end, it;
00190 begin = receivers.begin();
00191 end = receivers.end();
00192 for(it = begin; it != end; ++it)
00193 {
00194 delete (*it);
00195 }
00196 receivers.clear();
00197 }
00198
00199
00200 static bool remove_receiver(F p, Receivers& receivers)
00201 {
00202 ReceiversIter begin, end, it;
00203 begin = receivers.begin();
00204 end = receivers.end();
00205 for(it = begin; it != end; ++it)
00206 {
00207 if ((*it)->compare(p))
00208 {
00209 delete (*it);
00210 receivers.erase(it);
00211 return true;
00212 }
00213 }
00214 return false;
00215 }
00216 };
00217
00218
00219
00220
00221
00222 template <class R, class A1 = UNUSABLE,
00223 class A2 = UNUSABLE,
00224 class A3 = UNUSABLE,
00225 class A4 = UNUSABLE,
00226 class A5 = UNUSABLE>
00227 class Signal
00228 {
00229 typedef IFunctor<R, A1, A2, A3, A4, A5>* IFunctorPtr;
00230 public:
00231 Signal(){}
00232 ~Signal(){ReceiversOperations<IFunctorPtr>::clear(receivers);}
00233
00234 public:
00235
00236 template <class T>
00237 void add_slot(T* p, R (T::*func)(A1, A2, A3, A4, A5))
00238 {
00239 IFunctorPtr func_ptr = new Functor<T, R, A1, A2, A3, A4, A5>(p, func);
00240 receivers.push_back(func_ptr);
00241 }
00242
00243
00244 template <class T>
00245 bool remove_slot(T* p, R (T::*func)(A1, A2, A3, A4, A5))
00246 {
00247 Functor<T, R, A1, A2, A3, A4, A5> func_obj(p, func);
00248 return ReceiversOperations<IFunctorPtr>::
00249 remove_receiver(&func_obj, receivers);
00250 }
00251
00252
00253 void broadcast(A1 arg1, A2 arg2, A3 arg3, A4 arg4, A5 arg5)
00254 {
00255 ReceiversIter begin, end, it;
00256 begin = receivers.begin();
00257 end = receivers.end();
00258 IFunctorPtr ptr = 0;
00259 for(it= begin; it != end; ++it)
00260 {
00261 ptr = *it;
00262 (*ptr)(arg1, arg2, arg3, arg4, arg5);
00263 }
00264 }
00265
00266 size_t count() {return receivers.size();}
00267
00268 private:
00269 typedef typename std::vector<IFunctorPtr> Receivers;
00270 typedef typename std::vector<IFunctorPtr>::iterator ReceiversIter;
00271 Receivers receivers;
00272 };
00273
00274
00275 template <class R>
00276 class Signal<R, UNUSABLE, UNUSABLE, UNUSABLE, UNUSABLE, UNUSABLE>
00277 {
00278 typedef IFunctor<R>* IFunctorPtr;
00279
00280 public:
00281 Signal(){}
00282 ~Signal(){ReceiversOperations<IFunctorPtr>::clear(receivers);}
00283
00284 public:
00285
00286 template <class T>
00287 void add_slot(T* p, R (T::*func)())
00288 {
00289 IFunctorPtr func_ptr = new Functor<T, R>(p, func);
00290 receivers.push_back(func_ptr);
00291 }
00292
00293
00294 template <class T>
00295 bool remove_slot(T* p, R (T::*func)())
00296 {
00297 Functor<T, R> func_obj(p, func);
00298 return ReceiversOperations<IFunctorPtr>::
00299 remove_receiver(&func_obj, receivers);
00300 }
00301
00302
00303 void broadcast()
00304 {
00305 ReceiversIter begin, end, it;
00306 begin = receivers.begin();
00307 end = receivers.end();
00308 IFunctorPtr ptr = 0;
00309 for(it= begin; it != end; ++it)
00310 {
00311 ptr = *it;
00312 (*ptr)();
00313 }
00314 }
00315
00316 size_t count() {return receivers.size();}
00317
00318 private:
00319 typedef typename std::vector<IFunctorPtr> Receivers;
00320 typedef typename std::vector<IFunctorPtr>::iterator ReceiversIter;
00321 Receivers receivers;
00322 };
00323
00324
00325 template <class R, class A1>
00326 class Signal<R, A1, UNUSABLE, UNUSABLE, UNUSABLE, UNUSABLE>
00327 {
00328 typedef IFunctor<R, A1>* IFunctorPtr;
00329 public:
00330 Signal(){}
00331 ~Signal(){ReceiversOperations<IFunctorPtr>::clear(receivers);}
00332
00333 public:
00334
00335 template <class T>
00336 void add_slot(T* p, R (T::*func)(A1))
00337 {
00338 IFunctorPtr func_ptr = new Functor<T, R, A1>(p, func);
00339 receivers.push_back(func_ptr);
00340 }
00341
00342
00343 template <class T>
00344 bool remove_slot(T* p, R (T::*func)(A1))
00345 {
00346 Functor<T, R, A1> func_obj(p, func);
00347 return ReceiversOperations<IFunctorPtr>::
00348 remove_receiver(&func_obj, receivers);
00349 }
00350
00351
00352 void broadcast(A1 arg1)
00353 {
00354 ReceiversIter begin, end, it;
00355 begin = receivers.begin();
00356 end = receivers.end();
00357 IFunctorPtr ptr = 0;
00358 for(it= begin; it != end; ++it)
00359 {
00360 ptr = *it;
00361 (*ptr)(arg1);
00362 }
00363 }
00364
00365 size_t count() {return receivers.size();}
00366
00367 private:
00368 typedef typename std::vector<IFunctorPtr> Receivers;
00369 typedef typename std::vector<IFunctorPtr>::iterator ReceiversIter;
00370 Receivers receivers;
00371 };
00372
00373
00374 template <class R, class A1,
00375 class A2>
00376 class Signal<R, A1, A2, UNUSABLE, UNUSABLE, UNUSABLE>
00377 {
00378 typedef IFunctor<R, A1, A2>* IFunctorPtr;
00379 public:
00380 Signal(){}
00381 ~Signal(){ReceiversOperations<IFunctorPtr>::clear(receivers);}
00382
00383 public:
00384
00385 template <class T>
00386 void add_slot(T* p, R (T::*func)(A1, A2))
00387 {
00388 IFunctorPtr func_ptr = new Functor<T, R, A1, A2>(p, func);
00389 receivers.push_back(func_ptr);
00390 }
00391
00392
00393
00394 template <class T>
00395 bool remove_slot(T* p, R (T::*func)(A1, A2))
00396 {
00397 Functor<T, R, A1, A2> func_obj(p, func);
00398 return ReceiversOperations<IFunctorPtr>::
00399 remove_receiver(&func_obj, receivers);
00400 }
00401
00402
00403 void broadcast(A1 arg1, A2 arg2)
00404 {
00405 ReceiversIter begin, end, it;
00406 begin = receivers.begin();
00407 end = receivers.end();
00408 IFunctorPtr ptr = 0;
00409 for(it= begin; it != end; ++it)
00410 {
00411 ptr = *it;
00412 (*ptr)(arg1, arg2);
00413 }
00414 }
00415
00416 unsigned int count() {return receivers.size();}
00417
00418 private:
00419 typedef typename std::vector<IFunctorPtr> Receivers;
00420 typedef typename std::vector<IFunctorPtr>::iterator ReceiversIter;
00421 Receivers receivers;
00422 };
00423
00424
00425 template <class R, class A1,
00426 class A2,
00427 class A3>
00428 class Signal<R, A1, A2, A3, UNUSABLE, UNUSABLE>
00429 {
00430 typedef IFunctor<R, A1, A2, A3>* IFunctorPtr;
00431 public:
00432 Signal(){}
00433 ~Signal(){ReceiversOperations<IFunctorPtr>::clear(receivers);}
00434
00435 public:
00436
00437 template <class T>
00438 void add_slot(T* p, R (T::*func)(A1, A2, A3))
00439 {
00440 IFunctorPtr func_ptr = new Functor<T, R, A1, A2, A3>(p, func);
00441 receivers.push_back(func_ptr);
00442 }
00443
00444
00445 template <class T>
00446 bool remove_slot(T* p, R (T::*func)(A1, A2, A3))
00447 {
00448 Functor<T, R, A1, A2, A3> func_obj(p, func);
00449 return ReceiversOperations<IFunctorPtr>::
00450 remove_receiver(&func_obj, receivers);
00451 }
00452
00453
00454 void broadcast(A1 arg1, A2 arg2, A3 arg3)
00455 {
00456 ReceiversIter begin, end, it;
00457 begin = receivers.begin();
00458 end = receivers.end();
00459 IFunctorPtr ptr = 0;
00460 for(it= begin; it != end; ++it)
00461 {
00462 ptr = *it;
00463 (*ptr)(arg1, arg2, arg3);
00464 }
00465 }
00466
00467 size_t count() {return receivers.size();}
00468
00469 private:
00470 typedef typename std::vector<IFunctorPtr> Receivers;
00471 typedef typename std::vector<IFunctorPtr>::iterator ReceiversIter;
00472 Receivers receivers;
00473 };
00474
00475
00476 template <class R, class A1,
00477 class A2,
00478 class A3,
00479 class A4>
00480 class Signal<R, A1, A2, A3, A4, UNUSABLE>
00481 {
00482 typedef IFunctor<R, A1, A2, A3, A4>* IFunctorPtr;
00483 public:
00484 Signal(){}
00485 ~Signal(){ReceiversOperations<IFunctorPtr>::clear(receivers);}
00486
00487 public:
00488
00489 template <class T>
00490 void add_slot(T* p, R (T::*func)(A1, A2, A3, A4))
00491 {
00492 IFunctorPtr func_ptr = new Functor<T, R, A1, A2, A3, A4>(p, func);
00493 receivers.push_back(func_ptr);
00494 }
00495
00496
00497 template <class T>
00498 bool remove_slot(T* p, R (T::*func)(A1, A2, A3, A4))
00499 {
00500 Functor<T, R, A1, A2, A3, A4> func_obj(p, func);
00501 return ReceiversOperations<IFunctorPtr>::
00502 remove_receiver(&func_obj, receivers);
00503 }
00504
00505
00506 void broadcast(A1 arg1, A2 arg2, A3 arg3, A4 arg4)
00507 {
00508 ReceiversIter begin, end, it;
00509 begin = receivers.begin();
00510 end = receivers.end();
00511 IFunctorPtr ptr = 0;
00512 for(it= begin; it != end; ++it)
00513 {
00514 ptr = *it;
00515 (*ptr)(arg1, arg2, arg3, arg4);
00516 }
00517 }
00518
00519 size_t count() {return receivers.size();}
00520
00521 private:
00522 typedef typename std::vector<IFunctorPtr> Receivers;
00523 typedef typename std::vector<IFunctorPtr>::iterator ReceiversIter;
00524 Receivers receivers;
00525 };
00526
00527 };
00528
00529 #endif
00530