summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNao Pross <naopross@thearcway.org>2019-01-22 01:52:21 +0100
committerNao Pross <naopross@thearcway.org>2019-01-22 02:31:45 +0100
commitc21f73c4501d4bb488a97b360003b7bbc4e24619 (patch)
tree745dc0890c334b2a160bda0b8b1fa917140013ce
parentDelete flatevolvable.h and flatevolvable.cpp (diff)
downloadflatland-c21f73c4501d4bb488a97b360003b7bbc4e24619.tar.gz
flatland-c21f73c4501d4bb488a97b360003b7bbc4e24619.zip
Re-implement flattask.h in task.hpp
Note: On this commit NOTHING compiles.
-rw-r--r--engine/flattask.cpp53
-rw-r--r--engine/include/flattask.h69
-rw-r--r--engine/include/task.hpp38
-rw-r--r--engine/task.cpp23
4 files changed, 61 insertions, 122 deletions
diff --git a/engine/flattask.cpp b/engine/flattask.cpp
deleted file mode 100644
index 02e584e..0000000
--- a/engine/flattask.cpp
+++ /dev/null
@@ -1,53 +0,0 @@
-#include "flattask.h"
-
-bool task_prior::operator()(task_s* s, task_s* g) const
-{
- return s->getPriority() <= g->getPriority();
-}
-
-/* Static variable definition */
-task_set task_s::pre_process_tasks;
-task_set task_s::post_process_tasks;
-
-task_s::task_s(bool pre_process, Uint8 priority)
-
- : pre_process(pre_process), priority(priority)
-{
- /* Push into the public callback list */
- if (pre_process)
- pre_process_tasks.insert(this);
- else
- post_process_tasks.insert(this);
-}
-
-task_s::~task_s()
-{
- /* Remove from the public callback list */
- if (pre_process)
- pre_process_tasks.erase(this);
- else
- post_process_tasks.erase(this);
-}
-
-Uint8 task_s::getPriority() const
-{
- return priority;
-}
-
-void task_s::setPriority(Uint8 priority)
-{
- this->priority = priority;
-}
-
-void task_s::executePreProcess()
-{
- for (task_s * task : task_s::pre_process_tasks)
- task->exec();
-}
-
-void task_s::executePostProcess()
-{
- for (task_s * task : task_s::post_process_tasks)
- task->exec();
-}
-
diff --git a/engine/include/flattask.h b/engine/include/flattask.h
deleted file mode 100644
index 3f5c475..0000000
--- a/engine/include/flattask.h
+++ /dev/null
@@ -1,69 +0,0 @@
-#ifndef __FLAT_TASK_H__
-#define __FLAT_TASK_H__
-
-#include <set>
-#include "types.h"
-
-struct task_prior
-{
- bool operator()(task_s* s, task_s* g) const;
-};
-
-typedef std::set<task_s*, task_prior> task_set;
-
-class task_s
-{
- /* Call priority of a task */
- /* 0 = maximum priority */
- /* 5 = default priority */
- Uint8 priority;
- bool pre_process;
-
- /* Callback list */
- static task_set pre_process_tasks;
- static task_set post_process_tasks;
-
-public:
-
- task_s(bool pre_process = false, Uint8 priority = 5);
-
- virtual ~task_s();
-
- virtual void exec() = 0;
-
- Uint8 getPriority() const;
-
- void setPriority(Uint8 priority);
-
- /* Execute tasks */
- static void executePreProcess();
- static void executePostProcess();
-};
-
-template<class T, typename arg_t = void*>
-class FlatTask : public task_s
-{
- typedef void (T::*callback_t)(arg_t);
-
- T * object;
- callback_t callback;
-
- arg_t args;
-
-public:
-
- FlatTask( T *object,
- callback_t callback,
- arg_t args,
- bool pre_process = false,
- Uint8 priority = 5)
-
- : task_s(pre_process, priority), object(object), callback(callback), args(args) {}
-
- virtual void exec() override
- {
- (object->*callback)(args);
- }
-};
-
-#endif
diff --git a/engine/include/task.hpp b/engine/include/task.hpp
new file mode 100644
index 0000000..8c4f48f
--- /dev/null
+++ b/engine/include/task.hpp
@@ -0,0 +1,38 @@
+#pragma once
+
+#include <functional>
+#include <set>
+
+namespace flat {
+ namespace core {
+ class task {
+ public:
+ enum class priority : unsigned {
+ max = 0,
+ high = 1,
+ none = 2,
+ low = 3,
+ min = 4,
+ };
+
+ // to pass a member function (method) use std::bind(f, obj)
+ task(std::function<void()> callback, priority p = priority::none);
+ task() = delete;
+
+ inline void operator()() const { m_callback(); }
+ friend bool operator<(const task& lhs, const task& rhs);
+
+ private:
+ const priority m_priority;
+ std::function<void()> m_callback;
+ };
+
+ struct job : public std::set<task> {
+ inline void add_task(task t) {
+ insert(begin(), t);
+ }
+
+ void invoke_tasks();
+ };
+ }
+} \ No newline at end of file
diff --git a/engine/task.cpp b/engine/task.cpp
new file mode 100644
index 0000000..537a6b1
--- /dev/null
+++ b/engine/task.cpp
@@ -0,0 +1,23 @@
+#include "task.hpp"
+
+#include <functional>
+#include <set>
+
+
+namespace flat {
+ namespace core {
+ bool operator<(const task& lhs, const task& rhs) {
+ // task::priority::max is 0, a higher number is lower priority
+ return lhs.m_priority < rhs.m_priority;
+ }
+
+ task::task(std::function<void()> callback, task::priority p)
+ : m_priority(p), m_callback(callback) {}
+
+ void job::invoke_tasks() {
+ for (const task& t : *this) {
+ t();
+ }
+ }
+ }
+} \ No newline at end of file