Skip to content

Commit

Permalink
libdoomsday|Busy Mode: Busy tasks support C++ lambda functions
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Jul 22, 2015
1 parent 09a5e46 commit 853dff4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
15 changes: 14 additions & 1 deletion doomsday/apps/libdoomsday/include/doomsday/busymode.h
Expand Up @@ -24,14 +24,16 @@
#include "libdoomsday.h"
#include <de/Observers>
#include <de/Time>
#include <de/String>
#include <functional>

/// Busy mode worker function.
typedef int (*busyworkerfunc_t) (void *parm);

/// POD structure for defining a task processable in busy mode.
struct LIBDOOMSDAY_PUBLIC BusyTask
{
busyworkerfunc_t worker; ///< Worker thread that does processing while in busy mode.
std::function<int (void *)> worker; ///< Worker thread that does processing while in busy mode.
void *workerData; ///< Data context for the worker thread.

int mode; ///< Busy mode flags @ref busyModeFlags
Expand Down Expand Up @@ -123,6 +125,17 @@ class LIBDOOMSDAY_PUBLIC BusyMode
*/
int runNewTaskWithName(int mode, busyworkerfunc_t worker, void *workerData, de::String const &taskName);

/**
* Run a single task with a std::function callback.
*
* @param mode Busy mode flags.
* @param worker Worker callback.
* @param taskName Task name (drawn next to the progress bar).
*
* @return Return value from the worker.
*/
int runNewTaskWithName(int mode, de::String const &taskName, std::function<int (void *)> worker);

/**
* Abnormally aborts the currently running task. Call this when the task encounters
* an unrecoverable error. Calling this causes the Abort audience to be notified.
Expand Down
10 changes: 9 additions & 1 deletion doomsday/apps/libdoomsday/src/busymode.cpp
Expand Up @@ -149,7 +149,7 @@ void BusyMode::abort(String const &message)
* @param workerData Data context for the worker thread.
* @param taskName Optional task name (drawn with the progress bar).
*/
static BusyTask *newTask(int mode, busyworkerfunc_t worker, void* workerData,
static BusyTask *newTask(int mode, std::function<int (void *)> worker, void* workerData,
String const &taskName)
{
BusyTask *task = new BusyTask;
Expand All @@ -176,6 +176,14 @@ static void deleteTask(BusyTask *task)
delete task;
}

int BusyMode::runNewTaskWithName(int mode, String const &taskName, std::function<int (void *)> worker)
{
BusyTask *task = newTask(mode, worker, nullptr, taskName);
int result = runTask(task);
deleteTask(task);
return result;
}

int BusyMode::runNewTaskWithName(int mode, busyworkerfunc_t worker, void *workerData,
String const &taskName)
{
Expand Down

0 comments on commit 853dff4

Please sign in to comment.