summaryrefslogtreecommitdiffstats
path: root/engine/include/core/task.hpp
blob: 0840e082643dfaf3a57c2469d30a0444b0eb7d3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#pragma once

#include "priority.hpp"

#include <unordered_set>
#include <functional>
#include <variant>
#include <memory>

namespace flat {
    namespace core {
        // forward decl
        class job;
        class task;

        class task : public prioritized {
        public:
            using ptr = std::shared_ptr<task>;
            using callback = std::function<void(void)>;

            task() = delete;
            task(callback f, priority_t p = priority_t::none);

            inline void operator()() const { m_callback(); }

        private:
            callback m_callback;
        };

        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 = priority_t::none);

            /// 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(); } 
        };
    }
}