11

Command, undo, and queues

A warehouse editor whose operations are command objects, with undo/redo and a FIFO command queue.

Demo 11Source on GitHub ↗

A warehouse inventory editor whose operations are command objects:

  • Command objects. AddStockCommand and MoveItemCommand undo with a clean arithmetic inverse; AdjustQuantityCommand and CancelLineCommand undo from a snapshot captured inside execute() (not the constructor, since a queued command runs later than it is built).
  • CommandHistory is the undo/redo stack; a fresh execute clears the redo branch.
  • CommandQueue is a FIFO batch processor that can keep a batch undoable.
  • RestockLogFn is the std::function shortcut for a queued action with nothing to undo.

The C++23 codetabs path uses std::move_only_function so the command slot can hold a move-only capture; the C++20 path uses std::function.

Build, run, observe

./scripts/build.sh cpp23
./scripts/verify.sh cpp23      # 20/20 tests pass
./examples/11-command/demo.sh cpp23

The demo executes a mix of commands, undoes and redoes them, drains a FIFO batch, and writes restock-log entries. Chapter 11 walks through the two undo strategies and when a plain callable beats a full command object. Builds standalone and under the cpp20 preset (20 tests).