summaryrefslogtreecommitdiffstats
path: root/engine/include/flattask.h
blob: bba5b1a7cac7c29ed2114be9720f666aef11b602 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#ifndef __FLAT_TASK_H__
#define __FLAT_TASK_H__

#include <set>
#include "types.hpp"

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