{"id":13537260,"url":"https://github.com/shuvalov-mdb/xstate-cpp-generator","last_synced_at":"2025-04-02T04:30:28.563Z","repository":{"id":40666317,"uuid":"306868508","full_name":"shuvalov-mdb/xstate-cpp-generator","owner":"shuvalov-mdb","description":"C++ State Machine generator for Xstate","archived":false,"fork":false,"pushed_at":"2021-11-01T15:12:20.000Z","size":171,"stargazers_count":55,"open_issues_count":1,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-15T22:45:19.437Z","etag":null,"topics":["cpp","fsm","fsm-library","state-machine","state-management","xstate","xstate-cpp"],"latest_commit_sha":null,"homepage":"","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/shuvalov-mdb.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-10-24T11:27:24.000Z","updated_at":"2025-02-12T13:38:16.000Z","dependencies_parsed_at":"2022-09-17T06:50:33.149Z","dependency_job_id":null,"html_url":"https://github.com/shuvalov-mdb/xstate-cpp-generator","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shuvalov-mdb%2Fxstate-cpp-generator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shuvalov-mdb%2Fxstate-cpp-generator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shuvalov-mdb%2Fxstate-cpp-generator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shuvalov-mdb%2Fxstate-cpp-generator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shuvalov-mdb","download_url":"https://codeload.github.com/shuvalov-mdb/xstate-cpp-generator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246756779,"owners_count":20828765,"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","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":["cpp","fsm","fsm-library","state-machine","state-management","xstate","xstate-cpp"],"created_at":"2024-08-01T09:00:56.921Z","updated_at":"2025-04-02T04:30:28.156Z","avatar_url":"https://github.com/shuvalov-mdb.png","language":"C++","funding_links":[],"categories":["Awesome XState","C++"],"sub_categories":["Articles"],"readme":"# C++ State Machine generator for Xstate\n\nThis package allows to convert TypeScript language State Machine developed\nusing [Xstate](https://github.com/davidkpiano/xstate) into C++ generated SM, no coding required.\n\n* Project location: https://github.com/shuvalov-mdb/xstate-cpp-generator\n* NPM TypeScript package location: https://www.npmjs.com/package/xstate-cpp-generator\n* Copyright Andrew Shuvalov, MIT [License](https://github.com/shuvalov-mdb/xstate-cpp-generator/blob/master/LICENSE)\n* Contact information: [Discussion](https://github.com/davidkpiano/xstate/discussions/1608) or file a bug\n\n## Tutorials and Documentation\n* Quick start is right below\n* [Tutorial](TUTORIAL.md)\n* Video tutorial: [Part 1](https://youtu.be/DnuJFUR1SgA), [Part 2](https://youtu.be/8TTfRrmNu0s), and [Part 3](https://youtu.be/HoH68709Sq8)\n\n## Features\n\n* Design and test the State Machine in [Xstate](https://github.com/davidkpiano/xstate) and then convert to C++ without any changes\n  * Use the [online vizualizer](https://xstate.js.org/viz/) to debug the State Machine\n* SM basic features supported: [States](https://xstate.js.org/docs/guides/states.html), [Events](https://xstate.js.org/docs/guides/events.html), [Transitions](https://xstate.js.org/docs/guides/transitions.html)\n  * SM extra features supported: [Actions](https://xstate.js.org/docs/guides/actions.html#declarative-actions)\n* Generated C++ is fully synchronized, safe to use in multi-threaded environemnt without any changes\n* No external dependencies except STL. No boost dependency.\n* Callback model:\n  * Entry, Exit and Trasition [Actions](https://xstate.js.org/docs/guides/actions.html#declarative-actions) are code\n   generated as static methods in the template object used to declare the State Machine and can be implemented by the user\n  * Every state and transtion callbacks are generated as virtual methods that can be overloaded by subclassing\n* Arbitrary user-defined data structure (called Context) can be stored in the SM\n* Any event can have an arbitrary user-defined payload attached. The event payload is propagated to related callbacks\n\n## Install and Quick Start Tutorial\n\n### 1. Install the xstate-cpp-generator TypeScript package, locally (or globally with `-g` option):\n\n```bash\n   npm install xstate-cpp-generator\n```\n### 2. Create a simple Xstate model file `engineer.ts` with few lines to trigger C++ generation at the end:\n(this example is located at https://github.com/shuvalov-mdb/xstate-cpp-generator/tree/master/demo-project)\n\n```TypeScript\nconst CppGen = require('xstate-cpp-generator');\nconst path = require('path');\n\nimport { Machine } from 'xstate';\n\nconst engineerMachine = Machine({\n    id: 'engineer',\n    initial: 'sleeping',\n    states: {\n        sleeping: {\n            entry: 'startWakeupTimer',\n            exit: 'morningRoutine',\n            on: {\n                'TIMER': { target: 'working', actions: ['startHungryTimer', 'startTiredTimer'] },\n            }\n        },\n        working: {\n            entry: ['checkEmail', 'startHungryTimer', 'checkIfItsWeekend' ],\n            on: {\n                'HUNGRY': { target: 'eating', actions: ['checkEmail']},\n                'TIRED': { target: 'sleeping' },\n                'ENOUGH': { target: 'weekend' }\n            },\n        },\n        eating: {\n            entry: 'startShortTimer',\n            exit: [ 'checkEmail', 'startHungryTimer' ],\n            on: {\n                'TIMER': { target: 'working', actions: ['startHungryTimer'] },\n                'TIRED': { target: 'sleeping' }\n            }\n        },\n        weekend: {\n            type: 'final',\n        }\n    }\n});\n\nCppGen.generateCpp({\n    xstateMachine: engineerMachine,\n    destinationPath: \"\",\n    namespace: \"engineer_demo\",\n    pathForIncludes: \"\",\n    tsScriptName: path.basename(__filename)\n  });\n\n```\nTo visualize this State Machine copy-paste the 'Machine' method call to the [online vizualizer](https://xstate.js.org/viz/).\n\n### 3. Generate C++\nInstall all required dependencies:\n\n```bash\n   npm install\n```\n\nAnd run the C++ generator:\n```bash\n   ts-node engineer.ts\n```\nYou should see new generated files:\n```\nengineer_sm.h  engineer_sm.cpp  engineer_test.cpp\n```\n\nThe `engineer_test.cpp` is an automatically generated Unit Test for the model. Create a simple `SConscript` file to compile it:\n\n```\nenv = Environment()\n\nLIBS =''\n\ncommon_libs = ['gtest_main', 'gtest', 'pthread']\nenv.Append( LIBS = common_libs )\n\nenv.Append(CCFLAGS=['-fsanitize=address,undefined',\n                    '-fno-omit-frame-pointer'],\n           LINKFLAGS='-fsanitize=address,undefined')\n\nenv.Program('engineer_test', ['engineer_sm.cpp', 'engineer_test.cpp'], \n            LIBS, LIBPATH='/opt/gtest/lib:/usr/local/lib', CXXFLAGS=\"-std=c++17\")\n\n```\nand run it with:\n```\n   scons\n   ./engineer_test\n```\n\n\n## Release Notes\n\n### V 1.0.3\n* Full support of entry, exit and transition Actions\n* Multi-threading bugfixes\n### V 1.0.4\n* Converted `onEnteredState()` from move sematics `\u0026\u0026` to shared_ptr\n* Started Tutorial\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshuvalov-mdb%2Fxstate-cpp-generator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshuvalov-mdb%2Fxstate-cpp-generator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshuvalov-mdb%2Fxstate-cpp-generator/lists"}