2016-12-05 14:41:00 -05:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "common.hpp"
|
|
|
|
#include "events/signal_receiver.hpp"
|
|
|
|
|
|
|
|
POLYBAR_NS
|
|
|
|
|
|
|
|
/**
|
2018-11-04 10:08:54 -05:00
|
|
|
* \brief Holds all signal receivers attached to the emitter
|
2016-12-05 14:41:00 -05:00
|
|
|
*/
|
|
|
|
extern signal_receivers_t g_signal_receivers;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper used to delegate emitted signals
|
|
|
|
* to attached signal receivers
|
|
|
|
*/
|
|
|
|
class signal_emitter {
|
|
|
|
public:
|
2016-12-09 03:40:46 -05:00
|
|
|
using make_type = signal_emitter&;
|
|
|
|
static make_type make();
|
2016-12-09 03:02:47 -05:00
|
|
|
|
2016-12-05 14:41:00 -05:00
|
|
|
explicit signal_emitter() = default;
|
|
|
|
virtual ~signal_emitter() {}
|
|
|
|
|
|
|
|
template <typename Signal>
|
|
|
|
bool emit(const Signal& sig) {
|
|
|
|
try {
|
|
|
|
for (auto&& item : g_signal_receivers.at(id<Signal>())) {
|
|
|
|
if (item.second->on(sig)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (...) {
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Signal, typename Next, typename... Signals>
|
|
|
|
bool emit(const Signal& sig) const {
|
|
|
|
return emit<Signal>(sig) || emit<Next, Signals...>(sig);
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:11:28 -05:00
|
|
|
template <int Priority, typename Signal, typename... Signals>
|
2016-12-05 14:41:00 -05:00
|
|
|
void attach(signal_receiver<Priority, Signal, Signals...>* s) {
|
|
|
|
attach<signal_receiver<Priority, Signal, Signals...>, Signal, Signals...>(s);
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:11:28 -05:00
|
|
|
template <int Priority, typename Signal, typename... Signals>
|
2016-12-05 14:41:00 -05:00
|
|
|
void detach(signal_receiver<Priority, Signal, Signals...>* s) {
|
|
|
|
detach<signal_receiver<Priority, Signal, Signals...>, Signal, Signals...>(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
template <typename Signal>
|
2017-01-01 14:29:38 -05:00
|
|
|
std::type_index id() const {
|
|
|
|
return typeid(Signal);
|
2016-12-05 14:41:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Receiver, typename Signal>
|
|
|
|
void attach(Receiver* s) {
|
|
|
|
attach(s, id<Signal>());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Receiver, typename Signal, typename Next, typename... Signals>
|
|
|
|
void attach(Receiver* s) {
|
|
|
|
attach(s, id<Signal>());
|
|
|
|
attach<Receiver, Next, Signals...>(s);
|
|
|
|
}
|
|
|
|
|
2017-01-01 14:29:38 -05:00
|
|
|
void attach(signal_receiver_interface* s, std::type_index id) {
|
2016-12-05 14:41:00 -05:00
|
|
|
g_signal_receivers[id].emplace(s->priority(), s);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Receiver, typename Signal>
|
|
|
|
void detach(Receiver* s) {
|
|
|
|
detach(s, id<Signal>());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Receiver, typename Signal, typename Next, typename... Signals>
|
|
|
|
void detach(Receiver* s) {
|
|
|
|
detach(s, id<Signal>());
|
|
|
|
detach<Receiver, Next, Signals...>(s);
|
|
|
|
}
|
|
|
|
|
2017-01-01 14:29:38 -05:00
|
|
|
void detach(signal_receiver_interface* d, std::type_index id) {
|
2016-12-05 14:41:00 -05:00
|
|
|
try {
|
|
|
|
auto& prio_map = g_signal_receivers.at(id);
|
|
|
|
const auto& prio_sink_pair = prio_map.equal_range(d->priority());
|
|
|
|
|
|
|
|
for (auto it = prio_sink_pair.first; it != prio_sink_pair.second;) {
|
|
|
|
if (d == it->second) {
|
|
|
|
it = prio_map.erase(it);
|
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (...) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
POLYBAR_NS_END
|