2021-12-16 23:48:52 +01:00
|
|
|
#include <hex/api/task.hpp>
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
#include <hex/api/localization.hpp>
|
2021-12-16 23:48:52 +01:00
|
|
|
|
2022-07-29 18:49:43 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
2021-12-16 23:48:52 +01:00
|
|
|
namespace hex {
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
std::list<Task *> Task::s_runningTasks;
|
|
|
|
std::mutex Task::s_taskMutex;
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
Task::Task(const std::string &unlocalizedName, u64 maxValue) : m_name(LangEntry(unlocalizedName)), m_maxValue(maxValue), m_currValue(0) {
|
2022-02-01 18:09:40 +01:00
|
|
|
std::scoped_lock lock(Task::s_taskMutex);
|
|
|
|
|
|
|
|
Task::s_runningTasks.push_back(this);
|
2021-12-16 23:48:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Task::~Task() {
|
|
|
|
this->finish();
|
|
|
|
}
|
|
|
|
|
2022-07-29 18:49:43 +02:00
|
|
|
Task::Task(hex::Task &&other) noexcept {
|
|
|
|
std::scoped_lock lock(Task::s_taskMutex);
|
|
|
|
|
|
|
|
this->m_name = other.m_name;
|
|
|
|
this->m_maxValue = other.m_maxValue;
|
|
|
|
this->m_currValue = other.m_currValue;
|
|
|
|
|
|
|
|
auto it = std::find(Task::s_runningTasks.begin(), Task::s_runningTasks.end(), &other);
|
|
|
|
if (it != Task::s_runningTasks.end()) {
|
|
|
|
*it = this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-16 23:48:52 +01:00
|
|
|
void Task::finish() {
|
2022-02-01 18:09:40 +01:00
|
|
|
std::scoped_lock lock(Task::s_taskMutex);
|
|
|
|
|
|
|
|
Task::s_runningTasks.remove(this);
|
2021-12-16 23:48:52 +01:00
|
|
|
}
|
|
|
|
|
2022-01-09 21:27:59 +01:00
|
|
|
void Task::setMaxValue(u64 maxValue) {
|
|
|
|
this->m_maxValue = maxValue;
|
|
|
|
}
|
|
|
|
|
2021-12-16 23:48:52 +01:00
|
|
|
void Task::update(u64 currValue) {
|
|
|
|
if (this->m_currValue < this->m_maxValue)
|
|
|
|
this->m_currValue = currValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
double Task::getProgress() const {
|
2022-01-09 21:27:59 +01:00
|
|
|
if (this->m_maxValue == 0)
|
|
|
|
return 100;
|
|
|
|
|
2021-12-16 23:48:52 +01:00
|
|
|
return static_cast<double>(this->m_currValue) / static_cast<double>(this->m_maxValue);
|
|
|
|
}
|
|
|
|
|
2022-01-09 21:27:59 +01:00
|
|
|
bool Task::isPending() const {
|
|
|
|
return this->m_maxValue == 0;
|
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
const std::string &Task::getName() const {
|
2021-12-16 23:48:52 +01:00
|
|
|
return this->m_name;
|
|
|
|
}
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
size_t Task::getRunningTaskCount() {
|
|
|
|
std::scoped_lock lock(Task::s_taskMutex);
|
|
|
|
|
|
|
|
return Task::s_runningTasks.size();
|
|
|
|
}
|
|
|
|
|
2021-12-16 23:48:52 +01:00
|
|
|
}
|