Skip to content

Instantly share code, notes, and snippets.

@arturgrigor
Forked from munro/signals.cpp
Created June 28, 2013 18:43
Show Gist options
  • Save arturgrigor/5887001 to your computer and use it in GitHub Desktop.
Save arturgrigor/5887001 to your computer and use it in GitHub Desktop.
#ifndef __signals_h__
#define __signals_h__
#include <list>
#include <memory>
#include <iostream>
#include <functional>
namespace signals {
template<typename... Values> class Signal {
private:
std::list<std::function<void(Values...)>> fns;
public:
void bind(std::function<void(Values...)> fn) {
fns.push_back(fn);
}
void trigger(Values... values) {
for (auto it = fns.begin(); it != fns.end(); ++it) {
(*it)(values...);
}
}
};
}
// Example
signals::Signal<int, int> onEvent;
onEvent.bind([](int a, int b) {
std::cout << "Event triggered " << a << ", " << b << "\n";
});
onEvent.trigger(10, 20);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment