state machine implemented by CPP