2

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)
ewok
  • 20,148
  • 51
  • 149
  • 254

4 Answers4

3

a hashmap of strings to vectors of functions ...

typedef unordered_map<string, vector<function<void()>>> CallbackMap;

the words were all there. Note you could use unordered_multimap instead of a hashmap of vectors, and the function type reflects your callback interface (you may want function<void(Event*)> or something, for example).

As Ram pointed out in the comment, map and multimap are the equivalent tree-based associative containers if you don't specifically need a hash (or don't have C++11, although you can also use boost::unordered_map).

... and one for how to loop through calling the functions ...

bool EventManager::fire_event(string const& event_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;
}

Oh, and registering is as simple as:

void EventManager::register_listener(string const &name,
                                     function<void()> callback)
{
    callbacks[name].push_back(callback);
}

(I'm letting it create the event entry lazily and on-demand).

Useless
  • 64,155
  • 6
  • 88
  • 132
  • @Useless Thanks for the help! how does this handle arguments to the callbacks? As I explained above, each callback for a given event takes the same arguments, but different events' callback may take different arguments – ewok Oct 03 '12 at 13:15
  • @Useless, please see my edit. This doesnt seem to come close to compiling – ewok Oct 03 '12 at 14:53
  • to use `std::function`, you need `#include ` – Useless Oct 03 '12 at 15:07
0

Something like:?

typedef void(*ExampleFunction) (std::string str, int bar);

void foo(std::string str, int bar){
}
void foo2(std::string str, int bar){
}

int main(){
    std::map<std::string,std::vector<ExampleFunction> > f_map;
    std::vector<ExampleFunction> v_func;
v_func.push_back(foo);
v_func.push_back(foo2);

f_map["tiny"] = v_func;
f_map["tiny"][0]("Woo",1);
return 1;
}
Jerdak
  • 3,997
  • 1
  • 24
  • 36
0

If C++ 11 is an option (VS2012, recent versions of gcc) then you can get a hashmap via the C++ library:

#include <unordered_map>

std::unordered_map<KeyType, ValueType> map {{x,y},{n,m1},{a,b}};
map[x] = y;
ValueType q = map[y];

and so on. The difference between std::unordered_map and std::map is that std::map is sorted, and so uses red black trees underneath. This might actually be efficient enough for your needs, in which case you're fine with C++03 too.

Note that I've discovered MSVC '12 doesn't support default assignment of maps as my example shows. If you need that, use boost::assign:

#include <boost/assign.hpp>

std::unordered_map<K,V> map = boost::assign::map_list_of<K,V> (x,y)(a,b)(n,q);
Community
  • 1
  • 1
0

First, you will need to define what a callback is. Your options basically boil down to a function pointer:

typedef void (* EventCallback)(void * userdata, ...);

Or an interface:

class IEventCallback
{
public:
    virtual void callback(void * userdata, ...) = 0;
};

I'd prefer the interface version -- you can wrap a function pointer in an object of a class implementing this interface; it's much harder to go the other way.

On your EventManager, define the map and a type (for making iteration easier):

private:
    typedef std::vector<IEventCallback &> EventCallbackList_t;
    typedef std::map<std::string, EventCallbackList_t> EventMap_t;

    EventMap_t m_eventMap;

Next, the implementation is rather straightforward:

void EventManager::register_event(std::string const & name)
{
    m_eventMap.insert(std::make_pair(name, EventCallbackList_t()));
}

void EventManager::register_listener(std::string const & name, IEventCallback & callback)
{
    EventMap_t::iterator event = m_eventMap.find(name);

    if (event == m_eventMap.end()) {
        throw "No such event.";
    }

    event->second.push_back(callback);
}

void EventManager::fire_event(std::string const & name, void * userdata, ...)
{
    EventMap_t::iterator event = m_eventMap.find(name);

    if (event == m_eventMap.end()) {
        throw "No such event.";
    }

    for (EventCallbackList_t i = event->second.begin(); i != event->second.end(); ++i) {
        i->callback(userdata, ...);
    }
}

There are still a few issues with this implementation that I'll leave it up to you to solve:

  • Concurrency. Make sure that register/fire operations are synchronized if they will be called from multiple threads.
  • Callback object ownership. Consider using a shared pointer type to manage their lifetimes.
cdhowie
  • 158,093
  • 24
  • 286
  • 300