Christopher Haster / events

Dependents:   SimpleHTTPExample

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Event.h Source File

Event.h

00001 /*  Event
00002  *
00003  *  Pendable event
00004  */
00005 #ifndef EVENT_H
00006 #define EVENT_H
00007 
00008 #include "EventQueue.h"
00009 #include "FuncPtr.h"
00010 #include "Binder.h"
00011 
00012 
00013 /** Pendable event class
00014  */
00015 template <typename F>
00016 class Event;
00017 
00018 /** Pendable event class
00019  */
00020 template <typename A0, typename A1, typename A2, typename A3, typename A4>
00021 class Event<void(A0, A1, A2, A3, A4)> {
00022 public:
00023     /** Create an event bound to a queue
00024      */
00025     Event(EventQueue *queue=0, FuncPtr<void(A0, A1, A2, A3, A4)> func=0) {
00026         _delay = 0;
00027         _period = -1;
00028         attach(queue, func);
00029     }
00030 
00031     /** Create an event bound to a queue
00032      */
00033     template <typename T, typename M>
00034     Event(EventQueue *queue, T *obj, M method) {
00035         _delay = 0;
00036         _period = -1;
00037         attach(queue, obj, method);
00038     }
00039 
00040     /** Attach an event to a queue
00041      */
00042     void attach(EventQueue *queue) {
00043         _queue = queue;
00044     }
00045 
00046     /** Attach a callback to an event
00047      */
00048     void attach(FuncPtr<void(A0, A1, A2, A3, A4)> func) {
00049         _func.attach(func);
00050     }
00051 
00052     /** Attach a callback to an event
00053      */
00054     template <typename T, typename M>
00055     void attach(T *obj, M method) {
00056         attach(FuncPtr<void(A0, A1, A2, A3, A4)>(obj, method));
00057     }
00058 
00059     /** Attach a func to an event
00060      */
00061     void attach(EventQueue *queue, FuncPtr<void(A0, A1, A2, A3, A4)> func) {
00062         attach(queue);
00063         attach(func);
00064     }
00065 
00066     /** Attach an event to a queue
00067      */
00068     template <typename T, typename M>
00069     void attach(EventQueue *queue, T *obj, M method) {
00070         attach(queue);
00071         attach(obj, method);
00072     }
00073 
00074     /** Set delay for when the event is dispatched after it is posted
00075      *  @param ms   Delay in milliseconds
00076      */
00077     void delay(int ms) {
00078         _delay = ms;
00079     }
00080 
00081     /** Set event to repeat periodically after it is posted
00082      *  @param ms   Period in milliseconds
00083      */
00084     void period(int ms) {
00085         _period = ms;
00086     }
00087 
00088     /** Set tolerance hint on when the event must be called, defaults to 0
00089      *  @param ms   Tolerance in milliseconds
00090      */
00091     void tolerance(int ms) {
00092         _tolerance = ms;
00093     }
00094 
00095     /** Post the event onto the bound queue
00096      *  @param ms   Max time to wait if memory is not available in milliseconds
00097      *  @return     True if the event was posted successfully
00098      */
00099     bool trigger(A0 a0, A1 a1, A2 a2, A3 a3, A4 a4, int ms=-1) {
00100         return _queue->event_trigger(
00101                 Binder<void(A0,A1,A2,A3,A4),A0,A1,A2,A3,A4>(_func,a0,a1,a2,a3,a4),
00102                 _delay, _period, _tolerance, ms);
00103     }
00104 
00105     /** Post the event onto the bound queue
00106      */
00107     void call(A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) {
00108         trigger(a0, a1, a2, a3, a4);
00109     }
00110 
00111     /** Post the event onto the bound queue
00112      */
00113     void operator()(A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) {
00114         return call(a0, a1, a2, a3, a4);
00115     }
00116 
00117     /** Test if event has been bound
00118      */
00119     operator bool() const {
00120         return _func && _queue;
00121     }
00122 
00123     /** Static thunk for passing as C-style function
00124      *  @param data Event to dispatch passed as void pointer
00125      */
00126     static void thunk(void *data, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) {
00127         return static_cast<Event<void(A0, A1, A2, A3, A4)>*>(data)
00128                 ->call(a0, a1, a2, a3, a4);
00129     }
00130 
00131 private:
00132     FuncPtr<void(A0,A1,A2,A3,A4)> _func;
00133     EventQueue *_queue;
00134     int _delay;
00135     int _period;
00136     int _tolerance;
00137 };
00138 
00139 /** Pendable event class
00140  */
00141 template <typename A0, typename A1, typename A2, typename A3>
00142 class Event<void(A0, A1, A2, A3)> {
00143 public:
00144     /** Create an event bound to a queue
00145      */
00146     Event(EventQueue *queue=0, FuncPtr<void(A0, A1, A2, A3)> func=0) {
00147         _delay = 0;
00148         _period = -1;
00149         attach(queue, func);
00150     }
00151 
00152     /** Create an event bound to a queue
00153      */
00154     template <typename T, typename M>
00155     Event(EventQueue *queue, T *obj, M method) {
00156         _delay = 0;
00157         _period = -1;
00158         attach(queue, obj, method);
00159     }
00160 
00161     /** Attach an event to a queue
00162      */
00163     void attach(EventQueue *queue) {
00164         _queue = queue;
00165     }
00166 
00167     /** Attach a callback to an event
00168      */
00169     void attach(FuncPtr<void(A0, A1, A2, A3)> func) {
00170         _func.attach(func);
00171     }
00172 
00173     /** Attach a callback to an event
00174      */
00175     template <typename T, typename M>
00176     void attach(T *obj, M method) {
00177         attach(FuncPtr<void(A0, A1, A2, A3)>(obj, method));
00178     }
00179 
00180     /** Attach a func to an event
00181      */
00182     void attach(EventQueue *queue, FuncPtr<void(A0, A1, A2, A3)> func) {
00183         attach(queue);
00184         attach(func);
00185     }
00186 
00187     /** Attach an event to a queue
00188      */
00189     template <typename T, typename M>
00190     void attach(EventQueue *queue, T *obj, M method) {
00191         attach(queue);
00192         attach(obj, method);
00193     }
00194 
00195     /** Set delay for when the event is dispatched after it is posted
00196      *  @param ms   Delay in milliseconds
00197      */
00198     void delay(int ms) {
00199         _delay = ms;
00200     }
00201 
00202     /** Set event to repeat periodically after it is posted
00203      *  @param ms   Period in milliseconds
00204      */
00205     void period(int ms) {
00206         _period = ms;
00207     }
00208 
00209     /** Set tolerance hint on when the event must be called, defaults to 0
00210      *  @param ms   Tolerance in milliseconds
00211      */
00212     void tolerance(int ms) {
00213         _tolerance = ms;
00214     }
00215 
00216     /** Post the event onto the bound queue
00217      *  @param ms   Max time to wait if memory is not available in milliseconds
00218      *  @return     True if the event was posted successfully
00219      */
00220     bool trigger(A0 a0, A1 a1, A2 a2, A3 a3, int ms=-1) {
00221         return _queue->event_trigger(
00222                 Binder<void(A0,A1,A2,A3),A0,A1,A2,A3>(_func,a0,a1,a2,a3),
00223                 _delay, _period, _tolerance, ms);
00224     }
00225 
00226     /** Post the event onto the bound queue
00227      */
00228     void call(A0 a0, A1 a1, A2 a2, A3 a3) {
00229         trigger(a0, a1, a2, a3);
00230     }
00231 
00232     /** Post the event onto the bound queue
00233      */
00234     void operator()(A0 a0, A1 a1, A2 a2, A3 a3) {
00235         return call(a0, a1, a2, a3);
00236     }
00237 
00238     /** Test if event has been bound
00239      */
00240     operator bool() const {
00241         return _func && _queue;
00242     }
00243 
00244     /** Static thunk for passing as C-style function
00245      *  @param data Event to dispatch passed as void pointer
00246      */
00247     static void thunk(void *data, A0 a0, A1 a1, A2 a2, A3 a3) {
00248         return static_cast<Event<void(A0, A1, A2, A3)>*>(data)
00249                 ->call(a0, a1, a2, a3);
00250     }
00251 
00252 private:
00253     FuncPtr<void(A0,A1,A2,A3)> _func;
00254     EventQueue *_queue;
00255     int _delay;
00256     int _period;
00257     int _tolerance;
00258 };
00259 
00260 /** Pendable event class
00261  */
00262 template <typename A0, typename A1, typename A2>
00263 class Event<void(A0, A1, A2)> {
00264 public:
00265     /** Create an event bound to a queue
00266      */
00267     Event(EventQueue *queue=0, FuncPtr<void(A0, A1, A2)> func=0) {
00268         _delay = 0;
00269         _period = -1;
00270         attach(queue, func);
00271     }
00272 
00273     /** Create an event bound to a queue
00274      */
00275     template <typename T, typename M>
00276     Event(EventQueue *queue, T *obj, M method) {
00277         _delay = 0;
00278         _period = -1;
00279         attach(queue, obj, method);
00280     }
00281 
00282     /** Attach an event to a queue
00283      */
00284     void attach(EventQueue *queue) {
00285         _queue = queue;
00286     }
00287 
00288     /** Attach a callback to an event
00289      */
00290     void attach(FuncPtr<void(A0, A1, A2)> func) {
00291         _func.attach(func);
00292     }
00293 
00294     /** Attach a callback to an event
00295      */
00296     template <typename T, typename M>
00297     void attach(T *obj, M method) {
00298         attach(FuncPtr<void(A0, A1, A2)>(obj, method));
00299     }
00300 
00301     /** Attach a func to an event
00302      */
00303     void attach(EventQueue *queue, FuncPtr<void(A0, A1, A2)> func) {
00304         attach(queue);
00305         attach(func);
00306     }
00307 
00308     /** Attach an event to a queue
00309      */
00310     template <typename T, typename M>
00311     void attach(EventQueue *queue, T *obj, M method) {
00312         attach(queue);
00313         attach(obj, method);
00314     }
00315 
00316     /** Set delay for when the event is dispatched after it is posted
00317      *  @param ms   Delay in milliseconds
00318      */
00319     void delay(int ms) {
00320         _delay = ms;
00321     }
00322 
00323     /** Set event to repeat periodically after it is posted
00324      *  @param ms   Period in milliseconds
00325      */
00326     void period(int ms) {
00327         _period = ms;
00328     }
00329 
00330     /** Set tolerance hint on when the event must be called, defaults to 0
00331      *  @param ms   Tolerance in milliseconds
00332      */
00333     void tolerance(int ms) {
00334         _tolerance = ms;
00335     }
00336 
00337     /** Post the event onto the bound queue
00338      *  @param ms   Max time to wait if memory is not available in milliseconds
00339      *  @return     True if the event was posted successfully
00340      */
00341     bool trigger(A0 a0, A1 a1, A2 a2, int ms=-1) {
00342         return _queue->event_trigger(
00343                 Binder<void(A0,A1,A2),A0,A1,A2>(_func,a0,a1,a2),
00344                 _delay, _period, _tolerance, ms);
00345     }
00346 
00347     /** Post the event onto the bound queue
00348      */
00349     void call(A0 a0, A1 a1, A2 a2) {
00350         trigger(a0, a1, a2);
00351     }
00352 
00353     /** Post the event onto the bound queue
00354      */
00355     void operator()(A0 a0, A1 a1, A2 a2) {
00356         return call(a0, a1, a2);
00357     }
00358 
00359     /** Test if event has been bound
00360      */
00361     operator bool() const {
00362         return _func && _queue;
00363     }
00364 
00365     /** Static thunk for passing as C-style function
00366      *  @param data Event to dispatch passed as void pointer
00367      */
00368     static void thunk(void *data, A0 a0, A1 a1, A2 a2) {
00369         return static_cast<Event<void(A0, A1, A2)>*>(data)
00370                 ->call(a0, a1, a2);
00371     }
00372 
00373 private:
00374     FuncPtr<void(A0,A1,A2)> _func;
00375     EventQueue *_queue;
00376     int _delay;
00377     int _period;
00378     int _tolerance;
00379 };
00380 
00381 /** Pendable event class
00382  */
00383 template <typename A0, typename A1>
00384 class Event<void(A0, A1)> {
00385 public:
00386     /** Create an event bound to a queue
00387      */
00388     Event(EventQueue *queue=0, FuncPtr<void(A0, A1)> func=0) {
00389         _delay = 0;
00390         _period = -1;
00391         attach(queue, func);
00392     }
00393 
00394     /** Create an event bound to a queue
00395      */
00396     template <typename T, typename M>
00397     Event(EventQueue *queue, T *obj, M method) {
00398         _delay = 0;
00399         _period = -1;
00400         attach(queue, obj, method);
00401     }
00402 
00403     /** Attach an event to a queue
00404      */
00405     void attach(EventQueue *queue) {
00406         _queue = queue;
00407     }
00408 
00409     /** Attach a callback to an event
00410      */
00411     void attach(FuncPtr<void(A0, A1)> func) {
00412         _func.attach(func);
00413     }
00414 
00415     /** Attach a callback to an event
00416      */
00417     template <typename T, typename M>
00418     void attach(T *obj, M method) {
00419         attach(FuncPtr<void(A0, A1)>(obj, method));
00420     }
00421 
00422     /** Attach a func to an event
00423      */
00424     void attach(EventQueue *queue, FuncPtr<void(A0, A1)> func) {
00425         attach(queue);
00426         attach(func);
00427     }
00428 
00429     /** Attach an event to a queue
00430      */
00431     template <typename T, typename M>
00432     void attach(EventQueue *queue, T *obj, M method) {
00433         attach(queue);
00434         attach(obj, method);
00435     }
00436 
00437     /** Set delay for when the event is dispatched after it is posted
00438      *  @param ms   Delay in milliseconds
00439      */
00440     void delay(int ms) {
00441         _delay = ms;
00442     }
00443 
00444     /** Set event to repeat periodically after it is posted
00445      *  @param ms   Period in milliseconds
00446      */
00447     void period(int ms) {
00448         _period = ms;
00449     }
00450 
00451     /** Set tolerance hint on when the event must be called, defaults to 0
00452      *  @param ms   Tolerance in milliseconds
00453      */
00454     void tolerance(int ms) {
00455         _tolerance = ms;
00456     }
00457 
00458     /** Post the event onto the bound queue
00459      *  @param ms   Max time to wait if memory is not available in milliseconds
00460      *  @return     True if the event was posted successfully
00461      */
00462     bool trigger(A0 a0, A1 a1, int ms=-1) {
00463         return _queue->event_trigger(
00464                 Binder<void(A0,A1),A0,A1>(_func,a0,a1),
00465                 _delay, _period, _tolerance, ms);
00466     }
00467 
00468     /** Post the event onto the bound queue
00469      */
00470     void call(A0 a0, A1 a1) {
00471         trigger(a0, a1);
00472     }
00473 
00474     /** Post the event onto the bound queue
00475      */
00476     void operator()(A0 a0, A1 a1) {
00477         return call(a0, a1);
00478     }
00479 
00480     /** Test if event has been bound
00481      */
00482     operator bool() const {
00483         return _func && _queue;
00484     }
00485 
00486     /** Static thunk for passing as C-style function
00487      *  @param data Event to dispatch passed as void pointer
00488      */
00489     static void thunk(void *data, A0 a0, A1 a1) {
00490         return static_cast<Event<void(A0, A1)>*>(data)
00491                 ->call(a0, a1);
00492     }
00493 
00494 private:
00495     FuncPtr<void(A0,A1)> _func;
00496     EventQueue *_queue;
00497     int _delay;
00498     int _period;
00499     int _tolerance;
00500 };
00501 
00502 /** Pendable event class
00503  */
00504 template <typename A0>
00505 class Event<void(A0)> {
00506 public:
00507     /** Create an event bound to a queue
00508      */
00509     Event(EventQueue *queue=0, FuncPtr<void(A0)> func=0) {
00510         _delay = 0;
00511         _period = -1;
00512         attach(queue, func);
00513     }
00514 
00515     /** Create an event bound to a queue
00516      */
00517     template <typename T, typename M>
00518     Event(EventQueue *queue, T *obj, M method) {
00519         _delay = 0;
00520         _period = -1;
00521         attach(queue, obj, method);
00522     }
00523 
00524     /** Attach an event to a queue
00525      */
00526     void attach(EventQueue *queue) {
00527         _queue = queue;
00528     }
00529 
00530     /** Attach a callback to an event
00531      */
00532     void attach(FuncPtr<void(A0)> func) {
00533         _func.attach(func);
00534     }
00535 
00536     /** Attach a callback to an event
00537      */
00538     template <typename T, typename M>
00539     void attach(T *obj, M method) {
00540         attach(FuncPtr<void(A0)>(obj, method));
00541     }
00542 
00543     /** Attach a func to an event
00544      */
00545     void attach(EventQueue *queue, FuncPtr<void(A0)> func) {
00546         attach(queue);
00547         attach(func);
00548     }
00549 
00550     /** Attach an event to a queue
00551      */
00552     template <typename T, typename M>
00553     void attach(EventQueue *queue, T *obj, M method) {
00554         attach(queue);
00555         attach(obj, method);
00556     }
00557 
00558     /** Set delay for when the event is dispatched after it is posted
00559      *  @param ms   Delay in milliseconds
00560      */
00561     void delay(int ms) {
00562         _delay = ms;
00563     }
00564 
00565     /** Set event to repeat periodically after it is posted
00566      *  @param ms   Period in milliseconds
00567      */
00568     void period(int ms) {
00569         _period = ms;
00570     }
00571 
00572     /** Set tolerance hint on when the event must be called, defaults to 0
00573      *  @param ms   Tolerance in milliseconds
00574      */
00575     void tolerance(int ms) {
00576         _tolerance = ms;
00577     }
00578 
00579     /** Post the event onto the bound queue
00580      *  @param ms   Max time to wait if memory is not available in milliseconds
00581      *  @return     True if the event was posted successfully
00582      */
00583     bool trigger(A0 a0, int ms=-1) {
00584         return _queue->event_trigger(
00585                 Binder<void(A0),A0>(_func,a0),
00586                 _delay, _period, _tolerance, ms);
00587     }
00588 
00589     /** Post the event onto the bound queue
00590      */
00591     void call(A0 a0) {
00592         trigger(a0);
00593     }
00594 
00595     /** Post the event onto the bound queue
00596      */
00597     void operator()(A0 a0) {
00598         return call(a0);
00599     }
00600 
00601     /** Test if event has been bound
00602      */
00603     operator bool() const {
00604         return _func && _queue;
00605     }
00606 
00607     /** Static thunk for passing as C-style function
00608      *  @param data Event to dispatch passed as void pointer
00609      */
00610     static void thunk(void *data, A0 a0) {
00611         return static_cast<Event<void(A0)>*>(data)
00612                 ->call(a0);
00613     }
00614 
00615 private:
00616     FuncPtr<void(A0)> _func;
00617     EventQueue *_queue;
00618     int _delay;
00619     int _period;
00620     int _tolerance;
00621 };
00622 
00623 /** Pendable event class
00624  */
00625 template <>
00626 class Event<void()> {
00627 public:
00628     /** Create an event bound to a queue
00629      */
00630     Event(EventQueue *queue=0, FuncPtr<void()> func=0) {
00631         _delay = 0;
00632         _period = -1;
00633         attach(queue, func);
00634     }
00635 
00636     /** Create an event bound to a queue
00637      */
00638     template <typename T, typename M>
00639     Event(EventQueue *queue, T *obj, M method) {
00640         _delay = 0;
00641         _period = -1;
00642         attach(queue, obj, method);
00643     }
00644 
00645     /** Attach an event to a queue
00646      */
00647     void attach(EventQueue *queue) {
00648         _queue = queue;
00649     }
00650 
00651     /** Attach a callback to an event
00652      */
00653     void attach(FuncPtr<void()> func) {
00654         _func.attach(func);
00655     }
00656 
00657     /** Attach a callback to an event
00658      */
00659     template <typename T, typename M>
00660     void attach(T *obj, M method) {
00661         attach(FuncPtr<void()>(obj, method));
00662     }
00663 
00664     /** Attach a func to an event
00665      */
00666     void attach(EventQueue *queue, FuncPtr<void()> func) {
00667         attach(queue);
00668         attach(func);
00669     }
00670 
00671     /** Attach an event to a queue
00672      */
00673     template <typename T, typename M>
00674     void attach(EventQueue *queue, T *obj, M method) {
00675         attach(queue);
00676         attach(obj, method);
00677     }
00678 
00679     /** Set delay for when the event is dispatched after it is posted
00680      *  @param ms   Delay in milliseconds
00681      */
00682     void delay(int ms) {
00683         _delay = ms;
00684     }
00685 
00686     /** Set event to repeat periodically after it is posted
00687      *  @param ms   Period in milliseconds
00688      */
00689     void period(int ms) {
00690         _period = ms;
00691     }
00692 
00693     /** Set tolerance hint on when the event must be called, defaults to 0
00694      *  @param ms   Tolerance in milliseconds
00695      */
00696     void tolerance(int ms) {
00697         _tolerance = ms;
00698     }
00699 
00700     /** Post the event onto the bound queue
00701      *  @param ms   Max time to wait if memory is not available in milliseconds
00702      *  @return     True if the event was posted successfully
00703      */
00704     bool trigger(int ms=-1) {
00705         return _queue->event_trigger(_func,
00706                 _delay, _period, _tolerance, ms);
00707     }
00708 
00709     /** Post the event onto the bound queue
00710      */
00711     void call() {
00712         trigger();
00713     }
00714 
00715     /** Post the event onto the bound queue
00716      */
00717     void operator()() {
00718         return call();
00719     }
00720 
00721     /** Test if event has been bound
00722      */
00723     operator bool() const {
00724         return _func && _queue;
00725     }
00726 
00727     /** Static thunk for passing as C-style function
00728      *  @param data Event to dispatch passed as void pointer
00729      */
00730     static void thunk(void *data) {
00731         return static_cast<Event<void()>*>(data)
00732                 ->call();
00733     }
00734 
00735 private:
00736     FuncPtr<void()> _func;
00737     EventQueue *_queue;
00738     int _delay;
00739     int _period;
00740     int _tolerance;
00741 };
00742 
00743 
00744 #endif