-
Notifications
You must be signed in to change notification settings - Fork 0
/
state_machine_base.h
194 lines (145 loc) · 3.86 KB
/
state_machine_base.h
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#pragma once
#include <cassert>
#include <cstddef>
#include <memory>
#include <string_view>
#include <type_traits>
#include <vector>
#ifndef NDEBUG
#include <iostream>
#endif // !NDEBUG
#include <tgbot/types/Message.h>
#include "logging.h"
#include "state_base.h"
#include "state_machine.h"
namespace hanley_bot::state {
namespace utils {
template<typename Enum, typename Machine>
concept StateOf = std::is_same_v<typename Machine::State, Enum>;
template<typename Machine>
struct FactoryCreator {
public:
using ReturnType = typename Machine::StatePtr;
using State = typename Machine::State;
using FactoryType = ReturnType(*)();
static std::vector<FactoryType>& GetStorage() {
static std::vector<FactoryType> storage;
return storage;
}
#ifndef NDEBUG
template<typename T>
class DuplicateCheck {
public:
DuplicateCheck() {
static bool check = false;
if (check) {
std::cerr << "State " << typeid(T).name() << " was already registered!" << std::endl;
std::abort();
}
check = true;
}
};
#endif // !NDEBUG
template<typename T, State state>
static std::nullptr_t Register() {
#ifndef NDEBUG
static const DuplicateCheck<T> check;
#endif // !NDEBUG
static constexpr auto index = static_cast<size_t>(state);
auto& storage = GetStorage();
if (storage.size() >= index) {
storage.resize(index + 1);
}
assert(!storage[index]);
assert(storage.size() != 0);
auto factory = []() -> ReturnType {
return std::make_unique<T>();
};
storage[index] = factory;
return nullptr;
}
static ReturnType Create(State state) {
const auto index = static_cast<size_t>(state);
const auto& storage = GetStorage();
assert(storage.size() >= index);
return storage[index]();
}
};
} // namespace utils
template<typename Machine>
class StateMachineBase : public StateMachine {
public:
using MyState = StateInterface<Machine>;
using StatePtr = std::unique_ptr<MyState>;
using StateFactory = utils::FactoryCreator<Machine>;
template<typename Derived, utils::StateOf<Machine> auto MyState>
struct StateBase : public MyState {
static inline auto Registrator = StateFactory::template Register<Derived, MyState>();
};
protected:
explicit StateMachineBase(StatePtr&& initial_state) {
PushState(std::move(initial_state));
}
template<utils::StateOf<Machine> MyState>
explicit StateMachineBase(MyState state) : StateMachineBase(StateFactory::Create(state)) {}
public:
StatePtr& GetState() {
assert(!states_.empty());
return states_.back();
}
template<utils::StateOf<Machine> MyState>
StatePtr& PushState(MyState state) {
return PushState(StateFactory::Create(state));
}
StatePtr& PushState(StatePtr state) {
return states_.emplace_back(std::move(state));
}
StatePtr PopState() {
auto back = std::move(states_.back());
states_.pop_back();
return back;
}
void Do(StateValue result) {
if (result) {
PushState(result.ToEnum<typename Machine::State>());
return EnterState();
} else {
auto control_value = result.ToControlValue();
using enum StateValue::ControlValue;
switch (control_value) {
case kDoNothing:
{
return;
}
case kFinish:
{
return Finish();
}
case kPopState:
{
PopState();
return EnterState();
}
case kUnreachable:
{
LOG(error) << typeid(Machine).name() << " state " << typeid(*GetState()).name() << " returned unreachable flag!";
return Finish();
}
}
}
}
public:
void EnterState() final {
StopListeningForInput();
return Do(GetState()->OnEnter(static_cast<Machine&>(*this)));
}
void OnInput(const TgBot::Message::Ptr& input_message) final {
return Do(GetState()->OnInput(static_cast<Machine&>(*this), input_message));
}
void OnCallback(std::string_view data) final {
return Do(GetState()->OnCallback(static_cast<Machine&>(*this), data));
}
private:
std::vector<StatePtr> states_;
};
} // namespace hanley_bot::state