mirror of
https://github.com/transmission/transmission.git
synced 2026-02-14 23:19:34 +00:00
refactor: simplify session-thread.h with std::bind_front (#8262)
* chore: housekeeping * refactor: simplify `session-thread.h` with `std::bind_front`
This commit is contained in:
@@ -200,7 +200,7 @@ public:
|
||||
return thread_id_ == std::this_thread::get_id();
|
||||
}
|
||||
|
||||
void queue(std::function<void(void)>&& func) override
|
||||
void queue(callback_t&& func) override
|
||||
{
|
||||
work_queue_mutex_.lock();
|
||||
work_queue_.emplace_back(std::move(func));
|
||||
@@ -209,7 +209,7 @@ public:
|
||||
event_active(work_queue_event_.get(), 0, {});
|
||||
}
|
||||
|
||||
void run(std::function<void(void)>&& func) override
|
||||
void run(callback_t&& func) override
|
||||
{
|
||||
if (am_in_session_thread())
|
||||
{
|
||||
@@ -222,8 +222,7 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
using callback = std::function<void(void)>;
|
||||
using work_queue_t = std::list<callback>;
|
||||
using work_queue_t = std::list<callback_t>;
|
||||
|
||||
void session_thread_func(struct event_base* evbase)
|
||||
{
|
||||
|
||||
@@ -17,6 +17,9 @@ struct event_base;
|
||||
|
||||
class tr_session_thread
|
||||
{
|
||||
protected:
|
||||
using callback_t = std::function<void()>;
|
||||
|
||||
public:
|
||||
static void tr_evthread_init();
|
||||
|
||||
@@ -27,28 +30,19 @@ public:
|
||||
|
||||
[[nodiscard]] virtual bool am_in_session_thread() const noexcept = 0;
|
||||
|
||||
virtual void queue(std::function<void(void)>&& func) = 0;
|
||||
virtual void queue(callback_t&& func) = 0;
|
||||
|
||||
virtual void run(std::function<void(void)>&& func) = 0;
|
||||
virtual void run(callback_t&& func) = 0;
|
||||
|
||||
template<typename Func, typename... Args>
|
||||
void queue(Func&& func, Args&&... args)
|
||||
{
|
||||
// TODO(tearfur): Use C++20 P0780R2, GCC 9, clang 9
|
||||
queue(
|
||||
std::function<void(void)>{
|
||||
[func = std::forward<Func>(func), args = std::make_tuple(std::forward<Args>(args)...)]()
|
||||
{ std::apply(std::move(func), std::move(args)); },
|
||||
});
|
||||
queue(callback_t{ std::bind_front(std::forward<Func>(func), std::forward<Args>(args)...) });
|
||||
}
|
||||
|
||||
template<typename Func, typename... Args>
|
||||
void run(Func&& func, Args&&... args)
|
||||
{
|
||||
// TODO(tearfur): Use C++20 P0780R2, GCC 9, clang 9
|
||||
run(std::function<void(void)>{
|
||||
[func = std::forward<Func>(func), args = std::make_tuple(std::forward<Args>(args)...)]()
|
||||
{ std::apply(std::move(func), std::move(args)); },
|
||||
});
|
||||
run(callback_t{ std::bind_front(std::forward<Func>(func), std::forward<Args>(args)...) });
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user