diff options
author | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2017-08-03 17:03:59 +0200 |
---|---|---|
committer | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2017-08-03 17:03:59 +0200 |
commit | f7decf41d8d0062901cd39c42a3669a80537c7df (patch) | |
tree | d3032265df11eee6e3eaf8b4b081524bef5e72b2 /src/FSM.hpp | |
parent | 2017-07-29 (diff) | |
download | AltCraft-f7decf41d8d0062901cd39c42a3669a80537c7df.tar AltCraft-f7decf41d8d0062901cd39c42a3669a80537c7df.tar.gz AltCraft-f7decf41d8d0062901cd39c42a3669a80537c7df.tar.bz2 AltCraft-f7decf41d8d0062901cd39c42a3669a80537c7df.tar.lz AltCraft-f7decf41d8d0062901cd39c42a3669a80537c7df.tar.xz AltCraft-f7decf41d8d0062901cd39c42a3669a80537c7df.tar.zst AltCraft-f7decf41d8d0062901cd39c42a3669a80537c7df.zip |
Diffstat (limited to 'src/FSM.hpp')
-rw-r--r-- | src/FSM.hpp | 32 |
1 files changed, 32 insertions, 0 deletions
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 <utility> +#include <functional> +#include <map> + +template<class T> +class FSM { + T previousState; + T &state; + std::map<Transaction, std::function<void()>> handlers; +public: + using Transaction = std::pair<T, T>; + + 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<void()> handler) { + handlers[Transaction{state, state}] = handler; + } + + void RegisterTransactionHandler(Transaction transaction, std::function<void()> handler) { + handlers[transaction] = handler; + } +};
\ No newline at end of file |