Rename start_next() to push

PlaterJob refinements
This commit is contained in:
tamasmeszaros 2021-12-02 15:09:59 +01:00
parent 78118b7b1d
commit 2dc292b505
4 changed files with 39 additions and 22 deletions

View File

@ -129,7 +129,7 @@ void BoostThreadWorker::process_events()
})); }));
} }
bool BoostThreadWorker::start_next(std::unique_ptr<Job> job) bool BoostThreadWorker::push(std::unique_ptr<Job> job)
{ {
if (job) if (job)
m_input_queue.push(JobEntry{std::move(job)}); m_input_queue.push(JobEntry{std::move(job)});

View File

@ -104,7 +104,7 @@ public:
BoostThreadWorker &operator=(const BoostThreadWorker &) = delete; BoostThreadWorker &operator=(const BoostThreadWorker &) = delete;
BoostThreadWorker &operator=(BoostThreadWorker &&) = delete; BoostThreadWorker &operator=(BoostThreadWorker &&) = delete;
bool start_next(std::unique_ptr<Job> job) override; bool push(std::unique_ptr<Job> job) override;
bool is_idle() const override bool is_idle() const override
{ {

View File

@ -1,6 +1,9 @@
#ifndef PLATERWORKER_HPP #ifndef PLATERWORKER_HPP
#define PLATERWORKER_HPP #define PLATERWORKER_HPP
#include <map>
#include "Worker.hpp"
#include "BusyCursorJob.hpp" #include "BusyCursorJob.hpp"
#include "slic3r/GUI/GUI.hpp" #include "slic3r/GUI/GUI.hpp"
@ -16,9 +19,21 @@ class Plater;
template<class WorkerSubclass> template<class WorkerSubclass>
class PlaterWorker: public Worker { class PlaterWorker: public Worker {
WorkerSubclass m_w; WorkerSubclass m_w;
Plater *m_plater;
class PlaterJob : public Job { struct JobHolder : Job {
std::unique_ptr<Job> m_job; std::unique_ptr<Job> m_job;
void process(Ctl &ctl) override { m_job->process(ctl); };
void finalize(bool canceled, std::exception_ptr &e) override
{
m_job->finalize(canceled, e);
}
JobHolder(std::unique_ptr<Job> &&j) : m_job{std::move(j)} {}
};
template<class JobSubclass>
class PlaterJob : public Job {
JobSubclass m_job;
Plater *m_plater; Plater *m_plater;
public: public:
@ -49,12 +64,12 @@ class PlaterWorker: public Worker {
} wctl{c}; } wctl{c};
CursorSetterRAII busycursor{wctl}; CursorSetterRAII busycursor{wctl};
m_job->process(wctl); m_job.process(wctl);
} }
void finalize(bool canceled, std::exception_ptr &eptr) override void finalize(bool canceled, std::exception_ptr &eptr) override
{ {
m_job->finalize(canceled, eptr); m_job.finalize(canceled, eptr);
if (eptr) try { if (eptr) try {
std::rethrow_exception(eptr); std::rethrow_exception(eptr);
@ -64,10 +79,12 @@ class PlaterWorker: public Worker {
} }
} }
PlaterJob(std::unique_ptr<Job> j) template<class...Args>
: m_job{std::move(j)}, m_plater{wxGetApp().plater()} PlaterJob(Plater *p, Args&&...args)
: m_job{std::forward<Args>(args)...}, m_plater{p}
{ {
// TODO: decide if disabling slice button during UI job is what we want. // TODO: decide if disabling slice button during UI job is what we
// want.
// if (m_plater) // if (m_plater)
// m_plater->sidebar().enable_buttons(false); // m_plater->sidebar().enable_buttons(false);
} }
@ -84,29 +101,29 @@ class PlaterWorker: public Worker {
} }
}; };
public: public:
template<class... WorkerArgs> template<class... WorkerArgs>
PlaterWorker(Plater *plater, WorkerArgs &&...args) PlaterWorker(Plater *plater, WorkerArgs &&...args)
: m_w{std::forward<WorkerArgs>(args)...} : m_w{std::forward<WorkerArgs>(args)...}, m_plater{plater}
{ {
// Ensure that messages from the worker thread to the UI thread are // Ensure that messages from the worker thread to the UI thread are
// processed continuously. // processed continuously.
plater->Bind(wxEVT_IDLE, [this](wxIdleEvent &) { plater->Bind(wxEVT_IDLE, [this](wxIdleEvent &) {
m_w.process_events(); process_events();
}); });
} }
// Always package the job argument into a PlaterJob // Always package the job argument into a PlaterJob
bool start_next(std::unique_ptr<Job> job) override bool push(std::unique_ptr<Job> job) override
{ {
return m_w.start_next(std::make_unique<PlaterJob>(std::move(job))); return m_w.push(std::make_unique<PlaterJob<JobHolder>>(m_plater, std::move(job)));
} }
bool is_idle() const override { return m_w.is_idle(); } bool is_idle() const override { return m_w.is_idle(); }
void cancel() override { m_w.cancel(); } void cancel() override { m_w.cancel(); }
void cancel_all() override { m_w.cancel_all(); } void cancel_all() override { m_w.cancel_all(); }
void process_events() override {} void process_events() override { m_w.process_events(); }
}; };
}} // namespace Slic3r::GUI }} // namespace Slic3r::GUI

View File

@ -14,7 +14,7 @@ class Worker {
public: public:
// Queue up a new job after the current one. This call does not block. // Queue up a new job after the current one. This call does not block.
// Returns false if the job gets discarded. // Returns false if the job gets discarded.
virtual bool start_next(std::unique_ptr<Job> job) = 0; virtual bool push(std::unique_ptr<Job> job) = 0;
// Returns true if no job is running and no job message is left to be processed. // Returns true if no job is running and no job message is left to be processed.
// This means that nothing is left to finalize or take care of in the main thread. // This means that nothing is left to finalize or take care of in the main thread.
@ -63,7 +63,7 @@ bool queue_job(Worker &w, ProcessFn fn, FinishFn finishfn)
}; };
auto j = std::make_unique<LambdaJob>(std::move(fn), std::move(finishfn)); auto j = std::make_unique<LambdaJob>(std::move(fn), std::move(finishfn));
return w.start_next(std::move(j)); return w.push(std::move(j));
} }
template<class ProcessFn, class = std::enable_if_t<IsProcessFn<ProcessFn>>> template<class ProcessFn, class = std::enable_if_t<IsProcessFn<ProcessFn>>>
@ -74,7 +74,7 @@ bool queue_job(Worker &w, ProcessFn fn)
inline bool queue_job(Worker &w, std::unique_ptr<Job> j) inline bool queue_job(Worker &w, std::unique_ptr<Job> j)
{ {
return w.start_next(std::move(j)); return w.push(std::move(j));
} }
// Replace the current job queue with a new job. The signature is the same // Replace the current job queue with a new job. The signature is the same