diff options
Diffstat (limited to 'engine/task.cpp')
-rw-r--r-- | engine/task.cpp | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/engine/task.cpp b/engine/task.cpp index 760b1b9..f2b6058 100644 --- a/engine/task.cpp +++ b/engine/task.cpp @@ -1,17 +1,31 @@ #include "core/task.hpp" #include <functional> +#include <memory> namespace flat { namespace core { - task::task(std::function<void()> callback, priority_t p) - : m_callback(callback) {} + task::task(task::callback f, priority_t p) + : m_callback(f) {} + + std::shared_ptr<task> job::make_task(task::callback f, priority_t p) { + auto shared = std::make_shared<task>(f, p); + insert(shared); + return shared; + } void job::invoke_tasks() { - for (const task& t : *this) { - t(); - } + std::for_each(begin(), end(), [&](auto tp) { + // check that the task has not expired + if (auto t = tp.lock()) { + // run task + std::invoke(*t); + } else { + // delete task + erase(tp); + } + }); } } } |