Skip to content

Instantly share code, notes, and snippets.

@hadashiA
Created October 19, 2013 16:37
Show Gist options
  • Save hadashiA/7058213 to your computer and use it in GitHub Desktop.
Save hadashiA/7058213 to your computer and use it in GitHub Desktop.
c++11のlambdaでイベントハンドラ ref: http://qiita.com/hadashiA/items/3c3c10f9596c8f5fcdc9
#include <iostream>
#include <functional>
#include <string>
#include <map>
#include <vector>
using namespace std;
struct Event {
Event(const string& name, int data)
: name(name),
data(data) {
}
string name;
int data;
};
typedef function<bool(const Event&)> EventHandler;
int main(int argc, char **argv) {
// set evnet queues
vector<Event> queues;
queues.emplace_back("hoge", 100);
queues.emplace_back("hoge", 101);
// set handlers
multimap<string, EventHandler> handlers;
handlers.emplace("hoge", [](const Event& e) {
cout << "emit hoge event!!!" << e.data << endl;
return true;
});
handlers.emplace("moge", [](const Event& e) {
cout << "emit moge event!!!" << e.data << endl;
return true;
});
// trigger
for (auto event : queues) {
for (auto iter : handlers) {
if (event.name == iter.first) {
iter.second(event);
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment