HFSM2
  • HFSM2
  • Quick Tutorial
  • General Information
    • History
    • Goals
    • Development Principles
    • Future Plans
  • User Guide
    • Getting Started
    • Configuration
      • Feature Macros
      • Type Configuration
      • Context
    • Basic Features
      • Hierarchy
        • Root
        • Region
        • State
      • State Methods
        • Control
      • Transitions
        • Events
      • Update Cycle
    • Advanced Features
      • Transitions into Regions
      • Guards
        • Entry Guards
        • Exit Guards
      • State Injections
      • State Data Access
      • Dynamic States
      • Plans
    • Debugging and Tools
      • Activity Report
      • Structure Report
      • Transition History
      • Serialization *
      • Logging Support
  • Practical Topics
    • How-To
      • Designing Hierarchy
      • Reducing State Coupling
      • Reusing State Code
    • Common Patterns
      • Delayed Teardown
  • Users
Powered by GitBook
On this page
  • Quick Facts
  • Interface
  • Example
  • Serialization Between Different FSMs
  • SerialBuffer Size and Compression

Was this helpful?

  1. User Guide
  2. Debugging and Tools

Serialization *

PreviousTransition HistoryNextLogging Support

Last updated 4 years ago

Was this helpful?

Quick Facts

  • Version:

  • Enabled with HFSM_ENABLE_SERIALIZATION

  • Test:

Interface

hfsm2::Root::SerialBuffer

Buffer for serialization

hfsm2::Root::save(SerialBuffer&) const

Serialize the structural configuration

hfsm2::Root::load(const SerialBuffer&)

De-serialize the configuration and initialize FSM

Example

// Enable serialization
#define HFSM_ENABLE_SERIALIZATION

#include <hfsm2/machine.hpp>
#include <assert.h>

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<State2>();         // Request a transition to 'State2'
       fsm.update();                   // Process transitions
       assert(fsm.isActive<State2>()); // Check if transition completed

       fsm.save(buffer);               // Serialize FSM configuration into 'buffer'
    }
    
    {
       FSM::Instance fsm;              // Create a fresh FSM instance
       assert(fsm.isActive<State1>()); // Initial 'State1' is activated by default

       fsm.load(buffer);               // De-serialize FSM from 'buffer'
       assert(fsm.isActive<State2>()); // Check its configuration is restored
    }
}

Serialization Between Different FSMs

This can be useful for network replication between structurally equivalent server and client FSM instances implementing some specific logic.

SerialBuffer Size and Compression

However, the configuration data saved to SerialBuffer::payload is tightly packed to use the minimal number of bits.

The number of bits used in the payload is recorded in SerialBuffer::bitSize, which could be used for example in custom network replication logic to minimize network bandwidth usage.

Compressing SerialBuffer can also be used to further reduce the size serialized state.

As demonstrated in , it is allowed to exchange the SerialBuffer between different FSMs, so long as their hierarchical structure is exactly the same.

does not compress SerialBuffer.

1.3
test_serialization.cpp
test_serialization.cpp
HFSM2