From c9c78e191dd71bacd769bbeccd58b80e4376184b Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sat, 5 Aug 2017 20:07:10 +0500 Subject: 2017-08-05 --- src/FSM.hpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'src/FSM.hpp') diff --git a/src/FSM.hpp b/src/FSM.hpp index 9570e26..346d0f0 100644 --- a/src/FSM.hpp +++ b/src/FSM.hpp @@ -6,27 +6,31 @@ template class FSM { - T previousState; - T &state; - std::map> handlers; public: using Transaction = std::pair; + using Handler = std::function; - FSM(T &value) : state(value), previousState(value) {} + FSM(T initialState) : state(initialState), previousState(initialState) {} ~FSM() = default; void Update() { - auto handler = handlers[Transaction{previousState, state}]; + auto &handler = handlers[Transaction{previousState, state}]; if (handler) - handler(); + handler(state); + previousState = state; } - void RegisterHandler(T state, std::function handler) { + void RegisterHandler(T state, Handler handler) { handlers[Transaction{state, state}] = handler; } - void RegisterTransactionHandler(Transaction transaction, std::function handler) { + void RegisterTransactionHandler(Transaction transaction, Handler handler) { handlers[transaction] = handler; } + +private: + T previousState; + T state; + std::map handlers; }; \ No newline at end of file -- cgit v1.2.3