| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- #pragma once
- #include <stdlib.h>
- #include <stdio.h>
- #include <functional>
- #include <vector>
- #include <map>
- #include <string>
- #if __cplusplus <= 199711L
- // #error This file needs at least a C++11 compliant compiler, try using:
- // #error $ g++ -std=c++11 ..
- #endif
- using namespace std;
- struct ts {
- uint16_t x;
- uint16_t y;
- uint32_t time;
- };
- #define TEMPLATE template <typename Class>
- // Abstract Class for EventHandler to notify of a change
- class EventHandlerBase {
- public:
- virtual void execute() = 0;
- };
- // Event Handler Class : Handles Callback
- template <typename Class>
- class EventHandler : public EventHandlerBase {
- // Defining type for function pointer
- typedef void (Class::* _fptr)(struct ts);
- private:
- struct ts ts_sample = { 0, 0, 500 };
- public:
- // Object of the Listener
- Class* object;
- // Function for callback
- _fptr function;
- EventHandler(Class* obj, _fptr func) {
- object = obj;
- function = func;
- }
- void execute() {
- (object->*function)();
- }
- };
- // Class to create a event
- class Event {
- // To store all listeners of the event
- typedef std::map<int, EventHandlerBase*> EventHandlerMap;
- EventHandlerMap handlers;
- int count;
- public:
- template <typename Class>
- void addListener(Class* obj, void (Class::* func)(void)) {
- handlers[count] = new EventHandler<Class>(obj, func);
- count++;
- }
- void execute() {
- for (EventHandlerMap::iterator it = handlers.begin(); it != handlers.end(); ++it) {
- it->second->execute();
- }
- }
- };
- class EventManager {
- struct EventType {
- Event* event;
- string name;
- };
- std::vector<EventType> _events;
- static EventManager* _Instance;
- // Constructor
- EventManager() {};
- public:
- static EventManager* Instance() {
- if (!_Instance) {
- _Instance = new EventManager();
- }
- return _Instance;
- } // static EventManager* Instance()
- void createEvent(string name) {
- for (vector<EventType>::iterator it = _events.begin(); it != _events.end(); ++it) {
- EventType e = *it;
- if (e.name.compare(name) == 0)
- return;
- }
- EventType e;
- e.event = new Event();
- e.name = name;
- _events.push_back(e);
- } // createEvent
- template <typename Class>
- bool subscribe(string name, Class* obj, void (Class::* func)(void)) {
- for (vector<EventType>::iterator it = _events.begin(); it != _events.end(); ++it) {
- EventType e = *it;
- if (e.name.compare(name) == 0) {
- e.event->addListener(obj, func);
- return true;
- }
- }
- return false;
- }
- void execute(string name) {
- for (vector<EventType>::iterator it = _events.begin(); it != _events.end(); ++it) {
- EventType e = *it;
- if (e.name.compare(name) == 0) {
- e.event->execute();
- }
- }
- }
- };
|