summaryrefslogtreecommitdiffstats
path: root/engine/include/core
diff options
context:
space:
mode:
authorancarola <raffaele.ancarola@epfl.ch>2019-01-23 15:07:33 +0100
committerancarola <raffaele.ancarola@epfl.ch>2019-01-23 15:07:33 +0100
commit8443d3d03573066ab884dae0f7bc8dbbabf106b5 (patch)
treea81f054b62ec9a5c18ca9a8275a94416512c9fa1 /engine/include/core
parentsignal opt (diff)
parentMake job hold RAII pointers that can expire, update test (diff)
downloadflatland-8443d3d03573066ab884dae0f7bc8dbbabf106b5.tar.gz
flatland-8443d3d03573066ab884dae0f7bc8dbbabf106b5.zip
Merge remote-tracking branch 'nao/raii-task'
Ci siamo
Diffstat (limited to 'engine/include/core')
-rw-r--r--engine/include/core/priority.hpp17
-rw-r--r--engine/include/core/task.hpp19
2 files changed, 26 insertions, 10 deletions
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 <memory>
#include <set>
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<prioritized> lhs, const std::weak_ptr<prioritized>& 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 <functional>
-#include <set>
+#include <memory>
namespace flat {
namespace core {
+ // forward decl
+ class job;
+ class task;
+
class task : public prioritized {
public:
+ using callback = std::function<void(void)>;
+
task() = delete;
- task(std::function<void()> 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<void()> m_callback;
+ callback m_callback;
};
- struct job : public queue<task> {
- inline auto add_task(task t) {
- this->insert(t);
- }
-
+ struct job : private queue<std::weak_ptr<task>> {
+ std::shared_ptr<task> make_task(task::callback f, priority_t p = priority_t::none);
void invoke_tasks();
};
}