From 7937c3ca66652e1050f96c291645f767c5eef658 Mon Sep 17 00:00:00 2001 From: josh Date: Wed, 21 Feb 2024 23:05:36 -0500 Subject: [PATCH] Separate EventConnection class from Event file --- CMakeLists.txt | 4 +++- include/Event.h | 37 +++++++++---------------------------- include/EventConnection.h | 38 ++++++++++++++++++++++++++++++++++++++ src/EventConnection.cpp | 1 + 4 files changed, 51 insertions(+), 29 deletions(-) create mode 100644 include/EventConnection.h create mode 100644 src/EventConnection.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 64e0296..db6b628 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,7 +21,9 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake") include_directories("include") 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) diff --git a/include/Event.h b/include/Event.h index c3dfbfd..86355a5 100644 --- a/include/Event.h +++ b/include/Event.h @@ -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 #include @@ -5,22 +12,10 @@ // TODO: Document & Explain this -template -class Event; +#include template -class EventConnection { -private: - using delegate = std::function; -public: - EventConnection(Event *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 * owner; - delegate callback; - bool active = true; -}; +class EventConnection; template class Event { @@ -40,26 +35,12 @@ private: uint64_t listenerCounter = 0; }; - template EventConnection Event::operator+=(Event::delegate callback) { return Connect(callback); } template void Event::operator()(Args... args) { Invoke(args...);} -template -void EventConnection::Invoke(Args... e) { callback(e...); } - -template -bool EventConnection::Disconnect() { - if (active) { - owner->Disconnect(this); - active = false; - return true; - } - return false; -} - template void Event::Invoke(Args... args) { for (event_ptr &connection_ptr: this->listeners) { diff --git a/include/EventConnection.h b/include/EventConnection.h new file mode 100644 index 0000000..863ca37 --- /dev/null +++ b/include/EventConnection.h @@ -0,0 +1,38 @@ +#pragma once + +#include +#include + +template +class Event; + +template +class EventConnection { +private: + using delegate = std::function; +public: + EventConnection(Event *creator, delegate cb); + bool Disconnect(); // Breaks the event connection, but does not destroy the instance + void Invoke(Args... e); +private: + Event * owner; + delegate callback; + bool active = true; +}; + +template +EventConnection::EventConnection(Event *creator, EventConnection::delegate cb) : owner(creator), callback(std::move(cb)) {} + + +template +void EventConnection::Invoke(Args... e) { callback(e...); } + +template +bool EventConnection::Disconnect() { + if (active) { + owner->Disconnect(this); + active = false; + return true; + } + return false; +} diff --git a/src/EventConnection.cpp b/src/EventConnection.cpp new file mode 100644 index 0000000..ba7d7ab --- /dev/null +++ b/src/EventConnection.cpp @@ -0,0 +1 @@ +#include \ No newline at end of file