Files
DataPath/include/event.hpp
T

121 lines
2.9 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 02:35:47 +01:00
public:
std::function<void(event<_args...>& ptr, std::function<void(_args...)>& fn)> on_add;
std::function<void(event<_args...>& ptr, std::function<void(_args...)>& fn)> on_remove;
2019-04-11 01:42:56 +02:00
2020-01-23 02:35:47 +01:00
public:
event() : on_add(), on_remove()
{
_listeners.clear();
};
2019-04-11 01:42:56 +02:00
2020-01-23 02:35:47 +01:00
~event()
2019-04-11 01:42:56 +02:00
{
this->clear();
}
2020-01-23 02:35:47 +01:00
public /* Copy Constructor/Assignment */:
event(const event<_args...>&) = delete;
event<_args...>& operator=(const event<_args...>&) = delete;
2019-01-06 11:11:21 +01:00
2020-01-23 02:35:47 +01:00
public /* Mode Constructor/Assignment */:
event(event<_args...>&& rhs)
2019-01-06 11:11:21 +01:00
{
2020-01-23 02:35:47 +01:00
std::swap(_listeners, rhs._listeners);
2019-04-11 01:42:56 +02:00
}
2020-01-23 02:35:47 +01:00
event<_args...>& operator=(event<_args...>&& rhs)
{
std::swap(_listeners, rhs._listeners);
};
2019-04-11 01:42:56 +02:00
2020-01-23 02:35:47 +01:00
public /* Status */:
2019-04-11 01:42:56 +02:00
// 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
}
2020-01-23 02:35:47 +01:00
// Convert to bool (true if not empty, false if empty).
inline operator bool()
2019-04-11 01:42:56 +02:00
{
2020-01-23 02:35:47 +01:00
return !this->empty();
2019-04-11 01:42:56 +02:00
}
2020-01-23 02:35:47 +01:00
inline size_t count()
2019-01-06 11:11:21 +01:00
{
2020-01-23 02:35:47 +01:00
return _listeners.size();
2019-01-06 11:11:21 +01:00
}
2020-01-23 02:35:47 +01:00
public /* Listeners */:
// Add new listener.
inline void add(std::function<void(_args...)> listener)
2019-01-06 11:11:21 +01:00
{
2020-01-23 02:35:47 +01:00
if (on_add)
on_add(*this, listener);
_listeners.push_back(listener);
2019-01-06 11:11:21 +01:00
}
2019-04-11 01:42:56 +02:00
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.
2020-01-23 02:35:47 +01:00
inline void remove(std::function<void(_args...)> listener)
{
_listeners.remove(listener);
if (on_remove)
on_remove(*this, listener);
}
2019-04-11 01:42:56 +02:00
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;
}
2020-01-23 02:35:47 +01:00
// Remove all listeners.
inline void clear()
2019-04-11 01:42:56 +02:00
{
2020-01-23 02:35:47 +01:00
_listeners.clear();
2019-04-11 01:42:56 +02:00
}
2020-01-23 02:35:47 +01:00
public /* Calling */:
// Call Listeners with arguments.
template<typename... _largs>
inline void operator()(_args... args)
2019-04-11 01:42:56 +02:00
{
2020-01-23 02:35:47 +01:00
/// Not valid without the extra template.
for (auto& l : _listeners) {
l(args...);
}
2019-01-06 11:11:21 +01:00
}
};
}; // namespace datapath