string LoadCommand::Undo ( ) { // If we were able to Execute this LOAD command one, we should // be able to undo it whithout trouble. // But just in case we apply the same safety mechanism. // We must execute succesfully *all* commands, otherwise we just // cancel the whole thing string status = STATUS_OK; while ( commandsDone.size( ) > 0 && status == STATUS_OK ) { Command * currentCommand = commandsDone.top( ); if ( currentCommand->IsHistorizable( ) ) { ( (HistorizableCommand *) currentCommand )->Undo( ); } // We consider all OK statuses to be equivalent if ( status == STATUS_OK_SILENT || status == STATUS_OK ) { status = STATUS_OK; commandsDone.pop( ); commandsToDo.push( currentCommand ); } } // If we see as much as one error, we need to undo everything if ( status == STATUS_ERROR ) { cancelEverything( false ); } return status; }
void LoadCommand::cancelEverything ( bool undo ) { // We were Executing commands but something failed, // so now we undo everything. if ( undo ) { while ( commandsDone.size( ) > 0 ) { Command * currentCommand = commandsDone.top( ); if ( currentCommand->IsHistorizable( ) ) { ( (HistorizableCommand *) currentCommand )->Undo( ); } commandsDone.pop( ); commandsToDo.push( currentCommand ); } } // We were Undoing commands but something failed, // so now we redo everything we had undone else { while ( commandsToDo.size( ) > 0 ) { Command * currentCommand = commandsToDo.top( ); currentCommand->Execute( ); commandsToDo.pop( ); commandsDone.push( currentCommand ); } } }