/* * Low Latency IPC Library for high-speed traffic * Copyright (C) 2017-2019 Michael Fabian Dirks * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ #pragma once #include #include namespace datapath { template class event { std::list> _listeners; public: std::function& ptr, std::function& fn)> on_add; std::function& ptr, std::function& fn)> on_remove; public: event() : on_add(), on_remove() { _listeners.clear(); }; ~event() { this->clear(); } public /* Copy Constructor/Assignment */: event(const event<_args...>&) = delete; event<_args...>& operator=(const event<_args...>&) = delete; public /* Mode Constructor/Assignment */: event(event<_args...>&& rhs) { 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(); } // 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); return *this; } // 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; } // Remove all listeners. inline void clear() { _listeners.clear(); } public /* Calling */: // Call Listeners with arguments. template inline void operator()(_args... args) { /// Not valid without the extra template. for (auto& l : _listeners) { l(args...); } } }; }; // namespace datapath