Initial Commit
This commit is contained in:
81
include/Event.h
Normal file
81
include/Event.h
Normal file
@@ -0,0 +1,81 @@
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
|
||||
// TODO: Document & Explain this
|
||||
|
||||
template <typename ... Args>
|
||||
class Event;
|
||||
|
||||
template <typename ... Args>
|
||||
class EventConnection {
|
||||
private:
|
||||
using delegate = std::function<void(Args...)>;
|
||||
public:
|
||||
EventConnection(Event<Args...> *creator, delegate cb) : owner(creator), callback(std::move(cb)) {}
|
||||
bool Disconnect(); // Breaks the event connection, but does not destroy the instance
|
||||
void Invoke(Args... e);
|
||||
private:
|
||||
Event<Args...> * owner;
|
||||
delegate callback;
|
||||
bool active = true;
|
||||
};
|
||||
|
||||
template <typename ... Args>
|
||||
class Event {
|
||||
public:
|
||||
using delegate = std::function<void(Args...)>;
|
||||
using connection = EventConnection<Args ...>;
|
||||
using event_ptr = std::shared_ptr<connection>;
|
||||
public:
|
||||
void Await(Args& ... arg);
|
||||
void Invoke(Args... args);
|
||||
void operator()(Args... args);
|
||||
connection Connect(delegate callback);
|
||||
void Disconnect(connection &conn);
|
||||
connection operator+=(delegate callback);
|
||||
private:
|
||||
std::vector<event_ptr> listeners;
|
||||
uint64_t listenerCounter = 0;
|
||||
};
|
||||
|
||||
|
||||
template<typename... Args>
|
||||
EventConnection<Args...> Event<Args...>::operator+=(Event::delegate callback) { return Connect(callback); }
|
||||
|
||||
template<typename... Args>
|
||||
void Event<Args...>::operator()(Args... args) { Invoke(args...);}
|
||||
|
||||
template <typename... Args>
|
||||
void EventConnection<Args...>::Invoke(Args... e) { callback(e...); }
|
||||
|
||||
template <typename ... Args>
|
||||
bool EventConnection<Args...>::Disconnect() {
|
||||
if (active) {
|
||||
owner->Disconnect(this);
|
||||
active = false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename ... Args>
|
||||
void Event<Args...>::Invoke(Args... args) {
|
||||
for (event_ptr &connection_ptr: this->listeners) {
|
||||
connection_ptr->Invoke(args...);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename ... Args>
|
||||
EventConnection<Args...> Event<Args...>::Connect(delegate callback)
|
||||
{
|
||||
event_ptr retval(new connection(this, callback));
|
||||
this->listeners.push_back(retval);
|
||||
return *retval;
|
||||
}
|
||||
|
||||
template <typename ... Args>
|
||||
void Event<Args...>::Disconnect(connection &conn) {
|
||||
listeners.erase(std::remove(listeners.begin(), listeners.end(), 99), listeners.end());
|
||||
}
|
Reference in New Issue
Block a user