71 lines
2.5 KiB
C++
71 lines
2.5 KiB
C++
/// @file BasicEvent.hpp
|
|
/// @description Templated BasicEvent Hook Class modeled after C# events
|
|
/// @author Josh O'Leary - Redacted Software
|
|
/// @revision 3
|
|
/// @lastedit 2024-06-01
|
|
/// @license Unlicense - Public Domain
|
|
|
|
#pragma once
|
|
|
|
#include <chrono>
|
|
#include <functional>
|
|
#include <memory>
|
|
|
|
// TODO: ThreadSafeEvent / AsyncEvent version of the class.
|
|
// TODO: ConsumableEvent - a version that can be "consumed" by a callback by returning true. It will be sunk and not passed to further callbacks.
|
|
|
|
///
|
|
template <typename delegate, typename ... Args>
|
|
class Connection;
|
|
|
|
/// BasicEvent is a templated class that can be used to raise events that can be listened in on
|
|
/// at other points in a codebase without having to tightly couple the two pieces of code.
|
|
/// @param delegate A type that can be invoked via operator() 'Call operator'.
|
|
/// @param Args... The arguments that will be expected by this event and passed to it's delegate.
|
|
template <typename delegate, typename ... Args>
|
|
class BasicEvent {
|
|
public:
|
|
using connection = Connection<delegate, 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 delegate, typename... Args>
|
|
Connection<delegate, Args...> BasicEvent<delegate, Args...>::operator+=(delegate callback) { return Connect(callback); }
|
|
|
|
template<typename delegate, typename... Args>
|
|
void BasicEvent<delegate, Args...>::operator()(Args... args) { Invoke(args...);}
|
|
|
|
template <typename delegate, typename ... Args>
|
|
void BasicEvent<delegate, Args...>::Invoke(Args... args) {
|
|
for (event_ptr &connection_ptr: this->listeners) {
|
|
connection_ptr->Invoke(args...);
|
|
}
|
|
}
|
|
|
|
template <typename delegate, typename ... Args>
|
|
Connection<delegate, Args...> BasicEvent<delegate, Args...>::Connect(delegate callback)
|
|
{
|
|
event_ptr retval(new connection(this, callback));
|
|
this->listeners.push_back(retval);
|
|
return *retval;
|
|
}
|
|
|
|
template <typename delegate, typename ... Args>
|
|
void BasicEvent<delegate, Args...>::Disconnect(connection &conn) {
|
|
listeners.erase(std::remove(listeners.begin(), listeners.end(), 99), listeners.end());
|
|
}
|
|
|
|
template <typename ... Args>
|
|
using Event = BasicEvent<std::function<void(Args...)>, Args...>;
|
|
|