1
0

timerclient.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #ifndef __TIMER_CLIENT_H
  2. #define __TIMER_CLIENT_H
  3. #include <bfc/dispatch.h>
  4. #include <bfc/common.h>
  5. #include <bfc/depend.h>
  6. #include <map>
  7. #define DEFERREDCB_TIMERID -2
  8. class TimerClient;
  9. #ifdef _WIN32
  10. typedef UINT_PTR TimerToken ;
  11. #elif defined(__APPLE__)
  12. typedef EventLoopTimerRef TimerToken;
  13. #else
  14. #error port me!
  15. #endif
  16. typedef struct {
  17. TimerClient *origin;
  18. intptr_t param1;
  19. intptr_t param2;
  20. } deferred_callback;
  21. class NOVTABLE TimerClient : public Dispatchable
  22. {
  23. protected:
  24. TimerClient() { }
  25. public:
  26. int timerclient_setTimer(int id, int ms);
  27. int timerclient_killTimer(int id);
  28. void timerclient_postDeferredCallback(intptr_t param1, intptr_t param2=0, int mindelay=0);
  29. int timerclient_onDeferredCallback(intptr_t param1, intptr_t param2);
  30. void timerclient_timerCallback(TimerToken token);
  31. TimerClient *timerclient_getMasterClient();
  32. void timerclient_onMasterClientMultiplex();
  33. api_dependent *timerclient_getDependencyPtr();
  34. void timerclient_setSkipped(int s);
  35. int timerclient_getSkipped();
  36. void timerclient_setTimerDelay(int td);
  37. int timerclient_getTimerDelay();
  38. const wchar_t *timerclient_getName();
  39. enum {
  40. TIMERCLIENT_TIMERCALLBACK = 101,
  41. TIMERCLIENT_SETTIMER = 110,
  42. TIMERCLIENT_KILLTIMER = 120,
  43. TIMERCLIENT_GETMASTERCLIENT = 130,
  44. TIMERCLIENT_ONMASTERMUX = 140,
  45. TIMERCLIENT_GETDEPPTR = 150,
  46. TIMERCLIENT_SETSKIPPED = 160,
  47. TIMERCLIENT_GETSKIPPED = 170,
  48. TIMERCLIENT_SETTIMERDELAY = 180,
  49. TIMERCLIENT_GETTIMERDELAY = 190,
  50. TIMERCLIENT_POSTDEFERREDCB = 200,
  51. TIMERCLIENT_ONDEFERREDCB = 210,
  52. TIMERCLIENT_GETNAME = 220,
  53. };
  54. };
  55. inline void TimerClient::timerclient_timerCallback(TimerToken token) {
  56. _voidcall(TIMERCLIENT_TIMERCALLBACK, token);
  57. }
  58. inline int TimerClient::timerclient_setTimer(int id, int ms) {
  59. return _call(TIMERCLIENT_SETTIMER, 0, id, ms);
  60. }
  61. inline int TimerClient::timerclient_killTimer(int id) {
  62. return _call(TIMERCLIENT_KILLTIMER, 0, id);
  63. }
  64. inline TimerClient *TimerClient::timerclient_getMasterClient() {
  65. return _call(TIMERCLIENT_GETMASTERCLIENT, (TimerClient *)NULL);
  66. }
  67. inline void TimerClient::timerclient_onMasterClientMultiplex() {
  68. _voidcall(TIMERCLIENT_ONMASTERMUX);
  69. }
  70. inline api_dependent *TimerClient::timerclient_getDependencyPtr() {
  71. return _call(TIMERCLIENT_GETDEPPTR, (api_dependent *)NULL);
  72. }
  73. inline void TimerClient::timerclient_setSkipped(int s) {
  74. _voidcall(TIMERCLIENT_SETSKIPPED, s);
  75. }
  76. inline int TimerClient::timerclient_getSkipped() {
  77. return _call(TIMERCLIENT_GETSKIPPED, 0);
  78. }
  79. inline void TimerClient::timerclient_setTimerDelay(int td) {
  80. _voidcall(TIMERCLIENT_SETTIMERDELAY, td);
  81. }
  82. inline int TimerClient::timerclient_getTimerDelay() {
  83. return _call(TIMERCLIENT_GETTIMERDELAY, 0);
  84. }
  85. inline void TimerClient::timerclient_postDeferredCallback(intptr_t param1, intptr_t param2, int mindelay) {
  86. _voidcall(TIMERCLIENT_POSTDEFERREDCB, param1, param2, mindelay);
  87. }
  88. inline int TimerClient::timerclient_onDeferredCallback(intptr_t param1, intptr_t param2) {
  89. return _call(TIMERCLIENT_ONDEFERREDCB, 0, param1, param2);
  90. }
  91. inline const wchar_t *TimerClient::timerclient_getName() {
  92. return _call(TIMERCLIENT_GETNAME, (const wchar_t *)NULL);
  93. }
  94. class NOVTABLE TimerClientI : public TimerClient {
  95. protected:
  96. TimerClientI();
  97. public:
  98. virtual ~TimerClientI();
  99. virtual int timerclient_setTimer(intptr_t id, int ms);
  100. virtual int timerclient_killTimer(intptr_t id);
  101. // override this to catch your timer events
  102. virtual void timerclient_timerCallback(int id) { }
  103. virtual TimerClient *timerclient_getMasterClient() { return NULL; }
  104. virtual void timerclient_onMasterClientMultiplex() { };
  105. virtual api_dependent *timerclient_getDependencyPtr()=0;
  106. virtual void timerclient_setSkipped(int s) { skipped = s; }
  107. virtual int timerclient_getSkipped() { return skipped; }
  108. virtual void timerclient_setTimerDelay(int td) { timerdelay = td; }
  109. virtual int timerclient_getTimerDelay() { return timerdelay; }
  110. virtual void timerclient_postDeferredCallback(intptr_t param1, intptr_t param2=0, int mindelay=0);
  111. virtual int timerclient_onDeferredCallback(intptr_t param1, intptr_t param2) { return 1; };
  112. virtual const wchar_t *timerclient_getName() { return NULL; }
  113. protected:
  114. RECVS_DISPATCH;
  115. private:
  116. virtual void timerclient_handleDeferredCallback(TimerToken token);
  117. int skipped;
  118. int timerdelay;
  119. int disallowset;
  120. PtrList<deferred_callback> cbs;
  121. #ifdef _WIN32
  122. class TokenMap
  123. {
  124. public:
  125. void delItem(TimerToken) {}
  126. bool reverseGetItem(intptr_t id, TimerToken *token)
  127. {
  128. *token = id;
  129. return true;
  130. }
  131. bool getItem(TimerToken token, intptr_t *id)
  132. {
  133. *id = token;
  134. return true;
  135. }
  136. void addItem(TimerToken, intptr_t) {}
  137. };
  138. #else
  139. typedef std::map<TimerToken, intptr_t> TokenMap;
  140. #endif
  141. TokenMap tokens;
  142. };
  143. class NOVTABLE TimerClientDI : public TimerClientI, public DependentI {
  144. protected:
  145. TimerClientDI() { }
  146. public:
  147. api_dependent *timerclient_getDependencyPtr() { return this; }
  148. };
  149. #endif