Back to smallsm

Why not a switch

What goes wrong with a hand-written enum and switch as a machine grows.

An enum and a switch are the right tools for three states and two events. A hand-written switch becomes harder to maintain as states and events are added.

struct connection_t {
    e_state state = e_state::idle;
    std::string host;         // only meaningful while connecting
    int port = 0;             // only meaningful while connecting
    int file_descriptor = -1; // only meaningful while connected
    std::string reason;       // only meaningful while failed
};

Every field exists in every state, and only comments say when a field is valid. Setup code gets copied into each case, and any diagram of the machine has to be updated by hand.

smallsm stores each state's data in that state's struct. The compiler checks the transition table, and diagrams are generated from it.