Maze Builder Docs 8.10.23
Loading...
Searching...
No Matches
runtime_stack.h
Go to the documentation of this file.
1#ifndef RUNTIME_STACK_H
2#define RUNTIME_STACK_H
3
6#include <MazeBuilder/state.h>
7
8#include <algorithm>
9#include <functional>
10#include <memory>
11#include <ranges>
12#include <string>
13#include <string_view>
14#include <unordered_map>
15#include <vector>
16
17class randomizer;
18
21namespace mazes
22{
24 class runtime_stack final
25 {
26 public:
27 enum class Operation : unsigned int
28 {
29 PUSH = 0,
30 POP = 1,
31 ROTATE = 2,
32 CLEAR = 3
33 };
34
35 explicit runtime_stack(const runtime_app::context &ctx);
36
37 // Stack operations (deferred — applied by apply_pending_changes)
38 void push_state(state::ID state_id) noexcept;
39 void pop_state() noexcept;
40 void clear_states() noexcept;
41 void rotate() noexcept;
42
47 void visit_states(randomizer &rng, double elapsed) noexcept;
48
51 [[nodiscard]] bool is_empty() const noexcept;
52
53 [[nodiscard]] std::size_t count() const noexcept;
54
57 template <typename T>
58 void register_state(state::ID state_id)
59 {
60 m_factories.insert_or_assign(state_id, [this]()
61 { return std::make_unique<T>(runtime_context, this); });
62 }
63
65 template <typename Pointer>
66 [[nodiscard]] Pointer peek_state() const noexcept
67 {
68 auto reversed = m_states | std::views::reverse;
69
70 auto it = std::ranges::find_if(reversed, [](const auto &sp)
71 { return dynamic_cast<Pointer>(sp.get()) != nullptr; });
72
73 if (it != std::ranges::cend(reversed))
74 {
75 return dynamic_cast<Pointer>(it->get());
76 }
77
78 return nullptr;
79 }
80
81 private:
82 struct pending_change
83 {
84 explicit pending_change(Operation action, state::ID id = state::ID::TOTAL)
85 : action(action), state_id(id)
86 {
87 }
88
89 Operation action;
90 state::ID state_id;
91 };
92
93 struct state_id_hash
94 {
95 std::size_t operator()(state::ID id) const noexcept
96 {
97 return std::hash<unsigned int>{}(static_cast<unsigned int>(id));
98 }
99 };
100
101 // Apply all pending push/pop/clear operations
102 void apply_pending_changes() noexcept;
103
104 [[nodiscard]] std::unique_ptr<state> create_state(state::ID state_id)
105 {
106 if (const auto &found = m_factories.find(state_id); found != m_factories.cend())
107 {
108 return found->second();
109 }
110
111 throw std::runtime_error("Create state failed. No factory for state ID: " +
112 std::to_string(static_cast<unsigned int>(state_id)));
113 }
114
115 runtime_app::context runtime_context;
116
117 std::unordered_map<state::ID,
118 std::function<std::unique_ptr<state>()>,
119 state_id_hash>
120 m_factories;
121
122 std::vector<std::unique_ptr<state>> m_states;
123 std::vector<pending_change> m_pending;
124 };
125} // namespace mazes
126
127#endif // RUNTIME_STACK_H
Provides random-number generating capabilities.
Definition randomizer.h:16
void register_state(state::ID state_id)
Register a factory for a concrete state type. The factory is called with (runtime_context,...
Definition runtime_stack.h:58
bool is_empty() const noexcept
Checks if the runtime stack is empty.
void visit_states(randomizer &rng, double elapsed) noexcept
Visit each state top-to-bottom, calling update(args, elapsed) until one returns false....
Pointer peek_state() const noexcept
Find the topmost state that matches the pointer type T.
Definition runtime_stack.h:66
struct representing the context passed to states in the runtime_app stack
Definition runtime_app.h:34
struct representing a state in the runtime stack
Definition state.h:18