Created
April 26, 2016 23:42
-
-
Save ananace/975f379186c4fad3a11ef3f2e2d9b487 to your computer and use it in GitHub Desktop.
Simple C#-like event handling in C++11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <functional> | |
#include <vector> | |
template<typename... Args> | |
class Event | |
{ | |
public: | |
typedef std::function<void(Args...)> EventHook; | |
Event() = default; | |
Event(const Event&) = default; | |
~Event() = default; | |
Event& operator=(const Event&) = default; | |
Event& operator+=(const EventHook& hook) | |
{ | |
mHooks.push_back(hook); | |
return *this; | |
} | |
template<typename Class> | |
Event& addMember(void(Class::*funcPtr)(Args...), Class* classPtr) | |
{ | |
mHooks.push_back([funcPtr, classPtr](Args... args) { | |
std::invoke(std::mem_fn(funcPtr), classPtr, std::forward<Args>(args)...); | |
}); | |
return *this; | |
} | |
void operator()(Args... args) const | |
{ | |
for (auto& hook : mHooks) | |
hook(std::forward<Args>(args)...); | |
} | |
private: | |
std::vector<EventHook> mHooks; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Event.hpp" | |
#include <cassert> | |
void sum(int a, int& out) | |
{ | |
out += a; | |
} | |
class Averager | |
{ | |
public: | |
Averager(int value) | |
: mValue(value) | |
{ | |
} | |
void average(int a, int& test) | |
{ | |
test = (a + mValue) / 2; | |
} | |
private: | |
int mValue; | |
}; | |
int main(int argc, char** argv) | |
{ | |
Event<int, int&> testEvent; | |
testEvent += sum; | |
int result = 4; | |
testEvent(2, result); | |
assert(result == 2 + 4); | |
// TODO: Event.clear(), event -=, etc. | |
// Exercise for the reader. | |
testEvent = Event<int, int&>(); | |
Averager avg(8); | |
testEvent.addMember(&Averager::average, &avg); | |
testEvent(4, result); | |
assert(result == (4 + 8) / 2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment