### Serialization Example Source: https://doc.hfsm.dev/user-guide/debugging-and-tools/serialization An example demonstrating how to enable serialization, create a `SerialBuffer`, save the FSM's state, and then load it back into a fresh FSM instance. ```APIDOC ## Serialization Example ### Description This example shows the practical usage of HFSM2 serialization. It includes enabling the feature, creating an FSM, saving its state to a `SerialBuffer`, and then loading that state into a new FSM instance to verify restoration. ### Code ```cpp // Enable serialization #define HFSM_ENABLE_SERIALIZATION #include #include using M = hfsm2::Machine; using FSM = M::PeerRoot< struct State1, struct State2 >; struct State1 : FSM::State { /* .. */ }; struct State2 : FSM::State { /* .. */ }; int main() { // Buffer for serialization // Members: // bitSize - Number of payload bits used // payload - Serialized data FSM::Instance::SerialBuffer buffer; { FSM::Instance fsm; // Create a new FSM instance fsm.changeTo(); // Request a transition to 'State2' fsm.update(); // Process transitions assert(fsm.isActive()); // Check if transition completed fsm.save(buffer); // Serialize FSM configuration into 'buffer' } { FSM::Instance fsm; // Create a fresh FSM instance assert(fsm.isActive()); // Initial 'State1' is activated by default fsm.load(buffer); // De-serialize FSM from 'buffer' assert(fsm.isActive()); // Check its configuration is restored } } ``` ``` -------------------------------- ### Query Documentation Dynamically Source: https://doc.hfsm.dev/quick-tutorial Perform an HTTP GET request on the current page URL with the 'ask' query parameter to ask questions and get direct answers with relevant excerpts. ```HTTP GET https://doc.hfsm.dev/quick-tutorial.md?ask= ``` -------------------------------- ### Query Documentation Dynamically Source: https://doc.hfsm.dev/user-guide/advanced-features/guards/entry-guards Perform an HTTP GET request on the current page URL with the `ask` query parameter to get dynamic documentation answers. The question should be specific and self-contained. ```http GET https://doc.hfsm.dev/user-guide/advanced-features/guards/entry-guards.md?ask= ``` -------------------------------- ### Query Documentation Dynamically Source: https://doc.hfsm.dev/user-guide/advanced-features/state-injections Perform an HTTP GET request on a documentation URL with the 'ask' query parameter to get dynamic answers. The question should be specific and self-contained. Use this for clarifications or to retrieve related documentation. ```http GET https://doc.hfsm.dev/user-guide/advanced-features/state-injections.md?ask= ``` -------------------------------- ### Query Documentation Dynamically Source: https://doc.hfsm.dev/practical-topics/how-to/reducing-state-coupling Perform an HTTP GET request on the current page URL with the `ask` query parameter to get dynamic information. The question should be specific and self-contained. ```http GET https://doc.hfsm.dev/practical-topics/how-to/reducing-state-coupling.md?ask= ``` -------------------------------- ### Query Documentation with GET Request Source: https://doc.hfsm.dev/practical-topics/common-patterns/delayed-teardown Perform an HTTP GET request to the current page URL with the `ask` query parameter to dynamically query the documentation. Use this mechanism when the answer is not explicitly present, you need clarification, or want to retrieve related documentation sections. ```http GET https://doc.hfsm.dev/practical-topics/common-patterns/delayed-teardown.md?ask= ``` -------------------------------- ### Query Documentation with 'ask' Parameter Source: https://doc.hfsm.dev/user-guide/basic-features/update-cycle Perform an HTTP GET request to query the documentation dynamically. Use this when the answer is not explicitly present, you need clarification, or want to retrieve related documentation sections. ```http GET https://doc.hfsm.dev/user-guide/basic-features/update-cycle.md?ask= ``` -------------------------------- ### Query Documentation with GET Request Source: https://doc.hfsm.dev/user-guide/configuration/type-configuration To get additional information not directly present on a page, perform an HTTP GET request with the 'ask' query parameter. The question should be specific and self-contained. ```http GET https://doc.hfsm.dev/user-guide/configuration/type-configuration.md?ask= ``` -------------------------------- ### Query Documentation with 'ask' Parameter Source: https://doc.hfsm.dev/users Perform an HTTP GET request on the current page URL with the 'ask' query parameter to dynamically query the documentation. The question should be specific and self-contained. ```http GET https://doc.hfsm.dev/users.md?ask= ``` -------------------------------- ### Query HFSM2 Documentation Dynamically Source: https://doc.hfsm.dev/general-information/goals Perform an HTTP GET request to the current page URL with the 'ask' query parameter to get specific information not directly present on the page. The question should be specific and self-contained. ```http GET https://doc.hfsm.dev/general-information/goals.md?ask= ``` -------------------------------- ### Query Documentation Dynamically Source: https://doc.hfsm.dev/user-guide/configuration/context Perform an HTTP GET request to the current page URL with the 'ask' query parameter to get dynamic answers to your questions. The question should be specific and in natural language. Use this when the answer is not explicit, for clarification, or to retrieve related sections. ```http GET https://doc.hfsm.dev/user-guide/configuration/context.md?ask= ``` -------------------------------- ### Querying Documentation Dynamically Source: https://doc.hfsm.dev/user-guide/basic-features/hierarchy/state To get additional information not directly present on a page, perform an HTTP GET request with the 'ask' query parameter. The question should be specific and self-contained. This mechanism is useful for clarifications or retrieving related documentation sections. ```http GET https://doc.hfsm.dev/user-guide/basic-features/hierarchy/state.md?ask= ``` -------------------------------- ### Query Documentation Dynamically Source: https://doc.hfsm.dev/user-guide/debugging-and-tools/activity-report Perform an HTTP GET request to the current page URL with the 'ask' query parameter to ask specific, self-contained questions in natural language. The response will include a direct answer and relevant excerpts from the documentation. ```http GET https://doc.hfsm.dev/user-guide/debugging-and-tools/activity-report.md?ask= ``` -------------------------------- ### Query Documentation Dynamically Source: https://doc.hfsm.dev/practical-topics/how-to/designing-hierarchy Perform an HTTP GET request to query documentation dynamically. Use the 'ask' query parameter with a specific, self-contained question in natural language. The response includes a direct answer and relevant excerpts. ```http GET https://doc.hfsm.dev/practical-topics/how-to/designing-hierarchy.md?ask= ``` -------------------------------- ### Query Documentation Dynamically Source: https://doc.hfsm.dev/user-guide/basic-features/state-methods/control Perform an HTTP GET request on the current page URL with the `ask` query parameter to dynamically query the documentation. The question should be specific and self-contained. Use this mechanism for clarification, additional context, or retrieving related documentation sections. ```http GET https://doc.hfsm.dev/user-guide/basic-features/state-methods/control.md?ask= ``` -------------------------------- ### Query Documentation Dynamically Source: https://doc.hfsm.dev/user-guide/debugging-and-tools/transition-history Perform an HTTP GET request on the current page URL with the `ask` query parameter to dynamically query the documentation. The question should be specific and in natural language. Use this mechanism for clarifications or to retrieve related documentation. ```http GET https://doc.hfsm.dev/user-guide/debugging-and-tools/transition-history.md?ask= ``` -------------------------------- ### Query Documentation Dynamically Source: https://doc.hfsm.dev/general-information/future-plans Perform an HTTP GET request to the current page URL with the 'ask' query parameter to dynamically query the documentation. Use this when the answer is not explicitly present, for clarification, or to retrieve related documentation sections. ```http GET https://doc.hfsm.dev/general-information/future-plans.md?ask= ``` -------------------------------- ### Query HFSM Documentation Dynamically Source: https://doc.hfsm.dev/user-guide/getting-started Perform an HTTP GET request to the current page URL with the `ask` query parameter to dynamically query the documentation. Use this for specific questions, clarifications, or to retrieve related sections when information is not explicitly present. ```http GET https://doc.hfsm.dev/user-guide/getting-started.md?ask= ``` -------------------------------- ### Query Documentation Dynamically Source: https://doc.hfsm.dev/practical-topics/how-to/reusing-state-code Perform an HTTP GET request on the current page URL with the 'ask' query parameter to retrieve specific information or clarification not explicitly present on the page. The question should be specific and self-contained. ```http GET https://doc.hfsm.dev/practical-topics/how-to/reusing-state-code.md?ask= ``` -------------------------------- ### Configure HFSM2 Functionality Source: https://doc.hfsm.dev/quick-tutorial Configure optional HFSM2 functionality using #define directives. This example enables Plans for straightforward transition cycles. ```cpp #define HFSM2_ENABLE_PLANS ``` -------------------------------- ### Query Documentation Dynamically Source: https://doc.hfsm.dev/general-information Perform an HTTP GET request to the current page URL with the `ask` query parameter to dynamically query the documentation. The question should be specific and in natural language. Use this when information is not explicitly present or for clarification. ```http GET https://doc.hfsm.dev/general-information.md?ask= ``` -------------------------------- ### Query Documentation Dynamically Source: https://doc.hfsm.dev/practical-topics/common-patterns Perform an HTTP GET request on the current page URL with the 'ask' query parameter to dynamically query the documentation. Use this when the answer is not explicitly present, you need clarification, or want to retrieve related sections. ```http GET https://doc.hfsm.dev/practical-topics/common-patterns.md?ask= ``` -------------------------------- ### Query Documentation Dynamically Source: https://doc.hfsm.dev/user-guide Perform an HTTP GET request on a documentation page URL with the 'ask' query parameter to dynamically query for information. The question should be specific and self-contained. Use this when the answer is not explicitly present, for clarification, or to retrieve related documentation. ```http GET https://doc.hfsm.dev/user-guide.md?ask= ``` -------------------------------- ### Define Done State Source: https://doc.hfsm.dev/quick-tutorial Define the 'Done' state, a terminal state in this example. ```cpp struct Done : FSM::State {}; ``` -------------------------------- ### Configure Machine Types with hfsm2::Config Source: https://doc.hfsm.dev/user-guide/configuration/type-configuration Use hfsm2::MachineT with a custom hfsm2::Config to define derivative types for your HFSM2 machine. This example shows how to specify a custom context and random number generator. ```cpp using M = hfsm2::MachineT< hfsm2::Config ::ContextT ::RandomT >; ``` -------------------------------- ### Serialize and De-serialize FSM Configuration Source: https://doc.hfsm.dev/user-guide/debugging-and-tools/serialization Demonstrates how to create an FSM, transition its state, save its configuration to a buffer, and then load that configuration into a fresh FSM instance. Ensure `HFSM_ENABLE_SERIALIZATION` is defined before including the header. ```cpp #define HFSM_ENABLE_SERIALIZATION #include #include using M = hfsm2::Machine; using FSM = M::PeerRoot< struct State1, struct State2 >; struct State1 : FSM::State { /* .. */ }; struct State2 : FSM::State { /* .. */ }; int main() { // Buffer for serialization // Members: // bitSize - Number of payload bits used // payload - Serialized data FSM::Instance::SerialBuffer buffer; { FSM::Instance fsm; fsm.changeTo(); fsm.update(); assert(fsm.isActive()); fsm.save(buffer); } { FSM::Instance fsm; assert(fsm.isActive()); fsm.load(buffer); assert(fsm.isActive()); } } ``` -------------------------------- ### Main Function - Client Code Source: https://doc.hfsm.dev/quick-tutorial The main function demonstrates how to use the defined state machine. It includes creating context and FSM instances, and interacting with the FSM. ```cpp int main() { ``` -------------------------------- ### Create Context and FSM Instances Source: https://doc.hfsm.dev/quick-tutorial Instantiate the context and the state machine. Assertions check the initial active states. ```cpp Context context; context.powerOn = true; FSM::Instance fsm{context}; assert(fsm.isActive()); assert(fsm.isActive()); ``` -------------------------------- ### Query Documentation Dynamically Source: https://doc.hfsm.dev/user-guide/debugging-and-tools/structure-report Use this method to ask questions about the documentation when information is not explicitly present. The question should be specific and in natural language. ```http GET https://doc.hfsm.dev/user-guide/debugging-and-tools/structure-report.md?ask= ``` -------------------------------- ### Define On State with Plan Source: https://doc.hfsm.dev/quick-tutorial Define the 'On' state, which utilizes a plan for sequential state transitions. The enter method sets up the plan, and planSucceeded is called upon its completion. ```cpp struct On : FSM::State { void enter(PlanControl& control) { auto plan = control.plan(); plan.change(); plan.change(); plan.change(); plan.change(); } void exit(PlanControl& /*control*/) {} void planSucceeded(FullControl& control) { control.changeTo(); } void planFailed(FullControl& /*control*/) {} }; ``` -------------------------------- ### Include HFSM2 C/C++ Headers Source: https://doc.hfsm.dev/quick-tutorial Include the necessary C/C++ headers for using HFSM2. ```cpp #include ``` -------------------------------- ### React to Events for Transitions Source: https://doc.hfsm.dev/quick-tutorial Use fsm.react() to trigger transitions based on external events. Assertions check the FSM's state after reacting to an event. ```cpp fsm.react(Event{}); assert(fsm.isActive()); ``` -------------------------------- ### Define Type Configuration Source: https://doc.hfsm.dev/quick-tutorial Optionally define the type configuration for the state machine, specifying the context type. ```cpp using Config = hfsm2::Config::ContextT; ``` -------------------------------- ### Include HFSM2 Header Source: https://doc.hfsm.dev/quick-tutorial Include the main HFSM2 header file to use the machine functionality. ```cpp #include ``` -------------------------------- ### Define Green State with Reaction Source: https://doc.hfsm.dev/quick-tutorial Define the 'Green' state. The react method handles external events, and control.succeed() advances the plan. ```cpp struct Green : FSM::State { void react(const Event&, FullControl& control) { control.succeed(); } }; ``` -------------------------------- ### Update FSM for Transitions Source: https://doc.hfsm.dev/quick-tutorial Call fsm.update() to process transitions. Assertions verify the FSM's state after updates. ```cpp fsm.update(); assert(fsm.isActive()); fsm.update(); assert(fsm.isActive()); ``` -------------------------------- ### Serialization Interface Source: https://doc.hfsm.dev/user-guide/debugging-and-tools/serialization The HFSM2 library provides a `SerialBuffer` for serialization and methods `save` and `load` on the FSM instance to manage state persistence. ```APIDOC ## `hfsm2::Root::SerialBuffer` ### Description Buffer for serialization. ## `hfsm2::Root::save(SerialBuffer&) const` ### Description Serialize the structural configuration. ### Method `const` method that takes a `SerialBuffer` reference to store the FSM's current configuration. ## `hfsm2::Root::load(const SerialBuffer&)` ### Description De-serialize the configuration and initialize FSM. ### Method Method that takes a constant reference to a `SerialBuffer` to restore the FSM's configuration. ``` -------------------------------- ### Continue Updating FSM Source: https://doc.hfsm.dev/quick-tutorial Continue calling fsm.update() to process remaining transitions until the desired state is reached. Assertions confirm the final states. ```cpp fsm.update(); assert(fsm.isActive()); fsm.update(); assert(fsm.isActive()); return 0; } ``` -------------------------------- ### Declare State Machine Structure Source: https://doc.hfsm.dev/quick-tutorial Declare the state machine structure, defining states and their hierarchy. States need to be forward declared. ```cpp #define S(s) struct s using FSM = M::PeerRoot< S(Off), // initial top-level state M::Composite, S(Done) >; #undef S ``` -------------------------------- ### Define Off State Source: https://doc.hfsm.dev/quick-tutorial Define the 'Off' state. The entryGuard method is called before state activation and can be used to re-route transitions based on context. ```cpp struct Off : FSM::State { void entryGuard(FullControl& control) { if (control.context().powerOn) control.changeTo(); } }; ``` -------------------------------- ### Define Context Structure Source: https://doc.hfsm.dev/quick-tutorial Define the context structure that holds shared data between the state machine and its host. This is optional and can be omitted if the host object itself is used. ```cpp struct Context { bool powerOn; }; ``` -------------------------------- ### Define HFSM2 Machine Alias Source: https://doc.hfsm.dev/quick-tutorial Define a convenient alias for hfsm2::MachineT using the configured type. ```cpp using M = hfsm2::MachineT; ``` -------------------------------- ### Define Red State Source: https://doc.hfsm.dev/quick-tutorial Define the 'Red' state. The update method is called periodically and signals successful completion of the current plan step. ```cpp struct Red : FSM::State { void update(FullControl& control) { control.succeed(); } }; ``` -------------------------------- ### Define Event Structure Source: https://doc.hfsm.dev/quick-tutorial Define an event structure to be used for external stimuli that the FSM can react to. Transitions in HFSM2 are not strictly event-based. ```cpp struct Event {}; ``` -------------------------------- ### Define Yellow State Source: https://doc.hfsm.dev/quick-tutorial Define the 'Yellow' state. Its update method signals successful completion, advancing the plan. ```cpp struct Yellow : FSM::State { void update(FullControl& control) { control.succeed(); } }; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.