From 3c9184a2e3eb3583dfd2d6ff1ed60903aac9ba78 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Wed, 23 Jan 2019 15:06:02 +0100 Subject: Make job hold RAII pointers that can expire, update test Note: does not work, idk why --- engine/include/core/priority.hpp | 17 +++++++++++++++-- engine/include/core/task.hpp | 19 +++++++++++-------- 2 files changed, 26 insertions(+), 10 deletions(-) (limited to 'engine/include/core') diff --git a/engine/include/core/priority.hpp b/engine/include/core/priority.hpp index 773b2f7..1219d26 100644 --- a/engine/include/core/priority.hpp +++ b/engine/include/core/priority.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include namespace flat { @@ -32,8 +33,20 @@ namespace flat { return lhs.priority() < rhs.priority(); } - bool operator()(const prioritized * const& lhs, const prioritized * const& rhs) { - return lhs->priority() < rhs->priority(); + bool operator()(const std::weak_ptr lhs, const std::weak_ptr& rhs) { + if (auto l = lhs.lock()) { + if (auto r = rhs.lock()) { + // if both valid, check their priority + // in case they are the same, left is prioritized + return l->priority() < r->priority(); + } else { + // if right is expired, left is prioritized + return true; + } + } else { + // if left is expired, the right is prioritized + return false; + } } }; diff --git a/engine/include/core/task.hpp b/engine/include/core/task.hpp index 90e3d97..d886fcd 100644 --- a/engine/include/core/task.hpp +++ b/engine/include/core/task.hpp @@ -3,26 +3,29 @@ #include "priority.hpp" #include -#include +#include namespace flat { namespace core { + // forward decl + class job; + class task; + class task : public prioritized { public: + using callback = std::function; + task() = delete; - task(std::function callback, priority_t p = priority_t::none); + task(callback f, priority_t p = priority_t::none); inline void operator()() const { m_callback(); } private: - std::function m_callback; + callback m_callback; }; - struct job : public queue { - inline auto add_task(task t) { - this->insert(t); - } - + struct job : private queue> { + std::shared_ptr make_task(task::callback f, priority_t p = priority_t::none); void invoke_tasks(); }; } -- cgit v1.2.1