EventManager.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #pragma once
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <functional>
  5. #include <vector>
  6. #include <map>
  7. #include <string>
  8. #if __cplusplus <= 199711L
  9. // #error This file needs at least a C++11 compliant compiler, try using:
  10. // #error $ g++ -std=c++11 ..
  11. #endif
  12. using namespace std;
  13. struct ts {
  14. uint16_t x;
  15. uint16_t y;
  16. uint32_t time;
  17. };
  18. #define TEMPLATE template <typename Class>
  19. // Abstract Class for EventHandler to notify of a change
  20. class EventHandlerBase {
  21. public:
  22. virtual void execute() = 0;
  23. };
  24. // Event Handler Class : Handles Callback
  25. template <typename Class>
  26. class EventHandler : public EventHandlerBase {
  27. // Defining type for function pointer
  28. typedef void (Class::* _fptr)(struct ts);
  29. private:
  30. struct ts ts_sample = { 0, 0, 500 };
  31. public:
  32. // Object of the Listener
  33. Class* object;
  34. // Function for callback
  35. _fptr function;
  36. EventHandler(Class* obj, _fptr func) {
  37. object = obj;
  38. function = func;
  39. }
  40. void execute() {
  41. (object->*function)();
  42. }
  43. };
  44. // Class to create a event
  45. class Event {
  46. // To store all listeners of the event
  47. typedef std::map<int, EventHandlerBase*> EventHandlerMap;
  48. EventHandlerMap handlers;
  49. int count;
  50. public:
  51. template <typename Class>
  52. void addListener(Class* obj, void (Class::* func)(void)) {
  53. handlers[count] = new EventHandler<Class>(obj, func);
  54. count++;
  55. }
  56. void execute() {
  57. for (EventHandlerMap::iterator it = handlers.begin(); it != handlers.end(); ++it) {
  58. it->second->execute();
  59. }
  60. }
  61. };
  62. class EventManager {
  63. struct EventType {
  64. Event* event;
  65. string name;
  66. };
  67. std::vector<EventType> _events;
  68. static EventManager* _Instance;
  69. // Constructor
  70. EventManager() {};
  71. public:
  72. static EventManager* Instance() {
  73. if (!_Instance) {
  74. _Instance = new EventManager();
  75. }
  76. return _Instance;
  77. } // static EventManager* Instance()
  78. void createEvent(string name) {
  79. for (vector<EventType>::iterator it = _events.begin(); it != _events.end(); ++it) {
  80. EventType e = *it;
  81. if (e.name.compare(name) == 0)
  82. return;
  83. }
  84. EventType e;
  85. e.event = new Event();
  86. e.name = name;
  87. _events.push_back(e);
  88. } // createEvent
  89. template <typename Class>
  90. bool subscribe(string name, Class* obj, void (Class::* func)(void)) {
  91. for (vector<EventType>::iterator it = _events.begin(); it != _events.end(); ++it) {
  92. EventType e = *it;
  93. if (e.name.compare(name) == 0) {
  94. e.event->addListener(obj, func);
  95. return true;
  96. }
  97. }
  98. return false;
  99. }
  100. void execute(string name) {
  101. for (vector<EventType>::iterator it = _events.begin(); it != _events.end(); ++it) {
  102. EventType e = *it;
  103. if (e.name.compare(name) == 0) {
  104. e.event->execute();
  105. }
  106. }
  107. }
  108. };