]> git.tdb.fi Git - libs/game.git/blob - source/game/systemscheduler.h
2ab4804f6293d080a7962c1fc3ffb891ea3ad2df
[libs/game.git] / source / game / systemscheduler.h
1 #ifndef MSP_GAME_SYSTEMSCHEDULER_H_
2 #define MSP_GAME_SYSTEMSCHEDULER_H_
3
4 #include <vector>
5 #include "reflection.h"
6
7 namespace Msp::Game {
8
9 class System;
10
11 class scheduling_error: public std::logic_error
12 {
13 public:
14         scheduling_error(const std::string &w): logic_error(w) { }
15 };
16
17 class SystemScheduler
18 {
19 public:
20         struct Group
21         {
22                 std::vector<System *> systems;
23         };
24
25 private:
26         struct GraphNode
27         {
28                 System *system = nullptr;
29                 Reflection::ClassBase *type = nullptr;
30                 std::vector<GraphNode *> predecessors;
31                 unsigned scheduled_order = 0;
32         };
33
34         Reflection::Reflector &reflector;
35         std::vector<GraphNode> nodes;
36         std::vector<Group> groups;
37
38 public:
39         SystemScheduler(Reflection::Reflector &);
40
41         void add_system(System &);
42         void remove_system(System &);
43         void schedule();
44 private:
45         static int get_order(const GraphNode &, const GraphNode &);
46         static int get_explicit_order(const GraphNode &, const GraphNode &);
47         static int get_data_order(const GraphNode &, const GraphNode &);
48 public:
49         const std::vector<Group> &get_groups() const { return groups; }
50 };
51
52 } // namespace Msp::Game
53
54 #endif