Skip to content

Instantly share code, notes, and snippets.

@Azhng
Last active April 22, 2016 19:53
Show Gist options
  • Save Azhng/1e92d795ba9ef8a5c71607137f973000 to your computer and use it in GitHub Desktop.
Save Azhng/1e92d795ba9ef8a5c71607137f973000 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <map>
#include <functional>
#include <memory>
using namespace std;
template <class T>
class IPluggable {
public:
IPluggable() {};
virtual T& on(string evt, function<void(T&)> handler) = 0;
virtual T& notify() = 0;
virtual T& beNotified(string evt) = 0;
protected:
map<string, function<void(T&)>> handlers;
~IPluggable() {}
};
class Engine : public IPluggable<Engine> {
public:
Engine() {};
Engine& on(string evt, function<void(Engine&)> handler) {
cout << "Adding handlers" << endl;
this->handlers[evt] = handler;
return (*this);
}
Engine& notify() {
cout << "broadcasting" << endl;
return (*this);
}
Engine& beNotified(string evt) {
this->handlers[evt](*this);
return (*this);
}
Engine& func1(Engine e) {
e.func2();
cout << "func1 -> WTF ? " << endl;
return (*this);
}
Engine& func2() {
return (*this);
}
Engine& func3() {
return (*this);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment