add StateMachine
This commit is contained in:
39
include/td/common/StateMachine.h
Normal file
39
include/td/common/StateMachine.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#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
|
||||
Reference in New Issue
Block a user