Add CancellableJob for cancelling only a specific job instance

This commit is contained in:
tamasmeszaros 2023-05-31 12:12:21 +02:00
parent 0e452dcf74
commit 4d272b09b1
2 changed files with 61 additions and 0 deletions

View File

@ -192,6 +192,7 @@ set(SLIC3R_GUI_SOURCES
GUI/Jobs/BoostThreadWorker.cpp
GUI/Jobs/UIThreadWorker.hpp
GUI/Jobs/BusyCursorJob.hpp
GUI/Jobs/CancellableJob.hpp
GUI/Jobs/PlaterWorker.hpp
GUI/Jobs/ArrangeJob.hpp
GUI/Jobs/ArrangeJob.cpp

View File

@ -0,0 +1,60 @@
#ifndef CANCELLABLEJOB_HPP
#define CANCELLABLEJOB_HPP
#include <atomic>
#include "Job.hpp"
namespace Slic3r { namespace GUI {
template<class JobSubclass>
class CancellableJob: public Job {
JobSubclass m_job;
std::atomic_bool &m_flag;
public:
template<class... Args>
CancellableJob(std::atomic_bool &flag, Args &&...args)
: m_job{std::forward<Args>(args)...}, m_flag{flag}
{}
void process(Ctl &ctl) override
{
m_flag.store(false);
struct CancelCtl : public Ctl
{
Ctl &basectl;
std::atomic_bool &cflag;
CancelCtl (Ctl &c, std::atomic_bool &f): basectl{c}, cflag{f} {}
void update_status(int st, const std::string &msg = "") override
{
basectl.update_status(st, msg);
}
bool was_canceled() const override
{
return cflag.load() || basectl.was_canceled();
}
std::future<void> call_on_main_thread(
std::function<void()> fn) override
{
return basectl.call_on_main_thread(fn);
}
} cctl(ctl, m_flag);
m_job.process(cctl);
}
void finalize(bool canceled, std::exception_ptr &eptr) override
{
m_job.finalize(m_flag.load() || canceled, eptr);
}
};
}} // namespace Slic3r::GUI
#endif // CANCELLABLEJOB_HPP