Files
DataPath/include/event.hpp
T

120 lines
2.7 KiB
C++
Raw Normal View History

2019-01-06 11:11:21 +01:00
/*
2019-04-11 01:42:56 +02:00
* Low Latency IPC Library for high-speed traffic
* Copyright (C) 2017-2019 Michael Fabian Dirks <info@xaymar.com>
*
* 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
*/
2019-01-06 11:11:21 +01:00
#pragma once
#include <functional>
#include <list>
namespace datapath {
template<typename... _args>
class event {
2020-01-23 01:51:15 +01:00
std::list<std::function<void(_args...)>> _listeners;
2019-01-06 11:11:21 +01:00
2020-01-23 01:51:15 +01:00
std::function<void()> _listen_cb;
std::function<void()> _silence_cb;
2019-04-11 01:42:56 +02:00
public /* functions */:
// Destructor
inline ~event()
{
this->clear();
}
// Add new listener.
inline void add(std::function<void(_args...)> listener)
2019-01-06 11:11:21 +01:00
{
2020-01-23 01:51:15 +01:00
if (_listeners.size() == 0) {
if (_listen_cb) {
_listen_cb();
2019-04-11 01:42:56 +02:00
}
}
2020-01-23 01:51:15 +01:00
_listeners.push_back(listener);
2019-01-06 11:11:21 +01:00
}
2019-04-11 01:42:56 +02:00
// Remove existing listener.
inline void remove(std::function<void(_args...)> listener)
2019-01-06 11:11:21 +01:00
{
2020-01-23 01:51:15 +01:00
_listeners.remove(listener);
if (_listeners.size() == 0) {
if (_silence_cb) {
2019-04-11 01:42:56 +02:00
silence_cb();
}
}
}
// Check if empty / no listeners.
inline bool empty()
{
2020-01-23 01:51:15 +01:00
return _listeners.empty();
2019-01-06 11:11:21 +01:00
}
2019-04-11 01:42:56 +02:00
// Remove all listeners.
inline void clear()
{
2020-01-23 01:51:15 +01:00
_listeners.clear();
if (_silence_cb) {
_silence_cb();
2019-04-11 01:42:56 +02:00
}
}
public /* operators */:
// Call Listeners with arguments.
/// Not valid without the extra template.
2019-01-06 11:11:21 +01:00
template<typename... _largs>
2019-04-11 01:42:56 +02:00
inline void operator()(_args... args)
2019-01-06 11:11:21 +01:00
{
2020-01-23 01:51:15 +01:00
for (auto& l : _listeners) {
2019-01-06 11:11:21 +01:00
l(args...);
}
}
2019-04-11 01:42:56 +02:00
// Convert to bool (true if not empty, false if empty).
inline operator bool()
2019-01-06 11:11:21 +01:00
{
2019-04-11 01:42:56 +02:00
return !this->empty();
2019-01-06 11:11:21 +01:00
}
2019-04-11 01:42:56 +02:00
// Add new listener.
inline event<_args...>& operator+=(std::function<void(_args...)> listener)
2019-01-06 11:11:21 +01:00
{
2019-04-11 01:42:56 +02:00
this->add(listener);
return *this;
2019-01-06 11:11:21 +01:00
}
2019-04-11 01:42:56 +02:00
// Remove existing listener.
inline event<_args...>& operator-=(std::function<void(_args...)> listener)
2019-01-06 11:11:21 +01:00
{
2019-04-11 01:42:56 +02:00
this->remove(listener);
return *this;
}
public /* events */:
void set_listen_callback(std::function<void()> cb)
{
2020-01-23 01:51:15 +01:00
this->_listen_cb = cb;
2019-04-11 01:42:56 +02:00
}
void set_silence_callback(std::function<void()> cb)
{
2020-01-23 01:51:15 +01:00
this->_silence_cb = cb;
2019-01-06 11:11:21 +01:00
}
};
}; // namespace datapath