Separate EventConnection class from Event file
This commit is contained in:
@@ -21,7 +21,9 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
|
|||||||
include_directories("include")
|
include_directories("include")
|
||||||
|
|
||||||
add_library(Event SHARED ${SOURCES}
|
add_library(Event SHARED ${SOURCES}
|
||||||
src/Event.cpp)
|
src/Event.cpp
|
||||||
|
include/EventConnection.h
|
||||||
|
src/EventConnection.cpp)
|
||||||
|
|
||||||
set_target_properties(Event PROPERTIES LINKER_LANGUAGE CXX)
|
set_target_properties(Event PROPERTIES LINKER_LANGUAGE CXX)
|
||||||
|
|
||||||
|
@@ -1,3 +1,10 @@
|
|||||||
|
/// @file Event.hpp
|
||||||
|
/// @description Templated Event Hook Class modeled after C# events
|
||||||
|
/// @author Josh O'Leary - Redacted Software
|
||||||
|
/// @revision 3
|
||||||
|
/// @lastedit 2024-02-21
|
||||||
|
/// @license Unlicense - Public Domain
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
@@ -5,22 +12,10 @@
|
|||||||
|
|
||||||
// TODO: Document & Explain this
|
// TODO: Document & Explain this
|
||||||
|
|
||||||
template <typename ... Args>
|
#include <EventConnection.h>
|
||||||
class Event;
|
|
||||||
|
|
||||||
template <typename ... Args>
|
template <typename ... Args>
|
||||||
class EventConnection {
|
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>
|
template <typename ... Args>
|
||||||
class Event {
|
class Event {
|
||||||
@@ -40,26 +35,12 @@ private:
|
|||||||
uint64_t listenerCounter = 0;
|
uint64_t listenerCounter = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
EventConnection<Args...> Event<Args...>::operator+=(Event::delegate callback) { return Connect(callback); }
|
EventConnection<Args...> Event<Args...>::operator+=(Event::delegate callback) { return Connect(callback); }
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
void Event<Args...>::operator()(Args... args) { Invoke(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>
|
template <typename ... Args>
|
||||||
void Event<Args...>::Invoke(Args... args) {
|
void Event<Args...>::Invoke(Args... args) {
|
||||||
for (event_ptr &connection_ptr: this->listeners) {
|
for (event_ptr &connection_ptr: this->listeners) {
|
||||||
|
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;
|
||||||
|
}
|
1
src/EventConnection.cpp
Normal file
1
src/EventConnection.cpp
Normal file
@@ -0,0 +1 @@
|
|||||||
|
#include <EventConnection.h>
|
Reference in New Issue
Block a user