Finite state machine with a declared transition table.
Hand managed state already exists in this library, and that is why this module exists. CDataChunk keeps its whole life cycle in one boolean (bIsInitializedWithDataAllocated), CBitStream has an EMode that decides what is allowed, and CJobManager has Initialize/Stop with nothing that says which calls are valid at which moment. In all of them the transition rule is spread over 'if' statements inside the methods, and no place declares it.
The machine adds that place. The decisions below are the ones that change observable behaviour, and they are written here because none of them can be deduced from the code.
1. An event without a transition is neither an error nor a success. Dispatch returns false and the state does not change. It does not throw, interrupt or log on its own, but it calls SetOnUnhandled, when set, for whoever wants to turn it into a log entry or an assertion. Swallowing it silently would be exactly the hidden behaviour the library refuses.
2. Run to completion: a Dispatch from inside a callback is queued, never recursive. The current transition ends, and only then the new event is handled. Reentering would put two half done transitions on the same stack, with the state changing under the suspended one. The same reentrant design brought down the CMutex of this library through infinite recursion. Here reentrancy is impossible by construction, not by the caller's discipline.
3. The table is immutable during a dispatch. Declaring a transition, a callback or a name from inside a callback is refused, and the method returns false. The machine holds references into its own tables while it runs the transition, and a push_back in the middle would invalidate them (a read of freed memory, on the hottest path of the module). Refusing costs nothing. Copying everything on each dispatch to tolerate the case would cost on every transition that never does it.
4. Callback order is fixed: OnExit( from ) -> transition action -> the state changes -> OnEnter( to ) During the action, Current() still returns the source. The transition is in progress, and announcing the destination before reaching it would lie to whoever asks from inside the action.
5. A transition to the same state has two forms, and they differ. AddTransition( A, e, A ) is external: it leaves A and enters A, firing OnExit and OnEnter. AddInternalTransition( A, e, ... ) is internal: it runs the action and fires neither.
6. The search follows insertion order, and the first guard that approves wins. Several transitions for the same (state, event) pair are legitimate when they have guards: that is how "if X go to B, otherwise go to C" is written. A second transition without a guard for the same pair would be unreachable, so it is refused instead of kept as dead code.
Nothing here is guaranteed to be thread safe.