From f7decf41d8d0062901cd39c42a3669a80537c7df Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Thu, 3 Aug 2017 20:03:59 +0500 Subject: 2017-08-03 --- src/FSM.hpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/FSM.hpp (limited to 'src/FSM.hpp') diff --git a/src/FSM.hpp b/src/FSM.hpp new file mode 100644 index 0000000..9570e26 --- /dev/null +++ b/src/FSM.hpp @@ -0,0 +1,32 @@ +#pragma once + +#include +#include +#include + +template +class FSM { + T previousState; + T &state; + std::map> handlers; +public: + using Transaction = std::pair; + + FSM(T &value) : state(value), previousState(value) {} + + ~FSM() = default; + + void Update() { + auto handler = handlers[Transaction{previousState, state}]; + if (handler) + handler(); + } + + void RegisterHandler(T state, std::function handler) { + handlers[Transaction{state, state}] = handler; + } + + void RegisterTransactionHandler(Transaction transaction, std::function handler) { + handlers[transaction] = handler; + } +}; \ No newline at end of file -- cgit v1.2.3