]> git.localhorst.tv Git - gong.git/blob - src/app/StateControl.hpp
code, assets, and other stuff stolen from blank
[gong.git] / src / app / StateControl.hpp
1 #ifndef GONG_APP_STATECONTROL_HPP_
2 #define GONG_APP_STATECONTROL_HPP_
3
4 #include <queue>
5
6
7 namespace gong {
8 namespace app {
9
10 class HeadlessApplication;
11 class State;
12
13 class StateControl {
14
15 public:
16         // add state to the front
17         void Push(State *s) {
18                 cue.emplace(PUSH, s);
19         }
20
21         // swap state at the front
22         void Switch(State *s) {
23                 cue.emplace(SWITCH, s);
24         }
25
26         // remove state at the front
27         void Pop() {
28                 cue.emplace(POP);
29         }
30
31         // remove all states
32         // application will exit if nothing is pushed after this
33         void PopAll() {
34                 cue.emplace(POP_ALL);
35         }
36
37         // pop states until this one is on top
38         void PopAfter(State *s) {
39                 cue.emplace(POP_AFTER, s);
40         }
41
42         // pop states until this one is removed
43         void PopUntil(State *s) {
44                 cue.emplace(POP_UNTIL, s);
45         }
46
47
48         void Commit(HeadlessApplication &);
49
50 private:
51         enum Command {
52                 PUSH,
53                 SWITCH,
54                 POP,
55                 POP_ALL,
56                 POP_AFTER,
57                 POP_UNTIL,
58         };
59         struct Memo {
60                 State *state;
61                 Command cmd;
62                 explicit Memo(Command c, State *s = nullptr): state(s), cmd(c) { }
63         };
64         std::queue<Memo> cue;
65
66 };
67
68 }
69 }
70
71 #endif