event: Improve event code
This commit is contained in:
+82
-27
@@ -1,19 +1,20 @@
|
|||||||
/*
|
/*
|
||||||
Low Latency IPC Library for high-speed traffic
|
* Low Latency IPC Library for high-speed traffic
|
||||||
Copyright (C) 2019 Michael Fabian Dirks <info@xaymar.com>
|
* Copyright (C) 2017-2019 Michael Fabian Dirks <info@xaymar.com>
|
||||||
|
*
|
||||||
This program is free software: you can redistribute it and/or modify
|
* This program is free software; you can redistribute it and/or modify
|
||||||
it under the terms of the GNU Affero General Public License as published
|
* it under the terms of the GNU General Public License as published by
|
||||||
by the Free Software Foundation, either version 3 of the License, or
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
(at your option) any later version.
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
This program is distributed in the hope that it will be useful,
|
* This program is distributed in the hope that it will be useful,
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
GNU Affero General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
You should have received a copy of the GNU Affero General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* 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
|
#pragma once
|
||||||
@@ -25,40 +26,94 @@ namespace datapath {
|
|||||||
class event {
|
class event {
|
||||||
std::list<std::function<void(_args...)>> listeners;
|
std::list<std::function<void(_args...)>> listeners;
|
||||||
|
|
||||||
public:
|
std::function<void()> listen_cb;
|
||||||
void add(std::function<void(_args...)> listener)
|
std::function<void()> silence_cb;
|
||||||
|
|
||||||
|
public /* functions */:
|
||||||
|
|
||||||
|
// Destructor
|
||||||
|
inline ~event()
|
||||||
{
|
{
|
||||||
|
this->clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add new listener.
|
||||||
|
inline void add(std::function<void(_args...)> listener)
|
||||||
|
{
|
||||||
|
if (listeners.size() == 0) {
|
||||||
|
if (listen_cb) {
|
||||||
|
listen_cb();
|
||||||
|
}
|
||||||
|
}
|
||||||
listeners.push_back(listener);
|
listeners.push_back(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
void remove(std::function<void(_args...)> listener)
|
// Remove existing listener.
|
||||||
|
inline void remove(std::function<void(_args...)> listener)
|
||||||
{
|
{
|
||||||
listeners.remove(listener);
|
listeners.remove(listener);
|
||||||
|
if (listeners.size() == 0) {
|
||||||
|
if (silence_cb) {
|
||||||
|
silence_cb();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Not valid without the extra template.
|
// 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<typename... _largs>
|
template<typename... _largs>
|
||||||
void operator()(_args... args)
|
inline void operator()(_args... args)
|
||||||
{
|
{
|
||||||
for (auto& l : listeners) {
|
for (auto& l : listeners) {
|
||||||
l(args...);
|
l(args...);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
operator bool()
|
// Convert to bool (true if not empty, false if empty).
|
||||||
|
inline operator bool()
|
||||||
{
|
{
|
||||||
return !listeners.empty();
|
return !this->empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
// Add new listener.
|
||||||
bool empty()
|
inline event<_args...>& operator+=(std::function<void(_args...)> listener)
|
||||||
{
|
{
|
||||||
return listeners.empty();
|
this->add(listener);
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
void clear()
|
// Remove existing listener.
|
||||||
|
inline event<_args...>& operator-=(std::function<void(_args...)> listener)
|
||||||
{
|
{
|
||||||
listeners.clear();
|
this->remove(listener);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public /* events */:
|
||||||
|
void set_listen_callback(std::function<void()> cb)
|
||||||
|
{
|
||||||
|
this->listen_cb = cb;
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_silence_callback(std::function<void()> cb)
|
||||||
|
{
|
||||||
|
this->silence_cb = cb;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}; // namespace datapath
|
}; // namespace datapath
|
||||||
|
|||||||
Reference in New Issue
Block a user