event: Improve formatting and stability
This commit is contained in:
+49
-48
@@ -26,72 +26,61 @@ namespace datapath {
|
|||||||
class event {
|
class event {
|
||||||
std::list<std::function<void(_args...)>> _listeners;
|
std::list<std::function<void(_args...)>> _listeners;
|
||||||
|
|
||||||
std::function<void()> _listen_cb;
|
public:
|
||||||
std::function<void()> _silence_cb;
|
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;
|
||||||
|
|
||||||
public /* functions */:
|
public:
|
||||||
|
event() : on_add(), on_remove()
|
||||||
|
{
|
||||||
|
_listeners.clear();
|
||||||
|
};
|
||||||
|
|
||||||
// Destructor
|
~event()
|
||||||
inline ~event()
|
|
||||||
{
|
{
|
||||||
this->clear();
|
this->clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add new listener.
|
public /* Copy Constructor/Assignment */:
|
||||||
inline void add(std::function<void(_args...)> listener)
|
event(const event<_args...>&) = delete;
|
||||||
{
|
event<_args...>& operator=(const event<_args...>&) = delete;
|
||||||
if (_listeners.size() == 0) {
|
|
||||||
if (_listen_cb) {
|
|
||||||
_listen_cb();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_listeners.push_back(listener);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove existing listener.
|
public /* Mode Constructor/Assignment */:
|
||||||
inline void remove(std::function<void(_args...)> listener)
|
event(event<_args...>&& rhs)
|
||||||
{
|
{
|
||||||
_listeners.remove(listener);
|
std::swap(_listeners, rhs._listeners);
|
||||||
if (_listeners.size() == 0) {
|
|
||||||
if (_silence_cb) {
|
|
||||||
silence_cb();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
event<_args...>& operator=(event<_args...>&& rhs)
|
||||||
|
{
|
||||||
|
std::swap(_listeners, rhs._listeners);
|
||||||
|
};
|
||||||
|
|
||||||
|
public /* Status */:
|
||||||
// Check if empty / no listeners.
|
// Check if empty / no listeners.
|
||||||
inline bool empty()
|
inline bool empty()
|
||||||
{
|
{
|
||||||
return _listeners.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>
|
|
||||||
inline void operator()(_args... args)
|
|
||||||
{
|
|
||||||
for (auto& l : _listeners) {
|
|
||||||
l(args...);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Convert to bool (true if not empty, false if empty).
|
// Convert to bool (true if not empty, false if empty).
|
||||||
inline operator bool()
|
inline operator bool()
|
||||||
{
|
{
|
||||||
return !this->empty();
|
return !this->empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline size_t count()
|
||||||
|
{
|
||||||
|
return _listeners.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
public /* Listeners */:
|
||||||
// Add new listener.
|
// Add new listener.
|
||||||
|
inline void add(std::function<void(_args...)> listener)
|
||||||
|
{
|
||||||
|
if (on_add)
|
||||||
|
on_add(*this, listener);
|
||||||
|
_listeners.push_back(listener);
|
||||||
|
}
|
||||||
inline event<_args...>& operator+=(std::function<void(_args...)> listener)
|
inline event<_args...>& operator+=(std::function<void(_args...)> listener)
|
||||||
{
|
{
|
||||||
this->add(listener);
|
this->add(listener);
|
||||||
@@ -99,21 +88,33 @@ namespace datapath {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Remove existing listener.
|
// Remove existing listener.
|
||||||
|
inline void remove(std::function<void(_args...)> listener)
|
||||||
|
{
|
||||||
|
_listeners.remove(listener);
|
||||||
|
if (on_remove)
|
||||||
|
on_remove(*this, listener);
|
||||||
|
}
|
||||||
inline event<_args...>& operator-=(std::function<void(_args...)> listener)
|
inline event<_args...>& operator-=(std::function<void(_args...)> listener)
|
||||||
{
|
{
|
||||||
this->remove(listener);
|
this->remove(listener);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public /* events */:
|
// Remove all listeners.
|
||||||
void set_listen_callback(std::function<void()> cb)
|
inline void clear()
|
||||||
{
|
{
|
||||||
this->_listen_cb = cb;
|
_listeners.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_silence_callback(std::function<void()> cb)
|
public /* Calling */:
|
||||||
|
// Call Listeners with arguments.
|
||||||
|
template<typename... _largs>
|
||||||
|
inline void operator()(_args... args)
|
||||||
{
|
{
|
||||||
this->_silence_cb = cb;
|
/// Not valid without the extra template.
|
||||||
|
for (auto& l : _listeners) {
|
||||||
|
l(args...);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}; // namespace datapath
|
}; // namespace datapath
|
||||||
|
|||||||
Reference in New Issue
Block a user