Callback.h 5.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /******************************************************************************/
  2. struct Callback
  3. {
  4. void (*func)();
  5. void set (void func()) {T.func=func;}
  6. void call( ) { func();}
  7. };
  8. struct CallbackUser
  9. {
  10. void (*func)(Ptr user);
  11. Ptr user;
  12. void set (void func(Ptr user), Ptr user=null) {T.func=func; T.user=user;}
  13. void call( ) { func(user);}
  14. };
  15. /******************************************************************************/
  16. struct Callbacks
  17. {
  18. #if EE_PRIVATE
  19. Bool initialized()C {return _callbacks.initialized();}
  20. #endif
  21. void add(void func( ) ); // add custom function to the queue, it will be called in the 'update' method
  22. void add(void func(Ptr user), Ptr user=null); // add custom function to the queue, it will be called in the 'update' method
  23. T1(TYPE) void add(void func(TYPE *user), TYPE *user=null) {add((void(*)(Ptr))func, user);} // add custom function to the queue, it will be called in the 'update' method
  24. T1(TYPE) void add(void func(TYPE &user), TYPE &user ) {add((void(*)(Ptr))func, &user);} // add custom function to the queue, it will be called in the 'update' method
  25. void include(void func( ) ); // add custom function to the queue if it's not listed yet, it will be called in the 'update' method
  26. void include(void func(Ptr user), Ptr user=null); // add custom function to the queue if it's not listed yet, it will be called in the 'update' method
  27. T1(TYPE) void include(void func(TYPE *user), TYPE *user=null) {include((void(*)(Ptr))func, user);} // add custom function to the queue if it's not listed yet, it will be called in the 'update' method
  28. T1(TYPE) void include(void func(TYPE &user), TYPE &user ) {include((void(*)(Ptr))func, &user);} // add custom function to the queue if it's not listed yet, it will be called in the 'update' method
  29. void exclude(void func( ) ); // remove function from the queue
  30. void exclude(void func(Ptr user), Ptr user=null); // remove function from the queue
  31. T1(TYPE) void exclude(void func(TYPE *user), TYPE *user=null) {exclude((void(*)(Ptr))func, user);} // remove function from the queue
  32. T1(TYPE) void exclude(void func(TYPE &user), TYPE &user ) {exclude((void(*)(Ptr))func, &user);} // remove function from the queue
  33. void update(); // call all callbacks and remove them from the queue
  34. void del(); // manually remove all queued callbacks and delete all used memory
  35. protected:
  36. Memc<Callback > _callbacks , _temp_callbacks;
  37. Memc<CallbackUser> _callbacks_user, _temp_callbacks_user;
  38. };
  39. /******************************************************************************/
  40. STRUCT_PRIVATE(ThreadSafeCallbacks , Callbacks)
  41. //{
  42. #if EE_PRIVATE
  43. Bool initialized()C {return super::initialized();}
  44. #endif
  45. void add(void func( ) ); // add custom function to the queue, it will be called in the 'update' method
  46. void add(void func(Ptr user), Ptr user=null); // add custom function to the queue, it will be called in the 'update' method
  47. T1(TYPE) void add(void func(TYPE *user), TYPE *user=null) {add((void(*)(Ptr))func, user);} // add custom function to the queue, it will be called in the 'update' method
  48. T1(TYPE) void add(void func(TYPE &user), TYPE &user ) {add((void(*)(Ptr))func, &user);} // add custom function to the queue, it will be called in the 'update' method
  49. void include(void func( ) ); // add custom function to the queue if it's not listed yet, it will be called in the 'update' method
  50. void include(void func(Ptr user), Ptr user=null); // add custom function to the queue if it's not listed yet, it will be called in the 'update' method
  51. T1(TYPE) void include(void func(TYPE *user), TYPE *user=null) {include((void(*)(Ptr))func, user);} // add custom function to the queue if it's not listed yet, it will be called in the 'update' method
  52. T1(TYPE) void include(void func(TYPE &user), TYPE &user ) {include((void(*)(Ptr))func, &user);} // add custom function to the queue if it's not listed yet, it will be called in the 'update' method
  53. void exclude(void func( ) ); // remove function from the queue
  54. void exclude(void func(Ptr user), Ptr user=null); // remove function from the queue
  55. T1(TYPE) void exclude(void func(TYPE *user), TYPE *user=null) {exclude((void(*)(Ptr))func, user);} // remove function from the queue
  56. T1(TYPE) void exclude(void func(TYPE &user), TYPE &user ) {exclude((void(*)(Ptr))func, &user);} // remove function from the queue
  57. void update(); // call all callbacks and remove them from the queue
  58. void del(); // manually remove all queued callbacks and delete all used memory
  59. ThreadSafeCallbacks() {}
  60. private:
  61. SyncLock _lock;
  62. NO_COPY_CONSTRUCTOR(ThreadSafeCallbacks);
  63. };
  64. /******************************************************************************/