Separate EventConnection class from Event file
This commit is contained in:
38
include/EventConnection.h
Normal file
38
include/EventConnection.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <Event.h>
|
||||
|
||||
template <typename ... Args>
|
||||
class Event;
|
||||
|
||||
template <typename ... Args>
|
||||
class EventConnection {
|
||||
private:
|
||||
using delegate = std::function<void(Args...)>;
|
||||
public:
|
||||
EventConnection(Event<Args...> *creator, delegate 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>
|
||||
EventConnection<Args...>::EventConnection(Event<Args...> *creator, EventConnection::delegate cb) : owner(creator), callback(std::move(cb)) {}
|
||||
|
||||
|
||||
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;
|
||||
}
|
Reference in New Issue
Block a user