{"id":13732272,"url":"https://github.com/mpusz/fsm-variant","last_synced_at":"2025-08-20T02:08:37.209Z","repository":{"id":139724954,"uuid":"108593939","full_name":"mpusz/fsm-variant","owner":"mpusz","description":"Finite State Machine implementation using std::variant","archived":false,"fork":false,"pushed_at":"2019-09-14T15:49:09.000Z","size":17,"stargazers_count":87,"open_issues_count":0,"forks_count":12,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-06-02T18:04:43.973Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mpusz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2017-10-27T20:44:54.000Z","updated_at":"2025-04-13T00:36:53.000Z","dependencies_parsed_at":null,"dependency_job_id":"f06059a0-407c-4e47-9987-d104cc8e9139","html_url":"https://github.com/mpusz/fsm-variant","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/mpusz/fsm-variant","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpusz%2Ffsm-variant","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpusz%2Ffsm-variant/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpusz%2Ffsm-variant/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpusz%2Ffsm-variant/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mpusz","download_url":"https://codeload.github.com/mpusz/fsm-variant/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpusz%2Ffsm-variant/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271252993,"owners_count":24726918,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-20T02:00:09.606Z","response_time":69,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T02:01:51.246Z","updated_at":"2025-08-20T02:08:37.163Z","avatar_url":"https://github.com/mpusz.png","language":"C++","funding_links":[],"categories":["GameProgramming"],"sub_categories":[],"readme":"# mp::fsm\n\nImplementation of Finite State Machine presented by me on CppCon 2018 in a talk\n[Effective replacement of dynamic polymorphism with std::variant](https://github.com/CppCon/CppCon2018/blob/master/Presentations/effective_replacement_of_dynamic_polymorphism_with_stdvariant/effective_replacement_of_dynamic_polymorphism_with_stdvariant__mateusz_pusz__cppcon_2018.pdf). \n\nIt uses `std::variant` and `std::optional` under the hood.\n\n\n## How to?\n\nFSM works with any types defining **events**. **State** is represented by any type too.\nAll states have to be put into the variant and the initial state should appear the first\non the list. Both states and events can be stateful.\n\n```cpp\n#include \u003cmp/fsm.h\u003e\n\nstruct event_connect {};\nstruct event_disconnect {};\n\nstruct state_idle {};\nstruct state_connected {};\n\nusing state = std::variant\u003cstate_idle, state_connected\u003e;\n\nclass sample_fsm : public mp::fsm\u003csample_fsm, state\u003e {\npublic:\n  template\u003ctypename State, typename Event\u003e\n  auto on_event(State\u0026, const Event\u0026) { return std::nullopt; }\n\n  auto on_event(state_idle\u0026, const event_connect\u0026) { return state_connected{}; }\n  auto on_event(state_connected\u0026, const event_disconnect\u0026) { return state_idle{}; }\n};\n\nint main()\n{\n  sample_fsm fsm;\n  fsm.dispatch(event_connect());\n  fsm.dispatch(event_disconnect());\n}\n```\n\n\n## Building, testing and installation\n\nFor detailed information on project compilation, testing and reuse please refer to\n[INSTALL.md](INSTALL.md).\n\n\n## Acknowledgements\n\nThe FSM design was inspired by:\n- http://khuttun.github.io/2017/02/04/implementing-state-machines-with-std-variant.html\n- http://mooooo.ooo/sumtypes-and-statemachines/\n\nThank you!","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmpusz%2Ffsm-variant","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmpusz%2Ffsm-variant","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmpusz%2Ffsm-variant/lists"}