Skip to content

Commit

Permalink
impr: Allow tasks to be created without getting the task handle as pa…
Browse files Browse the repository at this point in the history
…rameter
  • Loading branch information
WerWolv committed Aug 3, 2024
1 parent 5dfd8c8 commit e9fb022
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/libimhex/include/hex/api/task_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ namespace hex {
*/
static TaskHolder createTask(const UnlocalizedString &unlocalizedName, u64 maxValue, std::function<void(Task &)> function);

/**
* @brief Creates a new asynchronous task that gets displayed in the Task Manager in the footer
* @param unlocalizedName Name of the task
* @param maxValue Maximum value of the task
* @param function Function to be executed
* @return A TaskHolder holding a weak reference to the task
*/
static TaskHolder createTask(const UnlocalizedString &unlocalizedName, u64 maxValue, std::function<void()> function);

/**
* @brief Creates a new asynchronous task that does not get displayed in the Task Manager
* @param unlocalizedName Name of the task
Expand All @@ -145,6 +154,14 @@ namespace hex {
*/
static TaskHolder createBackgroundTask(const UnlocalizedString &unlocalizedName, std::function<void(Task &)> function);

/**
* @brief Creates a new asynchronous task that does not get displayed in the Task Manager
* @param unlocalizedName Name of the task
* @param function Function to be executed
* @return A TaskHolder holding a weak reference to the task
*/
static TaskHolder createBackgroundTask(const UnlocalizedString &unlocalizedName, std::function<void()> function);

/**
* @brief Creates a new synchronous task that will execute the given function at the start of the next frame
* @param function Function to be executed
Expand Down
18 changes: 18 additions & 0 deletions lib/libimhex/source/api/task_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,29 @@ namespace hex {
return createTask(std::move(unlocalizedName), maxValue, false, std::move(function));
}

TaskHolder TaskManager::createTask(const UnlocalizedString &unlocalizedName, u64 maxValue, std::function<void()> function) {
log::debug("Creating task {}", unlocalizedName.get());
return createTask(std::move(unlocalizedName), maxValue, false,
[function = std::move(function)](Task&) {
function();
}
);
}

TaskHolder TaskManager::createBackgroundTask(const UnlocalizedString &unlocalizedName, std::function<void(Task &)> function) {
log::debug("Creating background task {}", unlocalizedName.get());
return createTask(std::move(unlocalizedName), 0, true, std::move(function));
}

TaskHolder TaskManager::createBackgroundTask(const UnlocalizedString &unlocalizedName, std::function<void()> function) {
log::debug("Creating background task {}", unlocalizedName.get());
return createTask(std::move(unlocalizedName), 0, true,
[function = std::move(function)](Task&) {
function();
}
);
}

void TaskManager::collectGarbage() {
{
std::scoped_lock lock(s_queueMutex);
Expand Down

0 comments on commit e9fb022

Please sign in to comment.