obs-StreamFX/source/util/util-event.hpp
Michael Fabian 'Xaymar' Dirks 5a3954ae0e project: Fix License, License headers and Copyright information
Fixes several files incorrectly stated a different license from the actual project, as well as the copyright headers included in all files. This change has no effect on the licensing terms, it should clear up a bit of confusion by contributors. Plus the files get a bit smaller, and we have less duplicated information across the entire project.

Overall the project is GPLv2 if not built with Qt, and GPLv3 if it is built with Qt. There are no parts licensed under a different license, all have been adapted from other compatible licenses into GPLv2 or GPLv3.
2023-04-05 18:59:08 +02:00

163 lines
3.8 KiB
C++

// 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
#pragma once
#include "common.hpp"
#include "warning-disable.hpp"
#include <functional>
#include <list>
#include <mutex>
#include "warning-enable.hpp"
namespace streamfx::util {
template<typename... _args>
class event {
std::list<std::function<void(_args...)>> _listeners;
std::recursive_mutex _lock;
std::function<void()> _cb_fill;
std::function<void()> _cb_clear;
public /* constructor */:
event() : _listeners(), _lock(), _cb_fill(), _cb_clear() {}
virtual ~event()
{
std::lock_guard<std::recursive_mutex> lg(_lock);
this->clear();
}
/* Copy Constructor */
event(const event<_args...>&) = delete;
/* Move Constructor */
event(event<_args...>&& other) noexcept : event()
{
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);
}
public /* operators */:
/* Copy Operator */
event<_args...>& operator=(const event<_args...>&) = delete;
/* Move Operator */
event<_args...>& operator=(event<_args...>&& other) noexcept
{
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;
}
/** Call the event, going through all listeners in the order they were registered in.
*/
template<typename... _largs>
inline void operator()(_args... args)
{
call<_largs...>(args...);
}
template<typename... _largs>
inline void call(_args... args)
{
std::lock_guard<std::recursive_mutex> lg(_lock);
for (auto& l : _listeners) {
l(args...);
}
}
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)
{
std::lock_guard<std::recursive_mutex> lg(_lock);
if (_listeners.size() == 0) {
if (_cb_fill) {
_cb_fill();
}
}
_listeners.push_back(listener);
}
inline event<_args...>& operator+=(std::function<void(_args...)> listener)
{
this->add(listener);
return *this;
}
/** 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();
}
}
}
inline event<_args...>& operator-=(std::function<void(_args...)> listener)
{
this->remove(listener);
return *this;
}
/** 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();
}
}
inline event<_args...>& operator=(std::nullptr_t)
{
clear();
return *this;
}
public /* callbacks */:
void set_listen_callback(std::function<void()> cb)
{
std::lock_guard<std::recursive_mutex> lg(_lock);
this->_cb_fill = cb;
}
void set_silence_callback(std::function<void()> cb)
{
std::lock_guard<std::recursive_mutex> lg(_lock);
this->_cb_clear = cb;
}
};
} // namespace streamfx::util