You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.2KB

  1. #include <assert.h>
  2. #include "SqCommand.h"
  3. #include "UndoRedoStack.h"
  4. #if 0
  5. std::shared_ptr<SqCommand> UndoRedoStack::popUndo()
  6. {
  7. assert(canUndo());
  8. auto returnValue = undoList.front();
  9. undoList.pop_front();
  10. return returnValue;
  11. }
  12. std::shared_ptr<SqCommand> UndoRedoStack::popRedo()
  13. {
  14. assert(canRedo());
  15. auto returnValue = redoList.front();
  16. redoList.pop_front();
  17. return returnValue;
  18. }
  19. void UndoRedoStack::pushUndo(std::shared_ptr<SqCommand> cmd)
  20. {
  21. undoList.push_front(cmd);
  22. }
  23. void UndoRedoStack::pushRedo(std::shared_ptr<SqCommand> cmd)
  24. {
  25. redoList.push_front(cmd);
  26. }
  27. #endif
  28. bool UndoRedoStack::canUndo() const
  29. {
  30. return !undoList.empty();
  31. }
  32. bool UndoRedoStack::canRedo() const
  33. {
  34. return !redoList.empty();
  35. }
  36. void UndoRedoStack::execute(std::shared_ptr<SqCommand> cmd)
  37. {
  38. cmd->execute();
  39. undoList.push_front(cmd);
  40. redoList.clear();
  41. }
  42. void UndoRedoStack::undo()
  43. {
  44. assert(canUndo());
  45. CommandPtr cmd = undoList.front();
  46. cmd->undo();
  47. undoList.pop_front();
  48. redoList.push_front(cmd);
  49. }
  50. void UndoRedoStack::redo()
  51. {
  52. assert(canRedo());
  53. CommandPtr cmd = redoList.front();
  54. cmd->execute();
  55. redoList.pop_front();
  56. undoList.push_front(cmd);
  57. }