From a141bbbea503ae7b320eba614a80ca2f4e5c1cba Mon Sep 17 00:00:00 2001 From: Vaxry Date: Sun, 21 Apr 2024 01:47:24 +0100 Subject: [PATCH] helpers: Add new C++ Signal and Listener classes A memory-safe alternative to wl_signal --- src/helpers/signal/Listener.cpp | 13 +++++++++++++ src/helpers/signal/Listener.hpp | 24 ++++++++++++++++++++++++ src/helpers/signal/Signal.cpp | 22 ++++++++++++++++++++++ src/helpers/signal/Signal.hpp | 19 +++++++++++++++++++ 4 files changed, 78 insertions(+) create mode 100644 src/helpers/signal/Listener.cpp create mode 100644 src/helpers/signal/Listener.hpp create mode 100644 src/helpers/signal/Signal.cpp create mode 100644 src/helpers/signal/Signal.hpp diff --git a/src/helpers/signal/Listener.cpp b/src/helpers/signal/Listener.cpp new file mode 100644 index 00000000..ae506343 --- /dev/null +++ b/src/helpers/signal/Listener.cpp @@ -0,0 +1,13 @@ +#include "Listener.hpp" +#include "Signal.hpp" + +CSignalListener::CSignalListener(std::function handler) : m_fHandler(handler) { + ; +} + +void CSignalListener::emit(std::any data) { + if (!m_fHandler) + return; + + m_fHandler(data); +} diff --git a/src/helpers/signal/Listener.hpp b/src/helpers/signal/Listener.hpp new file mode 100644 index 00000000..d2edb253 --- /dev/null +++ b/src/helpers/signal/Listener.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include +#include +#include + +class CSignal; + +class CSignalListener { + public: + CSignalListener(std::function handler); + + CSignalListener(CSignalListener&&) = delete; + CSignalListener(CSignalListener&) = delete; + CSignalListener(const CSignalListener&) = delete; + CSignalListener(const CSignalListener&&) = delete; + + void emit(std::any data); + + private: + std::function m_fHandler; +}; + +typedef std::shared_ptr CHyprSignalListener; diff --git a/src/helpers/signal/Signal.cpp b/src/helpers/signal/Signal.cpp new file mode 100644 index 00000000..a8adc275 --- /dev/null +++ b/src/helpers/signal/Signal.cpp @@ -0,0 +1,22 @@ +#include "Signal.hpp" +#include + +void CSignal::emit(std::any data) { + bool dirty = false; + + for (auto& l : m_vListeners) { + if (const CHyprSignalListener L = l.lock()) + L->emit(data); + else + dirty = true; + } + + if (dirty) + std::erase_if(m_vListeners, [](const auto& other) { return !other.lock(); }); +} + +CHyprSignalListener CSignal::registerListener(std::function handler) { + CHyprSignalListener listener = std::make_shared(handler); + m_vListeners.emplace_back(std::weak_ptr(listener)); + return listener; +} diff --git a/src/helpers/signal/Signal.hpp b/src/helpers/signal/Signal.hpp new file mode 100644 index 00000000..38f9ea0c --- /dev/null +++ b/src/helpers/signal/Signal.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include +#include +#include +#include + +#include "Listener.hpp" + +class CSignal { + public: + void emit(std::any data); + + // + [[nodiscard("Listener is unregistered when the ptr is lost")]] CHyprSignalListener registerListener(std::function handler); + + private: + std::vector> m_vListeners; +};