I am implementing a simple event system in c++. the system is designed to identify events based on a string (the name of the event), and then call a list of callback functions when the event is fired. here is a simple blueprint:
class EventManager:
public:
register_event(string name) // creates a new entry in the event table
register_listener(string name, callback) // adds the callback to name's entry in the event table
fire_event(string name// executes all functions in the event table entry for name
private:
hashmap<string, vector<function>> //the event table
What I am currently struggling with is how to create a hashmap of strings to vectors of functions, and then loop through those functions to execute them. you can assume that every callback knows it's own function types, so every callback will have arguments (void* userdata, ...)
and I will handle managing the va_list in the callback.
if someone can give me a quick snippet that shows how to create the hashmap, and one for how to loop through calling the functions, that would be useful.
EDIT Using Useless's answer, I now get the following errors:
EventManager.h
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef unordered_map<string, vector<function<void()>>> CallbackMap;
class EventManager{
public:
EventManager(){
callbacks = CallbackMap();
}
void EventManager::RegisterEvent(string const& name);
void EventManager::RegisterListener(string const &name, function<void()> callback);
void EventManager::FireEvent(string name);
private:
CallbackMap callbacks;
};
EventManager.cpp
#include "EventManager.h"
#include <string>
using namespace std;
void EventManager::RegisterEvent(string const& name){
callbacks[name] = NULL;
}
void EventManager::RegisterListener(string const &name, function<void()> callback)
{
callbacks[name].push_back(callback);
}
bool EventManager::FireEvent(string name){
auto event_callbacks = callbacks.find(event_name);
if (event_callbacks == callbacks.end()){
return false; // ?
}
// or std::for_each
for (auto cb = event_callbacks->second.begin();
cb != event_callbacks->second.end(); ++cb)
{
(*cb)();
}
return true;
}
terminal
$ g++ EventManager.cpp -std=c++0x
In file included from EventManager.cpp:1:0:
EventManager.h:7:38: error: ‘function’ was not declared in this scope
EventManager.h:7:52: error: template argument 1 is invalid
EventManager.h:7:52: error: template argument 2 is invalid
EventManager.h:7:53: error: template argument 2 is invalid
EventManager.h:7:53: error: template argument 5 is invalid
EventManager.h:7:55: error: expected unqualified-id before ‘>’ token
EventManager.h:11:5: error: ‘CallbackMap’ does not name a type
EventManager.h:18:47: error: ‘function’ has not been declared
EventManager.h:18:55: error: expected ‘,’ or ‘...’ before ‘<’ token
EventManager.h: In constructor ‘EventManager::EventManager()’:
EventManager.h:14:9: error: ‘callbacks’ was not declared in this scope
EventManager.h:14:33: error: ‘CallbackMap’ was not declared in this scope
EventManager.cpp: In member function ‘void EventManager::RegisterEvent(const string&)’:
EventManager.cpp:7:5: error: ‘callbacks’ was not declared in this scope
EventManager.cpp: At global scope:
EventManager.cpp:10:57: error: ‘function’ has not been declared
EventManager.cpp:10:65: error: expected ‘,’ or ‘...’ before ‘<’ token
EventManager.cpp: In member function ‘void EventManager::RegisterListener(const string&, int)’:
EventManager.cpp:12:5: error: ‘callbacks’ was not declared in this scope
EventManager.cpp:12:31: error: ‘callback’ was not declared in this scope
EventManager.cpp: At global scope:
EventManager.cpp:15:6: error: prototype for ‘bool EventManager::FireEvent(std::string)’ does not match any in class ‘EventManager’
EventManager.h:19:10: error: candidate is: void EventManager::FireEvent(std::string)