40 lines
719 B
C++
40 lines
719 B
C++
#pragma once
|
|
|
|
#include <memory>
|
|
|
|
namespace td {
|
|
|
|
template <typename TDerived, typename TReturn, typename... TArgs>
|
|
class StateMachine {
|
|
public:
|
|
class State {
|
|
public:
|
|
State(TDerived& a_StateMachine) : m_StateMachine(a_StateMachine) {}
|
|
virtual ~State() {}
|
|
|
|
virtual TReturn Update(TArgs... args) = 0;
|
|
|
|
protected:
|
|
TDerived& m_StateMachine;
|
|
};
|
|
|
|
StateMachine() {}
|
|
virtual ~StateMachine() {}
|
|
|
|
TReturn Update(TArgs... args) {
|
|
assert(m_State);
|
|
return m_State->Update(args...);
|
|
}
|
|
|
|
template <typename T, typename... Args>
|
|
void ChangeState(Args&&... args) {
|
|
m_State = std::make_unique<T>(static_cast<TDerived&>(*this), args...);
|
|
}
|
|
|
|
private:
|
|
std::unique_ptr<State> m_State;
|
|
};
|
|
|
|
|
|
} // namespace td
|