diff options
Diffstat (limited to '')
-rw-r--r-- | engine/include/core/task.hpp | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/engine/include/core/task.hpp b/engine/include/core/task.hpp index d886fcd..77e1da6 100644 --- a/engine/include/core/task.hpp +++ b/engine/include/core/task.hpp @@ -2,7 +2,9 @@ #include "priority.hpp" +#include <unordered_set> #include <functional> +#include <variant> #include <memory> namespace flat { @@ -13,6 +15,7 @@ namespace flat { class task : public prioritized { public: + using ptr = std::shared_ptr<task>; using callback = std::function<void(void)>; task() = delete; @@ -24,9 +27,24 @@ namespace flat { callback m_callback; }; - struct job : private queue<std::weak_ptr<task>> { - std::shared_ptr<task> make_task(task::callback f, priority_t p = priority_t::none); + class job : protected queue<std::weak_ptr<task>> { + public: + /// add a task function owned by the job object + void add_task(task::callback f, priority_t p); + + /// add a task function not owned by the job object (weak_ptr) + std::shared_ptr<task> delegate_task(task::callback f, priority_t p = priority_t::none); + + /// add a task methods not owned by the job object (weak_ptr), + /// this allows to make the task die when the owner object goes out of scope + template<typename R, typename T> + inline std::shared_ptr<task> delegate_task(R T::*mf, T& obj, priority_t p = priority_t::none) { + return delegate_task(std::bind(mf, obj), p); + } + + /// run tasks void invoke_tasks(); + inline void operator()() { invoke_tasks(); } }; } } |