Skip to content

Instantly share code, notes, and snippets.

@bl4ckb0ne
Last active March 26, 2017 20:27
Show Gist options
  • Save bl4ckb0ne/9642b0fd720fec5b82a0b530b2dc5224 to your computer and use it in GitHub Desktop.
Save bl4ckb0ne/9642b0fd720fec5b82a0b530b2dc5224 to your computer and use it in GitHub Desktop.
#include <string>
#include <vector>
#include <tuple>
#include <iostream>
#include <experimental/tuple>
#define HI std::cout << __PRETTY_FUNCTION__ << '\n';
template <class Sig>
struct MsgQueue;
template <class... Params>
struct MsgQueue<void(Params...)> {
void operator()(Params... params) {
HI
_queue.emplace_back(std::forward<Params>(params)...);
}
template <class F>
void operator >> (F &&f) {
HI
for(auto const &msg : _queue)
std::experimental::apply(f, msg);
_queue.clear();
}
private:
std::vector<std::tuple<Params...>> _queue;
};
//////////////////////////////////////////////////////////////////////////////////////////
struct Foo {
MsgQueue<void(int amount, std::string from)> takeDamage;
void update() {
HI
takeDamage >> [](int dmg, std::string from) {
std::cout << "Taking " << dmg << " dmg from " << from << '\n';
};
}
};
int main() {
Foo f;
f.takeDamage(10, "Zblorg");
f.takeDamage(15, "Glurb");
std::cout << "-----------------\n";
f.update();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment