mirror of
https://github.com/rad4day/Waybar.git
synced 2023-12-21 10:22:59 +01:00
fix(util): avoid creating temporary functor for each event
This commit is contained in:
parent
285a264aae
commit
8a0e76c8d8
@ -8,6 +8,7 @@
|
|||||||
#include <queue>
|
#include <queue>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
namespace waybar {
|
namespace waybar {
|
||||||
|
|
||||||
@ -36,24 +37,26 @@ struct SafeSignal : sigc::signal<void(std::decay_t<Args>...)> {
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
using signal_t = sigc::signal<void(std::decay_t<Args>...)>;
|
using signal_t = sigc::signal<void(std::decay_t<Args>...)>;
|
||||||
|
using slot_t = decltype(std::declval<signal_t>().make_slot());
|
||||||
using arg_tuple_t = std::tuple<std::decay_t<Args>...>;
|
using arg_tuple_t = std::tuple<std::decay_t<Args>...>;
|
||||||
// ensure that unwrapped methods are not accessible
|
// ensure that unwrapped methods are not accessible
|
||||||
using signal_t::emit_reverse;
|
using signal_t::emit_reverse;
|
||||||
using signal_t::make_slot;
|
using signal_t::make_slot;
|
||||||
|
|
||||||
void handle_event() {
|
void handle_event() {
|
||||||
auto fn = signal_t::make_slot();
|
|
||||||
for (std::unique_lock lock(mutex_); !queue_.empty(); lock.lock()) {
|
for (std::unique_lock lock(mutex_); !queue_.empty(); lock.lock()) {
|
||||||
auto args = queue_.front();
|
auto args = queue_.front();
|
||||||
queue_.pop();
|
queue_.pop();
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
std::apply(fn, args);
|
std::apply(cached_fn_, args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Glib::Dispatcher dp_;
|
Glib::Dispatcher dp_;
|
||||||
std::mutex mutex_;
|
std::mutex mutex_;
|
||||||
std::queue<arg_tuple_t> queue_;
|
std::queue<arg_tuple_t> queue_;
|
||||||
|
// cache functor for signal emission to avoid recreating it on each event
|
||||||
|
const slot_t cached_fn_ = make_slot();
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace waybar
|
} // namespace waybar
|
||||||
|
Loading…
Reference in New Issue
Block a user