summaryrefslogtreecommitdiffstats
path: root/engine/include/core/priority.hpp
blob: d6c9b35439638a2162450f6fd378d33f8f5c05d3 (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
#pragma once

#include <memory>
#include <set>

namespace flat {
    namespace core {
        enum class priority_t : unsigned  {
            max = 0,
            higher = 1,
            high = 2,
            none = 3,
            low = 4,
            lower = 5,
            min = 6,
        };

        class prioritized {
        private:
            const priority_t m_priority;

        public:
            prioritized(priority_t priority = priority_t::none)
                : m_priority(priority) {}

            const priority_t priority() const {
                return m_priority;
            }
        };       

        struct prioritize {
            bool operator()(const prioritized& lhs, const prioritized& 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;
                }
            }
        };

        template<typename Prioritized>
        using queue = std::multiset<Prioritized, prioritize>;
    }
}