46 lines
1.2 KiB
C++
46 lines
1.2 KiB
C++
/// @file EventConnection.hpp
|
|
/// @description Callback handler for event connections
|
|
/// @author Josh O'Leary - Redacted Software
|
|
/// @revision 3
|
|
/// @lastedit 2024-02-21
|
|
/// @license Unlicense - Public Domain
|
|
|
|
#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;
|
|
}
|