48 lines
1.0 KiB
C++
48 lines
1.0 KiB
C++
#pragma once
|
|
|
|
#include <cassert>
|
|
#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;
|
|
|
|
template <typename T, typename... Args>
|
|
T* ChangeState(Args&&... args) {
|
|
return m_StateMachine.template ChangeState<T>(std::forward<Args>(args)...);
|
|
}
|
|
|
|
protected:
|
|
TDerived& m_StateMachine;
|
|
};
|
|
|
|
StateMachine() {}
|
|
StateMachine(StateMachine&&) = default;
|
|
virtual ~StateMachine() {}
|
|
|
|
virtual TReturn Update(TArgs... args) {
|
|
assert(m_State && "You must change state at least once before updating !");
|
|
return m_State->Update(args...);
|
|
}
|
|
|
|
template <typename T, typename... Args>
|
|
T* ChangeState(Args&&... args) {
|
|
m_State = std::make_unique<T>(static_cast<TDerived&>(*this), std::forward<Args>(args)...);
|
|
return static_cast<T*>(m_State.get());
|
|
}
|
|
|
|
private:
|
|
std::unique_ptr<State> m_State;
|
|
};
|
|
|
|
|
|
} // namespace td
|