2023-02-28 01:15:26 +00:00
|
|
|
// AUTOGENERATED COPYRIGHT HEADER START
|
|
|
|
// Copyright (C) 2020-2023 Michael Fabian 'Xaymar' Dirks <info@xaymar.com>
|
|
|
|
// Copyright (C) 2022 lainon <GermanAizek@yandex.ru>
|
|
|
|
// AUTOGENERATED COPYRIGHT HEADER END
|
2018-11-08 07:07:49 +00:00
|
|
|
|
|
|
|
#pragma once
|
2020-04-05 04:13:14 +00:00
|
|
|
#include "common.hpp"
|
2022-08-29 10:29:44 +00:00
|
|
|
|
|
|
|
#include "warning-disable.hpp"
|
2018-11-08 07:07:49 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <list>
|
2020-02-14 06:28:57 +00:00
|
|
|
#include <mutex>
|
2022-08-29 10:29:44 +00:00
|
|
|
#include "warning-enable.hpp"
|
2018-11-08 07:07:49 +00:00
|
|
|
|
2021-06-08 02:18:02 +00:00
|
|
|
namespace streamfx::util {
|
2018-11-08 07:07:49 +00:00
|
|
|
template<typename... _args>
|
|
|
|
class event {
|
2020-02-14 06:32:17 +00:00
|
|
|
std::list<std::function<void(_args...)>> _listeners;
|
|
|
|
std::recursive_mutex _lock;
|
2018-11-08 07:07:49 +00:00
|
|
|
|
2020-02-14 06:32:17 +00:00
|
|
|
std::function<void()> _cb_fill;
|
|
|
|
std::function<void()> _cb_clear;
|
2019-01-23 19:00:35 +00:00
|
|
|
|
2020-02-14 06:32:17 +00:00
|
|
|
public /* constructor */:
|
|
|
|
event() : _listeners(), _lock(), _cb_fill(), _cb_clear() {}
|
2020-02-13 04:08:39 +00:00
|
|
|
virtual ~event()
|
2019-04-02 17:11:26 +00:00
|
|
|
{
|
2020-02-14 06:32:17 +00:00
|
|
|
std::lock_guard<std::recursive_mutex> lg(_lock);
|
2019-04-02 17:11:26 +00:00
|
|
|
this->clear();
|
|
|
|
}
|
|
|
|
|
2020-02-14 06:32:17 +00:00
|
|
|
/* Copy Constructor */
|
|
|
|
event(const event<_args...>&) = delete;
|
2018-11-08 07:07:49 +00:00
|
|
|
|
2020-02-14 06:32:17 +00:00
|
|
|
/* Move Constructor */
|
2022-07-21 11:09:10 +00:00
|
|
|
event(event<_args...>&& other) noexcept : event()
|
2018-11-08 07:07:49 +00:00
|
|
|
{
|
2020-02-14 06:32:17 +00:00
|
|
|
std::lock_guard<std::recursive_mutex> lg(_lock);
|
2020-03-31 13:47:15 +00:00
|
|
|
std::lock_guard<std::recursive_mutex> lgo(other._lock);
|
2019-01-23 19:00:35 +00:00
|
|
|
|
2020-02-14 06:32:17 +00:00
|
|
|
_listeners.swap(other._listeners);
|
|
|
|
_cb_fill.swap(other._cb_fill);
|
|
|
|
_cb_clear.swap(other._cb_clear);
|
2019-01-23 19:00:35 +00:00
|
|
|
}
|
|
|
|
|
2020-02-14 06:32:17 +00:00
|
|
|
public /* operators */:
|
|
|
|
/* Copy Operator */
|
|
|
|
event<_args...>& operator=(const event<_args...>&) = delete;
|
|
|
|
|
|
|
|
/* Move Operator */
|
2022-07-21 11:09:10 +00:00
|
|
|
event<_args...>& operator=(event<_args...>&& other) noexcept
|
2019-01-23 19:00:35 +00:00
|
|
|
{
|
2020-02-14 06:32:17 +00:00
|
|
|
std::lock_guard<std::recursive_mutex> lg(_lock);
|
|
|
|
std::lock_guard<std::recursive_mutex> lgo(other._lock);
|
|
|
|
|
|
|
|
_listeners.swap(other._listeners);
|
|
|
|
_cb_fill.swap(other._cb_fill);
|
|
|
|
_cb_clear.swap(other._cb_clear);
|
|
|
|
|
|
|
|
return *this;
|
2018-11-08 07:07:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-14 06:32:17 +00:00
|
|
|
/** Call the event, going through all listeners in the order they were registered in.
|
|
|
|
*/
|
2018-11-08 07:07:49 +00:00
|
|
|
template<typename... _largs>
|
2019-01-23 19:00:35 +00:00
|
|
|
inline void operator()(_args... args)
|
2018-11-08 07:07:49 +00:00
|
|
|
{
|
2020-02-14 06:32:17 +00:00
|
|
|
call<_largs...>(args...);
|
|
|
|
}
|
|
|
|
template<typename... _largs>
|
|
|
|
inline void call(_args... args)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::recursive_mutex> lg(_lock);
|
|
|
|
for (auto& l : _listeners) {
|
2018-11-08 07:07:49 +00:00
|
|
|
l(args...);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-14 06:32:17 +00:00
|
|
|
public /* functions: listeners */:
|
|
|
|
/** Add a new listener to the event.
|
|
|
|
* @param listener A listener bound with std::bind or a std::function.
|
|
|
|
*/
|
|
|
|
inline void add(std::function<void(_args...)> listener)
|
2018-11-08 07:07:49 +00:00
|
|
|
{
|
2020-02-14 06:32:17 +00:00
|
|
|
std::lock_guard<std::recursive_mutex> lg(_lock);
|
|
|
|
if (_listeners.size() == 0) {
|
|
|
|
if (_cb_fill) {
|
|
|
|
_cb_fill();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_listeners.push_back(listener);
|
2018-11-08 07:07:49 +00:00
|
|
|
}
|
2019-01-23 19:00:35 +00:00
|
|
|
inline event<_args...>& operator+=(std::function<void(_args...)> listener)
|
2018-11-08 07:07:49 +00:00
|
|
|
{
|
2019-01-23 19:00:35 +00:00
|
|
|
this->add(listener);
|
|
|
|
return *this;
|
2018-11-08 07:07:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-14 06:32:17 +00:00
|
|
|
/** Remove an existing listener from the event.
|
|
|
|
* @param listener A listener bound with std::bind or a std::function.
|
|
|
|
*/
|
|
|
|
inline void remove(std::function<void(_args...)> listener)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::recursive_mutex> lg(_lock);
|
|
|
|
_listeners.remove(listener);
|
|
|
|
if (_listeners.size() == 0) {
|
|
|
|
if (_cb_clear) {
|
|
|
|
_cb_clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-23 19:00:35 +00:00
|
|
|
inline event<_args...>& operator-=(std::function<void(_args...)> listener)
|
2018-11-08 07:07:49 +00:00
|
|
|
{
|
2019-01-23 19:00:35 +00:00
|
|
|
this->remove(listener);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-02-14 06:32:17 +00:00
|
|
|
/** Check if there are any listeners for the event.
|
|
|
|
* @return bool `true` if there are none, otherwise `false`.
|
|
|
|
*/
|
|
|
|
inline bool empty()
|
|
|
|
{
|
|
|
|
std::lock_guard<std::recursive_mutex> lg(_lock);
|
|
|
|
return _listeners.empty();
|
|
|
|
}
|
|
|
|
inline operator bool()
|
|
|
|
{
|
|
|
|
return !this->empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Clear the list of listeners for the event.
|
|
|
|
*/
|
|
|
|
inline void clear()
|
|
|
|
{
|
|
|
|
std::lock_guard<std::recursive_mutex> lg(_lock);
|
|
|
|
_listeners.clear();
|
|
|
|
if (_cb_clear) {
|
|
|
|
_cb_clear();
|
|
|
|
}
|
|
|
|
}
|
2020-04-23 23:57:56 +00:00
|
|
|
inline event<_args...>& operator=(std::nullptr_t)
|
2020-02-14 06:32:17 +00:00
|
|
|
{
|
|
|
|
clear();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public /* callbacks */:
|
2019-01-23 19:00:35 +00:00
|
|
|
void set_listen_callback(std::function<void()> cb)
|
|
|
|
{
|
2020-02-14 06:32:17 +00:00
|
|
|
std::lock_guard<std::recursive_mutex> lg(_lock);
|
|
|
|
this->_cb_fill = cb;
|
2019-01-23 19:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void set_silence_callback(std::function<void()> cb)
|
|
|
|
{
|
2020-02-14 06:32:17 +00:00
|
|
|
std::lock_guard<std::recursive_mutex> lg(_lock);
|
|
|
|
this->_cb_clear = cb;
|
2018-11-08 07:07:49 +00:00
|
|
|
}
|
|
|
|
};
|
2021-06-08 02:18:02 +00:00
|
|
|
} // namespace streamfx::util
|