From fb2732a570c0a255a2e445a8a2b0df4916e5d9d7 Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Thu, 23 Jan 2020 02:35:47 +0100 Subject: [PATCH] event: Improve formatting and stability --- include/event.hpp | 97 ++++++++++++++++++++++++----------------------- 1 file changed, 49 insertions(+), 48 deletions(-) diff --git a/include/event.hpp b/include/event.hpp index cc7deab..1b9c118 100644 --- a/include/event.hpp +++ b/include/event.hpp @@ -26,72 +26,61 @@ namespace datapath { class event { std::list> _listeners; - std::function _listen_cb; - std::function _silence_cb; + public: + std::function& ptr, std::function& fn)> on_add; + std::function& ptr, std::function& fn)> on_remove; - public /* functions */: + public: + event() : on_add(), on_remove() + { + _listeners.clear(); + }; - // Destructor - inline ~event() + ~event() { this->clear(); } - // Add new listener. - inline void add(std::function listener) - { - if (_listeners.size() == 0) { - if (_listen_cb) { - _listen_cb(); - } - } - _listeners.push_back(listener); - } + public /* Copy Constructor/Assignment */: + event(const event<_args...>&) = delete; + event<_args...>& operator=(const event<_args...>&) = delete; - // Remove existing listener. - inline void remove(std::function listener) + public /* Mode Constructor/Assignment */: + event(event<_args...>&& rhs) { - _listeners.remove(listener); - if (_listeners.size() == 0) { - if (_silence_cb) { - silence_cb(); - } - } + std::swap(_listeners, rhs._listeners); } + event<_args...>& operator=(event<_args...>&& rhs) + { + std::swap(_listeners, rhs._listeners); + }; + public /* Status */: // Check if empty / no listeners. inline bool empty() { return _listeners.empty(); } - // Remove all listeners. - inline void clear() - { - _listeners.clear(); - if (_silence_cb) { - _silence_cb(); - } - } - - public /* operators */: - // Call Listeners with arguments. - /// Not valid without the extra template. - template - inline void operator()(_args... args) - { - for (auto& l : _listeners) { - l(args...); - } - } - // Convert to bool (true if not empty, false if empty). inline operator bool() { return !this->empty(); } + inline size_t count() + { + return _listeners.size(); + } + + public /* Listeners */: // Add new listener. + inline void add(std::function listener) + { + if (on_add) + on_add(*this, listener); + _listeners.push_back(listener); + } inline event<_args...>& operator+=(std::function listener) { this->add(listener); @@ -99,21 +88,33 @@ namespace datapath { } // Remove existing listener. + inline void remove(std::function listener) + { + _listeners.remove(listener); + if (on_remove) + on_remove(*this, listener); + } inline event<_args...>& operator-=(std::function listener) { this->remove(listener); return *this; } - public /* events */: - void set_listen_callback(std::function cb) + // Remove all listeners. + inline void clear() { - this->_listen_cb = cb; + _listeners.clear(); } - void set_silence_callback(std::function cb) + public /* Calling */: + // Call Listeners with arguments. + template + inline void operator()(_args... args) { - this->_silence_cb = cb; + /// Not valid without the extra template. + for (auto& l : _listeners) { + l(args...); + } } }; }; // namespace datapath